Skip to content

Commit

Permalink
Fix naturaldelta sub second precision (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Feb 24, 2024
2 parents 584d470 + c94f951 commit 1399e04
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/humanize/time.py
Expand Up @@ -105,7 +105,8 @@ def naturaldelta(
Returns:
str (str or `value`): A natural representation of the amount of time
elapsed unless `value` is not datetime.timedelta or cannot be
converted to int. In that case, a `value` is returned unchanged.
converted to int (cannot be float due to 'inf' or 'nan').
In that case, a `value` is returned unchanged.
Raises:
OverflowError: If `value` is too large to convert to datetime.timedelta.
Expand All @@ -132,7 +133,8 @@ def naturaldelta(
delta = value
else:
try:
value = int(value)
int(value) # Explicitly don't support string such as "NaN" or "inf"
value = float(value)
delta = dt.timedelta(seconds=value)
except (ValueError, TypeError):
return str(value)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_time.py
Expand Up @@ -127,7 +127,7 @@ def test_naturaldelta_nomonths(test_input: dt.timedelta, expected: str) -> None:
(dt.timedelta(days=999_999_999), "2,739,726 years"),
],
)
def test_naturaldelta(test_input: int | dt.timedelta, expected: str) -> None:
def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None:
assert humanize.naturaldelta(test_input) == expected


Expand Down Expand Up @@ -340,6 +340,7 @@ def test_naturaldelta_minimum_unit_explicit(

# Act / Assert
assert humanize.naturaldelta(delta, minimum_unit=minimum_unit) == expected
assert humanize.naturaldelta(seconds, minimum_unit=minimum_unit) == expected


@freeze_time("2020-02-02")
Expand Down

0 comments on commit 1399e04

Please sign in to comment.