[Django] #34243: "TypeError: can't subtract offset-naive and offset-aware datetimes" when using timesince() with USE_TZ=True and >1 month interval

43 views
Skip to first unread message

Django

unread,
Jan 5, 2023, 6:11:27 AM1/5/23
to django-...@googlegroups.com
#34243: "TypeError: can't subtract offset-naive and offset-aware datetimes" when
using timesince() with USE_TZ=True and >1 month interval
-----------------------------------------+-------------------------------
Reporter: Sage Abdullah | Owner: Sage Abdullah
Type: Bug | Status: assigned
Component: Utilities | Version: 4.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+-------------------------------
As of 8d67e16493c903adc9d049141028bc0fff43f8c8, calling `timesince()` with
a datetime object that's one month (or more) in the past and the `USE_TZ`
setting is set to `True` results in the following crash:

{{{
TypeError: can't subtract offset-naive and offset-aware datetimes
}}}

Test:

{{{
...

class TimesinceTests(TestCase):
...
@requires_tz_support
def test_long_interval_with_tz(self):
now = timezone.now()
d = now - datetime.timedelta(days=31)
self.assertEqual(timesince(d), "1\xa0month")
}}}

I believe this is because the pivot instantiated here:
https://github.com/django/django/blob/d2310f6473593d28c14b63a72253408b568e100a/django/utils/timesince.py#L93-L100
does not take into account the `datetime` object's `tzinfo`. Adding `0,
d.tzinfo` arguments to the `datetime.datetime` call seems to fix this.

Happy to send a PR.

--
Ticket URL: <https://code.djangoproject.com/ticket/34243>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 5, 2023, 6:22:18 AM1/5/23
to django-...@googlegroups.com
#34243: "TypeError: can't subtract offset-naive and offset-aware datetimes" when
using timesince() with USE_TZ=True and >1 month interval
-------------------------------+-----------------------------------------

Reporter: Sage Abdullah | Owner: Sage Abdullah
Type: Bug | Status: assigned
Component: Utilities | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Sage Abdullah):

* version: 4.1 => dev


--
Ticket URL: <https://code.djangoproject.com/ticket/34243#comment:1>

Django

unread,
Jan 5, 2023, 6:25:52 AM1/5/23
to django-...@googlegroups.com
#34243: "TypeError: can't subtract offset-naive and offset-aware datetimes" when
using timesince() with USE_TZ=True and >1 month interval
-------------------------------+-----------------------------------------
Reporter: Sage Abdullah | Owner: Sage Abdullah
Type: Bug | Status: assigned
Component: Utilities | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------

Comment (by Mariusz Felisiak):

Thanks for the report, however `test_long_interval_with_tz` works for me
on the current `main` branch 🤔

--
Ticket URL: <https://code.djangoproject.com/ticket/34243#comment:2>

Django

unread,
Jan 5, 2023, 6:30:18 AM1/5/23
to django-...@googlegroups.com
#34243: "TypeError: can't subtract offset-naive and offset-aware datetimes" when
using timesince() with USE_TZ=True and >1 month interval
-------------------------------+-----------------------------------------
Reporter: Sage Abdullah | Owner: Sage Abdullah
Type: Bug | Status: assigned
Component: Utilities | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------

Comment (by Sage Abdullah):

Whoops, sorry, I haven't properly tested the function as I currently don't
have a local Django dev environment.

I'm testing this on a shell with my Django project, I think this should be
reproducible:

{{{
>>> from django.utils import timezone
>>> from django.utils.timesince import timesince
>>> import datetime
>>> timesince(timezone.now() - datetime.timedelta(days=31))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/sage/Code/github/wagtail/wagtail/venv/lib/python3.10/site-
packages/django/utils/timesince.py", line 103, in timesince
remaining_time = (now - pivot).total_seconds()


TypeError: can't subtract offset-naive and offset-aware datetimes
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34243#comment:3>

Reply all
Reply to author
Forward
0 new messages