18

I know how to do this the other way around... it would be:

>>> dt.rfc822()
'Sun, 09 Mar 1997 13:45:00 -0500'
S.Lott
  • 384,516
  • 81
  • 508
  • 779
Udbhav
  • 353
  • 1
  • 3
  • 10

3 Answers3

34

In [1]: import rfc822     # This only works for python 2 series

In [2]: rfc822.parsedate_tz('Sun, 09 Mar 1997 13:45:00 -0500')
Out[2]: (1997, 3, 9, 13, 45, 0, 0, 1, 0, -18000)

in python3 parsedate_tz has moved to email.utils


>>> import email.utils   # this works on Python2.5 and up
>>> email.utils.parsedate_tz('Sun, 09 Mar 1997 13:45:00 -0500')
(1997, 3, 9, 13, 45, 0, 0, 1, -1, -18000)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • I thought rf822 was deprecated? – Udbhav Oct 14 '09 at 20:45
  • Thanks, fixed it. There is no deprecation warning even in 2.6 :( – John La Rooy Oct 14 '09 at 20:59
  • 16
    The above technique does not return `datetime`, rather a `tuple`. The complete code is `datetime.datetime.fromtimestamp( email.utils.mktime_tz(email.utils.parsedate_tz( rfcdate )), pytz.utc )`. Ref: http://parand.com/say/index.php/2008/02/11/parsing-and-normalizing-dates-with-timezones-in-python/ – AppleGrew Aug 11 '12 at 14:04
  • 3
    Continuation on @AppleGrew comment: Or by using rfc822 module `datetime.datetime.fromtimestamp(rfc822.mktime_tz(rfc822.parsedate_tz(rfc822String)))` – theta Jul 10 '13 at 02:21
  • 3
    In python 3, try email.utils.parsedate_to_datetime. – Reece Oct 03 '13 at 00:27
  • Email utils appears to be RFC2822. is that the same as 822? – Thomas Ahle Nov 07 '13 at 12:16
  • 1
    @ThomasAhle, the `rfc822` module predated RFC2822. Turns out it's not a great idea to name the module after the RFC :) This was fixed in Python3. The current one is actually RFC5322 – John La Rooy Nov 07 '13 at 20:44
  • The `rfc822` module was deprecated already in Python 2.3; the [documentation](https://docs.python.org/2/library/rfc822.html) points you to [`email`](https://docs.python.org/2/library/email.html) package instead, which was introduced in Python 2.2. – tripleee Oct 09 '14 at 12:37
  • 3
    You can simplify slightly; there's an `utcfromtimestamp` method for dates in UTC: `datetime.datetime.utcfromtimestamp(email.utils.mktime_tz(email.utils.parsedate_tz(rfcdate)))` – tripleee Oct 09 '14 at 12:38
8

As of python 3.3 there is email.utils.parsedate_to_datetime(date)

>>> from email.utils import parsedate_to_datetime
>>> datestr = 'Sun, 09 Mar 1997 13:45:00 -0500'
>>> parsedate_to_datetime(datestr)
datetime.datetime(1997, 3, 9, 13, 45, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))
lod
  • 1,098
  • 10
  • 13
-1

If you strip off the time zone, you can do it like this:

datetime.datetime.strptime('Sun, 09 Mar 1997 13:45:00', '%a, %d %b %Y %H:%M:%S')
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 2
    This doesn't appear to have timezones – Thomas Ahle Nov 07 '13 at 12:16
  • He specifically asked how to do it for rfc822 date/time. Since rfc822 includes the timezone, you're answer doesn't solve it. Even worse, it actually returns a different time, depending on the timezone the code was run in, and the timezone the string was generated in (which in web-applications may vary greatly). – Dolf Andringa Jul 17 '17 at 06:11