Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

with_timezone

Time zone


Description

Change the time zone, and restore it afterwards.

Usage

with_timezone(tz, code)

local_timezone(tz, .local_envir = parent.frame())

Arguments

tz

[character(1)] a valid time zone specification, note that time zone names might be platform dependent.

code

[any]
Code to execute in the temporary environment

.local_envir

The environment to apply the change to.

Details

with_time_zone() runs the code with the specified time zone and resets it afterwards.

local_time_zone() changes the time zone for the caller execution environment.

Value

[any]
The results of the evaluation of the code argument.

See Also

withr for examples

Examples

Sys.time()
with_timezone("Europe/Paris", print(Sys.time()))
with_timezone("US/Pacific", print(Sys.time()))

fun1 <- function() {
  local_timezone("CET")
  print(Sys.time())
}

fun2 <- function() {
  local_timezone("US/Pacific")
  print(Sys.time())
}
Sys.time()
fun1()
fun2()
Sys.time()

withr

Run Code 'With' Temporarily Modified Global State

v2.4.2
MIT + file LICENSE
Authors
Jim Hester [aut, cre], Kirill Müller [aut], Kevin Ushey [aut], Hadley Wickham [aut], Winston Chang [aut], Jennifer Bryan [ctb], Richard Cotton [ctb], RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.