We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

Generating RFC 822-style Date Strings

Fredrik Lundh | June 2003 | Originally posted to online.effbot.org

The RSS 2.0 specification uses RFC 822-style date strings to store publication dates and build dates.

Here’s a snippet from my publishing tool that takes an “yyyymmdd” or “yyyymmddhhmmss” string, and generates an RSS-compatible string. It uses Python’s calendar module to calculate weekdays, and to get day and month names. Tweak as necessary:

def formatpubdate(date):
    # convert a yyyymmddhhmmss (UTC) string to RSS pubDate format
    from calendar import weekday, month_abbr, day_abbr
    year, month, day = date[:4], date[4:6], date[6:8]
    hour, minute, second = date[8:10], date[10:12], date[12:14]
    if not hour:
        hour = "12"
    if not minute:
        minute = "00"
    if not second:
        second = "00"
    wday = weekday(int(year), int(month), int(day))
    return "%s, %s %s %s %s:%s:%s GMT" % (
        day_abbr[wday], day, month_abbr[int(month)], year,
        hour, minute, second
        )