Fix negative Span when dates are not Gregorian

Fixes #12525
This commit is contained in:
Nick Hall 2022-01-29 23:03:15 +00:00
parent 7c0e138ad2
commit 810d2fca24
2 changed files with 35 additions and 5 deletions

View File

@ -77,14 +77,14 @@ class Span:
self.precision = 2 self.precision = 2
self.negative = False self.negative = False
if self.valid: if self.valid:
if self.date1.calendar != Date.CAL_GREGORIAN:
self.date1 = self.date1.to_calendar("gregorian")
if self.date2.calendar != Date.CAL_GREGORIAN:
self.date2 = self.date2.to_calendar("gregorian")
if self.date1.sortval < self.date2.sortval: if self.date1.sortval < self.date2.sortval:
self.date1 = date2 self.date1 = date2
self.date2 = date1 self.date2 = date1
self.negative = True self.negative = True
if self.date1.calendar != Date.CAL_GREGORIAN:
self.date1 = self.date1.to_calendar("gregorian")
if self.date2.calendar != Date.CAL_GREGORIAN:
self.date2 = self.date2.to_calendar("gregorian")
if self.date1.get_modifier() == Date.MOD_NONE: if self.date1.get_modifier() == Date.MOD_NONE:
if self.date2.get_modifier() == Date.MOD_NONE: if self.date2.get_modifier() == Date.MOD_NONE:
val = self.date1.sortval - self.date2.sortval val = self.date1.sortval - self.date2.sortval

View File

@ -38,7 +38,7 @@ from ...datehandler import get_date_formats, set_format
from ...datehandler import parser as _dp from ...datehandler import parser as _dp
from ...datehandler import displayer as _dd from ...datehandler import displayer as _dd
from ...datehandler._datedisplay import DateDisplayEn from ...datehandler._datedisplay import DateDisplayEn
from ...lib.date import Date, DateError, Today, calendar_has_fixed_newyear from ...lib.date import Date, DateError, Today, calendar_has_fixed_newyear, Span
date_tests = {} date_tests = {}
@ -432,6 +432,36 @@ class ArithmeticDateTest(BaseDateTest):
self.assertEqual(val1, val2, self.assertEqual(val1, val2,
"'%s' should be '%s' but was '%s'" % (exp1, val2, val1)) "'%s' should be '%s' but was '%s'" % (exp1, val2, val1))
#-------------------------------------------------------------------------
#
# SpanTest
#
#-------------------------------------------------------------------------
class SpanTest(BaseDateTest):
"""
Test spans.
"""
tests = [((2000, 1, 31), (2000, 1, 1), 30),
((1799, 11, 19), (8, 2, 18, Date.CAL_FRENCH), 10),
((8, 2, 18, Date.CAL_FRENCH), (1799, 11, 4), 5),
((8, 2, 18, Date.CAL_FRENCH), (3, 2, 9, Date.CAL_FRENCH), 1836)]
def test_evaluate(self):
for value1, value2, duration in self.tests:
date1 = self._get_date(value1)
date2 = self._get_date(value2)
span1 = Span(date1, date2)
self.assertEqual(int(span1), duration)
span2 = Span(date2, date1)
self.assertEqual(int(span2), -duration)
def _get_date(self, value):
date = Date()
if len(value) == 4:
date.set_calendar(value[3])
date.set_yr_mon_day(value[0], value[1], value[2])
return date
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# SwedishDateTest # SwedishDateTest