Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do POSIXct timezones work in R

I have a set of standard unix integer timestamps, all in UTC (GMT), that I'm feeding into R that I wish to plot. I've been using code of the form:

d$date_time <- as.POSIXct(d$date_time,origin="1970-01-01",tz="GMT")

to covert my column of standard unix timestamp UTC integers into what I assume is some kind of plottable set of objects. I can plot this and the data looks approximately good, but I have no idea whether all of my data is being offset in any way by the local timezone of my computer or any other timezone adjustments. This is because I don't understand what adjustments get made to the data (if any) when a) I make the call to as.POSIXct() and b) when I plot the data. So these are my questions:

  1. When I specify tz="GMT" above, what exactly is this telling the computer to do? I see three possibilities: i) "your data is in GMT and you want it converted to your local time" ii) "your data is always assumed to be local time and you want it converted to GMT" iii) "your data is always assumed to be GMT and you want me to leave it in GMT, so don't make any adjustments".
  2. When I plot the data (with xyplot), does the plotting function make any visual adjustments to the time? If so, what adjustments?

I think if someone could explain how the internal data structures store timezone information as well as how those data structures are transformed by various commands it would help clear things up. Basically, I would like to work with UTC from the beginning right up to the point of display, where I might wish to make adjustments for timezones, though ideally explicitly rather than the computer silently deciding for me.

like image 749
Bryce Thomas Avatar asked Dec 13 '12 12:12

Bryce Thomas


People also ask

How do you set the time zone in R?

It should be possible to set the time zone via the environment variable TZ: see the section on 'Time zone names' for suitable values. Sys. timezone() will return the value of TZ if set (and on some OSes it is always set), otherwise it will try to retrieve a value which if set for TZ would give the current time zone.

What is the difference between POSIXlt and POSIXct?

There are two POSIX date/time classes, which differ in the way that the values are stored internally. The POSIXct class stores date/time values as the number of seconds since January 1, 1970, while the POSIXlt class stores them as a list with elements for second, minute, hour, day, month, and year, among others.

What time zone does r think dates are in by default?

In R, and most other programming languages, UTC is the common, underlying representation of (date)time, and things like timezones are defined in terms of UTC (EST is UTC-5, hence the “-5” above).

How many time zones are in R?

Romeo Time Zone – R Time Zone (Military Time) Currently has same time zone offset as R (UTC -5) but different time zone name. Romeo Time Zone (R) is 5 hours behind Coordinated Universal Time (UTC). This time zone is a military time zone.


1 Answers

R's Date and Time classes are very powerful, but they take some getting used to. TZ adjustments in particular are tricky, but they are related the construction and conversion particular to/from character.

Consider the following example which limits itself to numeric for input. We have fine control:

R> tt <- as.POSIXct(0, origin="1970-01-01")
R> str(tt)
 POSIXct[1:1], format: "1970-01-01"
R> tt
[1] "1970-01-01 CST"
R> 
R> tt <- as.POSIXct(600, origin="1970-01-01")
R> tt
[1] "1970-01-01 00:10:00 CST"
R> 
tt <- as.POSIXct(600, origin="1970-01-01", tz="UTC")
R> tt
[1] "1970-01-01 00:10:00 UTC"
R> 
R> as.numeric(tt)
[1] 600
R> 

You get the whole date arithmetic, conversion, difftime() etc and can still pass the pure numeric values on. Also:

R> tt <- as.POSIXct(600, origin="1970-01-01", tz="UTC")
R> tt
[1] "1970-01-01 00:10:00 UTC"
R> tt2 <- tt + 1.234567
R> tt2
[1] "1970-01-01 00:10:01.234566 UTC"
R> 

You can use attributes() to check if a TZ has been set:

R> attributes(tt)
$tzone
[1] "UTC"

$class
[1] "POSIXct" "POSIXt" 

R> 

So if you are careful about creation and conversion you can indeed have a full toolkit around UTC-based time data.

like image 55
Dirk Eddelbuettel Avatar answered Oct 05 '22 22:10

Dirk Eddelbuettel