69

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.

2
  • Have you read ?timezone and tried anything therein? Jun 16, 2011 at 16:02
  • 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. Jan 27, 2017 at 9:08

4 Answers 4

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

96

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

3
  • 4
    On a Mac, when you reset the R session, this seems to go away and is back to ""
    – Jas
    Jan 5, 2018 at 20:18
  • 4
    For other regions you can use this timezone list. I used Sys.setenv(TZ = "America/Sao_Paulo") and worked nice.
    – Murta
    Mar 20, 2020 at 2: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, 2020 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"
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.

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.
    – Paul PUGET
    Jun 17, 2011 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.

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. Oct 27, 2017 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.