[R] Daylight savings time and conversion to POSIXt (arghh!)

Don MacQueen macq at llnl.gov
Sat May 4 00:44:22 CEST 2002


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:
>  strptime
function (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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list