Skip to content

daylight saving / time zone issues with as.POSIXlt/as.POSIXct (PR#10392)

3 messages · Tony Plate, Peter Dalgaard, Brian Ripley

#
Running under Windows XP 64 bit, as.POSIXlt()/as.POSIXct() seem
to think that US time zones (EST5EDT, MST7MDT) switched from daylight
savings back to standard time on Oct 28, 2007, whereas the switch
is actually on Sun Nov 04, 2007.

Examples:

 > Sys.timezone()
[1] "Mountain Daylight Time"
 > as.POSIXct("2007-10-30 12:38:47")
[1] "2007-10-30 12:38:47 Mountain Daylight Time"
 > # *** Should report 2007-10-30 14:38:47 EDT:
 > as.POSIXlt(as.POSIXct("2007-10-30 12:38:47"), "EST5EDT")
[1] "2007-10-30 13:38:47 EST"
 > Sys.time()
[1] "2007-11-01 09:22:28 Mountain Daylight Time"

 > # Bad behavior is manifested in different ways with TZ="MST7MDT"
 > Sys.setenv(TZ="MST7MDT")
 > # *** Should report "12:38:47 MDT"
 > as.POSIXct("2007-10-30 12:38:47")
[1] "2007-10-30 12:38:47 MST"
 > as.POSIXlt(as.POSIXct("2007-10-30 12:38:47"), "EST5EDT")
[1] "2007-10-30 14:38:47 EST"
 > # *** Should report "2007-11-01 09:23:09 MDT"
 > Sys.time()
[1] "2007-11-01 08:23:09 MST"
 >
 > sessionInfo()
R version 2.6.0 Patched (2007-10-11 r43143)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
 >


Furthermore, with the timezone "Mountain Daylight Time"
(which is the default I get when I start R), the switch
appears to be on Nov 5 in 2006, whereas it actually was
on Oct 29 in 2006.

 > # New R session
 > Sys.timezone()
[1] "Mountain Daylight Time"
 > # *** wrong switch in 2006 ***
 > as.POSIXct("2006-10-30 12:38:47")+(-4:7)*(24*3600)
  [1] "2006-10-26 12:38:47 Mountain Daylight Time"
  [2] "2006-10-27 12:38:47 Mountain Daylight Time"
  [3] "2006-10-28 12:38:47 Mountain Daylight Time"
  [4] "2006-10-29 12:38:47 Mountain Daylight Time"
  [5] "2006-10-30 12:38:47 Mountain Daylight Time"
  [6] "2006-10-31 12:38:47 Mountain Daylight Time"
  [7] "2006-11-01 12:38:47 Mountain Daylight Time"
  [8] "2006-11-02 12:38:47 Mountain Daylight Time"
  [9] "2006-11-03 12:38:47 Mountain Daylight Time"
[10] "2006-11-04 12:38:47 Mountain Daylight Time"
[11] "2006-11-05 11:38:47 Mountain Standard Time"
[12] "2006-11-06 11:38:47 Mountain Standard Time"
 > as.POSIXct("2007-10-30 12:38:47")+(-4:7)*(24*3600)
  [1] "2007-10-26 12:38:47 Mountain Daylight Time"
  [2] "2007-10-27 12:38:47 Mountain Daylight Time"
  [3] "2007-10-28 12:38:47 Mountain Daylight Time"
  [4] "2007-10-29 12:38:47 Mountain Daylight Time"
  [5] "2007-10-30 12:38:47 Mountain Daylight Time"
  [6] "2007-10-31 12:38:47 Mountain Daylight Time"
  [7] "2007-11-01 12:38:47 Mountain Daylight Time"
  [8] "2007-11-02 12:38:47 Mountain Daylight Time"
  [9] "2007-11-03 12:38:47 Mountain Daylight Time"
[10] "2007-11-04 11:38:47 Mountain Standard Time"
[11] "2007-11-05 11:38:47 Mountain Standard Time"
[12] "2007-11-06 11:38:47 Mountain Standard Time"
 > Sys.setenv(TZ="MST7MDT")
 > Sys.timezone()
[1] "MST"
 > as.POSIXct("2006-10-30 12:38:47")+(-4:7)*(24*3600)
  [1] "2006-10-26 13:38:47 MDT" "2006-10-27 13:38:47 MDT"
  [3] "2006-10-28 13:38:47 MDT" "2006-10-29 12:38:47 MST"
  [5] "2006-10-30 12:38:47 MST" "2006-10-31 12:38:47 MST"
  [7] "2006-11-01 12:38:47 MST" "2006-11-02 12:38:47 MST"
  [9] "2006-11-03 12:38:47 MST" "2006-11-04 12:38:47 MST"
[11] "2006-11-05 12:38:47 MST" "2006-11-06 12:38:47 MST"
 > # *** wrong switch in 2007 ***
 > as.POSIXct("2007-10-30 12:38:47")+(-4:7)*(24*3600)
  [1] "2007-10-26 13:38:47 MDT" "2007-10-27 13:38:47 MDT"
  [3] "2007-10-28 12:38:47 MST" "2007-10-29 12:38:47 MST"
  [5] "2007-10-30 12:38:47 MST" "2007-10-31 12:38:47 MST"
  [7] "2007-11-01 12:38:47 MST" "2007-11-02 12:38:47 MST"
  [9] "2007-11-03 12:38:47 MST" "2007-11-04 12:38:47 MST"
[11] "2007-11-05 12:38:47 MST" "2007-11-06 12:38:47 MST"
 >

I see this behavior on all the Windows systems I have tried:
Windows XP 64 bit, Windows XP 32 bit Pro, Windows XP home,
Windows 2000, with a variety of R versions.  The systems
have all relevant Windows updates applied (unless some were
inadvertently missed) and the systems otherwise appear to
behave correctly with respect to times and timezones.

I do not see this problem on Ubuntu Linux systems.

-- Tony Plate
#
tplate at acm.org wrote:
Not Our Problem. (This sort of thing never is. We are wholly dependent 
on the OS for this information). Check out

http://support.microsoft.com/kb/933360

  
    
#
On Thu, 1 Nov 2007, Peter Dalgaard wrote:

            
I think this may well be a long-standing Windows bug that US users have 
not had to suffer before.  From ?as.POSIXlt

      Where OSes describe their valid timezones can be obscure.  The
      help for 'tzset' (or '_tzset' on Windows) can be helpful, but it
      can also be inaccurate.  There is a cumbersome POSIX specification
      (listed under environment variable 'TZ' at <URL:...>
      which is often at least partially supported, but there may be
      other more user-friendly ways to specify timezones. Windows
      documents a specification of the form 'GST-1GDT', but seems always
      to apply the US rules for changing to/from DST with such a
      specification.

Recent versions of Microsoft documentation acknowledge this, e.g.

http://msdn2.microsoft.com/en-us/library/90s5c885(vs.80).aspx

says

  dzn

     Three-letter daylight-saving-time zone such as PDT. If daylight saving
     time is never in effect in the locality, set TZ without a value for
     dzn.  The C run-time library assumes the United States' rules for
     implementing the calculation of daylight saving time (DST).

Now, AFAIK the 'C run-time library' has not been updated to follow US 
politics.