Skip to content

datetime.strptime accepts incompatible arguments #124549

Open
@pganssle

Description

@pganssle

Bug report

Bug description:

If you over-specify a datetime strptime string, it seems that datetime.strptime when encountering a component that has been specified twice, will overwrite earlier values with more recent ones.

>>> datetime.strptime("200099", "%Y%y")
datetime.datetime(1999, 1, 1, 0, 0)

One approach would be to ban over-specified formats, but that would be a problem for someone who wants to, use a format like "%M %B" (e.g. something sortable by month number while also showing month name), so I think we don't want to do that.

Alternatively we could start raising an exception if you get different values from these two things, or possibly a warning.

Or we can just accept that if you use a weird format you should expect weird behavior.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions