python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.34k stars 448 forks source link

Encode support for the "fall back to short format" logic for time delta formatting #1075

Closed akx closed 7 months ago

akx commented 7 months ago

Fixes #892

akx commented 7 months ago

@jun66j5 Could you take a review look at this? Thank you.

jun66j5 commented 7 months ago

LGTM. Thanks!

Confirmed time delta formatting not returning empty string by the following snippet:

from datetime import timedelta
from babel import dates, localedata

for l in sorted(localedata.locale_identifiers()):
    for delta in (
        timedelta(seconds=1), timedelta(minutes=1), timedelta(hours=1),
        timedelta(days=1), timedelta(days=30), timedelta(days=365),
    ):
        for f in ('narrow', 'short', 'long'):
            kwargs = {'delta': delta, 'format': f, 'locale': l}
            rv = dates.format_timedelta(**kwargs)
            if not rv:
                print(repr(kwargs))

Also, long format with ja locale had the same issue but it goes away:

Before this pull request

>>> dates.format_timedelta(delta=timedelta(seconds=1), format='short', locale='ja')
'1 秒'
>>> dates.format_timedelta(delta=timedelta(seconds=60), format='short', locale='ja')
'1 分'
>>> dates.format_timedelta(delta=timedelta(seconds=3600), format='short', locale='ja')
'1 時間'
>>> dates.format_timedelta(delta=timedelta(days=1), format='short', locale='ja')
'1 日'
>>> dates.format_timedelta(delta=timedelta(days=30), format='short', locale='ja')
'1 か月'
>>> dates.format_timedelta(delta=timedelta(days=365), format='short', locale='ja')
'1 年'
>>> dates.format_timedelta(delta=timedelta(seconds=1), format='long', locale='ja')
''
>>> dates.format_timedelta(delta=timedelta(seconds=60), format='long', locale='ja')
''
>>> dates.format_timedelta(delta=timedelta(seconds=3600), format='long', locale='ja')
''
>>> dates.format_timedelta(delta=timedelta(days=1), format='long', locale='ja')
''
>>> dates.format_timedelta(delta=timedelta(days=30), format='long', locale='ja')
''
>>> dates.format_timedelta(delta=timedelta(days=365), format='long', locale='ja')
''

After this pull request

>>> dates.format_timedelta(delta=timedelta(seconds=1), format='short', locale='ja')
'1 秒'
>>> dates.format_timedelta(delta=timedelta(seconds=60), format='short', locale='ja')
'1 分'
>>> dates.format_timedelta(delta=timedelta(seconds=3600), format='short', locale='ja')
'1 時間'
>>> dates.format_timedelta(delta=timedelta(days=1), format='short', locale='ja')
'1 日'
>>> dates.format_timedelta(delta=timedelta(days=30), format='short', locale='ja')
'1 か月'
>>> dates.format_timedelta(delta=timedelta(days=365), format='short', locale='ja')
'1 年'
>>> dates.format_timedelta(delta=timedelta(seconds=1), format='long', locale='ja')
'1 秒'
>>> dates.format_timedelta(delta=timedelta(seconds=60), format='long', locale='ja')
'1 分'
>>> dates.format_timedelta(delta=timedelta(seconds=3600), format='long', locale='ja')
'1 時間'
>>> dates.format_timedelta(delta=timedelta(days=1), format='long', locale='ja')
'1 日'
>>> dates.format_timedelta(delta=timedelta(days=30), format='long', locale='ja')
'1 か月'
>>> dates.format_timedelta(delta=timedelta(days=365), format='long', locale='ja')
'1 年'