diff options
author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-04-14 17:41:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-14 17:41:03 +0200 |
commit | 198a19b692699ad3940373d9ed797fe9155f3f4a (patch) | |
tree | b0f0bae936c972e654ffb9f26229c2ffdd59530a /django | |
parent | 53aee470d5b35e2708864d5221d2b5655e10c091 (diff) | |
download | django-198a19b692699ad3940373d9ed797fe9155f3f4a.tar.gz |
Refs #34483 -- Fixed timesince()/timeuntil() with timezone-aware dates on different days and interval less than 1 day.
Follow up to 813015d67e2557fa859a07930a9becec4e5f64a0.
Regression in 8d67e16493c903adc9d049141028bc0fff43f8c8.
Diffstat (limited to 'django')
-rw-r--r-- | django/utils/timesince.py | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/django/utils/timesince.py b/django/utils/timesince.py index 94ba24d48a..766b6f7030 100644 --- a/django/utils/timesince.py +++ b/django/utils/timesince.py @@ -63,7 +63,11 @@ def timesince(d, now=None, reversed=False, time_strings=None, depth=2): if now and not isinstance(now, datetime.datetime): now = datetime.datetime(now.year, now.month, now.day) - now = now or datetime.datetime.now(datetime.timezone.utc if is_aware(d) else None) + # Compared datetimes must be in the same time zone. + if not now: + now = datetime.datetime.now(d.tzinfo if is_aware(d) else None) + elif is_aware(now) and is_aware(d): + now = now.astimezone(d.tzinfo) if reversed: d, now = now, d @@ -77,8 +81,7 @@ 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) - time_delta = delta - datetime.timedelta(days=delta.days) - if d.day > now.day or (d.day == now.day and time_delta.total_seconds() < 0): + if d.day > now.day or (d.day == now.day and d.time() > now.time()): total_months -= 1 years, months = divmod(total_months, 12) |