Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you convert dates/times from one time zone to another in R?

Tags:

r

If I have a date like this in London time: "2009-06-03 19:30", how can I convert it to the equivalent time in the US West Coast?

like image 473
David Smith Avatar asked Sep 08 '09 17:09

David Smith


People also ask

How do I change date format in R?

To format = , provide a character string (in quotes) that represents the current date format using the special “strptime” abbreviations below. For example, if your character dates are currently in the format “DD/MM/YYYY”, like “24/04/1968”, then you would use format = "%d/%m/%Y" to convert the values into dates.

What is POSIXct format in R?

POSIXct stores date and time in seconds with the number of seconds beginning at 1 January 1970. Negative numbers are used to store dates prior to 1970. Thus, the POSIXct format stores each date and time a single value in units of seconds. Storing the data this way, optimizes use in data.

What is UTC time now in 24 hour format?

UTC time in ISO-8601 is 19:36:05Z. Note that the Z letter without a space.

What is POSIXlt?

The POSIXlt class stores date/time values as a list of components (hour, min, sec, mon, etc.) making it easy to extract these parts. To get the current date, the Sys. Date function will return a Date object which can be converted to a different class if necessary.


2 Answers

Package lubridate holds two functions to convert timezones. According to the help pages:


force_tz returns a date-time that has the same clock time as x in the new time zone.

force_tz(time, tzone = "America/Los_Angeles") 


with_tz changes the time zone in which an instant is displayed. The clock time displayed for the instant changes, but the moment of time described remains the same.

with_tz(time, tzone = "America/Los_Angeles") 
like image 178
nnn Avatar answered Sep 20 '22 04:09

nnn


Use attr() (base R) or lubridate::with_tz() (tidyverse) to view the same moment of time in a different time zone.

The internal value of a POSIXct object is always in UTC (number of seconds since the beginning of 1970; see ?DateTimeClasses, but the displayed time when printed is determined by the tzone attribute. So change the tzone attribute (via attr() or lubridate::with_tz()) to display/print the same moment of time in a different time zone.

For example:

pb.txt <- "2009-06-03 19:30"   pb.date <- as.POSIXct(pb.txt, tz="Europe/London")  

Note tzone attribute determines time zone used when displayed/printed:

attributes(pb.date)$tzone [1] "Europe/London"  pb.date [1] "2009-06-03 19:30:00 BST" 

Note that internal value is seconds since origin in UTC, regardless of tzone attribute:

as.numeric(pb.date) [1] 1244053800 

In base R, the tzone attribute can be set/changed using attr():

attr(pb.date, "tzone") <- "America/Los_Angeles"  pb.date   [1] "2009-06-03 11:30:00 PDT"   

Equivalently, use lubridate::with_tz() (which also uses attr):

pb.date <- lubridate::with_tz(pb.date, "America/Los_Angeles") 

Note that display time is now local clock time in PDT:

pb.date   [1] "2009-06-03 11:30:00 PDT" 

but internal value has not changed:

as.numeric(pb.date) [1] 1244053800 

so tzone does not affect any operations, only the displayed value when printed.

like image 44
Chris Holbrook Avatar answered Sep 23 '22 04:09

Chris Holbrook