解析时间戳 - AM/PM 无法识别

Parsing time stamps - AM/PM not recognized

我正在尝试将一些时间戳(字符)转换为 POSIXct 对象:

> u <- "December 05, 2017 1:00 PM"

我正在使用 as.POSIXct 如下:

> as.POSIXct(u, format = "%B %d, %Y %H:%M %p", tz = "UTC")
[1] "2017-12-05 01:00:00 UTC"

我希望输出显示小时 13 而不是 01:

"2017-12-05 13:00:00 UTC"

我看到这里发布的问题:Parse timestamp with a.m./p.m 但我似乎有正确的输入和格式字符串(%p)。

as.POSIXct 似乎完全忽略了字符串的 AM/PM 部分,因此 AM 时间戳工作正常但 PM 时间戳滞后 12 小时。这里有一些更多的数据来说明这一点:

> sample_tstamps
 [1] "December 05, 2017 8:00 AM"  "December 05, 2017 9:00 AM"  "December 05, 2017 10:00 AM" "December 05, 2017 11:00 AM"
 [5] "December 05, 2017 12:00 PM" "December 05, 2017 1:00 PM"  "December 05, 2017 2:00 PM"  "December 05, 2017 3:00 PM" 
 [9] "December 05, 2017 4:00 PM"  "December 05, 2017 5:00 PM" 
> as.POSIXct(sample_tstamps, format = "%B %d, %Y %H:%M %p", tz = "UTC")
 [1] "2017-12-05 08:00:00 UTC" "2017-12-05 09:00:00 UTC" "2017-12-05 10:00:00 UTC" "2017-12-05 11:00:00 UTC"
 [5] "2017-12-05 12:00:00 UTC" "2017-12-05 01:00:00 UTC" "2017-12-05 02:00:00 UTC" "2017-12-05 03:00:00 UTC"
 [9] "2017-12-05 04:00:00 UTC" "2017-12-05 05:00:00 UTC"

使用 R 包 lubridate:

u <- "December 05, 2017 1:00 PM"
lubridate::mdy_hm(u)
#[1] "2017-12-05 13:00:00 UTC"

我们需要 %I 而不是 %H

as.POSIXct(u, format = "%B %d, %Y %I:%M %p", tz = "UTC")
#[1] "2017-12-05 13:00:00 UTC"