使用 as.POSIXct() 或 lubridate 包中的函数处理夏令时
假设我给出了这个向量x
。我想将它们转换为“America/Detroit”
时区。
x <- c("2018-03-10 23:30:00", "2018-03-11 00:00:00", "2018-03-11 00:30:00", "2018-03-11 01:00:00",
"2018-03-11 01:30:00", "2018-03-11 02:00:00", "2018-03-11 02:30:00",
"2018-03-11 03:00:00",
"2018-03-11 03:30:00", "2018-03-11 04:00:00", "2018-03-11 04:30:00")
如果我按如下方式使用 ymd_hms()
,由于 DST,我会得到两个 NA
。
ymd_hms(x, tz = "America/Detroit")
[1] "2018-03-10 23:30:00 EST" "2018-03-11 00:00:00 EST" "2018-03-11 00:30:00 EST"
[4] "2018-03-11 01:00:00 EST" "2018-03-11 01:30:00 EST" NA
[7] NA "2018-03-11 03:00:00 EDT" "2018-03-11 03:30:00 EDT"
[10] "2018-03-11 04:00:00 EDT" "2018-03-11 04:30:00 EDT"
Warning message:
2 failed to parse.
有没有办法将 x
格式化为日期时间,以便“2018-03-11 02:00:00”之后的所有内容都向前滚动一小时?最终的日期时间应如下所示:
c("2018-03-10 23:30:00 EST", "2018-03-11 00:00:00 EST", "2018-03-11 00:30:00 EST", "2018-03-11 01:00:00 EST",
"2018-03-11 01:30:00 EST", "2018-03-11 03:00:00 EDT", "2018-03-11 03:30:00 EDT",
"2018-03-11 04:00:00 EDT", "2018-03-11 04:30:00 EDT", "2018-03-11 05:00:00 EDT", "2018-03-11 05:30:00 EDT")
如果我使用 force_tz()
,它不会执行我想要的操作。
force_tz(ymd_hms(x), tz="America/Detroit", roll = TRUE)
[1] "2018-03-10 23:30:00 EST" "2018-03-11 00:00:00 EST" "2018-03-11 00:30:00 EST"
[4] "2018-03-11 01:00:00 EST" "2018-03-11 01:30:00 EST" "2018-03-11 03:00:00 EDT"
[7] "2018-03-11 03:00:00 EDT" "2018-03-11 03:00:00 EDT" "2018-03-11 03:30:00 EDT"
[10] "2018-03-11 04:00:00 EDT" "2018-03-11 04:30:00 EDT"
Suppose I have given this vector x
. I want to convert these to "America/Detroit"
time zone.
x <- c("2018-03-10 23:30:00", "2018-03-11 00:00:00", "2018-03-11 00:30:00", "2018-03-11 01:00:00",
"2018-03-11 01:30:00", "2018-03-11 02:00:00", "2018-03-11 02:30:00",
"2018-03-11 03:00:00",
"2018-03-11 03:30:00", "2018-03-11 04:00:00", "2018-03-11 04:30:00")
If I use ymd_hms()
as follows I get two NA
because of DST.
ymd_hms(x, tz = "America/Detroit")
[1] "2018-03-10 23:30:00 EST" "2018-03-11 00:00:00 EST" "2018-03-11 00:30:00 EST"
[4] "2018-03-11 01:00:00 EST" "2018-03-11 01:30:00 EST" NA
[7] NA "2018-03-11 03:00:00 EDT" "2018-03-11 03:30:00 EDT"
[10] "2018-03-11 04:00:00 EDT" "2018-03-11 04:30:00 EDT"
Warning message:
2 failed to parse.
Is there a way to format x
to date-time such that everything after "2018-03-11 02:00:00" rolls forward by one hour? The final date-times should look as follows:
c("2018-03-10 23:30:00 EST", "2018-03-11 00:00:00 EST", "2018-03-11 00:30:00 EST", "2018-03-11 01:00:00 EST",
"2018-03-11 01:30:00 EST", "2018-03-11 03:00:00 EDT", "2018-03-11 03:30:00 EDT",
"2018-03-11 04:00:00 EDT", "2018-03-11 04:30:00 EDT", "2018-03-11 05:00:00 EDT", "2018-03-11 05:30:00 EDT")
If I use force_tz()
, it doesn't do what I want.
force_tz(ymd_hms(x), tz="America/Detroit", roll = TRUE)
[1] "2018-03-10 23:30:00 EST" "2018-03-11 00:00:00 EST" "2018-03-11 00:30:00 EST"
[4] "2018-03-11 01:00:00 EST" "2018-03-11 01:30:00 EST" "2018-03-11 03:00:00 EDT"
[7] "2018-03-11 03:00:00 EDT" "2018-03-11 03:00:00 EDT" "2018-03-11 03:30:00 EDT"
[10] "2018-03-11 04:00:00 EDT" "2018-03-11 04:30:00 EDT"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的解释和所需的输出来看,您需要在EST(东部标准时间)中判断数据是多少时区。
还要注意
force_tz
和with_tz
force_tz
保持时间相同,但会改变时区,这导致该集合的不存在时间时区。使用_tz
当您想将时间转换为另一个时(告诉我在这里与其他地方相比,请告诉我多晚)。因此,我们将您的EST时区带到“美国/底特律”的时代,其中包括EST和EDT Times
解决方案
将其与之相比
You need to tell what timezone your data is, from your explanation and desired output it seems you have all times in EST (Eastern Standard Time).
Also note the difference between
force_tz
andwith_tz
force_tz
keeps the time the same but alters the timezone, this results in non-existing times for that set timezone.with_tz
you use when you want to convert times from one to another (tell me how late it is here versus somewhere else).So we take your EST timezone and convert that to the times of "America/Detroit" which includes both EST and EDT times
solution
Compare that to