Commit 6700c909 authored by Raphael Michel's avatar Raphael Michel Committed by Tim Graham
Browse files

Fixed #19210 -- Added leap year support to django.utils.timesince()

parent 0207bdd2
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
from __future__ import unicode_literals

import calendar
import datetime

from django.utils.html import avoid_wrapping
@@ -40,6 +41,10 @@ def timesince(d, now=None, reversed=False):
        now = datetime.datetime.now(utc if is_aware(d) else None)

    delta = (d - now) if reversed else (now - d)

    # Deal with leapyears by subtracing the number of leapdays
    delta -= datetime.timedelta(calendar.leapdays(d.year, now.year))

    # ignore microseconds
    since = delta.days * 24 * 60 * 60 + delta.seconds
    if since <= 0:
+3 −0
Original line number Diff line number Diff line
@@ -297,6 +297,9 @@ Templates
  the ability to register libraries and builtins explicitly through the
  template :setting:`OPTIONS <TEMPLATES-OPTIONS>`.

* The ``timesince`` and ``timeuntil`` filters were improved to deal with leap
  years when given large time spans.

Requests and Responses
^^^^^^^^^^^^^^^^^^^^^^

+4 −0
Original line number Diff line number Diff line
@@ -131,3 +131,7 @@ class TimesinceTests(unittest.TestCase):
        self.assertEqual(timesince(future), '0\xa0minutes')
        past = datetime.datetime(1980, 1, 1, tzinfo=naive())
        self.assertEqual(timeuntil(past), '0\xa0minutes')

    def test_thousand_years_ago(self):
        t = datetime.datetime(1007, 8, 14, 13, 46, 0)
        self.assertEqual(timesince(t, self.t), '1000\xa0years')