Skip to content

datetime.strptime: %j, %z, non-padded fields, lowercase %b/%p fail, and every mismatch escapes as .NET FormatException instead of ValueError #2133

Description

@antonsynd

Found by /verify-implementation plan-0ca7b7 (2026-09-26), measured @ 61bf20f with the built sharpyc; pre-existing at BASE acd1d40 unless noted.

datetime.datetime.strptime translates the python format into a .NET ParseExact format (Datetime.cs TranslateStrptimeFormat). Python (3.12) vs Sharpy:

call python3 sharpy
strptime("2021 003", "%Y %j") datetime(2021, 1, 3, 0, 0) FormatException
strptime("2021-01-03 +0530", "%Y-%m-%d %z") aware, offset +05:30 FormatException
strptime("2021-1-3", "%Y-%m-%d") datetime(2021, 1, 3, 0, 0) FormatException
strptime("jan 3 2021", "%b %d %Y") datetime(2021, 1, 3, 0, 0) FormatException
strptime("2021-01-03 1pm", "%Y-%m-%d %I%p") datetime(2021, 1, 3, 13, 0) FormatException
strptime("x", "%Y") inside except ValueError ValueError: time data 'x' does not match format '%Y' uncaught System.FormatException (unhandled exception)

The last row is the worst one: an expected failure isn't catchable by the python spelling. Class contract: strptime parses by python's directive grammar, the same way strftime now does (0eca2b3's Strftime.cs is the formatter twin), and a mismatch raises ValueError with python's message. Sibling of #2019.

Activity

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

    lane:runtime-throwThrows at runtime where python does not, or the wrong exception typereach:programAn ordinary program a Python programmer would write hits it

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions