Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error parsing unambiguous dates #233

Closed
gazpachoking opened this issue Mar 28, 2016 · 8 comments
Closed

Error parsing unambiguous dates #233

gazpachoking opened this issue Mar 28, 2016 · 8 comments
Milestone

Comments

@gazpachoking
Copy link

It seems in version 2.5.2, there started being problems parsing unambiguous dates that don't follow the specified dayfirst option:

>>> import dateutil
>>> dateutil.__version__
'2.5.2'
>>> dateutil.parser.parse('2010 09 25', dayfirst=True)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\Chase\projects\flexget\py27\lib\site-packages\dateutil\parser.py", line 1164, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "C:\Users\Chase\projects\flexget\py27\lib\site-packages\dateutil\parser.py", line 577, in parse
    ret = default.replace(**repl)
ValueError: month must be in 1..12

In previous versions it would produce a valid date.

>>> import dateutil
>>> dateutil.__version__
'2.5.1'
>>> from dateutil import parser
>>> parser.parse('2010 09 25', dayfirst=True)
datetime.datetime(2010, 9, 25, 0, 0)

Was this an intended change?

@pganssle
Copy link
Member

This was most certainly not an ambiguous change, and probably can be patched in a bugfix release. The 2.5.2 release doesn't have much in it (mainly it was just released to update the tzdata update), so I suggest using the 2.5.1 release if possible until we can get out a fix.

@pganssle pganssle added this to the 2.5.3 milestone Mar 28, 2016
@pganssle
Copy link
Member

Note - if anyone goes to fix this before I get around to it, please work off the 2.5.x branch, not the the master branch.

@rupert
Copy link

rupert commented Apr 29, 2016

The fix in #234 only works when the day part of YYYY-MM-DD is <= 12. I suppose you might want to support YYYY-DD-MM (!) - SQL Server uses this format for some languages.

In 2.5.1:

>>> import dateutil
>>> print dateutil.__version__
2.5.1
>>> from dateutil.parser import parse
>>> parse('2015-01-02')
datetime.datetime(2015, 1, 2, 0, 0)
>>> parse('2015-01-02', dayfirst=True)
datetime.datetime(2015, 1, 2, 0, 0)
>>> parse('2015-01-13', dayfirst=True)
datetime.datetime(2015, 1, 13, 0, 0)

In 2.5.3:

>>> import dateutil
>>> print dateutil.__version__
2.5.3
>>> from dateutil.parser import parse
>>> parse('2015-01-02')
datetime.datetime(2015, 1, 2, 0, 0)
>>> parse('2015-01-02', dayfirst=True)
datetime.datetime(2015, 2, 1, 0, 0)
>>> parse('2015-01-13', dayfirst=True)
datetime.datetime(2015, 1, 13, 0, 0)

Delorean bug: myusuf3/delorean#87

@pganssle
Copy link
Member

@rupert I'm not sure I understand the issue. 2015-01-13 is unambiguously YYYY-DD-MM. No other interpretation of those values constructs a valid date. The fundamental assumption of the parser is that you gave it a valid date, and it will be as charitable as possible in interpreting that date.

See my comments on #214 if you want dayfirst or yearfirst to be strict.

@rupert
Copy link

rupert commented Apr 29, 2016

Yes 2015-01-13 is handled correctly, the issue is with 2015-01-02 being parsed as YYYY-DD-MM instead of YYYY-MM-DD (i.e. the 2.5.1 behaviour is correct).

Day and month swapped in 2.5.3:

>>> parse('2015-01-02', dayfirst=True)
datetime.datetime(2015, 2, 1, 0, 0)

@rupert
Copy link

rupert commented Apr 29, 2016

To be honest this is more a problem with delorean defaulting dayfirst to True: https://github.com/myusuf3/delorean/blob/064bc8d0716e06838f11ac38fbf3e56ec63c4e76/delorean/interface.py#L15

@pganssle
Copy link
Member

Just set it to False?

That is the intended behavior change for dateutil version 2.5.2 - the combination of dayfirst and yearfirst are used to disambiguate between ymd, ydm, dmy, mdy as the "default" order for ambiguous triplets. See #81 and #238 for more details on how these work. At some point relatively soon, I will update the documentation with the chart in #238.

@rupert
Copy link

rupert commented Apr 29, 2016

Yes setting it to False works. Originally I wasn't sure if this was an intended change. Thanks for clarifiying; I'm actually now using dateutil directly.

delorean has some documentation for dayfirst and yearfirst which needs updating. I'll add a comment to the other issue. Thanks.

nigelmcnie added a commit to shoptime/trex that referenced this issue Apr 30, 2017
See
dateutil/dateutil#233 (comment)

Basically: dateutil used to parse 2013-03-01 as march 1, but due to a
change in 2.5.2, they decided to start parsing it as january 3. It's to
do with how they are trying to handle dates that look like yymmdd or
ddmmyy, etc, and they seem to think there is an obscure but valid case
where you'd want yyyyddmm. It's stupid but then relaxed parsing is a
minefield anyway.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants