summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphael Michel <mail@raphaelmichel.de>2015-06-05 00:46:52 +0200
committerTim Graham <timograham@gmail.com>2015-06-04 21:36:12 -0400
commit6700c909358d2ff9ff96f28dbc549906d8d32206 (patch)
tree3d07c68c5c54b263ab963237ed064f21ba81b5a8
parent0207bdd2d4157c542c981264c86706b78ca246e9 (diff)
Fixed #19210 -- Added leap year support to django.utils.timesince()
-rw-r--r--django/utils/timesince.py5
-rw-r--r--docs/releases/1.9.txt3
-rw-r--r--tests/utils_tests/test_timesince.py4
3 files changed, 12 insertions, 0 deletions
diff --git a/django/utils/timesince.py b/django/utils/timesince.py
index b4ae61e4d9..06accab2e0 100644
--- a/django/utils/timesince.py
+++ b/django/utils/timesince.py
@@ -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:
diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt
index ecd2af6ddc..e06946aaa6 100644
--- a/docs/releases/1.9.txt
+++ b/docs/releases/1.9.txt
@@ -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
^^^^^^^^^^^^^^^^^^^^^^
diff --git a/tests/utils_tests/test_timesince.py b/tests/utils_tests/test_timesince.py
index 90d33d00f7..ea1064f902 100644
--- a/tests/utils_tests/test_timesince.py
+++ b/tests/utils_tests/test_timesince.py
@@ -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')