Skip to content

Commit

Permalink
try with time zone detection
Browse files Browse the repository at this point in the history
  • Loading branch information
BFalquet committed May 16, 2024
1 parent 5034160 commit 5a1f85e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Imports:
glue (>= 1.0.0),
grid,
lifecycle (>= 0.2.0),
lubridate,
magrittr (>= 1.5),
methods,
nestcolor (>= 0.1.1),
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ importFrom(glue,glue)
importFrom(grid,stringWidth)
importFrom(grid,unit)
importFrom(lifecycle,deprecated)
importFrom(lubridate,date)
importFrom(magrittr,"%>%")
importFrom(methods,is)
importFrom(methods,setValidity)
Expand Down
1 change: 0 additions & 1 deletion R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#' @importFrom grid stringWidth unit
#' @importFrom glue glue
#' @importFrom lifecycle deprecated
#' @importFrom lubridate date
#' @importFrom magrittr %>%
#' @importFrom methods is setValidity
#' @importFrom nestcolor color_palette
Expand Down
15 changes: 12 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -438,20 +438,29 @@ listing_format_chevron <- function() {
#'
#' @return a `function` converting a date into `string`.
#'
#' @export
#' @note The date is extracted at the location of the measure, not at the location of the system.
#'
#' @export
#' @examples
#' format_date("%d%b%Y")("2021-01-01")
#' format_date("%d%b%Y")(as.Date("2021-01-01"))
#' format_date("%d%b%Y")(as.POSIXct("2021-01-01 00:00:01", tz = "NZ"))
#' format_date("%d%b%Y")(as.POSIXct("2021-01-01 00:00:01", tz = "US/Pacific"))
format_date <- function(date_format = "%d%b%Y") {
function(x, ...) {
toupper(
format(
# Extract the date at the location of the measure, not at the location of the System.
lubridate::date(x),
# Extract the date at the location of the measure, not at the location of the system.
as.Date(x, tz = attr(x, "tzone")),
date_format
)
)
}
}




# listing_id ----

#' Concatenate Site and Subject ID
Expand Down
9 changes: 9 additions & 0 deletions man/format_date.Rd

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

28 changes: 28 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,31 @@ test_that("gg_list is deprecated", {
}
)
})

test_that("format_date works as expected for POSIXct", {
d <- as.POSIXct("2019-01-01 00:00:01", tz = "NZ")

withr::with_timezone("Europe/Paris", {
foo <- format_date()
expect_identical(foo(d), "01JAN2019")
})

withr::with_timezone("NZ", {
foo <- format_date()
expect_identical(foo(d), "01JAN2019")
})
})

test_that("format_date works as expected for Date", {
d <- as.Date("2019-01-01 00:00:01", tz = "NZ")

withr::with_timezone("Europe/Paris", {
foo <- format_date()
expect_identical(foo(d), "01JAN2019")
})

withr::with_timezone("NZ", {
foo <- format_date()
expect_identical(foo(d), "01JAN2019")
})
})

0 comments on commit 5a1f85e

Please sign in to comment.