Skip to content

Commit bc23540

Browse files
authored
Merge pull request #31 from nuztalgia/naturaltime-timedelta
2 parents c692722 + cecce62 commit bc23540

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/humanize/time.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def naturaldelta(
206206

207207

208208
def naturaltime(
209-
value: dt.datetime | float,
209+
value: dt.datetime | dt.timedelta | float,
210210
future: bool = False,
211211
months: bool = True,
212212
minimum_unit: str = "seconds",
@@ -217,10 +217,11 @@ def naturaltime(
217217
This is more or less compatible with Django's `naturaltime` filter.
218218
219219
Args:
220-
value (datetime.datetime, int or float): A `datetime` or a number of seconds.
221-
future (bool): Ignored for `datetime`s, where the tense is always figured out
222-
based on the current time. For integers, the return value will be past tense
223-
by default, unless future is `True`.
220+
value (datetime.datetime, datetime.timedelta, int or float): A `datetime`, a
221+
`timedelta`, or a number of seconds.
222+
future (bool): Ignored for `datetime`s and `timedelta`s, where the tense is
223+
always figured out based on the current time. For integers and floats, the
224+
return value will be past tense by default, unless future is `True`.
224225
months (bool): If `True`, then a number of months (based on 30.5 days) will be
225226
used for fuzziness between years.
226227
minimum_unit (str): The lowest unit that can be used.

tests/test_time.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ def test_naturaldelta(test_input: int | dt.timedelta, expected: str) -> None:
157157
# regression tests for bugs in post-release humanize
158158
(NOW + dt.timedelta(days=10000), "27 years from now"),
159159
(NOW - dt.timedelta(days=365 + 35), "1 year, 1 month ago"),
160+
(dt.timedelta(days=-10000), "27 years from now"),
161+
(dt.timedelta(days=365 + 35), "1 year, 1 month ago"),
160162
(23.5, "23 seconds ago"),
161163
(30, "30 seconds ago"),
162164
(NOW - dt.timedelta(days=365 * 2 + 65), "2 years ago"),
@@ -200,6 +202,9 @@ def nt_nomonths(d: dt.datetime) -> str:
200202
# regression tests for bugs in post-release humanize
201203
(NOW + dt.timedelta(days=10000), "27 years from now"),
202204
(NOW - dt.timedelta(days=365 + 35), "1 year, 35 days ago"),
205+
(dt.timedelta(days=-10000), "27 years from now"),
206+
(dt.timedelta(days=365 + 35), "1 year, 35 days ago"),
207+
(23.5, "23 seconds ago"),
203208
(30, "30 seconds ago"),
204209
(NOW - dt.timedelta(days=365 * 2 + 65), "2 years ago"),
205210
(NOW - dt.timedelta(days=365 + 4), "1 year, 4 days ago"),

0 commit comments

Comments
 (0)