Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/humanize/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,22 @@ def precisedelta(
# years, days = divmod(years, days)
#
# The same applies for months, hours, minutes and milliseconds below
years, days = _quotient_and_remainder(
days, 365, YEARS, min_unit, suppress_set, format
)
months, days = _quotient_and_remainder(
days, 30.5, MONTHS, min_unit, suppress_set, format
)
if min_unit == YEARS:
years = _rounding_by_fmt(format, (days * 24 * 3600 + secs) / (365 * 24 * 3600))
days = secs = usecs = 0
else:
years, days = _quotient_and_remainder(
days, 365, YEARS, min_unit, suppress_set, format
)
if min_unit == MONTHS:
months = _rounding_by_fmt(
format, (days * 24 * 3600 + secs) / (30.5 * 24 * 3600)
)
days = secs = usecs = 0
else:
months, days = _quotient_and_remainder(
days, 30.5, MONTHS, min_unit, suppress_set, format
)

secs = days * 24 * 3600 + secs
days, secs = _quotient_and_remainder(
Expand Down
34 changes: 34 additions & 0 deletions tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,22 @@ def test_precisedelta_multiple_units(
(dt.timedelta(days=62), "days", "%d", "2 months and 1 day"),
(dt.timedelta(days=92), "days", "%d", "3 months"),
(dt.timedelta(days=120), "months", "%0.2f", "3.93 months"),
(dt.timedelta(hours=12), "months", "%0.2f", "0.02 months"),
(dt.timedelta(days=15, hours=6), "months", "%0.2f", "0.50 months"),
(dt.timedelta(days=30, hours=12), "months", "%0.2f", "1 month"),
(
dt.timedelta(days=380, hours=6),
"months",
"%0.2f",
"1 year and 0.50 months",
),
(dt.timedelta(days=182, hours=12), "years", "%0.4f", "0.5000 years"),
(dt.timedelta(hours=12), "years", "%0.4f", "0.0014 years"),
(dt.timedelta(hours=12), "years", "%0.2f", "0 years"),
(43_200, "months", "%0.2f", "0.02 months"),
(65_880, "months", "%0.2f", "0.03 months"),
(2_680_560, "years", "%0.2f", "0.09 years"),
(15_768_000, "years", "%0.4f", "0.5000 years"),
(dt.timedelta(days=183), "years", "%0.1f", "0.5 years"),
(0.01, "seconds", "%0.3f", "0.010 seconds"),
# 31 seconds will be truncated to 0 with %d and rounded to the nearest
Expand Down Expand Up @@ -769,6 +785,24 @@ def test_precisedelta_custom_format(
[],
"1 millisecond and 200 microseconds",
),
(
dt.timedelta(days=15, hours=6),
"days",
["days"],
"0.50 months",
),
(
dt.timedelta(days=182, hours=12),
"months",
["months"],
"0.50 years",
),
(
dt.timedelta(days=380, hours=6),
"months",
["years"],
"12.47 months",
),
(
dt.timedelta(microseconds=1200),
"microseconds",
Expand Down