[R] Entering times around the start of daylight savings time

Don MacQueen macq at llnl.gov
Thu Apr 29 17:30:31 CEST 2004


You might do better using seq.POSIXt. Your basic problem is that your 
sequence includes a time that doesn't exist, 02:00 on the transition 
day.

seq.POSIXt(ISOdatetime(2004,4,4,0,0,0),by='hour',len=5)
[1] "2004-04-04 00:00:00 PST" "2004-04-04 01:00:00 PST" "2004-04-04 
03:00:00 PDT" "2004-04-04 04:00:00 PDT" "2004-04-04 05:00:00 PDT"

seq.POSIXt(ISOdatetime(2004,4,4,0,0,0,'GMT'),by='hour',len=5)
[1] "2004-04-03 16:00:00 PST" "2004-04-03 17:00:00 PST" "2004-04-03 
18:00:00 PST" "2004-04-03 19:00:00 PST" "2004-04-03 20:00:00 PST"

Both of the above examples give answers that are correct for my 
timezone, and are the same on both an OS X system and a Solaris 
system. R 1.8.1. Note that they are in fact one hour apart:

>  diff(seq.POSIXt(ISOdatetime(2004,4,4,0,0,0,'GMT'),by='hour',len=5))
Time differences of 1, 1, 1, 1 hours
>  diff(seq.POSIXt(ISOdatetime(2004,4,4,0,0,0),by='hour',len=5))
Time differences of 1, 1, 1, 1 hours

>  diff(as.numeric(seq.POSIXt(ISOdatetime(2004,4,4,0,0,0),by='hour',len=5)))
[1] 3600 3600 3600 3600
> 
>diff(as.numeric(seq.POSIXt(ISOdatetime(2004,4,4,0,0,0,'GMT'),by='hour',len=5)))
[1] 3600 3600 3600 3600


Also, since I'm in US pacific and you're in US eastern, perhaps this 
example will come closer to yours:
>  seq.POSIXt(ISOdatetime(2004,4,4,8,0,0,'GMT'),by='hour',len=5)
[1] "2004-04-04 00:00:00 PST" "2004-04-04 01:00:00 PST" "2004-04-04 
03:00:00 PDT" "2004-04-04 04:00:00 PDT" "2004-04-04 05:00:00 PDT"

Again, note that it correctly makes the PST to PDT transition, giving 
times that are one hour apart.
> 
>diff(as.numeric(seq.POSIXt(ISOdatetime(2004,4,4,8,0,0,'GMT'),by='hour',len=5)))
[1] 3600 3600 3600 3600


Whatever the timezone, if the change from standard time to daylight 
savings time takes place at 02:00, then there are no "times between 
2:00 and 3:00". That is, the correct sequence of times is 01:58, 
01:59, 03:00, 03:01, etc., because at 2 AM we jump directly to 3 AM 
(that's what daylight savings time is, after all).

  seq.POSIXt(ISOdatetime(2004,4,4,1,58,0),by='min',len=5)
[1] "2004-04-04 01:58:00 PST" "2004-04-04 01:59:00 PST" "2004-04-04 
03:00:00 PDT" "2004-04-04 03:01:00 PDT" "2004-04-04 03:02:00 PDT"

When I first encountered this issue a while back, the advice was that 
if you specify times that don't exist (such as 02:00 on the 
transition day), what happens depends on your OS; and one should not 
expect it to make sense because in fact the time doesn't exist.

A final note, in my OS X system, I get

>  ISOdatetime(2004,4,4,0:4,0,0)
[1] "2004-04-04 00:00:00 PST" "2004-04-04 01:00:00 PST" NA 
"2004-04-04 03:00:00 PDT" "2004-04-04 04:00:00 PDT"

I would consider this correct, since it gives NA for 
ISOdatetime(2004,4,4,2,0,0), a time that doesn't exist. Excluding the 
NA, it results in one hour intervals.

-Don

At 10:28 AM -0400 4/29/04, Ian Kennedy wrote:
>I'm having problems entering dates and times around when daylight savings time
>starts. If I type (on R 1.8.1 on Gentoo Linux)
>
>>   ISOdatetime(2004,4,4,0:4,0,0,"GMT")
>[1] "2004-04-03 19:00:00 EST" "2004-04-03 20:00:00 EST"
>[3] "2004-04-03 22:00:00 EST" "2004-04-03 22:00:00 EST"
>[5] "2004-04-03 23:00:00 EST"
>
>Giving the times between 2:00 and 3:00 GMT on 4 April which are all off by one
>hour. I tried setting TZ (to "Canada/Eastern") but didn't see any change.
>
>For comparison I tried the same thing in R 1.8.1 for Windows and got a similar
>error, but the one hour that is wrong is one hour early, rather than one hour
>late:
>  > ISOdatetime(2004,4,4,0:4,0,0,"GMT")
>[1] "2004-04-03 19:00:00 Eastern Standard Time"
>[2] "2004-04-03 20:00:00 Eastern Standard Time"
>[3] "2004-04-03 20:00:00 Eastern Standard Time"
>[4] "2004-04-03 22:00:00 Eastern Standard Time"
>[5] "2004-04-03 23:00:00 Eastern Standard Time"
>
>If I try the same thing on R 1.9 for OS X, I get the correct result, that is
>one hour intervals.
>
>So far I've been able to enter times correctly for this period by using chron,
>multiplying by the number of seconds in a day and forcing the resulting
>number to be a POSIXct, but this seems too involved and probably unreliable.
>
>Thanks for any suggestions,
>
>Ian Kennedy
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA




More information about the R-help mailing list