Ju Ko
Ju Ko

Reputation: 508

How can I keep timezone shifts when converting characters to POSIXct

I have a large dataframe with a column containing date-times, encoded as a factor variable.My Sys.timezone() is "Europe/Berlin". The date-times have this format:

2015-05-05 17:27:04+05:00

where +05:00 represents the timeshift from GMT. Importantly, I have multiple timezones in my dataset, so I cannot set a specific timezone and ignore the last 6 characters of the strings. This is what I tried so far:

 # Test Date
 test <- "2015-05-05 17:27:04+05:00"

 # Removing the ":" to make it readable by %z
 A <- paste(substr(test,1,22),substr(test,24,25),sep = "");A

 # Returns
 # "2015-05-05 17:27:04+0500"


 output <- as.POSIXct(as.character(A, "%Y-%B-%D %H:%M:%S%z"))

 # Returns
 # "2015-05-05 17:27:04 CEST"

The output of "CEST" for +0500 is incorrect. Moreover, when I run this code on the whole column I see that every date is coded as CEST, regardless of the offset.

How can I keep the specified timezone when converting to POSIXct?

Upvotes: 1

Views: 300

Answers (1)

amonk
amonk

Reputation: 1795

In order to facilitate the process you can use lubridate package. E.g.

library("lubridate")#load the package
ymd_hms("2015-05-05 17:27:04+05:00",tz="GMT")#set the date format
[1] "2015-05-05 12:27:04 GMT"

Therefore you keep the timezone info. Finally:

as.POSIXct(ymd_hms("2015-05-05 17:27:04+05:00",tz="GMT"),tz = "GMT")#transform the date into another timezone
[1] "2015-05-05 12:27:04 GMT"

Upvotes: 1

Related Questions