Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How to parse HTTP time header?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Philip Semanchuk  
View profile  
 More options Nov 8, 2:48 pm
Newsgroups: comp.lang.python
From: Philip Semanchuk <phi...@semanchuk.com>
Date: Sat, 7 Nov 2009 22:48:28 -0500
Local: Sun, Nov 8 2009 2:48 pm
Subject: Re: How to parse HTTP time header?

On Nov 7, 2009, at 10:39 PM, Kevin Ar18 wrote:

> Basically, I'm wondering if it is part of the standard library  
> somewhere before I code my own.

> Page 20 of RFC2616 (HTTP) describes the format(s) for the time  
> header.  It wouldn't be too difficult for me to code up a solution  
> for the 3 standard formats, but what get's me is the little note  
> about how some servers may still send badly format time headers. :(
> So, I'm curious if this has already been done in the standard Python  
> library?

The parsedate() function in the rfc822 module does this and claims to  
be tolerant of slightly malformed dates, but that module is deprecated  
as of Python 2.5 in favor of the email module which hopefully has an  
equivalent function.

HTH
Philip


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Philip Semanchuk  
View profile  
 More options Nov 8, 3:19 pm
Newsgroups: comp.lang.python
From: Philip Semanchuk <phi...@semanchuk.com>
Date: Sat, 7 Nov 2009 23:19:11 -0500
Local: Sun, Nov 8 2009 3:19 pm
Subject: Re: How to parse HTTP time header?

On Nov 7, 2009, at 10:56 PM, Kevin Ar18 wrote:

Sorry, my mistake -- 2616 != 2822. I'm not sure if there's something  
in the standard library for parsing RFC 2616 dates.

When I faced the problem of parsing HTTP dates, I wrote my own  
function although this was in an application that was deliberately  
unforgiving of invalid input and therefore my code makes no allowances  
for it. FWIW, it parsed over 1 million dates without encountering any  
that raised an error.

Here it is, written in a time when I obviously didn't have total  
respect for PEP 8.

ASCTIME_FORMAT = "%a %b %d %H:%M:%S %Y"
RFC_850_FORMAT = "%A, %d-%b-%y %H:%M:%S GMT"
RFC_1123_FORMAT = "%a, %d %b %Y %H:%M:%S GMT"

def HttpDateToFloat(HttpDateString):
     # Per RFC 2616 section 3.3, HTTP dates can come in three flavors --
     # Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
     # Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
     # Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
     if not HttpDateString.endswith("GMT"):
         date = time.strptime(HttpDateString, ASCTIME_FORMAT)
     else:
         if "-" in HttpDateString:
             # RFC 850 format
             date = time.strptime(HttpDateString, RFC_850_FORMAT)
         else:
             # RFC 822/1123
             date = time.strptime(HttpDateString, RFC_1123_FORMAT)

     return calendar.timegm(date)

bye
Philip


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google