madroan
madroan

Reputation: 183

R lubridate time zone issue

I'd like to change the time zone of a POSIXct object in R, using the with_tz() function in the lubridate package.

This example I pulled from the web works for me:

meeting <- ymd_hms("2011-07-01 09:00:00", tz = "Pacific/Auckland")
with_tz(meeting, "America/Chicago")

But this one does not, using a snippet of some data:

atime <- as.POSIXct("2016-11-04 18:04:30", 
                    format="%Y-%m-%d %H:%M:%S", 
                    tz="PST")
atime_utc <- with_tz(atime, "UTC")

str() and tz() show that the new object has a time zone of "UTC", and is a POSIXct object, but the times are identical. There should be 8 hours between them after the time zone conversion.

Another solution using a different function would be fine, too.

Upvotes: 3

Views: 2544

Answers (1)

John M
John M

Reputation: 1134

The comments above should be well taken, but you can also try force_tz depending on your needs:

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
atime <- as.POSIXct("2016-11-04 18:04:30", 
                    format="%Y-%m-%d %H:%M:%S", 
                    tz="PST")
#> Warning in strptime(x, format, tz = tz): unknown timezone 'PST'
#> Warning in as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...): unknown
#> timezone 'PST'
tz(atime)
#> [1] "PST"
atime_utc <- with_tz(atime, "UTC")
force_tz(atime, "UTC")
#> [1] "2016-11-04 10:04:30 UTC"

Created on 2019-03-03 by the reprex package (v0.2.1)

Upvotes: 1

Related Questions