Skip to content

Parse midnight in one-based 24-hour clock patterns - #1452

Open
liugedragon wants to merge 1 commit into
python-babel:masterfrom
liugedragon:fix/parse-time-one-based-midnight
Open

liugedragon wants to merge 1 commit into
python-babel:masterfrom
liugedragon:fix/parse-time-one-based-midnight

Conversation

@liugedragon

Copy link
Copy Markdown

LDML k and kk patterns number hours from 1 to 24. Babel already formats midnight as 24, but parse_time() passes that value to datetime.time, which rejects it:

>>> format_time(time(0, 30), format="kk:mm", locale="en_US")
'24:30'
>>> parse_time("24:30", format="kk:mm", locale="en_US")
# raises babel.dates.ParseError

Normalize hour 24 to 0 when the pattern contains an actual lowercase k field. Use the existing pattern tokenizer to distinguish it from uppercase K and quoted text. This matches the LDML hour-field definition and the existing formatter.

Regression coverage checks all 24 hours across four patterns, including reordered fields, plus seven invalid-input controls for other hour cycles, quoted k, and out-of-range values. This change addresses the midnight value emitted by the formatter and preserves existing lenient parsing behavior for other inputs.

Validation on Windows / Python 3.13.4:

  • New cases before the fix: 4 failed, 99 passed.
  • New cases after the fix: 103 passed.
  • Date/time module: 2,676 passed, 2 xfailed.
  • Full suite with CLDR 49 data: 8,322 passed, 6 skipped, 2 xfailed.
  • Ruff checks and git diff --check: passed.

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.

1 participant