Search code examples
pythonparsingdatetimetimepython-2.5

How to parse a RFC 2822 date/time into a Python datetime?


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.


Solution

  • 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')