The problem:
I'm about to parse a log file in Python 2.6. Problems arose parsing the common log date string into a time object:
13/Sep/2012:06:27:18 +0200
What I tried already
Use dateutils.parser.parse
I already tried using dateutils.parser.parse
but it failed parsing it with the following error:
ValueError: unknown string format
Use time.strptime
I tried time.strptime
with the format string %d/%b/%Y:%H:%M:%S %z
but ran into trouble when parsing the timezone:
ValueError: 'z' is a bad directive in format '%d/%b/%Y:%H:%M:%S %z'
Does anyone know, where the error is? Or is it just the wrong approach?
Final solution
Finally I decided to use time.strptime
with stripping off the timezone information:
time.strptime(datestring[:-6], '%d/%b/%Y:%H:%M:%S')
The reason don't want to use dateutils
is that dateutils
is way slower than strptime
(which actually calls a C function).