W3CDate

Python module for parsing and printing common Internet date formats

This small Python module provides date parsing and formatting according to ISO 8601, RFC 822, and XML-RPC. The W3C DateTime formats, a subset of ISO 8601, are preferred (more info).

XML-RPC uses a different variant of ISO 8601 which omits date punctuation and timezones, and clients rely on this fact. (For example, w.bloggar refuses to recognize dates with dashes.) The postDate element in RSS is completely different again: it’s an RFC 822 email-style date. So this class converts between all three formats.

Formats

ISO8601 (W3C Date subset)
2003-01-15T02:30:45         (secs optional)
2003-01-15T02:30:45Z        (secs optional)
2003-01-15T02:30:45-05:00   (secs optional)
XML-RPC
20030115
20030115T02:30:45           (secs optional)
RFC822
Sun, 02 Feb 2003, 02:30:45 -0500

Usage

>>> from W3CDate import W3CDate >>> d = W3CDate() >>> d.parseRFC822("Fri, 15 Nov 2002, 15:44 -0500") >>> d.getRFC822() 'Fri, 15 Nov 2002 15:44:00 -0500' >>> d.getISO8601() '2002-11-15T15:44:00-05:00' >>> d.parseRFC822("Fri, 15 Nov 2002, 15:44 GMT") >>> d.getISO8601() '2002-11-15T15:44:00Z' >>> d.getRFC822() 'Fri, 15 Nov 2002 15:44:00 GMT' >>> d.getXMLRPC() '20021115T15:44:00' >>> d.parse('2003/01/10T08:45:12-05:00') >>> d.getISO8601() '2003-01-10T08:45:12-05:00' >>> d.getRFC822() 'Fri, 10 Jan 2003 08:45:12 -0500'

License: dual-licensed under the Python License and MPSL 1.1 (Mozilla License).

Download or View

W3CDate.py.txt (10K) - the module itself.