[R] Timezone issue with strftime/strptime and %z and %Z

Daniel Nüst daniel.nuest at gmail.com
Thu Mar 10 17:17:25 CET 2011


Hello!

I've been trying to get this right for quite a while now and fear
there is an easy solution I just don't see. I did not have this
problem in Linux, and I searched r-help and Google but did not find a
solution, but of course I am grateful for and resources I might not
have found our not understood yet.

I try to parse a time stamp with time zone. I essentially just want to
parse the time stamp "1995-05-25T15:30:00+10:00" and output it exactly
like it is, using the POSIX classes (or is that impossible?).

Please find the script and output/comments below.

Regards,
Daniel


> sessionInfo()
R version 2.12.1 (2010-12-16)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
[5] LC_TIME=German_Germany.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] rj_0.5.2-1

loaded via a namespace (and not attached):
[1] rJava_0.8-8  tools_2.12.1
>
> t1 <- strptime("1995-05-25T15:30:00+10:00", format = "%Y-%m-%dT%H:%M:%OS")
> t2 <- strptime("1995-05-25T15:30:00+10:00", format = "%Y-%m-%dT%H:%M:%OS%z")
>
> strftime(t1, format = "%Y-%m-%dT%H:%M:%OS")
[1] "1995-05-25T15:30:00"
> strftime(t1, format = "%Y-%m-%dT%H:%M:%OS%z")
[1] "1995-05-25T15:30:00Mitteleuropäische Sommerzeit"
> # Ends in "Mitteleuropäische Sommerzeit", not in +10:00, so time zone is ignored!
> # Also no difference beetween %z and %z !
> strftime(t1, format = "%Y-%m-%dT%H:%M:%OS%Z")
[1] "1995-05-25T15:30:00Mitteleuropäische Sommerzeit"
> # All this does NOT remove the "Mitteleuropäische Zeit" from the strftime output!!
>
> # Can locale solve the problem?
> Sys.getlocale(category = "LC_TIME")
[1] "German_Germany.1252"
> Sys.setlocale("LC_TIME", "English")
[1] "English_United States.1252"
>
> strftime(t1, format = "%Y-%m-%dT%H:%M:%OS%z")
[1] "1995-05-25T15:30:00Mitteleuropäische Sommerzeit"
> # [1] "1995-05-25T15:30:00Mitteleuropäische Sommerzeit" -- No change.
>
> # does t1 actually have time zone?
> attributes(t1)
$names
[1] "sec"   "min"   "hour"  "mday"  "mon"   "year"  "wday"  "yday"  "isdst"

$class
[1] "POSIXlt" "POSIXt"

>
> format(t1, format = "%Y-%m-%dT%H:%M:%OS%z") # usetz = TRUE) # no change on usetz
[1] "1995-05-25T15:30:00Mitteleuropäische Sommerzeit"
>
> # Is the : in offset the problem?
> t3 <- strptime("1995-05-25T15:30:00+1000", format = "%Y-%m-%dT%H:%M:%S%z")
> attributes(t3)
$names
[1] "sec"   "min"   "hour"  "mday"  "mon"   "year"  "wday"  "yday"  "isdst"

$class
[1] "POSIXlt" "POSIXt"

> format(t3, format = "%Y-%m-%dT%H:%M:%OS%z")
[1] "1995-05-25T07:30:00Mitteleuropäische Sommerzeit"
> # [1] "1995-05-25T07:30:00Mitteleuropäische Sommerzeit"
>
> strftime(t1, format = "%Y-%m-%dT%H:%M:%OS%z", tz = "+0200") # no effect on setting tz
[1] "1995-05-25T15:30:00Mitteleuropäische Sommerzeit"
>
> Sys.setenv(TZ="GMT") # no working effect on format and strftime



More information about the R-help mailing list