Change the time zone, and restore it afterwards.
Usage
with_timezone(tz, code)
local_timezone(tz, .local_envir = parent.frame())
Details
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.
See also
withr
for examples
Examples
Sys.time()
#> [1] "2024-07-31 11:43:49 UTC"
with_timezone("Europe/Paris", print(Sys.time()))
#> [1] "2024-07-31 13:43:49 CEST"
with_timezone("America/Los_Angeles", print(Sys.time()))
#> [1] "2024-07-31 04:43:49 PDT"
fun1 <- function() {
local_timezone("CET")
print(Sys.time())
}
fun2 <- function() {
local_timezone("America/Los_Angeles")
print(Sys.time())
}
Sys.time()
#> [1] "2024-07-31 11:43:49 UTC"
fun1()
#> [1] "2024-07-31 13:43:49 CEST"
fun2()
#> [1] "2024-07-31 04:43:49 PDT"
Sys.time()
#> [1] "2024-07-31 11:43:49 UTC"