7100: proper handling of dates with NY in Date.set

svn: r23275
This commit is contained in:
Vassilii Khachaturov 2013-10-08 11:23:30 +00:00
parent ed1d579bfc
commit c87066136e
3 changed files with 29 additions and 20 deletions

View File

@ -162,6 +162,12 @@ class DateHandlerTest(unittest.TestCase):
d.set(Date.QUAL_NONE,Date.MOD_NONE, Date.CAL_GREGORIAN, d.set(Date.QUAL_NONE,Date.MOD_NONE, Date.CAL_GREGORIAN,
(4, 77, 1789, False), "Text comment") (4, 77, 1789, False), "Text comment")
def test_invalid_month_with_ny(self):
d = Date()
with self.assertRaises(DateError):
d.set(Date.QUAL_NONE,Date.MOD_NONE, Date.CAL_GREGORIAN,
(4, 77, 1789, False), "Text comment", newyear=2)
def test_invalid_span_day(self): def test_invalid_span_day(self):
d = Date() d = Date()
with self.assertRaises(DateError): with self.assertRaises(DateError):

View File

@ -1574,6 +1574,7 @@ class Date(object):
self.recalc_sort_value() self.recalc_sort_value()
ny = self.get_new_year() ny = self.get_new_year()
year_delta = 0
if ny: # new year offset? if ny: # new year offset?
if ny == Date.NEWYEAR_MAR1: if ny == Date.NEWYEAR_MAR1:
split = (3, 1) split = (3, 1)
@ -1585,26 +1586,19 @@ class Date(object):
split = ny split = ny
else: else:
split = (0, 0) split = (0, 0)
if (self.get_month(), self.get_day()) >= split: if (self.get_month(), self.get_day()) >= split and split != (0, 0):
d1 = Date(self.get_year(), 1, 1) year_delta = -1
d1 = Date(self.get_year() + year_delta, self.get_month(), self.get_day())
d1.set_calendar(self.calendar) d1.set_calendar(self.calendar)
d1_val = d1.sortval self.sortval = d1.sortval
d2 = Date(self.get_year(), split[0], split[1])
d2.set_calendar(self.calendar) if text:
d2_val = d2.sortval self.text = text
self.sortval -= (d2_val - d1_val)
else:
d1 = Date(self.get_year(), 12, 31)
d1.set_calendar(self.calendar)
d1_val = d1.sortval
d2 = Date(self.get_year(), split[0], split[1])
d2.set_calendar(self.calendar)
d2_val = d2.sortval
self.sortval += (d1_val - d2_val) + 1
if modifier != Date.MOD_TEXTONLY: if modifier != Date.MOD_TEXTONLY:
sanity = Date(self) sanity = Date(self)
sanity.convert_calendar(self.calendar, known_valid = False) sanity.convert_calendar(self.calendar, known_valid = False)
# We don't do the roundtrip conversion on self, becaue # We don't do the roundtrip conversion on self, becaue
# it would remove uncertainty on day/month expressed with zeros # it would remove uncertainty on day/month expressed with zeros
@ -1616,17 +1610,23 @@ class Date(object):
for d,m,y,sl in zip(*[iter(zl)]*4): for d,m,y,sl in zip(*[iter(zl)]*4):
# each of d,m,y,sl is a pair from dateval and value, to compare # each of d,m,y,sl is a pair from dateval and value, to compare
for adjusted,original in d,m,y: for adjusted,original in d,m:
if adjusted != original and not(original == 0 and adjusted == 1): if adjusted != original and not(original == 0 and adjusted == 1):
log.debug("Sanity check failed - self: {}, sanity: {}".format( log.debug("Sanity check failed - self: {}, sanity: {}".format(
self.to_struct(), sanity.to_struct())) self.to_struct(), sanity.to_struct()))
raise DateError("Invalid day/month/year {} passed in value {}". raise DateError("Invalid day/month {} passed in value {}".
format(original, value))
adjusted,original = y
adjusted -= year_delta
if adjusted != original and not(original == 0 and adjusted == 1):
log.debug("Sanity check failed - self: {}, sanity: {}".format(
self.to_struct(), sanity.to_struct()))
raise DateError("Invalid year {} passed in value {}".
format(original, value)) format(original, value))
# ignore slash difference # ignore slash difference
if text:
self.text = text
def recalc_sort_value(self): def recalc_sort_value(self):
""" """

View File

@ -344,6 +344,9 @@ class MatchDateTest(BaseDateTest):
("1706-12-31 (Julian)", "1707-01-01 (Swedish)", True), ("1706-12-31 (Julian)", "1707-01-01 (Swedish)", True),
("1712-02-28 (Julian)", "1712-02-29 (Swedish)", True), ("1712-02-28 (Julian)", "1712-02-29 (Swedish)", True),
("1712-02-29 (Julian)", "1712-02-30 (Swedish)", True), ("1712-02-29 (Julian)", "1712-02-30 (Swedish)", True),
# See bug# 7100
("1233-12-01", "1234-12-01 (Mar25)", True),
("1234-01-04", "1234-01-04 (Mar25)", True),
] ]
def do_test(self, d1, d2, expected1, expected2=None): def do_test(self, d1, d2, expected1, expected2=None):