From ggrothendieck at volcanomail.com  Mon Aug  4 06:09:16 2003
From: ggrothendieck at volcanomail.com (ggrothendieck@volcanomail.com)
Date: Mon Aug  4 05:08:59 2003
Subject: [Rd] as.POSIXct Bug when used with POSIXlt arg and tz= arg (PR#3646)
Message-ID: <200308040309.h7439GJH027258@pubhealth.ku.dk>


Tracking down this bug was joint work with Jermoe Asselin (jerome at
hivnet.ubc.ca) and Patrick Connolly (p.connolly at hortresearch.co.nz).  We
collectively were able to determine that this is a problem in both Windows 2000
and in Linux and by testing it in our three time zones that it seems to be
daylight savings time related.

Conversion of POSIXlt datetimes to POSIXct appears to have problems.  Consider
the following where lt1 and lt2 are both POSIXlt dates that correspond to the
same date even though they were created using different statements.  Even
though lt1 and lt2 represent the same date, as.POSIXct(lt1,tz="") and
as.POSIXct(lt2,tz="") differ by one hour.

One clue is that the isdst flag is set in lt1 but not in lt2.  However, both
lt1 and lt2 are GMT dates and either the isdst flag should not be set or at
least subsequent processing should not depend on it.

Furthermore, even though lt1 is the one that has isdst set I am not entirely
sure whether its that one or the other one that gives the wrong answer.

> lt1 <- as.POSIXlt("2003-06-01 04:00:00",tz="GMT")
> lt1; as.POSIXct(lt1); as.POSIXct(lt1,tz=""); lt1[["isdst"]]
[1] "2003-06-01 04:00:00 GMT"
[1] "2003-06-01 Eastern Daylight Time"
[1] "2003-06-01 04:00:00 Eastern Daylight Time"
[1] 1

> lt2 <- as.POSIXlt(as.POSIXct("2003-06-01"),tz="GMT")
> lt2; as.POSIXct(lt2); as.POSIXct(lt2,tz=""); lt2[["isdst"]]
[1] "2003-06-01 04:00:00 GMT"
[1] "2003-06-01 Eastern Daylight Time"
[1] "2003-06-01 05:00:00 Eastern Daylight Time"
[1] 0
> 

> paste(R.version)
 [1] "i386-pc-mingw32" "i386"            "mingw32"         "i386, mingw32"  
 [5] ""                "1"               "7.1"             "2003"           
 [9] "06"              "16"              "R"              

> Sys.timezone()
[1] "Eastern Daylight Time"


This is not just a Windows problem.  Here is the example repeated on Jerome's
Linux machine in the Pacific Daylight Time zone.  In this case, lt1 and lt2
differ by 3 hours but as.POSIXct(lt1,tz="") and as.POSIXlt(lt2,tz="") differ by
4 hours!  Again, I am not sure which one is wrong but they should be the same.
The same comment on isdst applies here.

> lt1 <- as.POSIXlt("2003-06-01 04:00:00",tz="GMT")
> lt1; as.POSIXct(lt1); as.POSIXct(lt1,tz=""); lt1[["isdst"]]
[1] "2003-06-01 04:00:00 GMT"
[1] "2003-05-31 21:00:00 PDT"
[1] "2003-06-01 04:00:00 PDT"
[1] 1
> lt2 <- as.POSIXlt(as.POSIXct("2003-06-01"),tz="GMT")
> lt2; as.POSIXct(lt2); as.POSIXct(lt2,tz=""); lt2[["isdst"]]
[1] "2003-06-01 07:00:00 GMT"
[1] "2003-06-01 PDT"
[1] "2003-06-01 08:00:00 PDT"
[1] 0
> paste(R.version)
 [1] "i686-pc-linux-gnu" "i686"              "linux-gnu"
 [4] "i686, linux-gnu"   ""                  "1"
 [7] "7.1"               "2003"              "06"
[10] "16"                "R"
> Sys.timezone()
[1] ""

The problem does appear to be daylight savings time related since when run in
NZST (which is standard time) Patrick got this.   Note that isdst is not set
for either lt1 nor lt2.  Also lt1 and lt2 are the same number of hours apart as
are as.POSIXct(lt1,tz="") and as.POSIXct(lt2,tz="").  They are in the relation
expected so they are either both right or both wrong.

> lt1 <- as.POSIXlt("2003-06-01 04:00:00",tz="GMT")
> lt1; as.POSIXct(lt1); as.POSIXct(lt1,tz=""); lt1[["isdst"]]
[1] "2003-06-01 04:00:00 GMT"
[1] "2003-06-01 16:00:00 NZST"
[1] "2003-06-01 04:00:00 NZST"
[1] 0
> lt2 <- as.POSIXlt(as.POSIXct("2003-06-01"),tz="GMT")
> lt2; as.POSIXct(lt2); as.POSIXct(lt2,tz=""); lt2[["isdst"]]
[1] "2003-05-31 12:00:00 GMT"
[1] "2003-06-01 NZST"
[1] "2003-05-31 12:00:00 NZST"
[1] 0
> paste(R.version)
 [1] "i686-pc-linux-gnu" "i686"              "linux-gnu"        
 [4] "i686, linux-gnu"   ""                  "1"                
 [7] "7.1"               "2003"              "06"               
[10] "16"                "R"                
> Sys.timezone()
[1] ""


[Rd] as.POSIXct Bug when used with POSIXlt arg and tz= arg (PR#3646)
ggrothendieck@volcanomail.com