From 985637de17a85a0ef8ea02a60384d6281dc0ffcd Mon Sep 17 00:00:00 2001 From: Sskift <163287349+Sskift@users.noreply.github.com> Date: Tue, 3 Nov 2026 01:08:29 +0800 Subject: [PATCH] Preserve exact microsecond counts in precisedelta --- src/humanize/time.py | 2 +- tests/test_time.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/humanize/time.py b/src/humanize/time.py index 7c4a28eb..d1c2afe5 100644 --- a/src/humanize/time.py +++ b/src/humanize/time.py @@ -608,7 +608,7 @@ def precisedelta( secs, 60, MINUTES, min_unit, suppress_set, format ) - usecs = secs * 1e6 + usecs + usecs = secs * 1_000_000 + usecs secs, usecs = _quotient_and_remainder( usecs, 1e6, SECONDS, min_unit, suppress_set, format ) diff --git a/tests/test_time.py b/tests/test_time.py index 887a3fc5..344689d5 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -838,6 +838,25 @@ def test_precisedelta_suppress_units( ) +@pytest.mark.parametrize("microseconds", [2**53 - 1, 2**53 + 1, 2**53 + 3]) +@pytest.mark.parametrize("sign", [1, -1]) +def test_precisedelta_large_microsecond_count(microseconds: int, sign: int) -> None: + value = dt.timedelta(microseconds=sign * microseconds) + suppress = [ + "years", + "months", + "days", + "hours", + "minutes", + "seconds", + "milliseconds", + ] + assert ( + humanize.precisedelta(value, minimum_unit="microseconds", suppress=suppress) + == f"{microseconds} microseconds" + ) + + def test_precisedelta_bogus_call() -> None: assert humanize.precisedelta(None) == "None"