R中的字符POSIXct转换在夏令时转换(CEST / CET)时导致错误的时区值

时间:2012-10-31 11:26:19

标签: r posixct

我在将POSIXct转换为字符并返回到R中的POSIXct时遇到问题。我运行以下代码:

time_seq_01 <- seq(as.POSIXct("2012-10-28 02:00:00"), by = 900, length.out = 10)
time_seq_02 <- as.character(time_seq_01)
time_seq_03 <- as.POSIXct(time_seq_02)

或同等的:

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_03 <- as.POSIXct(time_seq_02)

这是2012年夏令时从中欧夏令时(CEST)过渡到中欧时间(CET)时的时间戳(10月02日上周日) :00 - 03:00)。

当我调用这些元素时,我得到了

time_seq_01
[1] "2012-10-28 02:00:00 CEST" "2012-10-28 02:15:00 CEST"
[3] "2012-10-28 02:30:00 CEST" "2012-10-28 02:45:00 CEST"
[5] "2012-10-28 02:00:00 CET"  "2012-10-28 02:15:00 CET" 
[7] "2012-10-28 02:30:00 CET"  "2012-10-28 02:45:00 CET" 
[9] "2012-10-28 03:00:00 CET"  "2012-10-28 03:15:00 CET" 
time_seq_02
[1] "2012-10-28 02:00:00 CEST" "2012-10-28 02:15:00 CEST"
[3] "2012-10-28 02:30:00 CEST" "2012-10-28 02:45:00 CEST"
[5] "2012-10-28 02:00:00 CET"  "2012-10-28 02:15:00 CET" 
[7] "2012-10-28 02:30:00 CET"  "2012-10-28 02:45:00 CET" 
[9] "2012-10-28 03:00:00 CET"  "2012-10-28 03:15:00 CET" 
time_seq_03
[1] "2012-10-28 02:00:00 CEST" "2012-10-28 02:15:00 CEST"
[3] "2012-10-28 02:30:00 CEST" "2012-10-28 02:45:00 CET" 
[5] "2012-10-28 02:00:00 CEST" "2012-10-28 02:15:00 CEST"
[7] "2012-10-28 02:30:00 CEST" "2012-10-28 02:45:00 CET" 
[9] "2012-10-28 03:00:00 CET"  "2012-10-28 03:15:00 CET" 

POSIXct序列的创建确实可以正常(time_seq_01)转换到字符(time_seq_02)。但是,从字符返回到POSIXct的转换会产生错误的时区(CET / CEST)值(time_seq_03)。当对这些元素进行排序时,可以清楚地看到这一点:

sort(time_seq_01)
[1] "2012-10-28 02:00:00 CEST" "2012-10-28 02:15:00 CEST"
[3] "2012-10-28 02:30:00 CEST" "2012-10-28 02:45:00 CEST"
[5] "2012-10-28 02:00:00 CET"  "2012-10-28 02:15:00 CET" 
[7] "2012-10-28 02:30:00 CET"  "2012-10-28 02:45:00 CET" 
[9] "2012-10-28 03:00:00 CET"  "2012-10-28 03:15:00 CET" 
sort(time_seq_03)
[1] "2012-10-28 02:00:00 CEST" "2012-10-28 02:00:00 CEST"
[3] "2012-10-28 02:15:00 CEST" "2012-10-28 02:15:00 CEST"
[5] "2012-10-28 02:30:00 CEST" "2012-10-28 02:30:00 CEST"
[7] "2012-10-28 02:45:00 CET"  "2012-10-28 02:45:00 CET" 
[9] "2012-10-28 03:00:00 CET"  "2012-10-28 03:15:00 CET" 

这会导致许多问题,例如在通过这些时间戳合并对象时。有没有办法克服这个问题?

我使用的系统:

Windows 7 64bit
R version 2.15.1 (2012-06-22)
Platform: x86_64-pc-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=German_Austria.1252  LC_CTYPE=German_Austria.1252   
[3] LC_MONETARY=German_Austria.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Austria.1252    
attached base packages:
[1] tools     stats     graphics  grDevices utils     datasets  methods 
[8] base     
other attached packages:
[1] pkgtools_0.1-3 roxygen2_2.2.2 digest_0.5.2   rj_1.1.0-4    
loaded via a namespace (and not attached):
[1] brew_1.0-6    plyr_1.7.1    rj.gd_1.1.0-1 stringr_0.6.1

1 个答案:

答案 0 :(得分:10)

这是一个解决方法,从POSIXctcharacter返回POSIXct,保留原始夏令时状态。

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)

据我所知,R对字符串到日期的支持不包括字符串中指定的DST标志。