I have a string that I’m parsing out from log files that looks like the following:
“[22/May/2011:23:02:21 +0000]”
What’s the best way (examples in Ruby would be most appreciated, as I’m using the Mongo Ruby driver) to get that stashed into MongoDB as a native Date type?
A few things to note:
ABBR_MONTHNAMESlist, otherwise, just make your own list.Date.parseto parse dates it is incredibly slow, the same goes forDateTime.parse,Time.parse, which use the same implementation.String#index,#[]and#splitto extract the parts you need.If you want to do this as fast as possible, something like the following is probably more appropriate. It doesn’t use regexes (which are useful, but not fast):
It takes advantage of the fact that all fields have fixed width (at least I assume they do). So all you need to do is extract the substrings. It also calculates the timezone offset as a number instead of a string.