/
as.POSIXlt.POSIXct.html
208 lines (167 loc) · 7.56 KB
/
as.POSIXlt.POSIXct.html
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>R: Date-time Conversion Functions</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="R.css">
</head><body>
<table width="100%" summary="page for as.POSIX*"><tr><td>as.POSIX*</td><td align="right">R Documentation</td></tr></table>
<h2>Date-time Conversion Functions</h2>
<h3>Description</h3>
<p>Functions to manipulate objects of classes <code>"POSIXlt"</code> and
<code>"POSIXct"</code> representing calendar dates and times.
</p>
<h3>Usage</h3>
<pre>
as.POSIXct(x, tz = "", ...)
as.POSIXlt(x, tz = "", ...)
## S3 method for class 'character'
as.POSIXlt(x, tz = "", format, ...)
## S3 method for class 'numeric'
as.POSIXlt(x, tz = "", origin, ...)
## S3 method for class 'POSIXlt'
as.double(x, ...)
</pre>
<h3>Arguments</h3>
<table summary="R argblock">
<tr valign="top"><td><code>x</code></td>
<td>
<p>An object to be converted.</p>
</td></tr>
<tr valign="top"><td><code>tz</code></td>
<td>
<p>A timezone specification to be used for the conversion,
<EM>if one is required</EM>. System-specific (see time zones),
but <code>""</code> is the current timezone, and <code>"GMT"</code> is UTC
(Universal Time, Coordinated).</p>
</td></tr>
<tr valign="top"><td><code>...</code></td>
<td>
<p>further arguments to be passed to or from other methods.</p>
</td></tr>
<tr valign="top"><td><code>format</code></td>
<td>
<p>character string giving a date-time format as used
by <code>strptime</code>.</p>
</td></tr>
<tr valign="top"><td><code>origin</code></td>
<td>
<p>a date-time object, or something which can be coerced by
<code>as.POSIXct(tz="GMT")</code> to such an object.</p>
</td></tr>
</table>
<h3>Details</h3>
<p>The <code>as.POSIX*</code> functions convert an object to one of the two
classes used to represent date/times (calendar dates plus time to the
nearest second). They can convert a wide variety of objects,
including objects of the other class and of classes <code>"Date"</code>,
<code>"date"</code> (from package <span class="pkg">date</span>),
<code>"chron"</code> and <code>"dates"</code> (from package
<span class="pkg">chron</span>) to these classes. Dates without times are
treated as being at midnight UTC.
</p>
<p>They can also convert character strings of the formats
<code>"2001-02-03"</code> and <code>"2001/02/03"</code> optionally followed by
white space and a time in the format <code>"14:52"</code> or
<code>"14:52:03"</code>. (Formats such as <code>"01/02/03"</code> are ambiguous
but can be converted via a format specification by
<code>strptime</code>.) Fractional seconds are allowed.
Alternatively, <code>format</code> can be specified for character vectors or
factors: if it is not specified and no standard format works for
all non-<code>NA</code> inputs an error is thrown.
</p>
<p>If <code>format</code> is specified, remember that some of the format
specifications are locale-specific, and you may need to set the
<code>LC_TIME</code> category appropriately <EM>via</EM>
<code>Sys.setlocale</code>. This most often affects the use of
<code>%b</code>, <code>%B</code> (month names) and <code>%p</code> (AM/PM).
</p>
<p>Logical <code>NA</code>s can be converted to either of the classes, but no
other logical vectors can be.
</p>
<p>The <code>as.double</code> method converts <code>"POSIXlt"</code> objects to
<code>"POSIXct"</code>.
</p>
<p>If you are given a numeric time as the number of seconds since an
epoch, see the examples.
</p>
<p>Character input is first converted to class <code>"POSIXlt"</code> by
<code>strptime</code>: numeric input is first converted to
<code>"POSIXct"</code>. Any conversion that needs to go between the two
date-time classes requires a timezone: conversion from
<code>"POSIXlt"</code> to <code>"POSIXct"</code> will validate times in the
selected timezone. One issue is what happens at transitions
to and from DST, for example in the UK
</p>
<PRE>as.POSIXct(strptime('2011-03-27 01:30:00', '%Y-%m-%d %H:%M:%S'))
as.POSIXct(strptime('2010-10-31 01:30:00', '%Y-%m-%d %H:%M:%S'))
</PRE>
are respectively invalid (the clocks went forward at 1:00 GMT to 2:00
BST) and ambiguous (the clocks went back at 2:00 BST to 1:00 GMT). What
happens in such cases is OS-specific: one should expect the first to
be <code>NA</code>, but the second could be interpreted as either BST or
GMT (and common OSes give both possible values). Note too (see
<code>strftime</code>), OS facilities may not format invalid times correctly.
<h3>Value</h3>
<p><code>as.POSIXct</code> and <code>as.POSIXlt</code> return an object of the
appropriate class. If <code>tz</code> was specified, <code>as.POSIXlt</code>
will give an appropriate <code>"tzone"</code> attribute. Date-times known
to be invalid will be returned as <code>NA</code>.
</p>
<h3>Note</h3>
<p>Some of the concepts used have to be extended backwards in time (the
usage is <EM>proleptic</EM>). For example, the origin of time for the
<code>"POSIXct"</code> class, ‘1970-01-01 00:00.00 UTC’, is before UTC
was defined. More importantly, conversion is done assuming the
Gregorian calendar which was introduced in 1582 and not used
universally until the 20th century. One of the re-interpretations
assumed by ISO 8601:2004 is that there was a year zero, even though
current year numbering (and zero) is a much later concept (525 AD for
year numbers from 1 AD).
</p>
<p>If you want to extract specific aspects of a time (such as the day of
the week) just convert it to class <code>"POSIXlt"</code> and extract the
relevant component(s) of the list, or if you want a character
representation (such as a named day of the week) use the
<code>format</code> method.
</p>
<p>If a timezone is needed and that specified is invalid on your system,
what happens is system-specific but attempts to set it will probably
be ignored.
</p>
<h3>See Also</h3>
<p>DateTimeClasses for details of the classes;
<code>strptime</code> for conversion to and from character
representations.
</p>
<p><code>Sys.timezone</code> for details of the (system-specific) naming
of time zones.
</p>
<p>locales for locale-specific aspects.
</p>
<h3>Examples</h3>
<pre>
(z <- Sys.time()) # the current datetime, as class "POSIXct"
unclass(z) # a large integer
floor(unclass(z)/86400) # the number of days since 1970-01-01 (UTC)
(z <- as.POSIXlt(Sys.time())) # the current datetime, as class "POSIXlt"
unlist(unclass(z)) # a list shown as a named vector
## suppose we have a time in seconds since 1960-01-01 00:00:00 GMT
## (the origin used by SAS)
z <- 1472562988
# ways to convert this
as.POSIXct(z, origin="1960-01-01") # local
as.POSIXct(z, origin="1960-01-01", tz="GMT") # in UTC
as.POSIXct(z, origin=ISOdatetime(1960,1,1,0,0,0)) # local
ISOdatetime(1960,1,1,0,0,0) + z # local
## SPSS dates (R-help 2006-02-16)
z <- c(10485849600, 10477641600, 10561104000, 10562745600)
as.Date(as.POSIXct(z, origin="1582-10-14", tz="GMT"))
as.POSIXlt(Sys.time(), "GMT") # the current time in UTC
## Not run: ## These may not be correct names on your system
as.POSIXlt(Sys.time(), "America/New_York") # in New York
as.POSIXlt(Sys.time(), "EST5EDT") # alternative.
as.POSIXlt(Sys.time(), "EST" ) # somewhere in Eastern Canada
as.POSIXlt(Sys.time(), "HST") # in Hawaii
as.POSIXlt(Sys.time(), "Australia/Darwin")
## End(Not run)
</pre>
</body></html>