使用POSIXct的奇数时间间隔

问题描述 投票:1回答:1

我可能在2017-10-01凌晨发现了一段奇怪的时间间隔。我有一些陈旧的代码,我经常使用这些代码创建30分钟的时间间隔来汇总和绘制计数观察值。间隔从日落开始到黎明结束 - 所以日期在午夜变化。对于几乎任何一对日期('night')我都要输入,我的代码工作正常。但是在2017-09-30的晚上,它会跳过两个时间间隔02:00和02:30。代码如下。

missing.times <- data.frame(isotime2=seq(as.POSIXct("2017-09-30 17:30:00", format="%Y-%m-%d %H:%M:%S"), as.POSIXct("2017-10-01 06:30:00", format="%Y-%m-%d %H:%M:%S"), by="30 min")); missing.times #missing 0200 and 0230

all.okay <- data.frame(isotime2=seq(as.POSIXct("2017-10-01 17:30:00", format="%Y-%m-%d %H:%M:%S"), as.POSIXct("2017-10-02 06:30:00", format="%Y-%m-%d %H:%M:%S"), by="30 min")); all.okay

我尝试了一个偷偷摸摸的解决方法,但我仍然最终产生了一个差距。

#create intervals for the next date from midnight to 06:30
workaround <- data.frame(isotime2=seq(as.POSIXct("2017-10-02 00:00:00"), as.POSIXct("2017-10-02 06:30:00"), by="30 min")); workaround; str(workaround)

#substitute the following date for the time-gap date 2017-10-01
workaround$isotime2 <-gsub("2017-10-02", "2017-10-01", workaround$isotime2); workaround; str(workaround)

#change the vector "isotime2" from character to POSIXct magically makes time disappear
workaround$isotime2 <-as.POSIXct(workaround$isotime2, format="%Y-%m-%d %H:%M:%S"); workaround; str(workaround)

我是否以某种方式创造了这个时间差距,或者R对时空中的折叠有所了解吗?它发生的时候我睡着了。

r posixct
1个回答
1
投票

因为@ptenax问得这么好......

使用夏令时不会改变的时区(让我们面对它,编码时每个人的存在的祸根)。

第一个data.frame使用澳大利亚/ ACT tz,它在凌晨2点变换,第二个data.frame使用Austalia / Perth tz,它不会因夏令时而改变。

missing.times <- data.frame(isotime2=seq(as.POSIXct("2017-09-30 17:30:00", format="%Y-%m-%d %H:%M:%S", tz="Australia/ACT"), as.POSIXct("2017-10-01 06:30:00", format="%Y-%m-%d %H:%M:%S", tz="Australia/ACT"), by="30 min"))
nrow(missing.times)
missing.times
# misses 2:00 and 2:30

missing.times <- data.frame(isotime2=seq(as.POSIXct("2017-09-30 17:30:00", format="%Y-%m-%d %H:%M:%S", tz="Australia/Perth"), as.POSIXct("2017-10-01 06:30:00", format="%Y-%m-%d %H:%M:%S", tz="Australia/Perth"), by="30 min"))
nrow(missing.times)
missing.times
# does not miss 2:00 and 2:30
© www.soinside.com 2019 - 2024. All rights reserved.