我想计算夏令时和非夏令时之间的时差。但我不知道如何让R知道一段时间是否是夏令时。计算夏令时和非夏令时之间的时差
例如,凤凰城在夏季不调整夏令时,而美国的大部分地区都是如此。如果我想计算下面的时差,它应该是3小时而不是2小时。 tzone =“America/Phoenix”会自动将时间设置为“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'
* *夏天* *不是冬天* * 3 *小时。看到我的答案。 –
谢谢,德克!你的代码有效。但我的代码中的意思是夏天。我把月份放在月份前面。为了澄清它,我将原始日期修改为22/5/2016 – Bin
啊。这是一种常见的日期格式,但很容易出错(至少在世界的这一部分)。最好坚持2016-05-22。 –