Skip to content

Commit 9660492

Browse files
committed
as.POSIXlt.POSIXxt
1 parent 6c83291 commit 9660492

35 files changed

+113
-58
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ S3method(as.POSIXxt,default)
22
S3method(as.POSIXxt,character)
33
S3method(as.POSIXxt,Date)
44
S3method(as.POSIXxt,POSIXt)
5+
S3method(as.POSIXlt,POSIXxt)
56
S3method(as.Date,POSIXxt)
67
S3method(format,POSIXxt)
78
S3method(c,POSIXxt)
@@ -19,6 +20,7 @@ export(as.POSIXxt.Date)
1920
export(as.POSIXxt.character)
2021
export(as.POSIXxt.default)
2122
export(as.POSIXxt.POSIXt)
23+
export(as.POSIXlt.POSIXxt)
2224
export(as.Date.POSIXxt)
2325
export(c.POSIXxt)
2426
export(rep.POSIXxt)

R/strptime.R

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,58 @@ as.POSIXxt <- function(x, tz="", ...) UseMethod("as.POSIXxt")
230230
#' @rdname strptime
231231
as.POSIXxt.POSIXt <- function(x, tz=attr(x, "tzone")[1L], ...)
232232
{
233-
if (!is.POSIXxt(x)) {
234-
if (is.null(tz)) tz <- ""
235-
if (!inherits(x, "POSIXct"))
233+
if (is.POSIXxt(x)) return(x)
234+
235+
if (inherits(x, "POSIXlt")) {
236+
x <- unclass(x)
237+
return(ISOdatetime(
238+
x[["year"]]+1900L,
239+
x[["mon"]]+1L,
240+
x[["mday"]],
241+
x[["hour"]],
242+
x[["min"]],
243+
x[["sec"]],
244+
tz=tz
245+
))
246+
}
247+
else {
248+
if (!inherits(x, "POSIXct")) {
249+
if (is.null(tz)) tz <- ""
236250
x <- as.POSIXct(x, tz=tz, ...)
251+
}
237252

238253
attr(x, "class") <- c("POSIXxt", attr(x, "class"))
254+
return(x)
239255
}
256+
}
240257

241-
x
258+
259+
#' @rdname strptime
260+
as.POSIXlt.POSIXxt <- function(x, tz=attr(x, "tzone")[1L], ..., locale=NULL)
261+
{
262+
if (!is.POSIXxt(x)) x <- as.POSIXxt(x)
263+
264+
y <- stringi::stri_datetime_fields(x, tz=tz, locale=locale)
265+
structure(
266+
list(
267+
sec=y[["Second"]],
268+
min=as.integer(y[["Minute"]]),
269+
hour=as.integer(y[["Hour"]]),
270+
mday=as.integer(y[["Day"]]),
271+
mon=as.integer(y[["Month"]]-1L),
272+
year=structure(as.integer(y[["Year"]]-1900L), names=names(x)),
273+
wday=as.integer(y[["DayOfWeek"]]-1L),
274+
yday=as.integer(y[["DayOfYear"]]-1L),
275+
isdst=-1L,
276+
zone=stringi::stri_timezone_info(tz=tz)[["Name"]],
277+
tzone=tz
278+
),
279+
class=c("POSIXlt", "POSIXt")
280+
)
242281
}
243282

283+
284+
244285
#' @rdname strptime
245286
as.POSIXxt.default <- function(x, tz="", ...)
246287
{

devel/sphinx/rapi/sprintf.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,6 @@ cat(stringx::sprintf("%8s=%+.3f", l, r), sep="\n")
8989
# coercion of the same argument to different types:
9090
stringx::printf(c("UNIX time %1$f is %1$s.", "%1$s is %1$f UNIX time."),
9191
Sys.time())
92-
## UNIX time 1630045184.143000 is 2021-08-27T16:19:44+1000.
93-
## 2021-08-27T16:19:44+1000 is 1630045184.143000 UNIX time.
92+
## UNIX time 1630049516.470000 is 2021-08-27T17:31:56+1000.
93+
## 2021-08-27T17:31:56+1000 is 1630049516.470000 UNIX time.
9494
```

devel/sphinx/rapi/strptime.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ as.POSIXxt(x, tz = "", ...)
4141
## S3 method for class 'POSIXt'
4242
as.POSIXxt(x, tz = attr(x, "tzone")[1L], ...)
4343

44+
## S3 method for class 'POSIXxt'
45+
as.POSIXlt(x, tz = attr(x, "tzone")[1L], ..., locale = NULL)
46+
4447
## Default S3 method:
4548
as.POSIXxt(x, tz = "", ...)
4649

@@ -132,26 +135,26 @@ Related function(s): [`sprintf`](sprintf.md), [`ISOdatetime`](ISOdatetime.md)
132135

133136
```r
134137
strftime(Sys.time()) # default format - ISO 8601
135-
## [1] "2021-08-27T16:19:45+1000"
138+
## [1] "2021-08-27T17:31:57+1000"
136139
f <- c("date_full", "%Y-%m-%d", "date_relative_short", "datetime_full")
137140
strftime(Sys.time(), f) # current default locale
138141
## [1] "Friday, 27 August 2021"
139142
## [2] "2021-08-27"
140143
## [3] "today"
141-
## [4] "Friday, 27 August 2021 at 4:19:45 pm Australian Eastern Standard Time"
144+
## [4] "Friday, 27 August 2021 at 5:31:57 pm Australian Eastern Standard Time"
142145
strftime(Sys.time(), f, locale="de_DE")
143146
## [1] "Freitag, 27. August 2021"
144147
## [2] "2021-08-27"
145148
## [3] "heute"
146-
## [4] "Freitag, 27. August 2021 um 16:19:45 Ostaustralische Normalzeit"
149+
## [4] "Freitag, 27. August 2021 um 17:31:57 Ostaustralische Normalzeit"
147150
strftime(Sys.time(), "date_short", locale="en_IL@calendar=hebrew")
148151
## [1] "19 Elul 5781"
149152
strptime("1970-01-01 00:00:00", "%Y-%m-%d %H:%M:%S", tz="GMT")
150153
## [1] "1970-01-01T00:00:00+0000"
151154
strptime("1970-01-01", "%Y-%m-%d") # missing time info replaced with current
152-
## [1] "1970-01-01T16:19:45+1000"
155+
## [1] "1970-01-01T17:31:57+1000"
153156
strptime("14 Nisan 5703", "date_short", locale="en_IL@calendar=hebrew")
154-
## [1] "1943-04-19T16:19:45+1000"
157+
## [1] "1943-04-19T17:31:57+1000"
155158
as.POSIXxt("1970-01-01")
156159
## [1] "1970-01-01T00:00:00+1000"
157160
as.POSIXxt("1970/01/01 12:00")

docs/_sources/rapi/sprintf.md.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,6 @@ cat(stringx::sprintf("%8s=%+.3f", l, r), sep="\n")
8989
# coercion of the same argument to different types:
9090
stringx::printf(c("UNIX time %1$f is %1$s.", "%1$s is %1$f UNIX time."),
9191
Sys.time())
92-
## UNIX time 1630045184.143000 is 2021-08-27T16:19:44+1000.
93-
## 2021-08-27T16:19:44+1000 is 1630045184.143000 UNIX time.
92+
## UNIX time 1630049516.470000 is 2021-08-27T17:31:56+1000.
93+
## 2021-08-27T17:31:56+1000 is 1630049516.470000 UNIX time.
9494
```

docs/_sources/rapi/strptime.md.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ as.POSIXxt(x, tz = "", ...)
4141
## S3 method for class 'POSIXt'
4242
as.POSIXxt(x, tz = attr(x, "tzone")[1L], ...)
4343

44+
## S3 method for class 'POSIXxt'
45+
as.POSIXlt(x, tz = attr(x, "tzone")[1L], ..., locale = NULL)
46+
4447
## Default S3 method:
4548
as.POSIXxt(x, tz = "", ...)
4649

@@ -132,26 +135,26 @@ Related function(s): [`sprintf`](sprintf.md), [`ISOdatetime`](ISOdatetime.md)
132135

133136
```r
134137
strftime(Sys.time()) # default format - ISO 8601
135-
## [1] "2021-08-27T16:19:45+1000"
138+
## [1] "2021-08-27T17:31:57+1000"
136139
f <- c("date_full", "%Y-%m-%d", "date_relative_short", "datetime_full")
137140
strftime(Sys.time(), f) # current default locale
138141
## [1] "Friday, 27 August 2021"
139142
## [2] "2021-08-27"
140143
## [3] "today"
141-
## [4] "Friday, 27 August 2021 at 4:19:45 pm Australian Eastern Standard Time"
144+
## [4] "Friday, 27 August 2021 at 5:31:57 pm Australian Eastern Standard Time"
142145
strftime(Sys.time(), f, locale="de_DE")
143146
## [1] "Freitag, 27. August 2021"
144147
## [2] "2021-08-27"
145148
## [3] "heute"
146-
## [4] "Freitag, 27. August 2021 um 16:19:45 Ostaustralische Normalzeit"
149+
## [4] "Freitag, 27. August 2021 um 17:31:57 Ostaustralische Normalzeit"
147150
strftime(Sys.time(), "date_short", locale="en_IL@calendar=hebrew")
148151
## [1] "19 Elul 5781"
149152
strptime("1970-01-01 00:00:00", "%Y-%m-%d %H:%M:%S", tz="GMT")
150153
## [1] "1970-01-01T00:00:00+0000"
151154
strptime("1970-01-01", "%Y-%m-%d") # missing time info replaced with current
152-
## [1] "1970-01-01T16:19:45+1000"
155+
## [1] "1970-01-01T17:31:57+1000"
153156
strptime("14 Nisan 5703", "date_short", locale="en_IL@calendar=hebrew")
154-
## [1] "1943-04-19T16:19:45+1000"
157+
## [1] "1943-04-19T17:31:57+1000"
155158
as.POSIXxt("1970-01-01")
156159
## [1] "1970-01-01T00:00:00+1000"
157160
as.POSIXxt("1970/01/01 12:00")

docs/genindex.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ <h1 id="index">Index</h1>
207207
<p>
208208
&#169; Copyright 2021, Marek Gagolewski.
209209
<span class="lastupdated">
210-
Last updated on 2021-08-27T16:19:50+1000.
210+
Last updated on 2021-08-27T17:32:03+1000.
211211
</span>
212212

213213
</p>

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ <h1>stringx: Drop-in replacements for base R string functions powered by stringi
326326
<p>
327327
&#169; Copyright 2021, Marek Gagolewski.
328328
<span class="lastupdated">
329-
Last updated on 2021-08-27T16:19:50+1000.
329+
Last updated on 2021-08-27T17:32:03+1000.
330330
</span>
331331

332332
</p>

docs/news.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ <h2>0.0.0 (2021-05-07)<a class="headerlink" href="#id5" title="Permalink to this
304304
<p>
305305
&#169; Copyright 2021, Marek Gagolewski.
306306
<span class="lastupdated">
307-
Last updated on 2021-08-27T16:19:50+1000.
307+
Last updated on 2021-08-27T17:32:03+1000.
308308
</span>
309309

310310
</p>

docs/rapi.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ <h1>R Package <em>stringx</em> Reference<a class="headerlink" href="#r-package-s
229229
<p>
230230
&#169; Copyright 2021, Marek Gagolewski.
231231
<span class="lastupdated">
232-
Last updated on 2021-08-27T16:19:50+1000.
232+
Last updated on 2021-08-27T17:32:03+1000.
233233
</span>
234234

235235
</p>

0 commit comments

Comments
 (0)