[R] time zone problems

Stephen Tucker brown_emu at yahoo.com
Wed Apr 4 10:28:24 CEST 2007


Hi Marc,

This R Help Desk article was very helpful for me:

http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf
Gabor Grothendieck and Thomas Petzoldt

And Gabor noted subsequent changes in behavior of POSIXt classes since the
aforementioned R News publication:

(1) http://tolstoy.newcastle.edu.au/R/e2/help/07/04/13626.html
(2) http://tolstoy.newcastle.edu.au/R/e2/help/07/04/13632.html

Basically, strptime() converts your character object to an POSIXlt object,
which may have a tzone attribute but it is ignored by everything- so you have
to convert it to POSIXct by as.POSIXct(format(myDateTimeObject),"GMT"). But
plot() will still try to make your figures according to system time ("").

Here are two possible solutions (if all of your work is in GMT):

1.
Sys.putenv(TZ = "GMT")
## your work
## (shouldn't have to use tzone.err)
Sys.putenv(TZ = "") 

2.
Use 'chron' objects as suggested in the R newsletter. This object class does
not include any time-zone information, so simplifies things.

Best regards,

ST


--- Marc Fischer <mlfischer at lbl.gov> wrote:

> Folks,
> 
> I'm having  trouble with how datetime objects with time zones are set 
> and plotted.   This may be the result of my running R (2.4.0) on a 
> Windoze XP box.  Perhaps not.  Here are two example problems I need 
> advise on if you have time:
> 
> 1)  I collect data with dates (often as a fractional day of year) in 
> UTC.  Using strptime to create date time objects appears to force the 
> data into the local time zone (including daylight time) of my 
> machine.  Setting the tz="UTC" or "GMT" inside strptime seems to be 
> ignored.  I made the following cumbersome work around:
> 
> foo$date=strptime((paste(yr,DOY), 0, 0), format="%Y %j %H %M",tz="") + 0
>
tzone.err=as.numeric(as.POSIXct(format(Sys.time(),tz="GMT"))-as.POSIXct(format(Sys.time(),tz="")))*3600
> foo$date=foo$date-tzone.err
> attributes(foo$date)$tzone="GMT"
> 
> Am I missing something obvious or is this a problem with Windoze?
> 
> 2) Once I have the data in GMT, I try to plot it using the standard 
> plot command but it converts the data back to local time before 
> making the plot.  Now the work around is:
> 
> plot(foo$date+tzone.err,foo$var, xlab="Date(GMT)")
> 
>   Very frustrating... I've read the help pages but can't find answers 
> to these issues.  Any help?
> 
> Best regards,
> 
> Marc
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list