summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvinay karanam <vinayinvicible@users.noreply.github.com>2017-01-02 19:10:44 +0530
committerTim Graham <timograham@gmail.com>2017-01-02 08:48:45 -0500
commit589a091b3bf70a379c0f96dbdce700e6756f4a36 (patch)
tree887d67a921fefc974c0aa83b8b2a5e7e8279eaae
parent9e8bfc5c62a40826a136c02d91e276e129551e54 (diff)
[1.9.x] Refs #27637 -- Fixed timesince, timeuntil on New Year's Eve in a leap year.
Backport of 6128c1736de98d8ab22829184409731b030cbff5 from master
-rw-r--r--django/utils/timesince.py7
-rw-r--r--docs/releases/1.9.13.txt2
-rw-r--r--tests/utils_tests/test_timesince.py6
3 files changed, 12 insertions, 3 deletions
diff --git a/django/utils/timesince.py b/django/utils/timesince.py
index 1bf24809d4..b0eefaf734 100644
--- a/django/utils/timesince.py
+++ b/django/utils/timesince.py
@@ -46,8 +46,11 @@ def timesince(d, now=None, reversed=False):
# Deal with leapyears by subtracing the number of leapdays
leapdays = calendar.leapdays(d.year, now.year)
- if leapdays != 0 and calendar.isleap(d.year):
- leapdays -= 1
+ if leapdays != 0:
+ if calendar.isleap(d.year):
+ leapdays -= 1
+ elif calendar.isleap(now.year):
+ leapdays += 1
delta -= datetime.timedelta(leapdays)
# ignore microseconds
diff --git a/docs/releases/1.9.13.txt b/docs/releases/1.9.13.txt
index 093b40f057..03ffd80400 100644
--- a/docs/releases/1.9.13.txt
+++ b/docs/releases/1.9.13.txt
@@ -10,4 +10,4 @@ Bugfixes
========
* Fixed a regression in the ``timesince`` and ``timeuntil`` filters that caused
- incorrect results for dates in a leap year.
+ incorrect results for dates in a leap year (:ticket:`27637`).
diff --git a/tests/utils_tests/test_timesince.py b/tests/utils_tests/test_timesince.py
index ecd0dfa336..ee74c3fb4e 100644
--- a/tests/utils_tests/test_timesince.py
+++ b/tests/utils_tests/test_timesince.py
@@ -128,6 +128,12 @@ class TimesinceTests(unittest.TestCase):
self.assertEqual(timeuntil(start_date + self.oneweek, start_date), '1\xa0week')
self.assertEqual(timesince(start_date, start_date + self.oneweek), '1\xa0week')
+ def test_leap_year_new_years_eve(self):
+ t = datetime.date(2016, 12, 31)
+ now = datetime.datetime(2016, 12, 31, 18, 0, 0)
+ self.assertEqual(timesince(t + self.oneday, now), '0\xa0minutes')
+ self.assertEqual(timeuntil(t - self.oneday, now), '0\xa0minutes')
+
def test_naive_datetime_with_tzinfo_attribute(self):
class naive(datetime.tzinfo):
def utcoffset(self, dt):