POSIXct to numeric using different timezones

自闭症网瘾萝莉.ら 提交于 2019-12-05 07:54:48

Timezone names are not as simple as you would like. See http://en.wikipedia.org/wiki/Tz_database for background and http://en.wikipedia.org/wiki/List_of_tz_database_time_zones for a list of the names that are used. By far the best thing is to use the tz = 'country / city' notation and to explicitly set the time zone of the local system.

So, here's a script that uses two different methods to encode the time zone:

Sys.setenv(TZ='GMT')
pst.abr <- as.POSIXct('2011-01-10 06:45:00', tz = 'PST')
est.abr <- as.POSIXct('2011-01-10 09:45:00', tz = 'EST')
pst.country.city <- as.POSIXct('2011-01-10 06:45:00', tz = 'America/Los_Angeles')
est.country.city <- as.POSIXct('2011-01-10 09:45:00', tz = 'America/New_York')

If we look at the POSIXct values that we would have like to have been PST, we see that they actually have two different values. Starting with the abbreviation (tz ='PST'), you get this:

> pst.abr
[1] "2011-01-10 06:45:00 UTC"
> as.numeric(pst.abr)
[1] 1294641900

You see that the data we defined using tz='PST' isn't actually in the PST timezone, but has inherited the system's timezone.

Compare this to the data we defined using the country\city:

> as.numeric(pst.country.city)
[1] 1294670700
> pst.country.city
[1] "2011-01-10 06:45:00 PST"

So, only the data that we explicitly encode with country/city information has the correct timezone information.

It's because tz="PST" means something else than what you think it does on your system. On linux you'll likely find the list of available full names in /usr/share/zoneinfo/zone.tab. For my linux distro using tz='America/Los_Angeles' works.

You'll find more info if you type ?Sys.timezone.

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