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
7 changes: 2 additions & 5 deletions src/humanize/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _date_and_delta(
value = value if precise else round(value)
delta = dt.timedelta(seconds=value)
date = now - delta
except (ValueError, TypeError):
except (ValueError, TypeError, OverflowError):
return None, value
return date, _abs_timedelta(delta)

Expand Down Expand Up @@ -117,9 +117,6 @@ def naturaldelta(
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.
Examples:
Compare two timestamps in a custom local timezone::
Expand Down Expand Up @@ -151,7 +148,7 @@ def naturaldelta(
int(value) # Explicitly don't support string such as "NaN" or "inf"
value = float(value)
delta = dt.timedelta(seconds=value)
except (ValueError, TypeError):
except (ValueError, TypeError, OverflowError):
return str(value)

use_months = months
Expand Down
13 changes: 12 additions & 1 deletion tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import datetime as dt
import math
import typing

import pytest
Expand Down Expand Up @@ -71,6 +72,8 @@ def test_date_and_delta() -> None:
assert_equal_datetime(date, result[0])
assert_equal_timedelta(d, result[1])
assert time._date_and_delta("NaN") == (None, "NaN")
assert time._date_and_delta(float("inf")) == (None, float("inf"))
assert time._date_and_delta(float("-inf")) == (None, float("-inf"))


# Tests for the public interface of humanize.time
Expand Down Expand Up @@ -128,13 +131,17 @@ def test_naturaldelta_nomonths(test_input: dt.timedelta, expected: str) -> None:
(dt.timedelta(days=365), "a year"),
(dt.timedelta(days=365 * 1_141), "1,141 years"),
("NaN", "NaN"), # Returns non-numbers unchanged.
(float("inf"), "inf"),
(float("-inf"), "-inf"),
# largest possible timedelta
(dt.timedelta(days=999_999_999), "2,739,726 years"),
],
)
def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None:
assert humanize.naturaldelta(test_input) == expected
if not isinstance(test_input, str):
if not isinstance(test_input, str) and not (
isinstance(test_input, float) and math.isinf(test_input)
):
assert humanize.naturaldelta(-test_input) == expected


Expand Down Expand Up @@ -179,6 +186,8 @@ def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None:
(NOW - dt.timedelta(days=365 * 2 + 65), "2 years ago"),
(NOW - dt.timedelta(days=365 + 4), "1 year, 4 days ago"),
("NaN", "NaN"),
(float("inf"), "inf"),
(float("-inf"), "-inf"),
],
)
def test_naturaltime(
Expand Down Expand Up @@ -817,6 +826,8 @@ def test_precisedelta_suppress_units(

def test_precisedelta_bogus_call() -> None:
assert humanize.precisedelta(None) == "None"
assert humanize.precisedelta(float("inf")) == "inf"
assert humanize.precisedelta(float("-inf")) == "-inf"

with pytest.raises(
ValueError,
Expand Down