Skip to content

datetime.strptime(dt.strftime("%c"), "%c")) fails when year is <1000. #124529

Description

@pganssle

Bug report

Bug description:

>>>fromdatetimeimportdatetime>>>datetime.strptime(datetime(1000, 1, 1).strftime("%c"), "%c")
datetime.datetime(1000, 1, 1, 0, 0)
>>>datetime.strptime(datetime(999, 1, 1).strftime("%c"), "%c")
Traceback (mostrecentcalllast):
File"<python-input-1>", line1, in<module>datetime.strptime(datetime(999, 1, 1).strftime("%c"), "%c")
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/home/nlx5/Documents/Programming/Python/cpython/Lib/_strptime.py", line573, in_strptime_datetimett, fraction, gmtoff_fraction=_strptime(data_string, format)
~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^File"/home/nlx5/Documents/Programming/Python/cpython/Lib/_strptime.py", line352, in_strptimeraiseValueError("time data %r does not match format %r"%
(data_string, format))
ValueError: timedata'Tue Jan 1 00:00:00 999'doesnotmatchformat'%c'

Discovered this when adding some hypothesis tests for strptime/strftime. I doubt this is a real problem anyone is going to have in the real world, but maybe.

I do not know if this is locale-specific or OS specific.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions