strptime - dates formatted differently on different computers

Marc Christiansen usenet at solar-empire.de
Tue Dec 11 15:18:03 EST 2012


Greg Donald <gdonald at gmail.com> wrote:
> On Mon, Dec 10, 2012 at 10:34:31PM -0700, Michael Torrie wrote:
>> I use a module I got from pypi called dateutil.  It has a nice submodule
>> called parser that can handle a variety of date formats with good
>> accuracy.  Not sure how it works, but it handles all the common American
>> date formats I've thrown at it.
> 
> from dateutil.parser import parse
> dt = parse( whatever )
> 
> I've throw all kind of date and timestamps at it.. have yet to see
> anything it won't parse.

Interesting. First thing I tried gave an error:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'LC_CTYPE=de_DE.utf8;LC_NUMERIC=de_DE.utf8;LC_TIME=de_DE.utf8;LC_COLLATE=C;LC_MONETARY=de_DE.utf8;LC_MESSAGES=C;LC_PAPER=de_DE.utf8;LC_NAME=de_DE.utf8;LC_ADDRESS=de_DE.utf8;LC_TELEPHONE=de_DE.utf8;LC_MEASUREMENT=de_DE.utf8;LC_IDENTIFICATION=de_DE.utf8'
>>> from dateutil.parser import parse
>>> parse('1. Januar 2013')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.3/site-packages/dateutil/parser.py", line 720, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/usr/lib64/python3.3/site-packages/dateutil/parser.py", line 310, in parse
    raise ValueError("unknown string format")
ValueError: unknown string format
>>> parse('1.2.2013') # ambiguous, I know
datetime.datetime(2013, 1, 2, 0, 0) # should be datetime.datetime(2013, 2, 1, 0, 0)

so it doesn't like long german dates and it misparses the numerical
form. And I even was so nice to set the locale :) (not that it succeeds
without…)
I admit I didn't read any documentation on it apart from help(parse)
which mentions a parserinfo argument, so one could probably give it a
hand at parsing.

The only thing this shows is that parsing dates is difficult.

Marc



More information about the Python-list mailing list