-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
with_timezone() ? #92
Comments
It seems that this would do it, although I still need to test it on older R versions, I am not sure when the cache was introduced: with_timezone <- function(tzone, expr) {
## Need to remove the cache first, and also on exit
clear <- function() assign(".sys.timezone", NA, envir = baseenv())
clear()
on.exit(clear(), add = TRUE)
with_envvar(c(TZ = tzone), expr)
} Sys.timezone()
#> [1] "Europe/London"
with_timezone("CET", Sys.timezone())
#> [1] "CET" |
We could probably also just restore the cache on exit, instead of setting it to with_timezone <- function(tzone, expr) {
old <- get0(".sys.timezone", baseenv(), mode = "character",
inherits = FALSE, ifnotfound = NA_character_)
on.exit(assign(".sys.timezone", old, envir = baseenv()), add = TRUE)
assign(".sys.timezone", NA, envir = baseenv())
withr::with_envvar(c(TZ = tzone), expr)
} |
Actually, there is something more to it, because this does not work: with_timezone("US/Pacific", Sys.time())
#> [1] "2019-05-08 10:44:45 BST" EDIT: no, it is good, just need to print inside: with_timezone("US/Pacific", print(Sys.time()))
#> [1] "2019-05-08 02:45:43 PDT" |
I think it is definitely worthwhile to have in withr, particularly because it has some fiddly bits with the cache. So a PR would be great! |
OK, I have an implementation in parsedate, so I'll submit a PR soon. |
Do you want to call it |
I guess |
I realize this is somewhat niche, but it can be useful for testing package that deal with timestamps.
The implementation is mostly straightforward, I can submit a PR, I need to write this for the parsedate package, anyway.
The text was updated successfully, but these errors were encountered: