It turns out lubridate
can handle this format:
library(lubridate)
ymd_hms(x)
## [1] "2011-10-13 02:17:13 UTC" "2011-10-12 22:17:13 UTC"
Or, to display in the local timezone:
with_tz(ymd_hms(x))
## [1] "2011-10-12 22:17:13 EDT" "2011-10-12 18:17:13 EDT"
For more flexibility (still using lubridate
):
parse_date_time(x, "%Y-%m-%d %H:%M:%OS%z")
For faster speed (amongst lubridate
options):
lubridate:::.strptime(x, "%Y-%m-%d %H:%M:%OS%OO")
Timings:
microbenchmark(
ymd_hms(x),
parse_date_time(x, "%Y-%m-%d %H:%M:%OS%z"),
lubridate:::.strptime(x, "%Y-%m-%d %H:%M:%OS%OO"),
strptime(gsub("(.*):", "\\1", x), format)
)
## Unit: microseconds
## expr min lq mean median uq max neval
## ymd_hms(x) 1523.819 1578.495 1715.14577 1629.5385 1744.3695 2850.393 100
## parse_date_time(x, "%Y-%m-%d %H:%M:%OS%z") 1108.676 1150.633 1273.77301 1190.3315 1264.8050 5947.204 100
## lubridate:::.strptime(x, "%Y-%m-%d %H:%M:%OS%OO") 89.838 103.390 112.45338 107.8425 115.2265 216.512 100
## strptime(gsub("(.*):", "\\\\1", x), format) 46.716 58.294 71.90934 69.9415 86.5860 105.044 100