Skip to content

Fix format_interval fallback ignoring the requested skeleton - #1524

Open
Yasser-Ameur wants to merge 1 commit into
python-babel:masterfrom
Yasser-Ameur:fix/format-interval-fallback-skeleton
Open

Yasser-Ameur wants to merge 1 commit into
python-babel:masterfrom
Yasser-Ameur:fix/format-interval-fallback-skeleton

Conversation

@Yasser-Ameur

Copy link
Copy Markdown

format_interval ignores the requested skeleton when the locale has no interval format for it. The two ends are formatted with the locale's default format, so a 24 hour skeleton prints AM and PM in English:

>>> from datetime import time
>>> from babel.dates import format_interval
>>> format_interval(time(10, 5, 1), time(10, 5, 30), "Hms", locale="en")
'10:05:01 AM – 10:05:30 AM'   # before
'10:05:01 – 10:05:30'                   # now

With two datetimes on the same day, "Hms" gave Jan 8, 2016, 9:00:00 AM – Jan 8, 2016, 9:00:00 PM where 09:00:00 – 21:00:00 was asked for.

Cause

_format_fallback_interval already formats both ends with the skeleton when that skeleton is in locale.datetime_skeletons, and falls back to the default format otherwise. But format_interval reuses the skeleton variable for its lookup in locale.interval_formats: when nothing matches, the variable has been set to None by the time the fallback is called, so the branch that uses the requested skeleton never ran. Both pieces come from #316.

Change

format_interval keeps the skeleton it was called with and passes that one to _format_fallback_interval. Skeletons that have an interval format, and skeleton=None, give the same output as before.

Tests

tests/test_date_intervals.py gets tests for the fallback with a requested skeleton (times and datetimes, with and without fuzzy), for skeleton=None, and for a skeleton that has an interval format. The first group fails on master.

Run locally on Python 3.14 after make import-cldr: tests/test_date_intervals.py and tests/test_dates.py give 2593 passed, 2 xfailed, and ruff check is clean on the two changed files.

When no interval format matches the skeleton, format_interval formats
both ends and joins them with the locale's interval fallback pattern.
The ends were formatted with the default date or time format instead of
the requested skeleton, so format_interval(t1, t2, "Hms", locale="en")
printed AM/PM although "Hms" is a 24-hour skeleton.

_format_fallback_interval has a "Use the given skeleton" branch for this,
added together with format_interval in 796c3d2. It became unreachable
from this path in cd70395 (both part of python-babel#316), when the fuzzy match
started overwriting `skeleton` before the fallback call: the value is
None when nothing matched, and always None with fuzzy=False.

Keep the requested skeleton in its own variable and pass that to the
fallback. Skeletons that have interval data, skeleton=None and unknown
skeletons behave as before.
@Yasser-Ameur
Yasser-Ameur marked this pull request as ready for review December 1, 2026 00:03

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