Skip to content

Commit

Permalink
cherried some of #584 for CRAN bugfix release
Browse files Browse the repository at this point in the history
  • Loading branch information
billdenney authored and sfirke committed Dec 19, 2024
1 parent d46127d commit e381eaf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# janitor 2.2.1 (2024-12-19)

This is a trivial bugfix release whose only purpose is fixing a test that was failing on CRAN due to the way timezones are handled in Debian. In making that fix (PR #584), we made a small - technically breaking - improvement to a function that works with SAS dates. >99.9% of janitor users should be unaffected by this release.

## Breaking changes

* `sas_numeric_to_date()` now warns for timezones other than "UTC" due to the way that SAS loads timezones, and the default timezone for `sas_numeric_to_date()` is now "UTC" instead of "" (#583, @billdenney)


# janitor 2.2.0 (2023-02-02)

## Breaking changes
Expand Down
15 changes: 11 additions & 4 deletions 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("SAS may not properly store timezones other than UTC. Consider confirming the accuracy of the resulting data.")
}
if (has_date & has_datetime) {
stop("Must not give both `date_num` and `datetime_num`")
} else if (has_time & has_datetime) {
Expand All @@ -37,9 +42,11 @@ sas_numeric_to_date <- function(date_num, datetime_num, time_num, tz="") {
if (!all(mask_na_match)) {
stop("The same values are not NA for both `date_num` and `time_num`")
}
ret <- as.POSIXct(86400*date_num + time_num, origin="1960-01-01", tz=tz)
} else if (has_datetime) {
ret <- as.POSIXct(datetime_num, origin="1960-01-01", tz=tz)
datetime_num <- 86400 * date_num + time_num
has_datetime <- TRUE
}
if (has_datetime) {
ret <- as.POSIXct(datetime_num, origin = "1960-01-01", tz = tz)
} else if (has_date) {
ret <- as.Date(date_num, origin="1960-01-01")
} else if (has_time) {
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.

2 changes: 1 addition & 1 deletion tests/testthat/test-clean-names.R
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ test_that("tbl_graph/tidygraph", {
skip_if_not_installed("tidygraph")
# create test graph to test clean_names
test_graph <-
tidygraph::play_erdos_renyi(10, 0.5) %>%
tidygraph::play_gnp(10, 0.5) %>%
# create nodes wi
tidygraph::bind_nodes(test_df) %>%
tidygraph::mutate_all(tidyr::replace_na, 1)
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-convert_to_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ test_that("convert_datetime works", {
as.POSIXct("2009-07-06 12:13:14", tz="UTC")
)
expect_equal(
convert_to_datetime("2009-07-06 12:13:14", tz="EST"),
as.POSIXct("2009-07-06 12:13:14", tz="EST"),
info="The tz argument is respected"
convert_to_datetime("2009-07-06 12:13:14", tz = "Etc/GMT-5"),
as.POSIXct("2009-07-06 12:13:14", tz = "Etc/GMT-5"),
info = "The tz argument is respected"
)
expect_equal(
convert_to_datetime(40000),
Expand All @@ -61,8 +61,8 @@ test_that("convert_datetime works", {
as.POSIXct("2009-07-06 02:24", tz="UTC")
)
expect_equal(
convert_to_datetime(40000.1, tz="EST"),
as.POSIXct("2009-07-06 02:24", tz="EST")
convert_to_datetime(40000.1, tz = "Etc/GMT-5"),
as.POSIXct("2009-07-06 02:24", tz = "Etc/GMT-5")
)
expect_equal(
convert_to_datetime("40000"),
Expand All @@ -73,8 +73,8 @@ test_that("convert_datetime works", {
as.POSIXct("2009-07-06 02:24", tz="UTC")
)
expect_equal(
convert_to_datetime("40000.1", tz="EST"),
as.POSIXct("2009-07-06 02:24", tz="EST")
convert_to_datetime("40000.1", tz = "Etc/GMT-5"),
as.POSIXct("2009-07-06 02:24", tz = "Etc/GMT-5")
)
expect_equal(
convert_to_datetime(factor("40000.1")),
Expand Down
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="EST"),
as.POSIXct(c(NA, "1960-01-01 19:00:01"), tz="EST")
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="EST"),
as.POSIXct(NA, tz="EST")
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 = "SAS may not properly store timezones other than UTC. Consider confirming the accuracy of the resulting data.",
fixed = TRUE
)
})

Expand Down

0 comments on commit e381eaf

Please sign in to comment.