I want to calculate time differences between daylight saving time and non-daylight saving time. But I do not know how to let R know that a time is daylight saving time or not.
For example, Phoenix do not adjust daylight saving time in the summer, whereas most areas in US do. If I wanna calculate the time differences in the following, it's supposed to be 3 hours rather than 2 hours. tzone = "America/Phoenix" will automatically set the time as "MST", which is a daylight saving time, but this is not what I want.
library(lubridate)
x <- "22/5/2016 23:50"
x <- dmy_hm(x)
x1 <- force_tz(x, tzone = "America/Phoenix")
x2 <- force_tz(x, tzone = "EST")
x1-x2
# The output is "Time difference of 2 hours". But actually it is supposed to be 3 hours.
I tried by setting tzone="EDT" or "MDT" to fix that. But it seems that R do not allow recognize those timezones.
> x2 <- force_tz(y, tzone = "EDT")
Warning messages:
1: In as.POSIXct.POSIXlt(lt) : unknown timezone 'EDT'
2: In as.POSIXlt.POSIXct(ct) : unknown timezone 'EDT'
> x3 <- force_tz(y, tzone = "MDT")
Warning messages:
1: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'EDT'
2: In as.POSIXct.POSIXlt(lt) : unknown timezone 'MDT'
3: In as.POSIXlt.POSIXct(ct) : unknown timezone 'MDT'
You have a problem because of EST
. From ?timezone
:
Beware that some of these designations may not be what you expect: in particular EST is a time zone used in Canada without daylight saving time...
Use US/Eastern
or America/New_York
instead of EST
. See ?OlsonNames()
for more information.
#DST
x1 = as.POSIXct("22/5/2016 23:50", format = "%d/%m/%Y %H:%M", tz = "America/New_York")
x2 = as.POSIXct("22/5/2016 23:50", format = "%d/%m/%Y %H:%M", tz = "America/Phoenix")
x2 - x1
#Time difference of 3 hours
#NOT DST
x1 = as.POSIXct("22/12/2016 23:50", format = "%d/%m/%Y %H:%M", tz = "America/New_York")
x2 = as.POSIXct("22/12/2016 23:50", format = "%d/%m/%Y %H:%M", tz = "America/Phoenix")
x2 - x1
#Time difference of 2 hours