[R] Automatic time zone conversion

Gabor Grothendieck ggrothendieck at gmail.com
Tue Dec 6 06:36:12 CET 2005


On 12/6/05, simon <sentientc at gmail.com> wrote:
> Hi,
>
> Thanks for the help. Your method does work. However, I am not sure
> if my R give CST a correct offset to timezone or at least display it
> normally.
>  > cdate
> [1] "2000-05-11_01:00:00.0000\005\003"
>  > format(as.POSIXct(paste(as.character(strptime(cdate,format=
> +"%Y-%m-%d_%H:%M:%S")),"GMT")),tz="CST",format="%Y%m%d %H:%M %Z")
> [1] "20000510 17:00 CST"
>  > as.POSIXct(as.character(strptime(cdate,format="%Y-%m-%d_%H:%M:%S")),
> + tz="CST")+(8*3600)
> [1] "2000-05-11 09:00:00 CST"
>
>  > x <- as.POSIXct(chartr("_", " ", cdate), tz = "GMT")
>  > attr(x, "tzone") <- NULL
>  > x
> [1] "2000-05-11 09:00:00 CST"
>
> One thing is strange here. When I tried to find out what the offset is in R.
>  > R.version.string
> [1] "R version 2.2.0, 2005-10-06"
>  > format(Sys.time(),format="%Z %z")
> [1] "CST +0000"
> While under command line(fedora core 3), my system display a different
> offset.
> $date +"%Z %z"
> CST +0800
>
> Thanks again for the help and best regard,
>
> Simon
>
> Gabor Grothendieck wrote:
>
> >Note that even that will not reliably work on all platforms.  The
> >only values for the tz= argument that reliably work across
> >platforms are tz = "" and tz = "GMT".  (See RNews 4/1 Help Desk.)
> >In fact, entering the above code into my machine
> >
> >       > R.version.string  # Windows XP
> >       [1] "R version 2.2.0, 2005-10-24"
> >
> >gives a different answer than on your machine:
> >
> >       > as.POSIXct(as.character(strptime(cdate,format="%Y-%m-%d_%H:%M:%S")),
> >       +       tz="CST")+(8*3600)
> >       [1] "2000-05-11 08:00:00 CST"
> >
> >Also if by CST you mean Central Standard Time as in Chicago, Houston
> >and Winnipeg then its not 8 hours from GMT.  See:
> >
> >       http://www.stacken.kth.se/~kvickers/timezone.html
> >
> >
> >Could it be that you just want to read it in as GMT but
> >display it in the current time zone?  If so, try this:
> >
> >       x <- as.POSIXct(chartr("_", " ", cdate), tz = "GMT")
> >       attr(x, "tzone") <- NULL
> >
> >
> >On 12/5/05, simon <sentientc at gmail.com> wrote:
> >
> >
> >>Dear R-help,
> >>
> >>I was trying to convert a date and time record extracted from a fortran
> >>subroutine I worte and I encounter some problem. The data read in time
> >>and date in a format like "2000-05-11_01:00:00.0000" in fortran output.
> >>It is in GMT. I need to convert it to CST (GMT+8). I did the following
> >>steps.
> >> > cdate
> >>[1] "2000-05-11_01:00:00.0000\005\003"
> >># I am not sure why the extra characters at the end but it doesn't
> >>affect the strptime function so I just ingored it.
> >> > strptime(cdate,format="%Y-%m-%d_%H:%M:%S")
> >>[1] "2000-05-11 01:00:00"
> >># In order to incoporate GMT into the record, I use paste function to
> >>stick it in.
> >> >as.POSIXct(as.character(strptime(cdate,format="%Y-%m-%d_%H:%M:%S")),tz="GMT")
> >>[1] "2000-05-11 01:00:00 GMT"
> >>#It is easier to just do a arthmatic to convert the timezone and ingore
> >>this attribute like
> >> >
> >>as.POSIXct(as.character(strptime(cdate,format="%Y-%m-%d_%H:%M:%S")),tz="CST")+(8*3600)
> >>[1] "2000-05-11 09:00:00 CST"
> >>I was wondering if there is a simpler method to do this.
> >>

You could try this:

now <- Sys.time()
now - as.POSIXct(format(now, tz = "GMT"))




More information about the R-help mailing list