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

David Winsemius dwinsemius at comcast.net
Fri Mar 11 15:36:57 CET 2011


On Mar 11, 2011, at 9:23 AM, Daniel Nüst wrote:

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

I am running a *nix variant, aka MacOSX in an English locale in the US  
Eastern TZ.

 > sessionInfo()
R version 2.12.1 (2010-12-16)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
  [1] reshape_0.8.3      plyr_1.2.1         fBasics_2110.79     
timeSeries_2130.90
  [5] timeDate_2130.91   MASS_7.3-9         SuppDists_1.1-8     
hexbin_1.24.0
  [9] abind_1.1-0        plotrix_3.0-2      gridBase_0.4-3      
quantmod_0.3-15
[13] TTR_0.20-2         Defaults_1.1-1     xts_0.7-5          zoo_1.6-4
[17] rms_3.2-0          Hmisc_3.8-3        survival_2.36-3    sos_1.3-0
[21] brew_1.0-4         lattice_0.19-13

loaded via a namespace (and not attached):
[1] cluster_1.13.2 tools_2.12.1
> 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
>>
>>
>
> ______________________________________________
> 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