Python 2.3 added a number of simple date and time types in thedatetime module. There’s no support for parsing strings in variousformats and returning a corresponding instance of one of the types.This PEP proposes adding a family of predefined parsing function forseveral commonly used date and time formats, and a facility for genericparsing.
The types provided by thedatetime module all have.isoformat() and.ctime() methods that return stringrepresentations of a time, and the.strftime() method can be usedto construct new formats. There are a number of additionalcommonly-used formats that would be useful to have as part of thestandard library; this PEP also suggests how to add them.
Useful formats to support include:
XXX The PerlParseDate.pm module supports many different input formats,both absolute and relative. Should we try to support them all?
Options:
datetime module:importdatetimed=datetime.parse_iso8601("2003-09-15T10:34:54")
.now(), so this would be pretty natural.:importdatetimed=datetime.date.parse_iso8601("2003-09-15T10:34:54")
importdatetimed=datetime.parser.parse_iso8601("2003-09-15T10:34:54")
Unresolved questions:
parse_iso8601("yyyy-mm-dd") returns adate instance,but parsing “yyyy-mm-ddThh:mm:ss” returns adatetime.) Shouldthere be an option to signal an error if a time is provided wherenone is expected, or if no time is provided?Is a strptime() implementation that returnsdatetime types sufficient?
XXX if yes, describe strptime here. Can the existing pure-Pythonimplementation be easily retargeted?
Not all input formats need to be supported as output formats, because it’spretty trivial to get thestrftime() argument right for simple thingssuch as YYYY/MM/DD. Only complicated formats need to be supported;RFC 2822is currently the only one I can think of.
Options:
importdatetimed=datetime.datetime(...)printd.strftime(d.RFC2822_FORMAT)# or datetime.RFC2822_FORMAT?
d=datetime.datetime(...)printd.rfc822_time()
Relevant functionality in other languages includes thePHP datefunction (Python implementation by Simon Willison athttp://simon.incutio.com/archive/2003/10/07/dateInPython)
Other useful links:
http://www.egenix.com/files/python/mxDateTime.htmlhttp://ringmaster.arc.nasa.gov/tools/time_formats.htmlhttp://www.thinkage.ca/english/gcos/expl/b/lib/0tosec.htmlhttps://moin.conectiva.com.br/DateUtil
This document has been placed in the public domain.
Source:https://github.com/python/peps/blob/main/peps/pep-0321.rst
Last modified:2025-02-01 08:59:27 GMT