[R] Time zones in POSIClt objects
Ivan Krylov
|kry|ov @end|ng |rom d|@root@org
Fri Oct 11 09:10:41 CEST 2024
В Thu, 10 Oct 2024 17:16:52 +0200
Jan van der Laan <rhelp using eoos.dds.nl> пишет:
> This is where it is unclear to me what the purpose is of the `zone`
> element of the POSIXlt object. It does allow for registering a time
> zone per element. It just seems to be ignored.
I think that since POSIXlt is an interface to what the C standard calls
the "broken-down" time (into parts, not in terms of functionality) and
both the C standard [1] and the POSIX mktime() [2] ignore the
tm_gmtoff/tm_zone fields (standard C because it's not defined there;
POSIX because it defers to standard C), these fields exist for
presentation purposes. They may be populated when constructing the time
object, but not used for later calculations.
Instead, the standard mktime() always uses the process-global timezone,
so when R processes POSIXlt values, it has to set the TZ environment
variable from the 'tzone' attribute, call tzset() to set global state in
the library, use mktime() to obtain seconds since epoch, and then reset
everything back.
> As I mentioned, fortunately, I only have local time and GMT and it
> would be fine to convert them to a single time zone if that is what
> it takes to work with them in R.
Since your data looks like the following:
times <- list(
year = c(2024L, 2024L),
month = c(1L, 1L),
day = c(1L, 1L),
hour = c(12L, 12L),
minutes = c(30L, 30L),
seconds = c(0, 0),
timezone = c("", "GMT")
)
how about converting all the times into POSIXct?
do.call(mapply, c(
\(year, month, day, hour, minutes, seconds, timezone)
"%04d-%02d-%02d %02d:%02d:%02d" |>
sprintf(year, month, day, hour, minutes, seconds) |>
as.POSIXct(format = "%Y-%m-%d %H:%M:%S", tz = timezone),
times
)) |> do.call(c, args=_)
Its 'tzone' attribute exists mostly for presentation purposes, so even
if you lose it, the exact point in UTC-relative time is still intact.
--
Best regards,
Ivan
[1]
https://en.cppreference.com/w/c/chrono/mktime
[2]
https://pubs.opengroup.org/onlinepubs/9799919799/functions/mktime.html
More information about the R-help
mailing list