Parsing text into dates?

George Sakkis gsakkis at rutgers.edu
Mon May 16 21:46:59 EDT 2005


"John Machin" <sjmachin at lexicon.net> wrote:

> On 16 May 2005 17:51:31 -0700, "George Sakkis" <gsakkis at rutgers.edu>
> wrote:
>
>
> >#=======================================================
> >
> >def parseDateTime(string, USA=False, implyCurrentDate=False,
> >                  yearHeuristic=_20thcenturyHeuristic):
> >    '''Tries to parse a string as a valid date and/or time.
> >
> >    It recognizes most common (and less common) date and time
formats.
>
> Impressive!
>
>
> >
> >    Examples:
> [snip]
> >        >>> str(parseDateTime('15.6.2001'))
> >        '2001-06-15'
> >        >>> str(parseDateTime('6.15.2001'))
> >        '2001-06-15'
>
> A dangerous heuristic -- 6.12.2001 (meaning 2001-12-06) can be easily
> typoed into 6.13.2001 or 6.15.2001 on the numeric keypad.

Sure, but how is this different from a typo of 2001-12-07 instead of
2001-12-06 ? There's no way you can catch all typos anyway by parsing
alone. Besides, 6.15.2001 is to be interpreted as 2001-06-15 in US
format. Currently the 'USA' flag is used only for ambiguous dates, but
that's easy to change to apply to all dates. Essentially you would gain
a little extra safety at the expense of a little lost recall over the
set of parseable dates.

George




More information about the Python-list mailing list