[R] Timezone issue with strftime/strptime and %z and %Z
David Winsemius
dwinsemius at comcast.net
Fri Mar 11 12:58:40 CET 2011
On Mar 10, 2011, at 8:46 PM, David Winsemius wrote:
>
> On Mar 10, 2011, at 11:17 AM, Daniel Nüst wrote:
>
>> 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?).
>
> Does this work?
>
> as.POSIXlt(gsub("T.*(\\+|\\-)..(:)", "", # get rid of the colon in
> the tz
> # but preserve the sign for the %z format string
> gsub("T", " ", "1995-05-25T15:30:00-1000")), # replace the
> "T" with a space
> format="%Y-%m-%d %H:%M:%S%z")
Daniel;
That example didn't have the full complexity of the question so I
didn't notoce that I had incorrectly constructed a regex OR within the
colon handling clause. I neverdid figure out how to do that properly,
but htis should handle it:
as.POSIXlt(gsub("T", " ", #change T to space
+ # but preserve the sign for the %z format string
+ gsub("(T..:..:.....):", "\\1",
"1995-05-25T15:30:00-10:00")), format="%Y-%m-%d %H:%M:%S%z")
[1] "1995-05-25 21:30:00"
To get output in GMT add tz argument to as.POSIXlt:
> as.POSIXlt(gsub("T", " ", #change T to space
+ # but preserve the sign for the %z format string
+ gsub("(T..:..:.....):", "\\1",
"1995-05-25T15:30:00-10:00")), format="%Y-%m-%d %H:%M:%S%z", tz="GMT")
[1] "1995-05-26 01:30:00 GMT"
--
David.
>>
>> 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
>>
> --
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> R-help at r-project.org 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list