Skip to content

Commit

Permalink
Merge pull request #246 from carterbox/intcomma-years
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Dec 13, 2021
2 parents 429e3d8 + 96b44e6 commit 49f2cd1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/humanize/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .i18n import _gettext as _
from .i18n import _ngettext
from .number import intcomma

__all__ = [
"naturaldelta",
Expand Down Expand Up @@ -264,7 +265,7 @@ def naturaldelta(
else:
return _ngettext("1 year, %d day", "1 year, %d days", days) % days
else:
return _ngettext("%d year", "%d years", years) % years
return _ngettext("%s year", "%s years", years) % intcomma(years)


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

texts.append(fmt_txt % value)

Expand Down
2 changes: 2 additions & 0 deletions tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def test_naturaldelta_nomonths(test_input, expected):
(dt.timedelta(days=65), "2 months"),
(dt.timedelta(days=9), "9 days"),
(dt.timedelta(days=365), "a year"),
(dt.timedelta(days=365 * 1_141), "1,141 years"),
("NaN", "NaN"),
],
)
Expand Down Expand Up @@ -446,6 +447,7 @@ def test_naturaltime_minimum_unit_explicit(minimum_unit, seconds, expected):
(3600 * 24 * 2, "seconds", "2 days"),
(3600 * 24 * 365, "seconds", "1 year"),
(3600 * 24 * 365 * 2, "seconds", "2 years"),
(3600 * 24 * 365 * 1_963, "seconds", "1,963 years"),
],
)
def test_precisedelta_one_unit_enough(val, min_unit, expected):
Expand Down

0 comments on commit 49f2cd1

Please sign in to comment.