Fixed #19210 -- Added leap year support to django.utils.timesince()
This commit is contained in:
parent
0207bdd2d4
commit
6700c90935
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue