使用 as.POSIXct() 或 lubridate 包中的函数处理夏令时

发布于 2025-01-20 23:13:11 字数 1716 浏览 1 评论 0原文

假设我给出了这个向量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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彡翼 2025-01-27 23:13:11

从您的解释和所需的输出来看,您需要在EST(东部标准时间)中判断数据是多少时区。

还要注意force_tzwith_tz

force_tz保持时间相同,但会改变时区,这导致该集合的不存在时间时区。

使用_tz当您想将时间转换为另一个时(告诉我在这里与其他地方相比,请告诉我多晚)。

因此,我们将您的EST时区带到“美国/底特律”的时代,其中包括EST和EDT Times

解决方案

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")

x <- ymd_hms(x, tz = "EST") # here we tell all dates we have are in EST

with_tz(x, "America/Detroit")

[1] "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"
 [8] "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(x, "America/Detroit", roll = T)

 [1] "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:00:00 EDT"
 [8] "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"

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 and with_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

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")

x <- ymd_hms(x, tz = "EST") # here we tell all dates we have are in EST

with_tz(x, "America/Detroit")

[1] "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"
 [8] "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"

Compare that to

force_tz(x, "America/Detroit", roll = T)

 [1] "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:00:00 EDT"
 [8] "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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文