datetime_at_tz() changes time zones while preserving UTC time (instead of clock time).
Usage
datetime_at_tz(x, tz = "", ...)
# S3 method for class 'datetimeoffset'
datetime_at_tz(
x,
tz = "",
...,
ambiguous = "error",
nonexistent = "error",
fill = NA_character_
)
# S3 method for class 'clock_zoned_time'
datetime_at_tz(x, tz = "", ...)
# S3 method for class 'POSIXt'
datetime_at_tz(x, tz = "", ...)
# Default S3 method
datetime_at_tz(x, tz = "", ...)Arguments
- x
A datetime object.
- tz
The target timezone to change to.
- ...
Ignored
- ambiguous
What to do when the "clock time" in the new time zone is ambiguous. See
clock::as_zoned_time.clock_naive_time().- nonexistent
What to do when the "clock time" in the new time zone doesn't exist. See
clock::as_zoned_time.clock_naive_time().- fill
If timezone and UTC offset info is missing what timezone to assume. See
fill_tz().
See also
set_tz() changes time zones while preserving clock time (instead of UTC time).
Examples
if(all(c("America/Los_Angeles", "America/New_York") %in% OlsonNames())) {
dt0 <- as_datetimeoffset("2020-01-01T01:01[America/Los_Angeles]")
dt <- datetime_at_tz(dt0, "America/New_York")
print(dt)
dt <- datetime_at_tz(as.POSIXct(dt0), "America/New_York")
print(dt)
dt <- datetime_at_tz(clock::as_zoned_time(dt0), "America/New_York")
print(dt)
# Can also use `lubridate::with_tz()`
if (requireNamespace("lubridate")) {
dt <- lubridate::with_tz(dt0, "America/New_York")
print(dt)
}
}
#> <datetimeoffset[1]>
#> [1] 2020-01-01T04:01-05:00[America/New_York]
#> [1] "2020-01-01 04:01:00 EST"
#> <zoned_time<second><America/New_York>[1]>
#> [1] "2020-01-01T04:01:00-05:00"
#> <datetimeoffset[1]>
#> [1] 2020-01-01T04:01-05:00[America/New_York]