#34243 closed Bug (fixed)

timesince() raises TypeError with USE_TZ=True and >1 month interval.

Reported by: Sage Abdullah Owned by: Sage Abdullah
Component: Utilities Version: dev
Severity: Release blocker Keywords:
Cc: GianpaoloBranca Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Sage Abdullah)

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
    @override_settings(USE_TZ=True)
    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.

Change History (10)

comment:1 by Sage Abdullah, 23 months ago

Version: 4.1dev

comment:2 by Mariusz Felisiak, 23 months ago

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

comment:3 by Sage Abdullah, 23 months ago

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

comment:4 by Mariusz Felisiak, 23 months ago

Cc: GianpaoloBranca added
Severity: NormalRelease blocker
Summary: "TypeError: can't subtract offset-naive and offset-aware datetimes" when using timesince() with USE_TZ=True and >1 month intervaltimesince() raises TypeError with USE_TZ=True and >1 month interval.
Triage Stage: UnreviewedAccepted

OK, with self.settings(USE_TZ=True) was missing:

    @requires_tz_support
    def test_long_interval_with_tz(self):
        with self.settings(USE_TZ=True):
            now = timezone.now()
            d = now - datetime.timedelta(days=40)
            self.assertEqual(timesince(d), "1\xa0month")

Regression in 8d67e16493c903adc9d049141028bc0fff43f8c8.

comment:5 by Sage Abdullah, 23 months ago

Description: modified (diff)
Severity: Release blockerNormal
Summary: timesince() raises TypeError with USE_TZ=True and >1 month interval."TypeError: can't subtract offset-naive and offset-aware datetimes" when using timesince() with USE_TZ=True and >1 month interval
Triage Stage: AcceptedUnreviewed

comment:6 by Sage Abdullah, 23 months ago

Severity: NormalRelease blocker
Summary: "TypeError: can't subtract offset-naive and offset-aware datetimes" when using timesince() with USE_TZ=True and >1 month intervaltimesince() raises TypeError with USE_TZ=True and >1 month interval.
Triage Stage: UnreviewedAccepted

Oops, ninja'd! I was adding override_settings to the ticket description.

comment:7 by Sage Abdullah, 23 months ago

Has patch: set

comment:8 by Carlton Gibson, 23 months ago

Good catch! Thanks for testing against main! 🎁

comment:9 by Mariusz Felisiak, 23 months ago

Triage Stage: AcceptedReady for checkin

comment:10 by GitHub <noreply@…>, 23 months ago

Resolution: fixed
Status: assignedclosed

In 8cf3831:

Fixed #34243 -- Fixed timesince() crash with timezone-aware dates and interval longer than 1 month.

Regression in 8d67e16493c903adc9d049141028bc0fff43f8c8.

Note: See TracTickets for help on using tickets.
Back to Top