Don MacQueen
2002-May-03 22:44 UTC
[R] Daylight savings time and conversion to POSIXt (arghh!)
I have asked this question before, and received some suggestions for work-arounds that get the job done--and they are much appreciated. But I would still like to find out if I'm missing something, and whether there is a direct way using POSIXt functions (as.POSIXct, as.POSIXlt, strptime, in particular). I have environmental data collected once per minute. Here is a subset of 3 input character strings; I have not found a way to correctly convert all three to POSIXt using POSIXt functions. I would appreciate help with this.> gdat <- c('2002-4-7 1:30:00',+ '2002-4-7 2:30:00', + '2002-4-7 3:30:00') The times are recorded with a fixed 8 hour offset from GMT (equivalently, they are recorded in "pacific standard time" year-round, even when the local convention is to switch to "pacific daylight-savings time". Therefore the times are an hour apart. They also cross the boundary from "standard" to "daylight-savings" in their timezone, so that in the daylight-savings convention they would represented with 1:30, 3:30, 4:30 (1:30 PST, 3:30 PDT, 4:30 PDT). Here is a (naive) attempt at conversion:> gtim <- as.POSIXct(gdat) > gtim[1] "2002-04-07 01:30:00 PST" "2002-04-07 01:30:00 PST" [3] "2002-04-07 03:30:00 PDT" Which doesn't work, as shown by:> diff(as.numeric(gtim))[1] 0 3600 The most serious problem, and the one for which I'm looking for help with, is that the first two times are converted so as to be the same, when they are in fact an hour apart. The third element is converted as if it were PDT, a 7 hour offset from GMT, when it is actually an 8 hour offset. This is not a problem. In fact, it is easy to handle both the first time and the last time correctly with> gcnv <- as.POSIXct(gdat,tz='GMT') + 28800 > gcnv[1] "2002-04-07 01:30:00 PST" "2002-04-07 01:30:00 PST" [3] "2002-04-07 04:30:00 PDT" And verify that the first and last are two hours apart with:> diff(as.numeric(gcnv))[1] 0 7200 But the middle one is still wrong. If they were correctly converted I would see 0 3600 7200. This suggests to me that as.POSIXct() isn't _fully_ honoring the tz argument. Am I right in concluding that this is fundamentally due to:> strptimefunction (x, format) .Internal(strptime(x, format)) and the fact that the internal strptime has no timezone argument? Somewhat more generally, if my input time character vector was specified to be all in GMT, I would think that it should be possible to convert this correctly to POSIXct or POSIXlt, but the same problem occurs gdatg <- c('2002-4-7 1:30:00 GMT', '2002-4-7 2:30:00 GMT', '2002-4-7 3:30:00 GMT')> as.POSIXct(gdatg)[1] "2002-04-07 01:30:00 PST" "2002-04-07 01:30:00 PST" [3] "2002-04-07 03:30:00 PDT"> as.POSIXct(gdatg,tz='GMT')[1] "2002-04-06 17:30:00 PST" "2002-04-06 17:30:00 PST" [3] "2002-04-06 19:30:00 PST"> as.POSIXlt(gdatg)[1] "2002-04-07 01:30:00" "2002-04-07 01:30:00" "2002-04-07 03:30:00"> as.POSIXlt(gdatg,tz='GMT')[1] "2002-04-07 01:30:00 GMT" "2002-04-07 01:30:00 GMT" [3] "2002-04-07 03:30:00 GMT"> strptime(gdatg,format='%Y-%m-%d %H:%M:%S')[1] "2002-04-07 01:30:00" "2002-04-07 01:30:00" "2002-04-07 03:30:00" Thanks -Don> version_ platform sparc-sun-solaris2.7 arch sparc os solaris2.7 system sparc, solaris2.7 status major 1 minor 5.0 year 2002 month 04 day 29 language R> > Sys.getlocale()[1] "C"> > Sys.getenv('TZ')TZ "US/Pacific" -- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA -------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2002-May-04 09:25 UTC
[R] Daylight savings time and conversion to POSIXt (arghh!)
Don MacQueen <macq at llnl.gov> writes:> The third element is converted as if it were PDT, a 7 hour offset from > GMT, when it is actually an 8 hour offset. This is not a problem. In > fact, it is easy to handle both the first time and the last time > correctly with > > > gcnv <- as.POSIXct(gdat,tz='GMT') + 28800 > > gcnv > [1] "2002-04-07 01:30:00 PST" "2002-04-07 01:30:00 PST" > [3] "2002-04-07 04:30:00 PDT" > > And verify that the first and last are two hours apart with: > > diff(as.numeric(gcnv)) > [1] 0 7200 > > But the middle one is still wrong. If they were correctly converted I > would see 0 3600 7200. > > This suggests to me that as.POSIXct() isn't _fully_ honoring the tz argument.Yes, there seems to be a bug there. I get a slightly different effect though:> as.POSIXct(gdat,tz='GMT')[1] "2002-04-07 03:30:00 CEST" "2002-04-07 04:30:00 CEST" [3] "2002-04-07 05:30:00 CEST"> unclass(as.POSIXct(gdat,tz='GMT'))[1] 1018143000 1018146600 1018150200> Sys.putenv(TZ="US/Pacific") > as.POSIXct(gdat,tz='GMT')[1] "2002-04-06 17:30:00 PST" "2002-04-06 19:30:00 PST" [3] "2002-04-06 19:30:00 PST"> unclass(as.POSIXct(gdat,tz='GMT'))[1] 1018143000 1018150200 1018150200 The workaround would be along the lines of> {x<-Sys.getenv("TZ");Sys.putenv(TZ="GMT")+ z<-as.POSIXct(gdat);Sys.putenv(TZ=x);z}+28800 [1] "2002-04-07 01:30:00 PST" "2002-04-07 03:30:00 PDT" [3] "2002-04-07 04:30:00 PDT" which is probably what as.POSIXlt should do internally too. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Possibly Parallel Threads
- {round,trunc}.POSIXt and daylight savings time (PR#1543)
- Problem(?) in strptime() -- short version
- Error with cut.POSIXt and daylight savings time switchover dates
- Result depends on order of factors in unbalanced designs (lme, anova)?
- Entering times around the start of daylight savings time