How to Change the Default Time Zone in R

How to change the default time zone in R?

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

change time zone in R without it returning to original time zone

as.Date unfortunately defaults to UTC as noted in the documentation, so you can manually specify the timezone to get it to work. Alternatively, you can use the date() extractor from lubridate which is better at respecting timezones.

df <- data.frame(c("2018-09-28 00:00:00Z","2018-09-29 01:00:00Z","2018-09-30 10:00:00Z"))
names(df) <- "startTime"
df$startTime <- as.POSIXct(df$startTime, tz="Etc/UTC")
attributes(df$startTime)$tzone <- "America/New_York"

as.Date(df$startTime, tz="America/New_York")
#> [1] "2018-09-27" "2018-09-28" "2018-09-30"
lubridate::date(df$startTime)
#> [1] "2018-09-27" "2018-09-28" "2018-09-30"

Created on 2018-09-25 by the reprex package (v0.2.0).

How to change default sys.time options?

You can set TZ="TURKEY" in your .Renvironment file. This will load each time you (re)start R, and thus you will always get the timezone you want.

.Renvironment files can be created for each R project, or globally for all your projects. You can see this answer for how to create a global .Renvironment file .

Change timezone in a POSIXct object

It doesn't work with POSIXct because base::as.POSIXct.default simply returns x if it's already POSIXct. You can change the timezone via the tzone attribute:

attr(data$dateTime, "tzone") <- "Europe/Paris"

R Change from stardard UTC time zone to multiple local time zones

Multiple problems here:

  1. format (and other time-related functions) only takes a length-1 argument for tz;
  2. time zones recognized by R do not include the popular "CST", "PST", etc.

To fix the first, the use of Map or mapply will suffice.

The second requires a little more research, unfortunately. Zones like "PST" and such, though popular in at least the US if not other countries, are not valid time zone strings (ref: CCTZ, a C++ library for translating between time zones, says so). Neither are "GMT-7", et al, though the latter can be faked by prepending with Etc/, as in: "Etc/GMT-7". Or you can go with the alternatives of "America/New_York" or "US/Eastern".

df$time_zone <- c("US/Eastern", "US/Pacific", "US/Central", "US/Eastern")
df
# day time_zone
# 1 2018-12-06 15:40:29 US/Eastern
# 2 2018-12-06 15:25:28 US/Pacific
# 3 2018-12-06 15:25:28 US/Central
# 4 2018-12-06 14:09:09 US/Eastern
mapply(format, df$day, tz = "GMT")
# [1] "2018-12-06 15:40:29" "2018-12-06 15:25:28" "2018-12-06 15:25:28"
# [4] "2018-12-06 14:09:09"
mapply(format, df$day, tz = df$time_zone)
# [1] "2018-12-06 10:40:29" "2018-12-06 07:25:28" "2018-12-06 09:25:28"
# [4] "2018-12-06 09:09:09"

All of the immediately-recognizable formats for R's time zones are found in a 594-element vector:

str(OlsonNames())
# chr [1:592] "Africa/Abidjan" "Africa/Accra" "Africa/Addis_Ababa" ...
# - attr(*, "Version")= chr "2018e"
set.seed(2)
sample(OlsonNames(), size=8)
# [1] "America/El_Salvador" "Etc/GMT+8" "Atlantic/Madeira"
# [4] "America/Creston" "Pacific/Port_Moresby" "Pacific/Ponape"
# [7] "America/Atka" "GB-Eire"
grep("US/", OlsonNames(), value = TRUE)
# [1] "US/Alaska" "US/Aleutian" "US/Arizona"
# [4] "US/Central" "US/East-Indiana" "US/Eastern"
# [7] "US/Hawaii" "US/Indiana-Starke" "US/Michigan"
# [10] "US/Mountain" "US/Pacific" "US/Pacific-New"
# [13] "US/Samoa"

In this example, you'll see one of the alternatives you can use: "Etc/GMT+8". Notice that + is to the west of the prime meridian, so

mapply(format, df$day, tz = "US/Eastern")
# [1] "2018-12-06 10:40:29" "2018-12-06 10:25:28" "2018-12-06 10:25:28"
# [4] "2018-12-06 09:09:09"
mapply(format, df$day, tz = "Etc/GMT+5")
# [1] "2018-12-06 10:40:29" "2018-12-06 10:25:28" "2018-12-06 10:25:28"
# [4] "2018-12-06 09:09:09"

Caveat emptor: using "US/Eastern" should take into account daylight savings where appropriate; "Etc/GMT+5" does not, I believe.

How to change a time zone in a data frame?

First, you can change the time zone of a POSIXct variable. It is not meaningful to "change the time zone in a data.frame", so setting a "tz" attribute of a data.frame does nothing.

[ Note: it is meaningful, however, to change the time zone of an xts object. See this post. ]

I gather that your timestamps are in GMT and you want to convert that to the equivalent in PST. If this is what you are intending, then this should work:

df$posix.date <- as.POSIXct(as.integer(df$posix.date),
origin="1970-01-01",
tz="American/Los_Angeles")

For example:

x <- as.POSIXct("2015-01-01 12:00:00", tz="Europe/London")
x
# [1] "2015-01-01 12:00:00 GMT"
as.POSIXct(as.integer(x),origin="1970-01-01",tz="America/Los_Angeles")
# [1] "2015-01-01 04:00:00 PST"

The issue here is that as.POSIXct(...) works differently depending on the class of the object passed to it. If you pass a character or integer, the time zone is set according to tz=.... If you pass an object that is already POSIXct, the tz=... argument is ignored. So here we convert x to integer so the tz=... argument is respected.

Really convoluted. If there's an easier way I'd love to hear about it.

Modifying timezone of a POSIXct object without changing the display

EDITED:

My previous solution was passing a character value to origin (i.e.origin="1970-01-01"). That only worked here because of a bug (#PR14973) that has now been fixed in R-devel.

origin was being coerced to POSIXct using the tz argument of the as.POSIXct call, and not "GMT" as it was documented to do. The behavior has been changed to match the documentation which, in this case, means that you have to specify your timezone for both the origin and the as.POSIXct call.

datetime
#[1] "2011-01-01 12:32:23.233 GMT"
as.POSIXct(as.numeric(datetime), origin=as.POSIXct("1970-01-01", tz="Europe/Paris"),
tz="Europe/Paris")
#[1] "2011-01-01 12:32:23.233 CET"

This will also works in older versions of R.



Related Topics



Leave a reply



Submit