File: date-time-zone.Rd

package info (click to toggle)
r-cran-clock 0.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,856 kB
  • sloc: cpp: 19,564; sh: 17; makefile: 2
file content (80 lines) | stat: -rw-r--r-- 2,268 bytes parent folder | download
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/posixt.R
\name{date-time-zone}
\alias{date-time-zone}
\alias{date_time_zone}
\alias{date_time_set_zone}
\title{Get or set the time zone}
\usage{
date_time_zone(x)

date_time_set_zone(x, zone)
}
\arguments{
\item{x}{\verb{[POSIXct / POSIXlt]}

A date-time vector.}

\item{zone}{\verb{[character(1)]}

A valid time zone to switch to.}
}
\value{
\itemize{
\item \code{date_time_zone()} returns a string containing the time zone.
\item \code{date_time_set_zone()} returns \code{x} with an altered printed time. The
underlying duration is not changed.
}
}
\description{
\itemize{
\item \code{date_time_zone()} gets the time zone.
\item \code{date_time_set_zone()} sets the time zone. This retains the \emph{underlying
duration}, but changes the \emph{printed time} depending on the zone that is
chosen.
}
}
\details{
This function is only valid for date-times, as clock treats R's Date class as
a \emph{naive} type, which always has a yet-to-be-specified time zone.
}
\examples{
library(magrittr)

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

x <- date_time_parse("2019-01-02 01:30:00", "America/New_York")
x

date_time_zone(x)

# If it is 1:30am in New York, what time is it in Los Angeles?
# Same underlying duration, new printed time
date_time_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_date_time()`!
x \%>\%
  as_naive_time() \%>\%
  as_date_time("America/Los_Angeles")

y <- date_time_parse("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_date_time(y_nt, "Europe/Helsinki"))

as_date_time(y_nt, "Europe/Helsinki", nonexistent = "roll-forward")
as_date_time(y_nt, "Europe/Helsinki", nonexistent = "roll-backward")
}