gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:18:22 24 4
gpt4 key购买 nike

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

最佳答案

这是一个解决方法,来自 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 对 string-to-datetime 的支持不包括在字符串中指定的 DST 标志。

关于r - R 中的字符 POSIXct 转换会导致夏令时转换 (CEST/CET) 时区值错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13156836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com