当使用 as.POSIXct 将日期从 "character" 格式转换为 "POSIXct" 时,对于 1 个特定日期不适用

NA for 1 particular date when converting dates from "character" format to "POSIXct" with as.POSIXct

我正在使用 as.POSIXct() 将字符串向量转换为日期格式。 这是奇怪的事情:

as.POSIXct("2017-03-26 03:00:00.000",format="%Y-%m-%d %H")

#Gives

"2017-03-26 03:00:00 CEST"

#While

as.POSIXct("2017-03-26 02:00:00.000",format="%Y-%m-%d %H")

#Outputs
NA

这真是令人困惑和沮丧。似乎该功能真的不喜欢特定时间: 02:00:00.000

我们可以指定%T时间。在格式中,有分、秒和毫秒。所以,%H 只匹配小时部分

as.POSIXct("2017-03-26 02:00:00.000",format="%Y-%m-%d %T")
[1] "2017-03-26 02:00:00 EDT"

或者也考虑毫秒

as.POSIXct("2017-03-26 02:00:00.000",format="%Y-%m-%d %H:%M:%OS")
#[1] "2017-03-26 02:00:00 EDT"

或使用lubridate

library(lubridate)
ymd_hms("2017-03-26 02:00:00.000")

这是一个夏令时问题,时间:

"2017-03-26 02:00:00.000" 在瑞典不存在,因为我们在这个日期更改为“夏令时”时损失了一个小时。