我想计算夏令时和非夏令时之间的时差。但我不知道如何让R知道一个时间是夏令时还是非夏令时。
例如,凤凰城在夏季不调整夏令时间,而美国大部分地区则是这样。如果我想计算下面的时间差,应该是3小时,而不是2小时。tzone =“美国/凤凰”将自动将时间设置为"MST",这是夏令时,但这不是我想要的。
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.
我试图通过设置tzone="EDT“或"MDT”来解决这个问题。但似乎R不允许识别这些时区。
> 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'
发布于 2017-02-21 20:42:05
您有一个问题,因为EST
。来自?timezone
请注意,有些指定可能不是您所期望的:特别是EST是一个在加拿大使用的时区,没有夏令时。
使用US/Eastern
或America/New_York
而不是EST
。有关详细信息,请参阅?OlsonNames()
。
#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
https://stackoverflow.com/questions/42376462
复制相似问题