Search code examples
rnaposixct

Handling of switch to daylight saving time with POSIXct in R


I have hourly data associated with time stamps of the following format.

xx <- c("2019-03-30 12:00", "2019-03-30 13:00", "2019-03-30 14:00", "2019-03-30 15:00", "2019-03-30 16:00", "2019-03-30 17:00", "2019-03-30 18:00", "2019-03-30 19:00", "2019-03-30 20:00", "2019-03-30 21:00", "2019-03-30 22:00", "2019-03-30 23:00", "2019-03-31 00:00", "2019-03-31 01:00", "2019-03-31 02:00","2019-03-31 03:00", "2019-03-31 04:00", "2019-03-31 05:00", "2019-03-31 06:00", "2019-03-31 07:00", "2019-03-31 08:00", "2019-03-31 09:00", "2019-03-31 10:00", "2019-03-31 11:00", "2019-03-31 12:00")

If I convert this to POSIXct, I get a format stripped of the hours:

> as.POSIXct(xx)
 [1] "2019-03-30 CET" "2019-03-30 CET" "2019-03-30 CET"
 [4] "2019-03-30 CET" "2019-03-30 CET" "2019-03-30 CET"
 [7] "2019-03-30 CET" "2019-03-30 CET" "2019-03-30 CET"
[10] "2019-03-30 CET" "2019-03-30 CET" "2019-03-30 CET"
[13] "2019-03-31 CET" "2019-03-31 CET" "2019-03-31 CET"
[16] "2019-03-31 CET" "2019-03-31 CET" "2019-03-31 CET"
[19] "2019-03-31 CET" "2019-03-31 CET" "2019-03-31 CET"
[22] "2019-03-31 CET" "2019-03-31 CET" "2019-03-31 CET"
[25] "2019-03-31 CET"

But I need to retain the hourly timestamp. However if I execute as.POSIXct() with the correct formatting option, I get the following problem:

> as.POSIXct(xx, format = "%Y-%m-%d %H:%M")
 [1] "2019-03-30 12:00:00 CET"  "2019-03-30 13:00:00 CET" 
 [3] "2019-03-30 14:00:00 CET"  "2019-03-30 15:00:00 CET" 
 [5] "2019-03-30 16:00:00 CET"  "2019-03-30 17:00:00 CET" 
 [7] "2019-03-30 18:00:00 CET"  "2019-03-30 19:00:00 CET" 
 [9] "2019-03-30 20:00:00 CET"  "2019-03-30 21:00:00 CET" 
[11] "2019-03-30 22:00:00 CET"  "2019-03-30 23:00:00 CET" 
[13] "2019-03-31 00:00:00 CET"  "2019-03-31 01:00:00 CET" 
[15] NA                         "2019-03-31 03:00:00 CEST"
[17] "2019-03-31 04:00:00 CEST" "2019-03-31 05:00:00 CEST"
[19] "2019-03-31 06:00:00 CEST" "2019-03-31 07:00:00 CEST"
[21] "2019-03-31 08:00:00 CEST" "2019-03-31 09:00:00 CEST"
[23] "2019-03-31 10:00:00 CEST" "2019-03-31 11:00:00 CEST"
[25] "2019-03-31 12:00:00 CEST"

Apparently POSIXct cannot handle switches in daylight saving time? What's going on here?

I know I can solve this by using lubridates' ymd_hm(), but I pose this question in order to get understanding of the workings here. Is it possible to solve this in base R, or does s.POSIXct have a basic disfunctionality here?

Thanks.

EDIT: SOLUTION

Thanks to zoowalk and Roland in the comments for this solution:

My timeseries was recorded without time switches. However my OS time zone does record time switches throughout the year. Accordingly, I need to hand a time zone to the function that equally does not have time switches, like UTC:

as.POSIXct(xx, format = "%Y-%m-%d %H:%M", tz="UTC")


Solution

  • SOLUTION

    Thanks to zoowalk and Roland in the comments for this solution:

    My timeseries was recorded without time switches. However my OS time zone does record time switches throughout the year. Accordingly, I need to hand a time zone to the function that equally does not have time switches, like UTC:

    as.POSIXct(xx, format = "%Y-%m-%d %H:%M", tz="UTC")