PEP 321: Date/Time Parsing and Formatting
Paddy McCarthy
paddy3118 at netscape.net
Tue Nov 18 00:47:08 EST 2003
Gerrit Holl wrote:
> Posted with permission from the author.
> I have some comments on this PEP, see the (coming) followup to this message.
>
> PEP: 321
> Title: Date/Time Parsing and Formatting
<<SNIP>>
>
> Abstract
> ========
>
> 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.
>
<<SNIP>>
>
> Unresolved questions:
>
> * Naming convention to use.
> * What exception to raise on errors? ValueError, or a specialized exception?
> * Should you know what type you're expecting, or should the parsing figure
> it out? (e.g. ``parse_iso8601("yyyy-mm-dd")`` returns a ``date`` instance,
> but parsing "yyyy-mm-ddThh:mm:ss" returns a ``datetime``.) Should
> there be an option to signal an error if a time is provided where
> none is expected, or if no time is provided?
> * Anything special required for I18N? For time zones?
>
I am in favour of there being an intelligent 'guess the format'
routine that would be easy to use, but maybe computationally
inefficient, backed up by a computationally efficient routine where
you specify the format. This latter case being split into two sub
items: first where the parsing routine is passed a constant
representing one of the standard formats and another where the parsing
routine is passed a string representing the format.
datetime.datetime.parse("
datetime.datetime.parse("1985-08-13 15:03")
gives: datetime(1985, 8, 13, 13, 5)
datetime.date.parse("1985-08-13 15:03")
gives: date(1985, 8, 13) # You asked for the date, date was found
# first in string and converted
datetime.date.parse("13/08/1985", "%d/%m/%Y")
gives: date(1985, 8, 13)
datetime.datetime.parse("1985-08-13 15:03", datetime.ISO8601)
gives: datetime(1985, 8, 13, 13, 5)
The idea being for the parser to be able to automatically extract a
date from one of the standard formats it knows, or to accept a
strptime type string for unknown formats.
(Apologies to Gerrit for using values from his reply)
Cheers, Paddy.
More information about the Python-list
mailing list