How to change the default time zone in R?

RTimezoneDate Arithmetic

R Problem Overview


How can I change the default timezone in R? I'm working with time series. All my time series are defined in UTC time zone, but if I print a date it is always done in CET/CEST time zone.

R Solutions


Solution 1 - R

Another way to do it, without changing the whole computer time is using the setenv command like this : Sys.setenv(TZ='GMT')

Solution 2 - R

See this good article on changing time zone in R:

http://blog.revolutionanalytics.com/2009/06/converting-time-zones.html

Shortly (in case the link will be unavailable in the future):

# your time string
pb.txt <- "2009-06-03 19:30"
# convert it to R object for London time zone
pb.date <- as.POSIXct(pb.txt, tz="Europe/London")
# convert it to PDT time zone
format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
[1] "2009-06-03 11:30:00 PDT"

# can be also done for many date at once
d <- c("2009-03-07 12:00", "2009-03-08 12:00", "2009-03-28 12:00", "2009-03-29 12:00", "2009-10-24 12:00", "2009-10-25 12:00", "2009-10-31 12:00", "2009-11-01 12:00")
t1 <- as.POSIXct(d,"America/Los_Angeles")
cbind(US=format(t1),UK=format(t1,tz="Europe/London"))

     US                    UK                   
[1,] "2009-03-07 12:00:00" "2009-03-07 20:00:00"
[2,] "2009-03-08 12:00:00" "2009-03-08 19:00:00"
[3,] "2009-03-28 12:00:00" "2009-03-28 19:00:00"
[4,] "2009-03-29 12:00:00" "2009-03-29 20:00:00"
[5,] "2009-10-24 12:00:00" "2009-10-24 20:00:00"
[6,] "2009-10-25 12:00:00" "2009-10-25 19:00:00"
[7,] "2009-10-31 12:00:00" "2009-10-31 19:00:00"
[8,] "2009-11-01 12:00:00" "2009-11-01 20:00:00"

Solution 3 - R

What operating system?

In general, see help(Startup) as you can set values via .Renviron and its site-wide variant.

But you should probably set this for your machine as a whole, which under Linux may alter the file /etc/timezone, and on Windows you'd set a system-wide environment variable TZ.

Lastly, if your formatted display of dates and time shows CET/CEST, this may already be set as a system default and your question really is how to set your UTC times correctly in your R objects.

Solution 4 - R

I found @Dirk's answer very useful for Ubuntu, so I thought I would expand on it.

From help(Startup) we see that environment variables are set by the Renviron.site file:

> Unless --no-environ was given on the command line, R searches for site and user files to process for setting environment variables. The name of the site file is the one pointed to by the environment variable R_ENVIRON; if this is unset, ‘R_HOME/etc/Renviron.site’ is used

We can find the path to R_HOME by using the function R.home(), which in my case returns:

> R.home()
[1] "/usr/lib/R"

Therefore, the Renviron.site file is found (for me) in /usr/lib/R/etc/.

Simply open this file, and insert the line:

TZ="UTC"

or similar.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul PUGETView Question on Stackoverflow
Solution 1 - RPaul PUGETView Answer on Stackoverflow
Solution 2 - RyukView Answer on Stackoverflow
Solution 3 - RDirk EddelbuettelView Answer on Stackoverflow
Solution 4 - RAlexView Answer on Stackoverflow