[Python Home] [PEP Index] [PEP Source] |
PEP: | 321 |
---|---|
Title: | Date/Time Parsing and Formatting |
Version: | 1736 |
Last-Modified: | 2003-11-18 10:58:37 -0800 (Tue, 18 Nov 2003) |
Author: | A.M. Kuchling <amk at amk.ca> |
Status: | Draft |
Type: | Standards Track |
Content-Type: | text/x-rst |
Python-Version: | 2.4 |
Created: | 16-Sep-2003 |
Post-History: |
Python 2.3 added a number of simple date and time types in the datetime module. There's no support for parsing strings in various formats and returning a corresponding instance of one of the types. This PEP proposes adding a family of predefined parsing function for several commonly used date and time formats, and a facility for generic parsing.
The types provided by the datetime module all have .isoformat() and .ctime() methods that return string representations of a time, and the .strftime() method can be used to construct new formats. There are a number of additional commonly-used formats that would be useful to have as part of the standard library; this PEP also suggests how to add them.
Useful formats to support include:
XXX The Perl ParseDate.pm [3] module supports many different input formats, both absolute and relative. Should we try to support them all?
Options:
Add functions to the datetime module:
import datetime d = datetime.parse_iso8601("2003-09-15T10:34:54")
Add class methods to the various types. There are already various class methods such as .now(), so this would be pretty natural.:
import datetime d = datetime.date.parse_iso8601("2003-09-15T10:34:54")
Add a separate module (possible names: date, date_parse, parse_date) or subpackage (possible names: datetime.parser) containing parsing functions:
import datetime d = datetime.parser.parse_iso8601("2003-09-15T10:34:54")
Unresolved questions:
Is a strptime() implementation that returns datetime types sufficient?
XXX if yes, describe strptime here. Can the existing pure-Python implementation be easily retargeted?
Not all input formats need to be supported as output formats, because it's pretty trivial to get the strftime() argument right for simple things such as YYYY/MM/DD. Only complicated formats need to be supported; RFC2822 is currently the only one I can think of.
Options:
Provide predefined format strings, so you could write this:
import datetime d = datetime.datetime(...) print d.strftime(d.RFC2822_FORMAT) # or datetime.RFC2822_FORMAT?
Provide new methods on all the objects:
d = datetime.datetime(...) print d.rfc822_time()
Relevant functionality in other languages includes the PHP date [5] function (Python implementation by Simon Willison at http://simon.incutio.com/archive/2003/10/07/dateInPython)
Other useful links:
http://www.egenix.com/files/python/mxDateTime.html http://ringmaster.arc.nasa.gov/tools/time_formats.html http://www.thinkage.ca/english/gcos/expl/b/lib/0tosec.html https://moin.conectiva.com.br/DateUtil
[1] | http://rfc2822.x42.com |
[2] | http://www.cl.cam.ac.uk/~mgk25/iso-time.html |
[3] | http://search.cpan.org/author/MUIR/Time-modules-2003.0211/lib/Time/ParseDate.pm |
[4] | http://www.opengroup.org/onlinepubs/007908799/xsh/asctime.html |
[5] | http://www.php.net/date |
This document has been placed in the public domain.