Confused by DateTime offsets

爱⌒轻易说出口 提交于 2019-12-08 11:04:27

问题


I'm trying to understand how to interpret GMT offsets so I can work with datetime objects in R. For example, suppose I have a datetime like "2011-04-04 10:45:00 GMT+10"

Q1: Should I read that as a Grenwich time and add 10 hours to get the local time? Or is it a local time and I need to subtract 10 hours to get GMT? I always understood it was the latter.

Q2: Why does R seem to use the former interpretation? For example

foo <- as.POSIXct("2011-04-04 10:45:00", tz="Etc/GMT+10")
attr(foo, "tzone") <- "GMT"
foo
[1] "2011-04-04 20:45:00 GMT"

Huh? I expected "2011-04-04 00:45:00 GMT"

Edit: More confusing still! The result is different if I specify a timezone using Country/City rather than a GMT offset.

foo <- as.POSIXct("2011-04-04 10:45:00", tz="Australia/Sydney")
foo
[1] "2011-04-04 10:45:00 EST"
attr(foo, "tzone") <- "GMT"
foo
[1] "2011-04-04 00:45:00 GMT"

What? Argh! Why??


回答1:


The help page for as.POSIXct says what happens is system specific. On a Mac I get:

> as.POSIXct("2011-04-04 10:45:00", tz="Etc/GMT+10")
[1] "2011-04-04 10:45:00 GMT+10"
> as.POSIXct("2011-04-04 10:45:00", tz="Australia/Sydney")
[1] "2011-04-04 10:45:00 EST"

(And that is presumably eastern Australia rather than the East Coast of the US which is more accurately entered as 'EST5EDT'.) And I think you will find that doing it with as.POSIXct.numeric will give you a value with the origin in GMT which will leave you scratching your head unless you take special care. The way to find the valid TZ's is described in ?Sys.timezone.




回答2:


GMT+10 actually means minus 10 hours from GMT using "POSIX Style Signs".

From: https://www.ietf.org/timezones/data/etcetera

# We use POSIX-style signs in the Zone names and the output abbreviations,
# even though this is the opposite of what many people expect.
# POSIX has positive signs west of Greenwich, but many people expect
# positive signs east of Greenwich.  For example, TZ='Etc/GMT+4' uses
# the abbreviation "GMT+4" and corresponds to 4 hours behind UT
# (i.e. west of Greenwich) even though many people would expect it to
# mean 4 hours ahead of UT (i.e. east of Greenwich).


来源:https://stackoverflow.com/questions/22056059/confused-by-datetime-offsets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!