GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `datetime.strptime` accepts incompatible arguments · Issue #124549 · python/cpython

datetime.strptime accepts incompatible arguments #124549

New issue
Copy link
New issue
Copy link
Closed
Closed
datetime.strptime accepts incompatible arguments#124549
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@pganssle
pganssle
opened on Sep 25, 2024
Issue body actions

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

  • gh-124549: Document that strptime() uses the last directive when a time unit is over-specified #125009
  • gh-124549: Warn on conflicting directives in datetime.*.strptime #132524
  • [3.15] gh-124549: Document that strptime() uses the last directive when a time unit is over-specified (GH-125009) #159950
  • [3.14] gh-124549: Document that strptime() uses the last directive when a time unit is over-specified (GH-125009) #159951

Activity

  1. pganssle
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Sep 25, 2024
  2. pganssle
    added this to Date and time issues 🕰️on Sep 25, 2024
  3. Mariatta commented on Sep 26, 2024

    @Mariatta
    Mariatta
    on Sep 26, 2024
    Member
    More actions

    I'd be ok with raising exception, seems like it's a user error for passing these kind of conflicting format specifiers.

    We could also start documenting this case, saying that the latest format specifiers that appears will prevail.

  4. Mariatta commented on Oct 5, 2024

    @Mariatta
    Mariatta
    on Oct 5, 2024
    Member
    More actions

    @pganssle I created a PR to document this weird behavior. I think it's still worth raising a warning though. #125009

  5. alliesw commented on Dec 17, 2024

    @alliesw
    alliesw
    on Dec 17, 2024
    More actions

    To avoid the issue where the last directive overwrites earlier values:
    since more than one directive is needed, maybe split the string and parse the components manually (handling the split and parsing based on the length of the string)?
    Maybe also apply custom logic to handle dynamic formats, especially if you encounter different scenarios with varying date formats.... (idk if your format is dynamic?)

  6. alliesw commented on Dec 17, 2024

    @alliesw
    alliesw
    on Dec 17, 2024
    More actions

    This is an example of what I'm getting at:

    def parse_date(input_str):
        if len(input_str) == 6:  # 4-digit year followed by 2-digit year
            full_year = input_str[:4]
            short_year = input_str[4:]
            year_full = datetime.strptime(full_year, "%Y")
            year_short = datetime.strptime(short_year, "%y")
            return year_short.replace(year=year_full.year)  # Use full year from the first part
        elif len(input_str) == 4:  # Only 4-digit year
            return datetime.strptime(input_str, "%Y")
        else:
            raise ValueError("Invalid date format")

    Example usage:

    input_str = "200099"
    parsed_date = parse_date(input_str)
    print(parsed_date)  # datetime.datetime(1999, 1, 1, 0, 0)
  7. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Dec 17, 2024
  8. StanFromIreland commented on Apr 14, 2025

    @StanFromIreland
    StanFromIreland
    on Apr 14, 2025
    Member
    More actions

    This is a stdlib issue (strptime.py) the extension-modules label can be removed.

  9. StanFromIreland
    removed
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Aug 29, 2025
  10. Mariatta
    added 3 commits that reference this issue on Oct 20, 2026

    gh-124549: Document that `strptime()` uses the last directive when a …

    fd6ed15

    [3.15] gh-124549: Document that `strptime()` uses the last directive …

    8e00228

    [3.14] gh-124549: Document that `strptime()` uses the last directive …

    d621204
  11. StanFromIreland
    closed this as completedon Oct 23, 2026
  12. github-project-automation
    moved this to Done in Date and time issues 🕰️on Oct 23, 2026
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

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

    Projects

    • Date and time issues 🕰️
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402520 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.