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

Use humanize.intcomma to format years in time module #246

Merged
merged 2 commits into from Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/humanize/time.py
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
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