[R] Timezone issue with strftime/strptime and %z and %Z
Daniel Nüst
daniel.nuest at gmail.com
Fri Mar 11 15:16:48 CET 2011
Update: I ran the following commands on a Linux machine:
> sessionInfo()
R version 2.12.1 (2010-12-16)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=de_DE.utf8 LC_NUMERIC=C
[3] LC_TIME=de_DE.utf8 LC_COLLATE=de_DE.utf8
[5] LC_MONETARY=de_DE.utf8 LC_MESSAGES=de_DE.utf8
[7] LC_PAPER=de_DE.utf8 LC_NAME=de_DE.utf8
[9] LC_ADDRESS=de_DE.utf8 LC_TELEPHONE=de_DE.utf8
[11] LC_MEASUREMENT=de_DE.utf8 LC_IDENTIFICATION=de_DE.utf8
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:00+0000"
> strftime(t1, format = "%Y-%m-%dT%H:%M:%OS%Z")
[1] "1995-05-25T15:30:00CEST"
>
> strftime(t2, format = "%Y-%m-%dT%H:%M:%OS")
[1] NA
> strftime(t2, format = "%Y-%m-%dT%H:%M:%OS%Z")
[1] NA
>
> t3 <- strptime("1995-05-25T15:30:00+1000", format = "%Y-%m-%dT%H:%M:%OS%z")
> strftime(t3, format = "%Y-%m-%dT%H:%M:%OS%z")
[1] "1995-05-25T07:30:00+0000"
> # Time zone is recognized and output has +0000
>
> t3b <- strptime("1995-05-25T15:30:00+1000", format = "%Y-%m-%dT%H:%M:%OS")
> strftime(t3b, format = "%Y-%m-%dT%H:%M:%OS%z")
[1] "1995-05-25T15:30:00+0000"
> # Time zone is ignored on parsing, so imho correct behaviour.
So while it still looses the time zone when having a ":" in it, it at
least prints it out correctly. So imho this is a Windows issue, maybe
this helps someone figuring out a solution. This would work fine for
me as the only manual thing I have to do is add a ":".
/Daniel
2011/3/11 Daniel Nüst <daniel.nuest at gmail.com>:
> 2011/3/11 David Winsemius <dwinsemius at comcast.net>:
>> On Mar 10, 2011, at 8:46 PM, David Winsemius wrote:
>>> On Mar 10, 2011, at 11:17 AM, Daniel Nüst wrote:
>>>> 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"
>
> This procudes the same output on my machine, but still does not solve
> the problem, which lies on the output side, sorry if I was not clear
> about that before:
>
> time <- 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")
> format(x = time, format = "%Y-%m-%d %H:%M:%S%z")
> # [1] "1995-05-26 01:30:00Mitteleuropäische Zeit"
> strftime(x = time, format = "%Y-%m-%d %H:%M:%S%z")
> # [1] "1995-05-26 03:30:00Mitteleuropäische Sommerzeit"
>
> But I need "...01:30:00+01:00", or "...01:30:00+0100", as I expect
> from %z because in ?strptime I read:
>
> "%z
> Signed offset in hours and minutes from UTC, so -0800 is 8 hours
> behind UTC."
>
> and
>
> "Note that when %z or %Z is used for output with an object with an
> assigned timezone an attempt is made to use the values for that
> timezone — but it is not guaranteed to succeed."
>
> Does this just mean it just won't work?
>
> Let me rephrase my question: How can I create a time object from the
> character string "1995-05-25T15:30:00-10:00" and get exactly the same
> character string again when formatting it/printing it?
>
> Thanks for any help!
> /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