Skip to content

Error parsing unambiguous dates #233

Closed

Description

@gazpachoking

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?

Activity

  1. pganssle commented on Mar 28, 2016

    @pganssle
    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.

  2. added this to the 2.5.3 milestone on Mar 28, 2016
  3. pganssle commented on Mar 28, 2016

    @pganssle
    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.

  4. rupert commented on Apr 29, 2016

    @rupert

    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

  5. pganssle commented on Apr 29, 2016

    @pganssle
    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.

  6. rupert commented on Apr 29, 2016

    @rupert

    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)
  7. rupert commented on Apr 29, 2016

    @rupert
  8. pganssle commented on Apr 29, 2016

    @pganssle
    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.

  9. rupert commented on Apr 29, 2016

    @rupert

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions