R在DST时间更改前后未始终转换时间

时间:2018-12-07 01:57:06

标签: r date timezone dst

在Windows计算机上使用R

as.POSIXct('2018-11-04 01:30:00', tz = 'America/Chicago')
# "2018-11-04 01:30:00 CDT"

as.POSIXct('2018-11-04 01:40:00', tz = 'America/Chicago')
# "2018-11-04 01:40:00 CST"

我知道这些时间是模棱两可的(从技术上讲可以是CDT或CST),R可能很难解释。但是,这些结果的随机性使得很难在这些时间段内转换时间。有没有办法解决这个问题,以获得更一致的结果?

1 个答案:

答案 0 :(得分:1)

如果您看一下整数表示,您会发现:

# divide by 60 to convert to minutes from seconds
as.integer(as.POSIXct('2018-11-04 01:30:00', tz = 'America/Chicago'))/60
# [1] 25688550

as.integer(as.POSIXct('2018-11-04 01:40:00', tz = 'America/Chicago'))/60
# [1] 25688620

相距70分钟,这意味着如果CDT继续,后者相当于2:40;这对我来说似乎很一致。我无法在快速搜索中找到关于特别是America/Chicago时区的夏令时开关的详细信息的参考。

此外,在这种情况下,您可以考虑查看POSIXlt表示形式:

dput(as.POSIXlt('2018-11-04 01:30:00', tz = 'America/Chicago'))
# structure(list(sec = 0, min = 30L, hour = 1L, mday = 4L, mon = 10L, 
#     year = 118L, wday = 0L, yday = 307L, isdst = 1L, zone = "CDT", 
#     gmtoff = NA_integer_), class = c("POSIXlt", "POSIXt"), tzone = "America/Chicago")

dput(as.POSIXlt('2018-11-04 01:40:00', tz = 'America/Chicago'))
# structure(list(sec = 0, min = 40L, hour = 1L, mday = 4L, mon = 10L, 
#     year = 118L, wday = 0L, yday = 307L, isdst = 0L, zone = "CST", 
#     gmtoff = NA_integer_), class = c("POSIXlt", "POSIXt"), tzone = "America/Chicago")

请注意两个对象之间isdst的区别...

要继续,我们确实需要找到有关CDT / CST划分方式的权威资料。但是FWIW,切换的确切时间似乎是1:30:05

as.POSIXct('2018-11-04 01:30:05', tz = 'America/Chicago')
# [1] "2018-11-04 01:30:05 CDT"

as.POSIXct('2018-11-04 01:30:06', tz = 'America/Chicago')
# [1] "2018-11-04 01:30:06 CST"

(为弄清楚这一点,我看了看输出:)

with(expand.grid(second = 0:59, minute = 30:40),
     as.POSIXct(sprintf('2018-11-04 01:%02d:%02d', minute, second),
                tz = 'America/Chicago'))