Skip to content

Fix parsing noon and midnight with AM/PM - #1338

Open
Yananix wants to merge 1 commit into
python-babel:masterfrom
Yananix:fix/parse-time-noon-midnight
Open

Fix parsing noon and midnight with AM/PM#1338
Yananix wants to merge 1 commit into
python-babel:masterfrom
Yananix:fix/parse-time-noon-midnight

Conversation

@Yananix

@Yananix Yananix commented Sep 9, 2026

Copy link
Copy Markdown

parse_time currently interprets "12:30 AM" as 12:30 and raises ValueError for "12:30 PM" because it adds 12 to an hour that is already 12. This also breaks parsing the strings produced by format_time for midnight and noon in en_US.

Normalize hour 12 to zero when an AM/PM marker is present and the format includes a period, then apply the existing PM offset. Inputs without a period keep their existing interpretation.

Tests cover all 24 hours round-tripping through both the default medium format and an explicit h:mm:ss a pattern, uppercase/lowercase AM/PM at noon and midnight, hour-only input, and 12:30 without a marker.

Validation on macOS with Python 3.12.13 and the repository's CLDR 48.2 data:

  • New regression cases: 10 fail before the fix; all 55 pass after it.
  • Full pytest suite: 7,885 passed, 7 skipped, 2 xfailed.
  • Ruff on both changed files and git diff --check pass.

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.

1 participant