Skip to content

Commit 985637d

Browse files
committed
Preserve exact microsecond counts in precisedelta
1 parent 38d9d91 commit 985637d

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/humanize/time.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ def precisedelta(
608608
secs, 60, MINUTES, min_unit, suppress_set, format
609609
)
610610

611-
usecs = secs * 1e6 + usecs
611+
usecs = secs * 1_000_000 + usecs
612612
secs, usecs = _quotient_and_remainder(
613613
usecs, 1e6, SECONDS, min_unit, suppress_set, format
614614
)

‎tests/test_time.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,25 @@ def test_precisedelta_suppress_units(
838838
)
839839

840840

841+
@pytest.mark.parametrize("microseconds", [2**53 - 1, 2**53 + 1, 2**53 + 3])
842+
@pytest.mark.parametrize("sign", [1, -1])
843+
def test_precisedelta_large_microsecond_count(microseconds: int, sign: int) -> None:
844+
value = dt.timedelta(microseconds=sign * microseconds)
845+
suppress = [
846+
"years",
847+
"months",
848+
"days",
849+
"hours",
850+
"minutes",
851+
"seconds",
852+
"milliseconds",
853+
]
854+
assert (
855+
humanize.precisedelta(value, minimum_unit="microseconds", suppress=suppress)
856+
== f"{microseconds} microseconds"
857+
)
858+
859+
841860
def test_precisedelta_bogus_call() -> None:
842861
assert humanize.precisedelta(None) == "None"
843862

0 commit comments

Comments
 (0)