Skip to content

Commit

Permalink
Warn about sas_numeric_to_date() timezone usage
Browse files Browse the repository at this point in the history
  • Loading branch information
billdenney committed Nov 18, 2024
1 parent a039fc0 commit 71f9736
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ These are all minor breaking changes resulting from enhancements and are not exp

* Remove dplyr verbs superseded in dplyr 1.0.0 (#547, @olivroy)

* Restyle the package and vignettes according to the [tidyverse style guide](https://style.tidyverse.org) (#548, olivroy)
* Restyle the package and vignettes according to the [tidyverse style guide](https://style.tidyverse.org) (#548, @olivroy)

* `sas_numeric_to_date()` now warns for timezones other than "UTC" due to potential misinterpretation (#583, @billdenney)

# janitor 2.2.0 (2023-02-02)

Expand Down
7 changes: 6 additions & 1 deletion R/sas_dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
#' sas_numeric_to_date(time_num = 3600) # 01:00:00
#' @family date-time cleaning
#' @export
sas_numeric_to_date <- function(date_num, datetime_num, time_num, tz = "") {
sas_numeric_to_date <- function(date_num, datetime_num, time_num, tz = "UTC") {
# Confirm that a usable set of input arguments is given
has_date <- !missing(date_num)
has_datetime <- !missing(datetime_num)
has_time <- !missing(time_num)
stopifnot(is.character(tz))
stopifnot(length(tz) == 1)
if (tz != "UTC") {
warning("`tz` in SAS does not appear to be stored with the source data; please verify timezone conversion is correct")
}
if (has_date & has_datetime) {
stop("Must not give both `date_num` and `datetime_num`")
} else if (has_time & has_datetime) {
Expand Down
2 changes: 1 addition & 1 deletion man/sas_numeric_to_date.Rd

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

14 changes: 10 additions & 4 deletions tests/testthat/test-sas_dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ test_that("sas_numeric_to_date", {
)
# NA management
expect_equal(
sas_numeric_to_date(date_num = c(NA, 1), time_num = c(NA, 1), tz = "Etc/GMT-5"),
as.POSIXct(c(NA, "1960-01-01 19:00:01"), tz = "Etc/GMT-5")
sas_numeric_to_date(date_num = c(NA, 1), time_num = c(NA, 1), tz = "UTC"),
as.POSIXct(c(NA, "1960-01-02 00:00:01"), tz = "UTC")
)
expect_equal(
sas_numeric_to_date(date_num = NA, time_num = NA, tz = "Etc/GMT-5"),
as.POSIXct(NA, tz = "Etc/GMT-5")
sas_numeric_to_date(date_num = NA, time_num = NA, tz = "UTC"),
as.POSIXct(NA, tz = "UTC")
)
# Timezone warning (#583)
expect_warning(
sas_numeric_to_date(date_num = 1, time_num = 1, tz = "America/New_York"),
regexp = "`tz` in SAS does not appear to be stored with the source data; please verify timezone conversion is correct",
fixed = TRUE
)
})

Expand Down

0 comments on commit 71f9736

Please sign in to comment.