I am trying to retrieve date from an email. At first it's easy:
message = email.parser.Parser().parse(file)
date = message['Date']
print date
and I receive:
'Mon, 16 Nov 2009 13:32:02 +0100'
But I need a nice datetime object, so I use:
datetime.strptime('Mon, 16 Nov 2009 13:32:02 +0100', '%a, %d %b %Y %H:%M:%S %Z')
which raises ValueError, since %Z isn't format for +0100
. But I can't find proper format for timezone in the documentation, there is only this %Z
for zone. Can someone help me on that?
For those who want to get the correct local time, here is what I did:
ValueError: 'z' is a bad directive in format...
(note: I have to stick to python 2.7 in my case)
I have had a similar problem parsing commit dates from the output of
git log --date=iso8601
which actually isn't the ISO8601 format (hence the addition of--date=iso8601-strict
in a later version).Since I am using
django
I can leverage the utilities there.https://github.com/django/django/blob/master/django/utils/dateparse.py
Instead of
strptime
you could use your own regular expression.