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?
Use
email.utils.parsedate_tz(date)
:In Python 3.3+,
email
message can parse the headers for you:Since Python 3.2+, it works if you replace
%Z
with%z
:Or using
email
package (Python 3.3+):if UTC offset is specified as
-0000
then it returns a naive datetime object that represents time in UTC otherwise it returns an aware datetime object with the correspondingtzinfo
set.To parse rfc 5322 date-time string on earlier Python versions (2.6+):
where
FixedOffset
is based ontzinfo
subclass from thedatetime
documentation:For python 3 you can use parsedate_to_datetime function:
Have you tried
More on RFC822, http://docs.python.org/library/rfc822.html
It's deprecated (parsedate_tz is now in
email.utils.parsedate_tz
), though.But maybe these answers help:
How to parse dates with -0400 timezone string in python?
python time to age part 2, timezones
email.utils
has aparsedate()
function for the RFC 2822 format, which as far as I know is not deprecated.Please note, however, that the
parsedate
method does not take into account the time zone andtime.mktime
always expects a local time tuple as mentioned here.So you'll still need to parse out the time zone and take into account the local time difference, too: