Skip to content

POSIXt oddness at end of 1969

2 messages · William Dunlap, Peter Dalgaard

#
A user here noticed the following difference between Linux and Windows versions
of R-2.15.3 (and R-3.0.0, I think) when using times within a second of the end of 1969:

f <- function (sec = -1) 
{
    x1 <- as.POSIXct(c(2 * sec, sec, 0), origin = "1970-01-01",  tz = "UTC")
    x2 <- as.POSIXlt(x1)
    x3 <- as.POSIXct(x2, origin = "1970-01-01", tz = "UTC")
    list(x1 = x1, x2 = x2, x3 = x3)
}

On Windows I get:
  > f(-1)
  $x1
  [1] "1969-12-31 23:59:58 UTC"
  [2] "1969-12-31 23:59:59 UTC"
  [3] "1970-01-01 00:00:00 UTC"

  $x2
  [1] "1969-12-31 23:59:58 UTC"
  [2] "1969-12-31 23:59:59 UTC"
  [3] "1970-01-01 00:00:00 UTC"
  
  $x3
  [1] "1969-12-31 23:59:58 UTC"
  [2] "1969-12-31 23:59:59 UTC"
  [3] "1970-01-01 00:00:00 UTC"

On Linux I get an NA in x3 for 1 second before 1970:
  > f(-1)
  $x1
  [1] "1969-12-31 23:59:58 UTC"
  [2] "1969-12-31 23:59:59 UTC"
  [3] "1970-01-01 00:00:00 UTC"
  
  $x2
  [1] "1969-12-31 23:59:58 UTC"
  [2] "1969-12-31 23:59:59 UTC"
  [3] "1970-01-01 00:00:00 UTC"
  
  $x3
  [1] "1969-12-31 23:59:58 UTC"
  [2] NA
  [3] "1970-01-01 00:00:00 UTC"

On Windows, sessionInfo() is
  R version 2.15.3 (2013-03-01)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  
  locale:
  [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
  [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
  [5] LC_TIME=English_United States.1252    
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     

On Linux,  sessionInfo() is
  > sessionInfo()
  R version 2.15.3 (2013-03-01)
  Platform: x86_64-unknown-linux-gnu (64-bit)
  
  locale:
   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8
   [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
   [7] LC_PAPER=C                 LC_NAME=C                  LC_ADDRESS=C
  [10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
Same thing happens on OS X. A bit of googling (did you know that Jimi Hendrix and the Band of Gypsies played at the Fillmore East on December 31, 1969?) reveals that the root issue is that mktime() returns -1 if the calendar time cannot be represented, which makes the case where the time _can_ be represented as -1 a bit tricky!

People have come up with heuristics to detect this case, but I wonder if it's worth it. Take a look at
http://stackoverflow.com/questions/14049047/unix-c-get-time-at-a-different-zone

There has to be a simpler way, though. 

-pd
On Apr 17, 2013, at 22:10 , William Dunlap wrote: