Skip to content

parse_time: handle 12 AM and 12 PM correctly on the 12-hour clock - #1366

Open
simpleqt wants to merge 1 commit into
python-babel:masterfrom
simpleqt:sq/r22-parse-time-12h
Open

simpleqt wants to merge 1 commit into
python-babel:masterfrom
simpleqt:sq/r22-parse-time-12h

Conversation

@simpleqt

@simpleqt simpleqt commented Oct 5, 2026

Copy link
Copy Markdown

Summary

parse_time adds a flat +12 offset whenever the format contains a period marker and the input contains pm, which breaks both 12 o'clock cases:

>>> from babel.dates import parse_time
>>> parse_time('12:00 PM', locale='en_US')
ValueError: hour must be in 0..23, not 24      # noon crashes

>>> parse_time('12:30 AM', locale='en_US')
datetime.time(12, 30)                          # midnight parsed as 12:30 PM-side

en_US's own medium time format is h:mm:ss a, so parsing noon in the most common locale crashes and midnight silently lands twelve hours off. This converts the 12-hour clock the way it works: 12 AM is 00:00, 12 PM is 12:00, and only hours 1–11 shift by twelve.

Verification

  • Red/green: five new test_parse_time cases (12:00 pm, 12:00 PM, 12:30 am, 12 AM, 12:00:15 pm) fail on master (the ValueError: hour must be in 0..23, not 24 and the wrong midnight) and pass with this change; all pre-existing am/pm cases are unaffected.
  • pytest tests/test_dates.py → 1196 passed; the 5 remaining failures are the same CLDR-data-drift failures present on master in my environment (skeleton/1076/1192/892 — unrelated to time parsing).
  • ruff check clean.
AI Disclosure

This PR was prepared with the assistance of an AI coding agent.

  • Tool(s): ZCode (GLM-based coding agent)
  • Used for: finding the 12 o'clock edge pair during the parse sweep, implementing the proper 12-hour conversion, and running the red/green and baseline verification described above.

The period handling added a flat +12 offset whenever the input
contained pm, which breaks both 12 o'clock cases:

    >>> parse_time('12:00 PM', locale='en_US')
    ValueError: hour must be in 0..23, not 24

    >>> parse_time('12:30 AM', locale='en_US')
    datetime.time(12, 30)     # midnight parsed as half past noon

en_US's own medium time format is 'h:mm:ss a', so parsing noon in the
most common locale crashes and midnight silently lands on the wrong
side of the day. Convert the 12-hour clock properly: 12 AM is 00:00
and 12 PM is 12:00, only 1-11 AM/PM shift by twelve hours.
Copilot AI balanced review requested due to automatic review settings October 5, 2026 13:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants