Skip to content

Commit

Permalink
Add a format argument to extract_fin_year()
Browse files Browse the repository at this point in the history
This argument keeps the default format (so existing code will work without modification) and allows for choosing a numeric (integer) output.
  • Loading branch information
Moohan committed Nov 24, 2023
1 parent c6eb882 commit 17cdd3d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
45 changes: 33 additions & 12 deletions R/extract_fin_year.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 33 in R/extract_fin_year.R

View check run for this annotation

Codecov / codecov/patch

R/extract_fin_year.R#L33

Added line #L33 was not covered by tests
}

if (inherits(date, "POSIXlt")) {
posix <- date

Check warning on line 37 in R/extract_fin_year.R

View check run for this annotation

Codecov / codecov/patch

R/extract_fin_year.R#L37

Added line #L37 was not covered by tests
} 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)

Check warning on line 47 in R/extract_fin_year.R

View check run for this annotation

Codecov / codecov/patch

R/extract_fin_year.R#L47

Added line #L47 was not covered by tests
}

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)
}
8 changes: 7 additions & 1 deletion man/extract_fin_year.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 17cdd3d

Please sign in to comment.