time_at_tz returns a date-time as it would appear in a
different time zone. The actual moment of time measured does not change,
just the time zone it is measured in. time_at_tz defaults to the
Universal Coordinated time zone (UTC) when an unrecognized time zone is
supplied.
time_force_tz returns the date-time that has the same clock
time as input time, but in the new time zone. Although the new date-time
has the same clock time (e.g. the same values in the seconds, minutes,
hours, etc.) it is a different moment of time than the input
date-time. Computation is vectorized over both time and tz arguments.
time_clock_at_tz retrieves day clock time in specified time
zones. Computation is vectorized over both dt and tz arguments, tz
defaults to the timezone of time.
Usage
time_at_tz(time, tz = "UTC")
time_force_tz(
time,
tz = "UTC",
tzout = tz[[1]],
roll_dst = c("boundary", "post")
)
time_clock_at_tz(time, tz = NULL, units = "secs")Arguments
- time
a date-time object (POSIXct, POSIXlt, Date) or a list of date-time objects. When a list, all contained elements are updated the new list is returned.
- tz
a character string containing the time zone to convert to. R must recognize the name contained in the string as a time zone on your system. For
time_force_tzandtime_clock_at_tzs,tzcan be a vector of heterogeneous time-zones, in which casetimeandtzarguments are paired. Iftimeandtzlengths differ, the smaller one is recycled according with usual R conventions.- tzout
timezone of the output date-time vector. Meaningful only when
tzargument is a vector of heterogenuous time-zones. This argument is necessary because R date-time vectors cannot hold elements with different time-zones.- roll_dst
same as in
time_addwhich see.- units
passed directly to
as.difftime().
Examples
x <- as.POSIXct("2009-08-07 00:00:00", tz = "America/New_York")
time_at_tz(x, "UTC")
#> [1] "2009-08-07 04:00:00 UTC"
time_force_tz(x, "UTC")
#> [1] "2009-08-07 UTC"
time_force_tz(x, "Europe/Amsterdam")
#> [1] "2009-08-07 CEST"
## DST skip:
y <- as.POSIXct("2010-03-14 02:05:05", tz = "UTC")
time_force_tz(y, "America/New_York", roll = "boundary")
#> [1] "2010-03-14 03:00:00 EDT"
time_force_tz(y, "America/New_York", roll = "post")
#> [1] "2010-03-14 03:05:05 EDT"
time_force_tz(y, "America/New_York", roll = "pre")
#> [1] "2010-03-14 01:05:05 EST"
time_force_tz(y, "America/New_York", roll = "NA")
#> [1] NA
## DST skipped and repeated
y <- as.POSIXct(c("2010-03-14 02:05:05 UTC", "2014-11-02 01:35:00"), tz = "UTC")
time_force_tz(y, "America/New_York", roll_dst = c("NA", "pre"))
#> [1] NA "2014-11-02 01:35:00 EDT"
time_force_tz(y, "America/New_York", roll_dst = c("boundary", "post"))
#> [1] "2010-03-14 03:00:00 EDT" "2014-11-02 01:35:00 EST"
## Heterogeneous time-zones:
x <- as.POSIXct(c("2009-08-07 00:00:01", "2009-08-07 01:02:03"), tz = "UTC")
time_force_tz(x, tz = c("America/New_York", "Europe/Amsterdam"))
#> [1] "2009-08-07 00:00:01 EDT" "2009-08-06 19:02:03 EDT"
time_force_tz(x, tz = c("America/New_York", "Europe/Amsterdam"), tzout = "America/New_York")
#> [1] "2009-08-07 00:00:01 EDT" "2009-08-06 19:02:03 EDT"
x <- as.POSIXct("2009-08-07 00:00:01", tz = "UTC")
time_force_tz(x, tz = c("America/New_York", "Europe/Amsterdam"))
#> [1] "2009-08-07 00:00:01 EDT" "2009-08-06 18:00:01 EDT"
## Local clock:
x <- as.POSIXct(c("2009-08-07 01:02:03", "2009-08-07 10:20:30"), tz = "UTC")
time_clock_at_tz(x, units = "secs")
#> Time differences in secs
#> [1] 3723 37230
time_clock_at_tz(x, units = "hours")
#> Time differences in hours
#> [1] 1.034167 10.341667
time_clock_at_tz(x, "Europe/Amsterdam")
#> Time differences in secs
#> [1] 10923 44430
x <- as.POSIXct("2009-08-07 01:02:03", tz = "UTC")
time_clock_at_tz(x, tz = c("America/New_York", "Europe/Amsterdam", "Asia/Shanghai"), unit = "hours")
#> Time differences in hours
#> [1] 21.034167 3.034167 9.034167