-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transferred
check_date()
and find_closest_year()
function from oe…
…li pkg to fHMM
- Loading branch information
1 parent
8a10a28
commit d3e9e7e
Showing
9 changed files
with
133 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#' Check date format | ||
#' | ||
#' @description | ||
#' This function checks if the input \code{date} has the format | ||
#' \code{"YYYY-MM-DD"}. | ||
#' | ||
#' @param date \[`character(1)`\]\cr | ||
#' The date in format \code{"YYYY-MM-DD"}. | ||
#' | ||
#' @return | ||
#' \code{as.Date(date)} if \code{date} has the format \code{"YYYY-MM-DD"}. | ||
#' Otherwise, the function throws an error. | ||
#' | ||
#' @keywords internal | ||
|
||
check_date <- function(date) { | ||
date <- try(as.Date(date, format = "%Y-%m-%d"), silent = TRUE) | ||
if (inherits(date, "try-error") || anyNA(date)) { | ||
stop("Date is not in required format 'YYYY-MM-DD'.", call. = FALSE) | ||
} | ||
return(date) | ||
} | ||
|
||
#' Find closest year | ||
#' | ||
#' @description | ||
#' This function takes a date as input and returns the closest year. | ||
#' | ||
#' @param date \[`character(1)`\]\cr | ||
#' The date in format \code{"YYYY-MM-DD"}. | ||
#' | ||
#' @return | ||
#' An \code{integer}, the closest year to the input date. | ||
#' | ||
#' @keywords internal | ||
|
||
find_closest_year <- function(date) { | ||
year <- as.numeric(format(date, "%Y")) | ||
ifelse( | ||
date <= as.Date(paste0(year, "-06-30")), | ||
year, | ||
year + 1 | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
test_that("checks of format 'YYYY-MM-DD' for dates work", { | ||
expect_equal( | ||
check_date(date = "2000-01-01"), | ||
as.Date("2000-01-01") | ||
) | ||
expect_error( | ||
check_date(date = "2000-02-30"), | ||
"Date is not in required format 'YYYY-MM-DD'." | ||
) | ||
expect_error( | ||
check_date(date = "2000-13-01"), | ||
"Date is not in required format 'YYYY-MM-DD'." | ||
) | ||
expect_error( | ||
check_date(date = "01.01.2021"), | ||
"Date is not in required format 'YYYY-MM-DD'." | ||
) | ||
}) | ||
|
||
test_that("finding closest year works", { | ||
expect_equal( | ||
find_closest_year(as.Date("2022-06-01")), | ||
2022 | ||
) | ||
expect_equal( | ||
find_closest_year(as.Date("2022-06-30")), | ||
2022 | ||
) | ||
expect_equal( | ||
find_closest_year(as.Date("2022-07-01")), | ||
2023 | ||
) | ||
expect_equal( | ||
find_closest_year(as.Date("2022-12-31")), | ||
2023 | ||
) | ||
}) |