[R] POSIXct bug?

Gabor Grothendieck ggrothendieck at myway.com
Sun Feb 27 05:36:04 CET 2005


Spencer Graves <spencer.graves <at> pdf.com> writes:

: 
: In R 2.0.1 under Windows 2000, at least in some cases, as.POSIXct 
: adds one to the date: 
: 
:  > March1.1959.POSIXct <- as.POSIXct("1959-03-01")
:  > March1.1959.POSIXlt <- as.POSIXlt("1959-03-01")
:  >
:  > (Mar2.59 <- as.Date(March1.1959.POSIXct))
: [1] "1959-03-02"
:  > as.Date(March1.1959.POSIXlt)
: [1] "1959-03-01"
:  >
:  > as.Date(as.POSIXct(Mar2.59))
: [1] "1959-03-02"
:  > as.Date(as.POSIXct(as.character(Mar2.59)))
: [1] "1959-03-03"
:  > print(POSIX.i <- as.POSIXct("1959-03-01"))
: [1] "1959-03-01 Pacific Standard Time"
:  > for(i in 1:11){
: +   print(date.i <- as.Date(POSIX.i))
: +   print(POSIX.i <- as.POSIXct(as.character(date.i)))
: + }
: [1] "1959-03-02"
: [1] "1959-03-02 Pacific Standard Time"
: [1] "1959-03-03"
: [1] "1959-03-03 Pacific Standard Time"
: [1] "1959-03-04"
: [1] "1959-03-04 Pacific Standard Time"
: [1] "1959-03-05"
: [1] "1959-03-05 Pacific Standard Time"
: [1] "1959-03-06"
: [1] "1959-03-06 Pacific Standard Time"
: [1] "1959-03-07"
: [1] "1959-03-07 Pacific Standard Time"
: [1] "1959-03-08"
: [1] "1959-03-08 Pacific Standard Time"
: [1] "1959-03-09"
: [1] "1959-03-09 Pacific Standard Time"
: [1] "1959-03-10"
: [1] "1959-03-10 Pacific Standard Time"
: [1] "1959-03-11"
: [1] "1959-03-11 Pacific Standard Time"
: [1] "1959-03-12"
: [1] "1959-03-12 Pacific Standard Time"
:  >      
:       Comments?

I am not sure that the code really speaks for itself.  What is the bug?
Note that as.Date converts dates relative to GMT, not the current time
zone.  If you want to convert a POSIXct date to a Date date relative
to the current timezone you can convert it to character first.  RNews 4/1
has a table that provides a number of such idioms.




More information about the R-help mailing list