[R] Chron, as.POSIXct problem

Gabor Grothendieck ggrothendieck at myway.com
Thu Nov 13 15:30:50 CET 2003


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




More information about the R-help mailing list