I\'m working with the POSIXct data type in R. In my work, I incorporate a function that returns two POSIXct dates in a vector. However, I am discovering some unexpected beha
From ?c.POSIXct:
Using
con "POSIXlt" objects converts them to the current time zone, and on "POSIXct" objects drops any "tzone" attributes (even if they are all marked with the same time zone).
See also here.
The problem is that the function c removes the timezone attribute:
attributes(myTime)
#$class
#[1] "POSIXct" "POSIXt"
#
#$tzone
#[1] "UTC"
attributes(c(myTime))
#$class
#[1] "POSIXct" "POSIXt"
To fix, you can e.g. use the setattr function from data.table, to modify the attribute in place:
(setattr(c(myTime), 'tzone', attributes(myTime)$tzone))
#[1] "2015-01-01 UTC"