Re: [R] time zone problems

From: Stephen Tucker <brown_emu_at_yahoo.com>
Date: Wed 04 Apr 2007 - 08:28:24 GMT


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

> 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@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.
>



R-help@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. Received on Wed Apr 04 18:32:31 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 04 Apr 2007 - 09:31:03 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.