Following on from my previous question, Python time to age, I have now come across a problem regarding the timezone, and it turns out that it’s not always going to be ‘+0200’. So when strptime tries to parse it as such, it throws up an exception.
I thought about just chopping off the +0200 with [:-6] or whatever, but is there a real way to do this with strptime?
I am using Python 2.5.2 if it matters.
>>> from datetime import datetime >>> fmt = '%a, %d %b %Y %H:%M:%S +0200' >>> datetime.strptime('Tue, 22 Jul 2008 08:17:41 +0200', fmt) datetime.datetime(2008, 7, 22, 8, 17, 41) >>> datetime.strptime('Tue, 22 Jul 2008 08:17:41 +0300', fmt) Traceback (most recent call last): File '<stdin>', line 1, in <module> File '/usr/lib/python2.5/_strptime.py', line 330, in strptime (data_string, format)) ValueError: time data did not match format: data=Tue, 22 Jul 2008 08:17:41 +0300 fmt=%a, %d %b %Y %H:%M:%S +0200
It looks like this is implemented only in >= 2.6, and I think you have to manually parse it.
I can’t see another solution than to remove the time zone data: