[R] Problems with addition in big POSIX dates

Martin Maechler maechler at stat.math.ethz.ch
Thu Aug 14 17:07:14 CEST 2003


>>>>> "Whit" == Whit Armstrong <Whit.Armstrong at tudor.com>
>>>>>     on Wed, 13 Aug 2003 15:23:58 -0400 writes:

    Whit> Have you noticed any problems with big dates (>=1/1/2040) in R?
    Whit> Here is the bit of code that I'm having trouble with:

    >> test.date <- strptime("1/1/2040",format="%m/%d/%Y")
    >> 
    >> unlist(test.date)
    Whit> sec   min  hour  mday   mon  year  wday  yday isdst 
    Whit> 0     0     0     1     0   140     0     0     0 
    >> 
    >> date.plus.one <- as.POSIXct(test.date) + 24*60*60
    >> date.plus.one.lt <- as.POSIXlt(date.plus.one)
    >> 
    >> unlist(date.plus.one.lt)
    Whit> sec   min  hour  mday   mon  year  wday  yday isdst 
    Whit> 0     0     0     2     0   140     0     1     0 

    Whit> Notice that wday (the weekday, 0=Sunday, 7=Saturday) doesn't change.

    Whit> Am I missing something?

Probably the "C library millenium bug" (not official name).
The C library standard type for timedates, "time_t", is
"usually" encoded using 32-bit integers, measuring seconds
since the beginning of 1970. 

It has been well-known for years that this will lead to integer
overflow from 19 Jan 2038 :

  > as.POSIXct(strptime("1/1/1970",format="%m/%d/%Y"))+ .Machine$integer.max
  [1] "2038-01-19 03:14:07 CET"

I had thought that the R implementation carefully used doubles
instead of integers everywhere, but I guess we somewhere rely on
system-internal things even here.

For me, on Intel- Linux (RH 7.3, newer gcc) it's even worse:

> unlist(date.plus.one.lt)
  sec   min  hour  mday   mon  year  wday  yday isdst 
    0     0     0     2     0   140     6     0     0 
				       ~~~~~~~~~
i.e. `wday' has been counted backwards, and `yday' has remained at 0.

-----

I guess we have to wait for 64-bit implementations of "time_t"
or write our own code that works around the many C-library-bugs
we (the R community) have encountered concerning POSIX-time
implementations.
Prof Brian Ripley will know more on this..

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><




More information about the R-help mailing list