Dear R list, I noticed the following 'problem' when changing the format of dates created with seq.dates() (from the Chron library) using as.POSIXct() (R 1.8.0 on OSX 10.2.8):> datesTest<-seq.dates(from="10/01/1952", length=3, by="days"); > datesTest[1] 10/01/52 10/02/52 10/03/52 # Now changing the format to show year as 1952.> datesTest<-format(as.POSIXct(datesTest), "%m/%d/%Y") > datesTest[1] "09/30/1952" "10/01/1952" "10/02/1952">The dates were shifted by one day. The work around is simple enough, e.g.,> datesTest<-format(as.POSIXct(datesTest+1), "%m/%d/%Y")[1] "10/01/1952" "10/02/1952" "10/03/1952" but I wonder if this is the intended behavior? Brian
Does not happen on Solaris or Linux, so looks like a MacOS X problem. Here is some crosschecks:> unclass(datesTest)[1] -6301 -6300 -6299 attr(,"format") [1] "m/d/y" attr(,"origin") month day year 1 1 1970> unclass(as.POSIXct(datesTest))[1] -544406400 -544320000 -544233600 On Wed, 12 Nov 2003, Brian Beckage wrote:> Dear R list, > > I noticed the following 'problem' when changing the format of dates > created with seq.dates() (from the Chron library) using as.POSIXct() > (R 1.8.0 on OSX 10.2.8): > > > datesTest<-seq.dates(from="10/01/1952", length=3, by="days"); > > datesTest > [1] 10/01/52 10/02/52 10/03/52 > > # Now changing the format to show year as 1952. > > > datesTest<-format(as.POSIXct(datesTest), "%m/%d/%Y") > > datesTest > [1] "09/30/1952" "10/01/1952" "10/02/1952" > > > > The dates were shifted by one day. The work around is simple enough, e.g., > > > datesTest<-format(as.POSIXct(datesTest+1), "%m/%d/%Y") > [1] "10/01/1952" "10/02/1952" "10/03/1952" > > but I wonder if this is the intended behavior? > > Brian > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
You are being hit by a timezone problem. Its not really shifting the days by one. Its working in the GMT timezone, not yours. If you can accept a date format that chron supports then this is the easiest solution since chron does not support timezones and so can't give you such problems in the first place. For example, the following stays in chron the whole time: format(datesTest, format="m/day/year") [1] "Oct/01/1952" "Oct/02/1952" "Oct/03/1952" If you must convert to POSIXt to take advantage of a format only supported by POSIXt then use POSIXlt and specify the timezone explictly: format(as.POSIXlt(datesTest,tz="GMT"), "%m/%d/%Y") [1] "10/01/1952" "10/02/1952" "10/03/1952" Its because of subtle problems like this that I think that some sort of naive (i.e. non-timezone) time such as chron or an alternative, should be in the base to encourage wider use. --- Date: Wed, 12 Nov 2003 10:05:39 -0500 From: Brian Beckage <bbeckage at uvm.edu> To: <r-help at stat.math.ethz.ch> Subject: [R] Chron, as.POSIXct problem Dear R list, I noticed the following 'problem' when changing the format of dates created with seq.dates() (from the Chron library) using as.POSIXct() (R 1.8.0 on OSX 10.2.8):> datesTest<-seq.dates(from="10/01/1952", length=3, by="days"); > datesTest[1] 10/01/52 10/02/52 10/03/52 # Now changing the format to show year as 1952.> datesTest<-format(as.POSIXct(datesTest), "%m/%d/%Y") > datesTest[1] "09/30/1952" "10/01/1952" "10/02/1952">The dates were shifted by one day. The work around is simple enough, e.g.,> datesTest<-format(as.POSIXct(datesTest+1), "%m/%d/%Y")[1] "10/01/1952" "10/02/1952" "10/03/1952" but I wonder if this is the intended behavior? Brian ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
From: Brian Beckage <bbeckage at uvm.edu>> > Thanks to all who responded to my posting. > > At 11:39 AM -0500 11/12/03, Gabor Grothendieck wrote: > >You are being hit by a timezone problem. Its not really shifting > >the days by one. Its working in the GMT timezone, not yours. > > > >If you can accept a date format that chron supports then this is the > >easiest solution since chron does not support timezones and so can't > >give you such problems in the first place. For example, > >the following stays in chron the whole time: > > > > format(datesTest, format="m/day/year") > > [1] "Oct/01/1952" "Oct/02/1952" "Oct/03/1952" > > > >If you must convert to POSIXt to take advantage of a format > >only supported by POSIXt then use POSIXlt and specify the timezone explictly: > > > > format(as.POSIXlt(datesTest,tz="GMT"), "%m/%d/%Y") > > [1] "10/01/1952" "10/02/1952" "10/03/1952" > > This solved the problem using as.POSIXlt(). I guess the tz argument > doesn't solve the problem using as.POSIXct(). In any case, I'm able > to use as.POSIXlt() in my current application.You can use POSIXct but its a bit trickier. Assuming datesTest is a chron vector, as before you can do this. format(as.POSIXct(datesTest), "%m/%d/%Y", tz="GMT") # right Note that in this case you have to use the tz parameter on format, NOT on as.POSIXct: format(as.POSIXct(datesTest, tz="GMT"), "%m/%d/%Y") # wrong