diff --git a/django/utils/timesince.py b/django/utils/timesince.py index 3a0d4afb1a..8a1174d6e3 100644 --- a/django/utils/timesince.py +++ b/django/utils/timesince.py @@ -88,6 +88,9 @@ def timesince(d, now=None, reversed=False, time_strings=None, depth=2): if count == 0: break result.append(avoid_wrapping(time_strings[name] % {"num": count})) + # prevent errors in long intervals + if name == "month" and count > 2: + seconds += 10 * 60 * 60 since -= seconds * count current_depth += 1 i += 1 diff --git a/tests/template_tests/filter_tests/test_timesince.py b/tests/template_tests/filter_tests/test_timesince.py index 10e4e51d89..abc52eb036 100644 --- a/tests/template_tests/filter_tests/test_timesince.py +++ b/tests/template_tests/filter_tests/test_timesince.py @@ -147,6 +147,14 @@ class TimesinceTests(TimezoneTestCase): ) self.assertEqual(output, "1\xa0day") + # Test for #33879 (wrong results for 11 months + several weeks) + @setup({"timesince19": "{{ earlier|timesince }}"}) + def test_timesince19(self): + output = self.engine.render_to_string( + "timesince19", {"earlier": self.today - timedelta(days=358)} + ) + self.assertEqual(output, "11\xa0months, 3\xa0weeks") + class FunctionTests(SimpleTestCase): def test_since_now(self):