views:

468

answers:

2

Is there an easy way to parse HTTP date-strings in Python? According to the standard, there are several ways to format HTTP date strings; the method should be able to handle this.

In other words, I want to convert a string like "Wed, 23 Sep 2009 22:15:29 GMT" to a python time-structure.

A: 
>>> import datetime
>>> datetime.datetime.strptime('Wed, 23 Sep 2009 22:15:29 GMT', '%a, %d %b %Y %H:%M:%S GMT')
datetime.datetime(2009, 9, 23, 22, 15, 29)
SilentGhost
this will handle just one format!
Agos
yes, and it's fairly easy to extend to handle any format. while `email.utils.parse` is more robust, it's less transparent as well.
SilentGhost
%a is locale dependent so will not generally work
stach
+9  A: 
>>> import email.utils as eut
>>> eut.parsedate('Wed, 23 Sep 2009 22:15:29 GMT')
(2009, 9, 23, 22, 15, 29, 0, 1, -1)

If you want a datetime.datetime object, you can do:

def my_parsedate(text):
    return datetime.datetime(*eut.parsedate(text)[:6])
ΤΖΩΤΖΙΟΥ
Yep, parsedate's probably the best compromise, though its "tolerant RFC 2822 parsing" is not 100% compatible with RFC 2616'2 demanding "MUST" -- e.g., epic fail on RFC 850 format with two-digit years, such as `Sunday, 06-Nov-94 08:49:37 GMT`, yet 2616 says a client MUST be able to parse RFC 850 dates (sigh).
Alex Martelli
email.Utils.parsedate seems sufficient, thanks. But it's confusing that it's sometimes called email.utils, and sometimes email.Utils. I guess that the email.Utils version is an old legacy variant which has been deprecated(?)
Troels Arvin
`email.utils.parsedate is email.Utils.parsedate -> True` It seems that *U*tils is a lazy loader.
J.F. Sebastian
Also note that email.util.parsedate() returns a tuple that can be passed directly to time.mktime() (this gives you a int of seconds from the epoch on your computer(local time, not UTC)).
driax