Skip to content

Parse month names in parse_date() - #1487

Open
Asimmimx wants to merge 1 commit into
python-babel:masterfrom
Asimmimx:parse-date-month-names
Open

Asimmimx wants to merge 1 commit into
python-babel:masterfrom
Asimmimx:parse-date-month-names

Conversation

@Asimmimx

Copy link
Copy Markdown

parse_date() only reads numbers, so it can't parse dates whose format spells the month out. That includes the default medium format of en_US and most other locales, so Babel can't parse its own output:

>>> from datetime import date
>>> from babel.dates import format_date, parse_date
>>> parse_date(format_date(date(2026, 11, 24), locale='en_US'), locale='en_US')
IndexError: list index out of range

This resolves the FIXME in parse_date(). When the pattern has a textual month field (MMM/MMMM/LLL/LLLL), it looks for the locale's month names (format and stand-alone, wide and abbreviated), then the English ones, and uses the remaining numbers for the year and day. Numeric patterns go through the same code as before.

Some locale details I had to handle:

  • Longer names are tried first, so "March" isn't read as "Mar".
  • Names have to be separate words, so "Summary 3 2026" isn't parsed as March 3. The exception is when the pattern itself attaches letters to the month, e.g. d בMMM y (he) or MMMM'ren' (eu).
  • A match inside a day name is skipped if there is another one. In Welsh, "Mawrth" is both Tuesday and March. In Spanish, "mar" is an abbreviation for both "martes" and "marzo".
  • A name ending in a digit must not match the start of a longer number. Tibetan month names are "month 1", "month 11", and so on.

Input with too few numbers (e.g. "Nov 2026") now raises ParseError instead of IndexError. #1282 fixed the ValueError case. #1365 was closed as already done in #1282, but the IndexError still happens on master.

To check this, I formatted 7 dates in short/medium/long/full for every locale in CLDR and parsed them back:

format before after
short 8029 ok 8029 ok
medium 1768 ok, 6261 IndexError 8029 ok
long 940 ok, 7089 IndexError 8015 ok, 14 ParseError
full 954 ok, 7061 IndexError, 14 ParseError 8001 ok, 28 ParseError

None of them parse to a wrong date, before or after. The remaining ParseErrors come from two locales: mgo, whose day names contain a digit ("Aneg 2"), and nnh. Parsing a month-name date takes about 35 µs, because the regexes are cached per locale.

Tests: added cases for the locales above, a format/parse round trip for a few locales in all four formats, and the ParseError cases. The full suite passes on Python 3.14, and ruff check is clean.

Prepared with AI assistance (Claude Code).

🤖 Generated with Claude Code

parse_date() only read numbers, so dates in formats that spell out the
month, which is the default medium format of en_US and most other
locales, raised an IndexError, e.g. parse_date('Nov 24, 2026',
locale='en_US').

Look for the locale's month names, then English ones, when the pattern
has a textual month field. Names must be separate words unless the
pattern attaches letters to the month (Hebrew, Basque, ...), and a match
inside a day name is skipped (in Welsh "Mawrth" is Tuesday and March).
Input with too few numbers now raises ParseError instead of IndexError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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