Skip to content

Inconsistent behaviour when dealing with dates close to year 0 #293

@alexwlchan

Description

@alexwlchan

I wrote the following simple test using the python-hypothesis library. It generates dates, renders them as strings, then unpacks them using parser.parse() and checks the date survived the trip:

from dateutil.parser import parse

@given(datetimes())
def test_round_trip(d):
    assert parse(str(d)) == d

If you try enough examples: this test fails:

  • Pass in d=datetime.datetime(4, 4, 1, 0, 0) – i.e., April 1st in the year 4
  • Casting to str(), this becomes '0004-04-01 00:00:00'
  • When unpacked with parse(), it becomes datetime.datetime(1, 4, 4, 0, 0) – i.e., April 4th in the year 1. Mismatch!

The same problem occurs for all datetime.datetime(M, M, 1, 0, 0) for 1 < M <= 12.

Given the four-digit year, I think this string is fairly unambiguous, but it seems to be getting confused somewhere, and treating the -01 as the year. But two-digit years are supposed to be within 50 years of the current year:

Converting two digit years

When a two digit year is found, it is processed considering the current year, so that the computed year is never more than 49 years after then current year, nor 50 years before the current year. In other words, if we are in year 2003, and the year 30 is found, it will be considered as 2030, but if the year 60 is found, it will be considered 1960.

So if it really is treating -01 as the year, then based on this rule it should be returning datetime.datetime(2001, 4, 4, 0, 0).

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