设置dst with lubridate
问题描述:
我想将小时法语时间的字符序列转换为POSIXct。
该序列在02:00(CEST => CET)包括一个导致重复输出时间的dst时钟改变。设置dst with lubridate
char_dates <- c("2017-10-29 01:00:00",
"2017-10-29 02:00:00",
"2017-10-29 02:00:00",
"2017-10-29 03:00:00")
ymd_hms(char_dates, tz = "Europe/Paris")
输出:
"2017-10-29 01:00:00 CEST" "2017-10-29 02:00:00 CET" "2017-10-29 02:00:00 CET" "2017-10-29 03:00:00 CET"
所需的输出(注意第二个时区):
"2017-10-29 01:00:00 CEST" "2017-10-29 02:00:00 CEST" "2017-10-29 02:00:00 CET" "2017-10-29 03:00:00 CET"
什么是一个很好的解决方案,以实现这一目标?
我们可以假设日期是排序的,或者用户可以提供一个布尔向量(dst:yes/no)来告诉lubridate选择哪个时区。
答
检查是否有重复项(从最后)和减去重复值的3600(一秒钟)。如果你有一个布尔向量,使用它而不是duplicated(temp, fromLast = TRUE)
。
char_dates <- c("2017-10-29 01:00:00",
"2017-10-29 02:00:00",
"2017-10-29 02:00:00",
"2017-10-29 03:00:00")
temp = ymd_hms(char_dates, tz = "Europe/Paris")
temp[duplicated(temp, fromLast = TRUE)] = temp[duplicated(temp, fromLast = TRUE)] - 3600
temp
#[1] "2017-10-29 01:00:00 CEST" "2017-10-29 02:00:00 CEST" "2017-10-29 02:00:00 CET"
#[4] "2017-10-29 03:00:00 CET"