FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #100 from tandav/naturaltime-timezone-aware-datetimes · python-humanize/humanize@75abba2 · GitHub

Commit 75abba2

Browse files
authored
Merge pull request #100 from tandav/naturaltime-timezone-aware-datetimes
2 parents bdc5cb6 + ed6c3b0 commit 75abba2

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎src/humanize/time.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ def naturaltime(
246246
str: A natural representation of the input in a resolution that makes sense.
247247
"""
248248
now = when or _now()
249+
250+
if isinstance(value, dt.datetime) and value.tzinfo is not None:
251+
value = dt.datetime.fromtimestamp(value.timestamp())
252+
249253
date, delta = _date_and_delta(value, now=now)
250254
if date is None:
251255
return str(value)

‎tests/test_time.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,40 @@ def test_naturaltime_minimum_unit_explicit(
418418
assert humanize.naturaltime(datetime, minimum_unit=minimum_unit) == expected
419419

420420

421+
@freeze_time("2020-02-02")
422+
@pytest.mark.parametrize(
423+
"test_input, expected",
424+
[
425+
(NOW_UTC, "now"),
426+
(NOW_UTC - dt.timedelta(seconds=1), "a second ago"),
427+
(NOW_UTC - dt.timedelta(seconds=30), "30 seconds ago"),
428+
(NOW_UTC - dt.timedelta(minutes=1, seconds=30), "a minute ago"),
429+
(NOW_UTC - dt.timedelta(minutes=2), "2 minutes ago"),
430+
(NOW_UTC - dt.timedelta(hours=1, minutes=30, seconds=30), "an hour ago"),
431+
(NOW_UTC - dt.timedelta(hours=23, minutes=50, seconds=50), "23 hours ago"),
432+
(NOW_UTC - dt.timedelta(days=1), "a day ago"),
433+
(NOW_UTC - dt.timedelta(days=500), "1 year, 4 months ago"),
434+
(NOW_UTC - dt.timedelta(days=365 * 2 + 35), "2 years ago"),
435+
(NOW_UTC + dt.timedelta(seconds=1), "a second from now"),
436+
(NOW_UTC + dt.timedelta(seconds=30), "30 seconds from now"),
437+
(NOW_UTC + dt.timedelta(minutes=1, seconds=30), "a minute from now"),
438+
(NOW_UTC + dt.timedelta(minutes=2), "2 minutes from now"),
439+
(NOW_UTC + dt.timedelta(hours=1, minutes=30, seconds=30), "an hour from now"),
440+
(NOW_UTC + dt.timedelta(hours=23, minutes=50, seconds=50), "23 hours from now"),
441+
(NOW_UTC + dt.timedelta(days=1), "a day from now"),
442+
(NOW_UTC + dt.timedelta(days=500), "1 year, 4 months from now"),
443+
(NOW_UTC + dt.timedelta(days=365 * 2 + 35), "2 years from now"),
444+
# regression tests for bugs in post-release humanize
445+
(NOW_UTC + dt.timedelta(days=10000), "27 years from now"),
446+
(NOW_UTC - dt.timedelta(days=365 + 35), "1 year, 1 month ago"),
447+
(NOW_UTC - dt.timedelta(days=365 * 2 + 65), "2 years ago"),
448+
(NOW_UTC - dt.timedelta(days=365 + 4), "1 year, 4 days ago"),
449+
],
450+
)
451+
def test_naturaltime_timezone(test_input: dt.datetime, expected: str) -> None:
452+
assert humanize.naturaltime(test_input) == expected
453+
454+
421455
@pytest.mark.parametrize(
422456
"val, min_unit, expected",
423457
[

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL