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

Daniel Nüst daniel.nuest at gmail.com
Fri Mar 11 15:23:42 CET 2011


2011/3/11 David Winsemius <dwinsemius at comcast.net>:
>
> On Mar 11, 2011, at 8:54 AM, Daniel Nüst wrote:
>> 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?
>
>> x <- 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")
>> x
> [1] "1995-05-26 01:30:00 GMT"
>> format(x, "%Y-%m-%d %H:%M:%S%z")
> [1] "1995-05-26 01:30:00+0000"
>> format(x, "%Y-%m-%dT%H:%M:%S%z")
> [1] "1995-05-26T01:30:00+0000"

My output is:

format(x, "%Y-%m-%d %H:%M:%S%z")
# [1] "1995-05-26 03:30:00Mitteleuropäische Zeit"
format(x, "%Y-%m-%dT%H:%M:%S%z")
# [1] "1995-05-26T01:30:00Mitteleuropäische Zeit"

What are your locale settings? The root of problem probably is my
German Windows, but I would think there is some way to fix that...

> Sys.getlocale(category = "LC_TIME")
[1] "German_Germany.1252"

> It is easy todeal with the "T" separator. You did say that the ":" was
> optional in the output, didn't you?

Yes, I don't worry too much about the ":" (as I am contacting a web
service with that data it might actually accept it without).

Thanks,

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
>>>
>>>
>>
>> ______________________________________________
>> 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