Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update annotations, docs, and tests: naturaltime can also accept a timedelta #31

Merged
merged 1 commit into from Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/humanize/time.py
Expand Up @@ -206,7 +206,7 @@ def naturaldelta(


def naturaltime(
value: dt.datetime | float,
value: dt.datetime | dt.timedelta | float,
future: bool = False,
months: bool = True,
minimum_unit: str = "seconds",
Expand All @@ -217,10 +217,11 @@ def naturaltime(
This is more or less compatible with Django's `naturaltime` filter.
Args:
value (datetime.datetime, int or float): A `datetime` or a number of seconds.
future (bool): Ignored for `datetime`s, where the tense is always figured out
based on the current time. For integers, the return value will be past tense
by default, unless future is `True`.
value (datetime.datetime, datetime.timedelta, int or float): A `datetime`, a
`timedelta`, or a number of seconds.
future (bool): Ignored for `datetime`s and `timedelta`s, where the tense is
always figured out based on the current time. For integers and floats, the
return value will be past tense by default, unless future is `True`.
months (bool): If `True`, then a number of months (based on 30.5 days) will be
used for fuzziness between years.
minimum_unit (str): The lowest unit that can be used.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_time.py
Expand Up @@ -157,6 +157,8 @@ def test_naturaldelta(test_input: int | dt.timedelta, expected: str) -> None:
# regression tests for bugs in post-release humanize
(NOW + dt.timedelta(days=10000), "27 years from now"),
(NOW - dt.timedelta(days=365 + 35), "1 year, 1 month ago"),
(dt.timedelta(days=-10000), "27 years from now"),
(dt.timedelta(days=365 + 35), "1 year, 1 month ago"),
(23.5, "23 seconds ago"),
(30, "30 seconds ago"),
(NOW - dt.timedelta(days=365 * 2 + 65), "2 years ago"),
Expand Down Expand Up @@ -200,6 +202,9 @@ def nt_nomonths(d: dt.datetime) -> str:
# regression tests for bugs in post-release humanize
(NOW + dt.timedelta(days=10000), "27 years from now"),
(NOW - dt.timedelta(days=365 + 35), "1 year, 35 days ago"),
(dt.timedelta(days=-10000), "27 years from now"),
(dt.timedelta(days=365 + 35), "1 year, 35 days ago"),
(23.5, "23 seconds ago"),
(30, "30 seconds ago"),
(NOW - dt.timedelta(days=365 * 2 + 65), "2 years ago"),
(NOW - dt.timedelta(days=365 + 4), "1 year, 4 days ago"),
Expand Down