A sample timestamp vector of characters in millisecond precision:
timestamp <- c("2021-03-09 00:00:01.000", "2021-03-09 00:00:03.200", "2021-03-09 00:00:03.200",
"2021-03-09 00:00:03.600", "2021-03-09 00:00:06.700", "2021-03-09 00:00:06.700",
"2021-03-09 00:00:07.600", "2021-03-09 00:00:09.700", "2021-03-09 00:00:09.700",
"2021-03-09 00:03:35.900", "2021-03-09 00:03:35.900", "2021-03-09 00:03:52.600",
"2021-03-09 00:03:52.600", "2021-03-09 00:04:00.700", "2021-03-09 00:04:02.600",
"2021-03-09 00:04:03.600", "2021-03-09 00:04:03.600")
The goal is to convert large vectors like these into a POSIXct vector of the same millisecond precision in a highly computationally efficient manner and with MST (US/Phoenix) as the time zone. (The precision is required for grouping and plotting purposes.)
My system time zone is in MST and these character timestamps are based on MST time zone. I tried using the fasttime package's fastPOSIXct function:
library(fasttime)
options(digits.secs = 6)
output1 <- fastPOSIXct(timestamp)
timestamp[1]
output1[1]
timestamp[4]
output1[4]
timestamp[12]
output1[12]
output2 <- fastPOSIXct(timestamp, tz = "MST")
output3 <- fastPOSIXct(timestamp, tz = "US/Phoenix")
The conversion seems pretty fast. However, there are issues:
(1) For output1 and output2, timestamps get converted to GMT, but the function appends MST as the time zone. Output3 displays the timestamps as it is, but appends GMT as the time zone and also gives a warning message saying unknown timezone 'US/Phoenix'. I read the function documentation; tz only seems to set the time zone for resulting POSIXct object. I would like to specify the time zone for the timestamps to be MST (US/Phoenix).
(2) For output3, the function converts "2021-03-09 00:00:01.100" into "2021-03-09 00:00:01.0" and "2021-03-09 00:00:03.600" into "2021-03-09 00:00:03.5". This is very weird. Overall, in my original data, I see that mostly 100 unit of milliseconds are converted into 0 and 600 into 5 at the end of each object. What is the issue here?
(3) Is there a way to specify digits.secs inside the function itself?