Change Timezone in a Posixct Object

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"

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.

Determine and set timezone in POSIXct, POSIXlt, strptime, etc. in R

If you do not use a timezone specifically, POSIXct and POSIXlt will reference to your local timezone. However, this is not entirely reliable. POSIXlt will not display the timezone in the output string.

Note, the tzone argument is not set.

t.ct <- as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z")
t.lt <- as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z")
t.ct
t.lt
attr(t.ct,"tzone") #""
attr(t.lt,"tzone") #NULL

If you do want to avoid ambiguous behaviour, you have to specifiy a time zone. The output string will still be different (by default POSIXlt shows no timezone), but the attribute is the same

t.ct <- as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="Europe/Helsinki")
t.lt <- as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="Europe/Helsinki")
t.ct
t.lt
attr(t.ct,"tzone") #Europe/Helsinki
attr(t.lt,"tzone") #Europe/Helsinki

Now, if you want to change time zones after the original assignment:

attr(t.ct, "tzone") <- "UTC" #this will SHIFT the time zone to UTC
attr(t.lt, "tzone") <- "UTC" #this will REPLACE the time zone to UTC
t.ct
t.lt

As for your problem with strftime and %z, this does not give you the time zone attribute. The difference in your case, probably comes from a combination of string formatting, object conversions and time zone formating, IMO. But maybe somebody more knowledgable, can clarify this.

How to get as.POSIXct to not change timezone of string?

x <- "2016-09-13 22:27:37.320 UTC"
as.POSIXct(x, format = "%Y-%m-%d %H:%M:%S", tz ='EST')
[1] "2016-09-13 22:27:37 EST"

change timezone for some POSIXct entries in a data frame in R

The problem is that tzone is a property of the entire vector. Each element cannot have their own timezone. You can change the timezone for the entire vector. Consider this example

x<-as.POSIXct(c("2012-02-01 00:00:00","2012-02-01 00:05:00"), tz="America/New_York")
attributes(x[1])$tzone
# [1] "America/New_York"

# does not change
attributes(x[1])$tzone<-"America/Los_Angeles"
attributes(x[1])$tzone
# [1] "America/New_York"

#changes
attributes(x)$tzone<-"America/Los_Angeles"
attributes(x[1])$tzone
# [1] "America/Los_Angeles"

If you have dates from different time zones, you can specify the time zone with a UTC offset and then they will all be converted to a common timezone

x<-as.POSIXct(c("2012-02-01 00:00:00-0800","2012-02-01 00:05:00-0500"), 
format="%Y-%m-%d %H:%M:%S%z", tz="America/Los_Angeles")
# [1] "2012-02-01 00:00:00 PST" "2012-01-31 21:05:00 PST"


Related Topics



Leave a reply



Submit