73

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.

smci
  • 32,567
  • 20
  • 113
  • 146
Paul PUGET
  • 1,849
  • 2
  • 12
  • 7

4 Answers4

101

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

Paul PUGET
  • 1,849
  • 2
  • 12
  • 7
  • 4
    On a Mac, when you reset the R session, this seems to go away and is back to "" – Jas Jan 05 '18 at 20:18
  • 4
    For other regions you can use [this timezone list](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). I used `Sys.setenv(TZ = "America/Sao_Paulo")` and worked nice. – Murta Mar 20 '20 at 02:49
  • 3
    @Jas 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. – DomQ Nov 16 '20 at 16:10
13

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"
yuk
  • 19,098
  • 13
  • 68
  • 99
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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 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. – Paul PUGET Jun 17 '11 at 11:45
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.

Alex
  • 15,186
  • 15
  • 73
  • 127
  • 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. – Sander W. van der Laan Oct 27 '17 at 08:14