Character POSIXct Conversion in R causes wrong timezone values on daylight saving time transition (CEST/CET))

后端 未结 1 914
执念已碎
执念已碎 2020-12-05 15:05

I have a problem converting POSIXct to character and back in POSIXct in R. I run the following code:

time_seq_01 <- seq(as.POSIXct(\"2012-10-28 02:00:00\"         


        
相关标签:
1条回答
  • 2020-12-05 15:31

    Here's a work around that goes from POSIXct to character back to POSIXct preserving the original daylight savings time status.

    Sys.setenv(TZ='Europe/Berlin') # to reproduce OP's example
    time_seq_01 <- seq(as.POSIXct("2012-10-28 02:00:00"), by = 900, length.out = 10)
    time_seq_02 <- format(time_seq_01,usetz = TRUE)
    
    time_seq_02_lt <- as.POSIXlt(time_seq_02)
    time_seq_02_lt$isdst <- as.POSIXlt(time_seq_01)$isdst
    time_seq_03 <- as.POSIXct(time_seq_02_lt)
    

    As far as I can tell, R's support for string-to-datetime doesn't include DST flags specified within the strings.

    0 讨论(0)
提交回复
热议问题