]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Encode support for the "fall back to short format" logic for time delta formatting...
authorAarni Koskela <akx@iki.fi>
Thu, 25 Apr 2024 18:23:10 +0000 (21:23 +0300)
committerGitHub <noreply@github.com>
Thu, 25 Apr 2024 18:23:10 +0000 (21:23 +0300)
Fixes #892

babel/dates.py
tests/test_dates.py

index 040820a6d6fe115678cb5ff6314abd1df6b801fa..c2e4f0cc7df29b59e1ba3c790e41761dea92f436 100644 (file)
@@ -943,7 +943,14 @@ def format_timedelta(
             else:
                 yield unit_rel_patterns['past']
         a_unit = f"duration-{a_unit}"
-        yield locale._data['unit_patterns'].get(a_unit, {}).get(format)
+        unit_pats = locale._data['unit_patterns'].get(a_unit, {})
+        yield unit_pats.get(format)
+        # We do not support `<alias>` tags at all while ingesting CLDR data,
+        # so these aliases specified in `root.xml` are hard-coded here:
+        # <unitLength type="long"><alias source="locale" path="../unitLength[@type='short']"/></unitLength>
+        # <unitLength type="narrow"><alias source="locale" path="../unitLength[@type='short']"/></unitLength>
+        if format in ("long", "narrow"):
+            yield unit_pats.get("short")
 
     for unit, secs_per_unit in TIMEDELTA_UNITS:
         value = abs(seconds) / secs_per_unit
@@ -956,7 +963,8 @@ def format_timedelta(
             for patterns in _iter_patterns(unit):
                 if patterns is not None:
                     pattern = patterns.get(plural_form) or patterns.get('other')
-                    break
+                    if pattern:
+                        break
             # This really should not happen
             if pattern is None:
                 return ''
index d3f2ad9e70d5b6de52f36b7ac38c88fa31034945..f25dd6146bdadc983108d6b67cce20e393285619 100644 (file)
@@ -742,3 +742,12 @@ def test_en_gb_first_weekday():
 
 def test_issue_798():
     assert dates.format_timedelta(timedelta(), format='narrow', locale='es_US') == '0s'
+
+
+def test_issue_892():
+    assert dates.format_timedelta(timedelta(seconds=1), format='narrow', locale='pt_BR') == '1 s'
+    assert dates.format_timedelta(timedelta(minutes=1), format='narrow', locale='pt_BR') == '1 min'
+    assert dates.format_timedelta(timedelta(hours=1), format='narrow', locale='pt_BR') == '1 h'
+    assert dates.format_timedelta(timedelta(days=1), format='narrow', locale='pt_BR') == '1 dia'
+    assert dates.format_timedelta(timedelta(days=30), format='narrow', locale='pt_BR') == '1 mês'
+    assert dates.format_timedelta(timedelta(days=365), format='narrow', locale='pt_BR') == '1 ano'