Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSIXct to numeric using different timezones

Tags:

r

posixct

I think I must not understand how POSIXct works, or something. As far as I understand, it is seconds since epoch, with epoch being a standard time like 1970-01-01 GMT.

I take two POSIXct times one in EST one in PST that are the same absolute time. Yet, when I convert them to a numeric value, the result is different... Could anyone point me to what I am doing wrong?

> pst = as.POSIXct('2011-01-10 06:45:00', tz = 'PST')
> est = as.POSIXct('2011-01-10 09:45:00', tz = 'EST')
> as.numeric(pst)
 [1] 1294641900
> as.numeric(est)
 [1] 1294670700

here is my session info:

> sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=C              LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C\
                  LC_ADDRESS=C
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
 [1] RSQLite_0.9-4      snow_0.3-8         RMySQL_0.8-0       DBI_0.2-5          gtools_2.6.2       reshape2_1.1       ggplot2_0.8.9      proto_0.3-9.2      reshape_0.8.4      fTrading_2100.76   fBasics_\
2110.79    MASS_7.3-12
[13] timeSeries_2130.92 timeDate_2131.00   plyr_1.7.1

loaded via a namespace (and not attached):
[1] stringr_0.4  tools_2.13.0
like image 457
vc273 Avatar asked Aug 09 '13 21:08

vc273


2 Answers

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.

like image 169
Andy Clifton Avatar answered Sep 29 '22 22:09

Andy Clifton


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.

like image 22
eddi Avatar answered Sep 29 '22 21:09

eddi