mx.DateTime.Parser.DateFromString('crap')

David Bolen db3l at fitlinxx.com
Thu Jul 10 11:38:33 EDT 2003


Koczian <Sibylle.Koczian at Bibliothek.Uni-Augsburg.de> writes:

> This doesn't raise an exception but returns the current date. I know
> the comment in the notes for the Parser submodule: "The Parser
> submodule ... will try very hard to come up with a reasonable output
> given a valid input." But this is no valid input, so I'd prefer an
> exception or at least not a valid DateTime object.

Double check that you're using a recent version of the egenix base
package (probably 2.0.3 or later).  The parser module functions such
as DateFromString were augmented to accept an optional list of parsers
to try, which allows you to override the default.  The default does
include an "unknown" parser which will default to the current date as
in prior versions, but if you exclude that you'll get a ValueError
exception if none of the other parsers match.

For example:

>>> import mx.DateTime
>>> print mx.DateTime.Parser._date_formats
('euro', 'us', 'altus', 'iso', 'altiso', 'lit', 'altlit', 'eurlit', 'unknown')
>>> print mx.DateTime.Parser.DateFromString('crap')
2003-07-10 00:00:00.00
>>> myformats=mx.DateTime.Parser._date_formats[:-1]
>>> print myformats
('euro', 'us', 'altus', 'iso', 'altiso', 'lit', 'altlit', 'eurlit')
>>> print mx.DateTime.Parser.DateFromString('crap',formats=myformats)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  <...snip...>
ValueError: unknown date format: "crap"


-- David




More information about the Python-list mailing list