Skip to content

parse_date raises IndexError for explicit fixed-width formats without separators #1515

Backlog

Description

@dongyikuan919

Overview Description

format_date emits dates with adjacent numeric fields for explicit patterns such as ddMMyyyy and yyyyMMdd. parse_date raises IndexError when it receives the same output and pattern.

Steps to Reproduce

from datetime import date
from babel.dates import format_date, parse_date

value = date(2004, 4, 1)
for pattern in ("ddMMyyyy", "yyyyMMdd", "dd.MM.yyyy"):
    text = format_date(value, pattern, locale="en_US")
    try:
        print(pattern, text, parse_date(text, format=pattern, locale="en_US"))
    except Exception as error:
        print(pattern, text, type(error).__name__, error)

print(parse_date("20040401", locale="en_US"))

Actual Results

ddMMyyyy 01042004 IndexError list index out of range
yyyyMMdd 20040401 IndexError list index out of range
dd.MM.yyyy 01.04.2004 2004-04-01
2004-04-01

Expected Results

Both explicit fixed-width patterns should parse their corresponding strings as date(2004, 4, 1), matching the separated pattern and the documented use of an explicit date format.

Reproducibility

Reproduced with the official Babel 2.19.0 PyPI wheel and unmodified babel/dates.py from master at 394f91a41e5e9582e4a61502be8269fe6bebb676. The source checkout used the official wheel's locale data. Python 3.10.14 on macOS 15 arm64.

Additional Information

parse_date currently captures each consecutive digit run with re.findall(r'(\d+)', string), which yields a single entry for these inputs. It then indexes that list for the year, month, and day. The existing basic ISO path already accepts 20040401 in the default mode, as the final control demonstrates. Current parser

OpenAI Codex assisted with reproduction and drafting.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions