3
votes

I need to write some codes to convert the data time from other timezone to local for different users in the world.

The function Sys.timezone() will return the abbreviation with three characters. For example,

> Sys.timezone() 
"EST"

EST stands for Eastern Standard Time (-5), but my computer actually is Australian Eastern Standard Time (AEST, +10, Australia/Brisbane). This code will return wrong result

> as.POSIXct(as.numeric(Sys.time()), origin = '1970-1-1', tz = Sys.timezone())
"2013-11-20 02:17:12 EST"

The actual time is

Sys.time() "2013-11-20 17:17:12 EST"

My question is how to get the system time zone with the Country/City name?

PS: Why does R use EST for Eastern Standard Time (-5) and Australian Eastern Standard Time (+10)?

sessionInfo() R version 3.0.2 (2013-09-25) Platform: x86_64-w64-mingw32/x64 (64-bit)

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

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

1

1 Answers

2
votes

"EST" is ambiguous as you point out. "EST5EDT" is somewhat less ambiguous for the US-EST. You don't mention your OS and you should have. The /usr/share/zoneinfo file is somewhere on your device if it's a NIX variant, and that file has the "official country/city" names. It would be easiest I suppose if GMT+10 or GMT-5 or GMT+10:00 work on your device but that cannot be determined for what you have posted so far. This is what I get when I search the R objects created by the example on:

 ?Sys.timezone

 tzones$name[ grep("Brisbane", tzones$name) ]
[1] "Australia/Brisbane"

My device returns:

> Sys.timezone() 
[1] ""   # so I'm guessing you are not on a Mac.

To your PS: You will need to ask the makers of your OS. R just uses the facilities of whoever designed your system.

And you should note that strptime will NOT convert TZ's on input. On the other hand you can convert on output with strftime