| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -246,6 +246,10 @@ def naturaltime( | |||
| 246 | 246 | str: A natural representation of the input in a resolution that makes sense. | |
| 247 | 247 | """ | |
| 248 | 248 | 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 | + | ||
| 249 | 253 | date, delta = _date_and_delta(value, now=now) | |
| 250 | 254 | if date is None: | |
| 251 | 255 | return str(value) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -418,6 +418,40 @@ def test_naturaltime_minimum_unit_explicit( | |||
| 418 | 418 | assert humanize.naturaltime(datetime, minimum_unit=minimum_unit) == expected | |
| 419 | 419 | ||
| 420 | 420 | ||
| 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 | + | ||
| 421 | 455 | @pytest.mark.parametrize( | |
| 422 | 456 | "val, min_unit, expected", | |
| 423 | 457 | [ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments