1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

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

This commit is contained in:
Raphael Michel 2015-06-05 00:46:52 +02:00 committed by Tim Graham
parent 0207bdd2d4
commit 6700c90935
3 changed files with 12 additions and 0 deletions

View File

@ -1,5 +1,6 @@
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:

View File

@ -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
^^^^^^^^^^^^^^^^^^^^^^

View File

@ -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')