mirror of https://github.com/django/django.git
Fixed #27637 -- Fixed timesince, timeuntil in leap year edge case.
This commit is contained in:
parent
cd7efa2033
commit
3e5c5e6754
|
@ -40,10 +40,15 @@ def timesince(d, now=None, reversed=False):
|
||||||
if not now:
|
if not now:
|
||||||
now = datetime.datetime.now(utc if is_aware(d) else None)
|
now = datetime.datetime.now(utc if is_aware(d) else None)
|
||||||
|
|
||||||
delta = (d - now) if reversed else (now - d)
|
if reversed:
|
||||||
|
d, now = now, d
|
||||||
|
delta = now - d
|
||||||
|
|
||||||
# Deal with leapyears by subtracing the number of leapdays
|
# Deal with leapyears by subtracing the number of leapdays
|
||||||
delta -= datetime.timedelta(calendar.leapdays(d.year, now.year))
|
leapdays = calendar.leapdays(d.year, now.year)
|
||||||
|
if leapdays != 0 and calendar.isleap(d.year):
|
||||||
|
leapdays -= 1
|
||||||
|
delta -= datetime.timedelta(leapdays)
|
||||||
|
|
||||||
# ignore microseconds
|
# ignore microseconds
|
||||||
since = delta.days * 24 * 60 * 60 + delta.seconds
|
since = delta.days * 24 * 60 * 60 + delta.seconds
|
||||||
|
|
|
@ -14,3 +14,6 @@ Bugfixes
|
||||||
|
|
||||||
* Fixed occasional missing plural forms in ``JavaScriptCatalog``
|
* Fixed occasional missing plural forms in ``JavaScriptCatalog``
|
||||||
(:ticket:`27418`).
|
(:ticket:`27418`).
|
||||||
|
|
||||||
|
* Fixed a regression in the ``timesince`` and ``timeuntil`` filters that caused
|
||||||
|
incorrect results for dates in a leap year.
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
===========================
|
||||||
|
Django 1.9.13 release notes
|
||||||
|
===========================
|
||||||
|
|
||||||
|
*Under development*
|
||||||
|
|
||||||
|
Django 1.9.13 fixes a bug in 1.9.12.
|
||||||
|
|
||||||
|
Bugfixes
|
||||||
|
========
|
||||||
|
|
||||||
|
* Fixed a regression in the ``timesince`` and ``timeuntil`` filters that caused
|
||||||
|
incorrect results for dates in a leap year.
|
|
@ -45,6 +45,7 @@ versions of the documentation contain the release notes for any later releases.
|
||||||
.. toctree::
|
.. toctree::
|
||||||
:maxdepth: 1
|
:maxdepth: 1
|
||||||
|
|
||||||
|
1.9.13
|
||||||
1.9.12
|
1.9.12
|
||||||
1.9.11
|
1.9.11
|
||||||
1.9.10
|
1.9.10
|
||||||
|
|
|
@ -105,6 +105,11 @@ class TimesinceTests(unittest.TestCase):
|
||||||
self.assertEqual(timeuntil(today - self.oneday, today), '0\xa0minutes')
|
self.assertEqual(timeuntil(today - self.oneday, today), '0\xa0minutes')
|
||||||
self.assertEqual(timeuntil(today + self.oneweek, today), '1\xa0week')
|
self.assertEqual(timeuntil(today + self.oneweek, today), '1\xa0week')
|
||||||
|
|
||||||
|
def test_leap_year(self):
|
||||||
|
start_date = datetime.date(2016, 12, 25)
|
||||||
|
self.assertEqual(timeuntil(start_date + self.oneweek, start_date), '1\xa0week')
|
||||||
|
self.assertEqual(timesince(start_date, start_date + self.oneweek), '1\xa0week')
|
||||||
|
|
||||||
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):
|
||||||
|
@ -117,3 +122,4 @@ class TimesinceTests(unittest.TestCase):
|
||||||
def test_thousand_years_ago(self):
|
def test_thousand_years_ago(self):
|
||||||
t = datetime.datetime(1007, 8, 14, 13, 46, 0)
|
t = datetime.datetime(1007, 8, 14, 13, 46, 0)
|
||||||
self.assertEqual(timesince(t, self.t), '1000\xa0years')
|
self.assertEqual(timesince(t, self.t), '1000\xa0years')
|
||||||
|
self.assertEqual(timeuntil(self.t, t), '1000\xa0years')
|
||||||
|
|
Loading…
Reference in New Issue