convert character containing hundredths of a second to a timestamp

39 Views Asked by At

"09JUN2022:00:00:00.04" is a character timestamp that I am trying to convert to a datetime format that has the date, time and hundredths of a second.

strptime with the format is what I have used before for timestamps but it doesn't convert this correctly

> strptime("09JUN2022:00:00:00.04", "%d%b%Y:%H:%M:%S.%OS")
[1] "2022-06-09 00:00:04 BST"

If I remove %OS it drops the time entirely

> strptime("09JUN2022:00:00:00.04", "%d%b%Y:%H:%M:%S")
[1] "2022-06-09 BST"

as.POSIXct has the same behaviour

> as.POSIXct("09JUN2022:00:00:00.04",format="%d%b%Y:%H:%M:%S.%OS")
[1] "2022-06-09 00:00:04 BST"
> as.POSIXct("09JUN2022:00:00:00.04",format="%d%b%Y:%H:%M:%S")
[1] "2022-06-09 BST"

lubridate does work but it requires me splitting up the string, is there a way to do it without that? It also takes .01 off the hundredths which I can live with but unsure why.

> dmy("09JUN2022") + hms("00:00:00.04")
[1] "2022-06-09 00:00:00.03 UTC"
> dmy("09JUN2022") + hms("00:00:00.06")
[1] "2022-06-09 00:00:00.05 UTC"
0

There are 0 best solutions below