1
0
mirror of https://github.com/django/django.git synced 2025-01-27 02:29:55 +00:00

[4.2.x] Fixed #34483 -- Fixed timesince()/timeuntil() with timezone-aware dates and interval less than 1 day.

Regression in 8d67e16493c903adc9d049141028bc0fff43f8c8.

Thanks Lorenzo Peña for the report.

Backport of 813015d67e2557fa859a07930a9becec4e5f64a0 from main
This commit is contained in:
nessita 2023-04-13 13:16:33 -03:00 committed by Natalia
parent 791407fef1
commit a3c14ea61b
3 changed files with 23 additions and 1 deletions

View File

@ -77,7 +77,8 @@ def timesince(d, now=None, reversed=False, time_strings=None, depth=2):
# Get years and months.
total_months = (now.year - d.year) * 12 + (now.month - d.month)
if d.day > now.day or (d.day == now.day and d.time() > now.time()):
time_delta = delta - datetime.timedelta(days=delta.days)
if d.day > now.day or (d.day == now.day and time_delta.total_seconds() < 0):
total_months -= 1
years, months = divmod(total_months, 12)

View File

@ -33,3 +33,7 @@ Bugfixes
* Fixed a regression in Django 4.2 where creating copies and deep copies of
``HttpRequest``, ``HttpResponse``, and their subclasses didn't always work
correctly (:ticket:`34482`, :ticket:`34484`).
* Fixed a regression in Django 4.2 where ``timesince`` and ``timeuntil``
template filters returned incorrect results for a datetime with a non-UTC
timezone when a time difference is less than 1 day (:ticket:`34483`).

View File

@ -1,4 +1,5 @@
import datetime
import zoneinfo
from django.test import TestCase
from django.test.utils import override_settings, requires_tz_support
@ -241,6 +242,22 @@ class TimesinceTests(TestCase):
with self.assertRaisesMessage(ValueError, msg):
timesince(self.t, self.t, depth=0)
@requires_tz_support
def test_less_than_a_day_with_zoneinfo(self):
now_with_zoneinfo = timezone.now().astimezone(
zoneinfo.ZoneInfo(key="Asia/Kathmandu") # UTC+05:45
)
tests = [
(now_with_zoneinfo, "0\xa0minutes"),
(now_with_zoneinfo - self.onemicrosecond, "0\xa0minutes"),
(now_with_zoneinfo - self.onesecond, "0\xa0minutes"),
(now_with_zoneinfo - self.oneminute, "1\xa0minute"),
(now_with_zoneinfo - self.onehour, "1\xa0hour"),
]
for value, expected in tests:
with self.subTest(value):
self.assertEqual(timesince(value), expected)
@requires_tz_support
@override_settings(USE_TZ=True)