17

我在将 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
4

1 回答 1

10

这是一个从后POSIXctcharacterPOSIXct保留原始夏令时状态的解决方法。

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 对 string-to-datetime 的支持不包括在字符串中指定的 DST 标志。

于 2012-10-31T14:31:27.457 回答