Change the time zone, and restore it afterwards.
with_timezone(tz, code)
local_timezone(tz, .local_envir = parent.frame())
[any]
The results of the evaluation of the code
argument.
with_timezone()
runs the code with the specified time zone and
resets it afterwards.
local_timezone()
changes the time zone for the caller
execution environment.
withr
for examples
Sys.time()
#> [1] "2025-03-14 15:23:08 UTC"
with_timezone("Europe/Paris", print(Sys.time()))
#> [1] "2025-03-14 16:23:08 CET"
with_timezone("America/Los_Angeles", print(Sys.time()))
#> [1] "2025-03-14 08:23:08 PDT"
fun1 <- function() {
local_timezone("CET")
print(Sys.time())
}
fun2 <- function() {
local_timezone("America/Los_Angeles")
print(Sys.time())
}
Sys.time()
#> [1] "2025-03-14 15:23:08 UTC"
fun1()
#> [1] "2025-03-14 16:23:08 CET"
fun2()
#> [1] "2025-03-14 08:23:08 PDT"
Sys.time()
#> [1] "2025-03-14 15:23:08 UTC"