Reputation: 167
I have a string that contains time
timedata <- "08:39:41:759:520"
I want to convert this date string value into milliseconds format .
Note:
I have tried with following commands:
as.POSIXct(timedata)
It throws following error:
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
How do I solve it and convert data into milliseconds/microseconds format
Upvotes: 2
Views: 348
Reputation: 132989
You need to fix your input string first. I assume it is hours:minutes:seconds:milliseconds:microseconds
.
timedata <- "08:39:41:759:520"
#replace last two :
timedata <- gsub(":(?=\\d*$)", ".",
gsub(":(?=\\d*$)", "",
timedata,
perl = TRUE),
perl = TRUE)
#coerce to POSIXct and subtract current date
timedata <- as.numeric(as.POSIXct(timedata, '%H:%M:%OS', tz='UTC')) -
as.numeric(as.POSIXct(Sys.Date()))
#this is a floating point number, print digits of interest (following digits can't be zero)
sprintf("%.6f",timedata)
#[1] "31181.759520"
Upvotes: 2