Speed up AbsoluteTime with string input
If the date format is sufficiently rigid, you might try string patterns or regular expressions.
AbsoluteTime[{"05-Mar-2004 10:15:00", {"Day", "-",
"MonthNameShort", "-", "Year", " ", "Hour24", ":", "Minute", ":",
"Second"}}] // RepeatedTiming
(* {0.00043, 3287470500} *)
This is about 10 times faster:
months = <|"Jan" -> 1, "Feb" -> 2, "Mar" -> 3, "Apr" -> 4, "May" -> 5,
"Jun" -> 6, "Jul" -> 7, "Aug" -> 8, "Sep" -> 9, "Oct" -> 10,
"Nov" -> 11, "Dec" -> 12|>
AbsoluteTime@
First@StringCases["05-Mar-2004 10:15:00",
day : DigitCharacter .. ~~ "-" ~~ mon : LetterCharacter .. ~~
"-" ~~ year : DigitCharacter .. ~~ Whitespace ~~
hour : DigitCharacter .. ~~ ":" ~~ min : DigitCharacter .. ~~
":" ~~ sec : DigitCharacter .. :> {ToExpression@year,
Lookup[months, mon], ToExpression@day, ToExpression@hour,
ToExpression@min, ToExpression@sec}] // RepeatedTiming
(* {0.000037, 3287470500} *)
If you use RegularExpression
instead of a string pattern, it may be even faster. I have not tried it. Note that ToExpression
is not secure for converting strings to integers when you have no control over the input.