Refs #27637 -- Fixed timesince, timeuntil on New Year's Eve in a leap year.

This commit is contained in:
vinay karanam 2017-01-02 19:10:44 +05:30 committed by Tim Graham
parent 26c9f529c9
commit 6128c1736d
4 changed files with 13 additions and 4 deletions

View File

@ -46,8 +46,11 @@ def timesince(d, now=None, reversed=False):
# Deal with leapyears by subtracing the number of leapdays # Deal with leapyears by subtracing the number of leapdays
leapdays = calendar.leapdays(d.year, now.year) leapdays = calendar.leapdays(d.year, now.year)
if leapdays != 0 and calendar.isleap(d.year): if leapdays != 0:
leapdays -= 1 if calendar.isleap(d.year):
leapdays -= 1
elif calendar.isleap(now.year):
leapdays += 1
delta -= datetime.timedelta(leapdays) delta -= datetime.timedelta(leapdays)
# ignore microseconds # ignore microseconds

View File

@ -16,4 +16,4 @@ Bugfixes
(:ticket:`27418`). (:ticket:`27418`).
* Fixed a regression in the ``timesince`` and ``timeuntil`` filters that caused * 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`).

View File

@ -10,4 +10,4 @@ Bugfixes
======== ========
* Fixed a regression in the ``timesince`` and ``timeuntil`` filters that caused * 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`).

View File

@ -110,6 +110,12 @@ class TimesinceTests(unittest.TestCase):
self.assertEqual(timeuntil(start_date + self.oneweek, start_date), '1\xa0week') self.assertEqual(timeuntil(start_date + self.oneweek, start_date), '1\xa0week')
self.assertEqual(timesince(start_date, start_date + self.oneweek), '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): def test_naive_datetime_with_tzinfo_attribute(self):
class naive(datetime.tzinfo): class naive(datetime.tzinfo):
def utcoffset(self, dt): def utcoffset(self, dt):