Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

akx
Copy link
Member

@akx akx commented Apr 24, 2024

Fixes #892

@akx
Copy link
Member Author

akx commented Apr 24, 2024

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

@akx akx added this to the Babel 2.15 milestone Apr 24, 2024
@akx akx marked this pull request as ready for review April 24, 2024 13:01
@akx akx mentioned this pull request Apr 25, 2024
@jun66j5
Copy link
Contributor

jun66j5 commented Apr 25, 2024

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 年'

@akx akx merged commit c2e6c6e into master Apr 25, 2024
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

format_timedelta() for minutes=1 and hours=1 with narrow format and pt_BR locale returns an empty string
2 participants