diff --git a/R/extract_fin_year.R b/R/extract_fin_year.R index 69be46a..8492690 100644 --- a/R/extract_fin_year.R +++ b/R/extract_fin_year.R @@ -5,32 +5,53 @@ #' #' @details The PHS accepted format for financial year is YYYY/YY e.g. 2017/18. #' -#' @param date A date which must be supplied with `Date`, `POSIXct`, `POSIXlt` or +#' @param date A date which must be supplied with `Date`, `POSIXct`, `POSIXlt` or #' `POSIXt` class. [base::as.Date()], #' [`lubridate::dmy()`][lubridate::ymd] and #' [`as.POSIXct()`][base::as.POSIXlt] are examples of functions which #' can be used to store dates as an appropriate class. #' +#' @param format The format to return the Financial Year +#' * (Default) As a character vector in the form '2017/18' +#' * As an integer e.g. 2017 for '2017/18'. +#' #' @return A character vector of financial years in the form '2017/18'. #' #' @examples #' x <- lubridate::dmy(c(21012017, 04042017, 17112017)) #' extract_fin_year(x) #' @export -extract_fin_year <- function(date) { +extract_fin_year <- function(date, format = c("full", "numeric")) { if (!inherits(date, c("Date", "POSIXt"))) { cli::cli_abort("{.arg date} must be a {.cls Date} or {.cls POSIXt} vector, not a {.cls {class(date)}} vector.") } - # Note: lubridate year and month coerce to double - # We only need integers for our purposes - posix <- as.POSIXlt(date, tz = lubridate::tz(date)) - y <- posix$year + 1900L - m <- posix$mon - fy <- y - (m < 3L) - next_fy <- (fy + 1L) %% 100L - out <- sprintf("%.4d/%02d", fy, next_fy) - out[is.na(date)] <- NA_character_ - out + if (missing(format)) { + format <- "full" + } else { + format <- match.arg(format) + } + + if (inherits(date, "POSIXlt")) { + posix <- date + } else { + posix <- as.POSIXlt(date, tz = lubridate::tz(date)) + } + + year <- posix$year + 1900L + month <- posix$mon + fy_num <- year - (month < 3L) + + if (format == "numeric") { + return(fy_num) + } + + next_fy <- (fy_num + 1L) %% 100L + + fin_year_chr <- sprintf("%.4d/%02d", fy_num, next_fy) + + fin_year_chr[is.na(date)] <- NA_character_ + + return(fin_year_chr) } diff --git a/man/extract_fin_year.Rd b/man/extract_fin_year.Rd index 5c06015..f14b9b4 100644 --- a/man/extract_fin_year.Rd +++ b/man/extract_fin_year.Rd @@ -4,7 +4,7 @@ \alias{extract_fin_year} \title{Extract the formatted financial year from a date} \usage{ -extract_fin_year(date) +extract_fin_year(date, format = c("full", "numeric")) } \arguments{ \item{date}{A date which must be supplied with \code{Date}, \code{POSIXct}, \code{POSIXlt} or @@ -12,6 +12,12 @@ extract_fin_year(date) \code{\link[lubridate:ymd]{lubridate::dmy()}} and \code{\link[base:as.POSIXlt]{as.POSIXct()}} are examples of functions which can be used to store dates as an appropriate class.} + +\item{format}{The format to return the Financial Year +\itemize{ +\item (Default) As a character vector in the form '2017/18' +\item As an integer e.g. 2017 for '2017/18'. +}} } \value{ A character vector of financial years in the form '2017/18'.