date-zone: Get or set the time zone

Description Usage Arguments Details Value Examples

Description

Usage

1
2
3

Arguments

x

[POSIXct / POSIXlt]

A date-time vector.

zone

[character(1)]

A valid time zone to switch to.

Details

This function is only valid for date-times, as clock treats R's Date class as a naive type, which always has a yet-to-be-specified time zone.

Value

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
library(magrittr)

# Cannot set or get the zone of Date.
# clock assumes that Dates are naive types, like naive-time.
x <- as.Date("2019-01-01")
try(date_zone(x))
try(date_set_zone(x, "America/New_York"))

x <- as.POSIXct("2019-01-02 01:30:00", tz = "America/New_York")
x

date_zone(x)

# If it is 1:30am in New York, what time is it in Los Angeles?
# Same underlying duration, new printed time
date_set_zone(x, "America/Los_Angeles")

# If you want to retain the printed time, but change the underlying duration,
# convert to a naive-time to drop the time zone, then convert back to a
# date-time. Be aware that this requires that you handle daylight saving time
# irregularities with the `nonexistent` and `ambiguous` arguments to
# `as.POSIXct()`!
x %>%
  as_naive_time() %>%
  as.POSIXct("America/Los_Angeles")

y <- as.POSIXct("2021-03-28 03:30:00", "America/New_York")
y

y_nt <- as_naive_time(y)
y_nt

# Helsinki had a daylight saving time gap where they jumped from
# 02:59:59 -> 04:00:00
try(as.POSIXct(y_nt, "Europe/Helsinki"))

as.POSIXct(y_nt, "Europe/Helsinki", nonexistent = "roll-forward")
as.POSIXct(y_nt, "Europe/Helsinki", nonexistent = "roll-backward")

clock documentation built on April 23, 2021, 1:06 a.m.