Reputation: 141
I may have discovered a curious gap in the passage of time in the early hours of 2017-10-01. I have some well-worn code that I use often to create 30 minute intervals for summarising and plotting count observations. The intervals start at around sunset and end around dawn - so the date changes at midnight. For almost any pair of dates ('night') I care to input, my code works fine. But for the night of 2017-09-30, it skips two intervals 02:00 and 02:30. Code below.
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
I tried a sneaky workaround, but I still ultimately produce a gap.
#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)
Have I somehow created this time gap, or does R know something about a fold in space-time? I was asleep when it happened.
Upvotes: 1
Views: 152
Reputation: 5861
Because @ptenax asked so nicely...
Use a time zone that doesn't change with daylight savings (let's face it, the bane of everyone's existence when coding).
First data.frame uses Australia/ACT tz which changes over at 2 AM, second data.frame uses Austalia/Perth tz which does not change for daylight savings.
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
Upvotes: 1