Skip to content

Parser error with unambiguous date in year 31 #687

@pganssle

Description

@pganssle

I started converting this test over to a hypothesis-based property test and found the following failure:

from dateutil.parser import parse
from datetime import datetime

dt = datetime(31, 12, 30)
dstr = dt.strftime('%B.%04Y.%d')    # December.0031.30
parse(dt.strftime('%B.%04Y.%d'))     # datetime(30, 12, 31, 0, 0)

Pretty low priority, but related to #293. Here's the full hypothesis test that generates these examples:

from datetime import datetime, time
import itertools

from hypothesis.strategies import integers, dates
from hypothesis import given

import pytest

from dateutil.parser import parserinfo, parse


def _get_ybd_formats():
    # If we have a 4-digit year, a non-numeric month (abbreviated or not),
    # and a day (1 or 2 digits), then there is no ambiguity as to which
    # token is a year/month/day.  This holds regardless of what order the
    # terms are in and for each of the separators below.
    try:
        datetime.now().strftime('%-d')
        PLATFORM_HAS_DASH_D = True
    except ValueError:
        PLATFORM_HAS_DASH_D = False

    seps = ['-', ' ', '/', '.']

    year_tokens = ['%04Y']
    month_tokens = ['%b', '%B']
    day_tokens = ['%d']

    if PLATFORM_HAS_DASH_D:
        day_tokens.append('%-d')

    prods = itertools.product(year_tokens, month_tokens, day_tokens)
    perms = [y for x in prods for y in itertools.permutations(x)]
    unambig_fmts = [sep.join(perm) for sep in seps for perm in perms]

    return unambig_fmts


@pytest.mark.parser
@pytest.mark.parametrize('fmt', _get_ybd_formats())
@given(dt=dates())
def test_ybd(dt, fmt):
    dt = datetime.combine(dt, time(0))  # Make this a datetime
    dstr = dt.strftime(fmt)
    res = parse(dstr)
    assert dt == res

In the end we may also want to change _get_ybd_formats over to a strategy, because I think repeating dozens of hypothesis tests separately is expensive.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions