Skip to content

parse_time cannot read explicit compact formats produced by format_time #1517

Backlog

Description

@dongyikuan919

Overview Description

format_time produces compact time strings with explicit patterns such as HHmm and HHmmss. parse_time raises an exception for that output when given the same pattern.

Steps to Reproduce

from datetime import time
from babel.dates import format_time, parse_time

for pattern, value in [("HHmm", time(15, 30)), ("HHmmss", time(15, 30, 45)),
                       ("ssmmHH", time(15, 30, 45))]:
    text = format_time(value, pattern, locale="en_US")
    try:
        print(pattern, text, parse_time(text, format=pattern, locale="en_US"))
    except (ValueError, IndexError) as exc:
        print(pattern, text, type(exc).__name__)

print(parse_time("15:30:45", format="HH:mm:ss", locale="en_US"))

Actual Results

HHmm 1530 ParseError
HHmmss 153045 ParseError
ssmmHH 453015 IndexError
15:30:45

Expected Results

The explicit numeric patterns should parse their corresponding strings as 15:30:00, 15:30:45, and 15:30:45 respectively, matching the formatted values.

Reproducibility

Reproduced on macOS arm64 with Python 3.10.14 using the official Babel 2.19.0 wheel and current master at 394f91a41e5e9582e4a61502be8269fe6bebb676 with the wheel's locale data.

Additional Information

The parser reads every consecutive digit run as one number. These examples contain one run, even though the pattern has two or three fixed-width numeric fields. The same parser already handles separated input, including 45:30:15 with ss:mm:HH. Current implementation

Related to #1515, which covers parse_date.

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