views:

802

answers:

3

I have a date of the form specified by RFC 2822 -- say Fri, 15 May 2009 17:58:28 +0000, as a string. Is there a quick and/or standard way to get it as a datetime object in Python 2.5? I tried to produce a strptime format string, but the +0000 timezone specifier confuses the parser.

+8  A: 

There is a parsedate function in email.util. It parses all valid RFC 2822 dates and some special cases.

ebo
+9  A: 
from email.utils import parsedate
print parsedate('Fri, 15 May 2009 17:58:28 +0000')

Documentation.

nosklo
+1 I didn't know about this function, really neat.
Nadia Alramli
Thank you; that does the trick. :)
millenomi
A: 

The problem is that parsedate will ignore the offset.

Do this instead:

from email.utils import parsedate_tz
print parsedate_tz('Fri, 15 May 2009 17:58:28 +0700')