Re: Build POSIXlt object from its internal representation list
Ben Bolker <[email protected]> Tue, 7 Jul 2026 12:14:35 -0400
| Newsgroups | gmane.comp.lang.r.general |
|---|---|
| Message-ID | <[email protected]> |
p <- as.POSIXlt(Sys.Date())
Do you mean unclass(p)?
x <- unclass(p)
str(x)
## List of 11
## $ sec : num 0
## $ min : int 0
class(x) <- c("POSIXlt", "POSIXt")
identical(x,p) ## TRUE
If you actually use unlist() then everything gets a bit harder,
because all the elements get coerced to character, and we have to undo
that ...
xx <- unlist(p)
str(xx)
## Named chr [1:11]
xx <- as.list(xx)
not_zone <- names(xx) != "zone"
xx[not_zone] <- as.integer(xx[not_zone])
class(xx) <- c("POSIXlt", "POSIXt")
This is not quite identical to the original object, because the sec
component is numeric (not integer), and because the time zone is
originally an attribute (but gets made into an element of the list); you
could reverse-engineer that with some more work.
On 2026-07-07 7:01 a.m., iagogv via R-help wrote:
> Hi all,
>
> From DateTimeClasses help:
>
> Class "POSIXlt" is internally a list [1] of vectors with components
> named sec, min, hour for the time, mday, mon, and year, for the date,
> wday, yday for the day of the week and day of the year, isdst, a
> Daylight Saving Time flag, and sometimes (both _optional_) zone, a
> string for the time zone, and gmtoff, offset in seconds from GMT
>
> If I unlist(x), where x is a POSIXlt object, I get that list, internal
> representation of x.
>
> Is there an inverse function returning the POSIXlt object from that
> list?
>
> Thank you!
>
> Best,
>
> Iago
>
> Links:
> ------
> [1] http://127.0.0.1:25406/library/base/help/list
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Dr. Benjamin Bolker
Professor, Mathematics & Statistics and Biology, McMaster University
Associate Chair (Graduate), Department of Mathematics & Statistics
Director, School of Computational Science and Engineering
> E-mail is sent at my convenience; I don't expect replies outside of working hours.