Skip to content

Commit 49f2cd1

Browse files
authored
Merge pull request #246 from carterbox/intcomma-years
2 parents 429e3d8 + 96b44e6 commit 49f2cd1

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/humanize/time.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from .i18n import _gettext as _
1515
from .i18n import _ngettext
16+
from .number import intcomma
1617

1718
__all__ = [
1819
"naturaldelta",
@@ -264,7 +265,7 @@ def naturaldelta(
264265
else:
265266
return _ngettext("1 year, %d day", "1 year, %d days", days) % days
266267
else:
267-
return _ngettext("%d year", "%d years", years) % years
268+
return _ngettext("%s year", "%s years", years) % intcomma(years)
268269

269270

270271
def naturaltime(
@@ -605,6 +606,10 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f") ->
605606
fmt_txt = _ngettext(singular_txt, plural_txt, value)
606607
if unit == min_unit and math.modf(value)[0] > 0:
607608
fmt_txt = fmt_txt.replace("%d", format)
609+
elif unit == YEARS:
610+
fmt_txt = fmt_txt.replace("%d", "%s")
611+
texts.append(fmt_txt % intcomma(value))
612+
continue
608613

609614
texts.append(fmt_txt % value)
610615

tests/test_time.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def test_naturaldelta_nomonths(test_input, expected):
118118
(dt.timedelta(days=65), "2 months"),
119119
(dt.timedelta(days=9), "9 days"),
120120
(dt.timedelta(days=365), "a year"),
121+
(dt.timedelta(days=365 * 1_141), "1,141 years"),
121122
("NaN", "NaN"),
122123
],
123124
)
@@ -446,6 +447,7 @@ def test_naturaltime_minimum_unit_explicit(minimum_unit, seconds, expected):
446447
(3600 * 24 * 2, "seconds", "2 days"),
447448
(3600 * 24 * 365, "seconds", "1 year"),
448449
(3600 * 24 * 365 * 2, "seconds", "2 years"),
450+
(3600 * 24 * 365 * 1_963, "seconds", "1,963 years"),
449451
],
450452
)
451453
def test_precisedelta_one_unit_enough(val, min_unit, expected):

0 commit comments

Comments
 (0)