首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >计算夏令时与非夏令时的时差

计算夏令时与非夏令时的时差
EN

Stack Overflow用户
提问于 2017-02-21 19:40:49
回答 3查看 863关注 0票数 1

我想计算夏令时和非夏令时之间的时差。但我不知道如何让R知道一个时间是夏令时还是非夏令时。

例如,凤凰城在夏季不调整夏令时间,而美国大部分地区则是这样。如果我想计算下面的时间差,应该是3小时,而不是2小时。tzone =“美国/凤凰”将自动将时间设置为"MST",这是夏令时,但这不是我想要的。

代码语言:javascript
运行
复制
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不允许识别这些时区。

代码语言:javascript
运行
复制
> 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'
EN

Stack Overflow用户

回答已采纳

发布于 2017-02-21 20:42:05

您有一个问题,因为EST。来自?timezone

请注意,有些指定可能不是您所期望的:特别是EST是一个在加拿大使用的时区,没有夏令时。

使用US/EasternAmerica/New_York而不是EST。有关详细信息,请参阅?OlsonNames()

代码语言:javascript
运行
复制
#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
票数 3
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42376462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档