How to change the default time zone in R?
Asked Answered
D

4

75

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.

Dowitcher answered 16/6, 2011 at 15:55 Comment(2)
Have you read ?timezone and tried anything therein?Diagram
I had a similar problem and needed a fast solution. I used the Jupyter for R. It is set up probably for a UTC time zone.Hargis
D
104

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

Dowitcher answered 20/6, 2011 at 7:31 Comment(3)
On a Mac, when you reset the R session, this seems to go away and is back to ""Fadein
For other regions you can use this timezone list. I used Sys.setenv(TZ = "America/Sao_Paulo") and worked nice.Riches
@Fadein That is correct, the R session doesn't (and shouldn't) save the process environment. Another approach is needed for people who rely on saving/restoring the session.Amianthus
B
14

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"
Batfowl answered 16/6, 2011 at 16:9 Comment(0)
A
10

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.

Annabell answered 16/6, 2011 at 15:59 Comment(1)
I did change my windows configuration. And I also had to change in R the R environment variable TZ like this : TZ="UTC". Thank you for the help.Dowitcher
H
7

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.

Hendley answered 17/6, 2015 at 3:10 Comment(1)
This is an awesome answer to "permanently" set the timezone in R. But what I don't get: why is it even showing this message? My R: v3.4.2 on macOS High Sierra.Copolymer

© 2022 - 2024 — McMap. All rights reserved.