From c87066136ef7da00b0760b4f420b8c4c6655a8cd Mon Sep 17 00:00:00 2001 From: Vassilii Khachaturov Date: Tue, 8 Oct 2013 11:23:30 +0000 Subject: [PATCH] 7100: proper handling of dates with NY in Date.set svn: r23275 --- .../gen/datehandler/test/datehandler_test.py | 6 +++ gramps/gen/lib/date.py | 40 +++++++++---------- gramps/gen/lib/test/date_test.py | 3 ++ 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/gramps/gen/datehandler/test/datehandler_test.py b/gramps/gen/datehandler/test/datehandler_test.py index 6b3664e05..9e52d6894 100644 --- a/gramps/gen/datehandler/test/datehandler_test.py +++ b/gramps/gen/datehandler/test/datehandler_test.py @@ -162,6 +162,12 @@ class DateHandlerTest(unittest.TestCase): d.set(Date.QUAL_NONE,Date.MOD_NONE, Date.CAL_GREGORIAN, (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): d = Date() with self.assertRaises(DateError): diff --git a/gramps/gen/lib/date.py b/gramps/gen/lib/date.py index 277d09a46..b586c021c 100644 --- a/gramps/gen/lib/date.py +++ b/gramps/gen/lib/date.py @@ -1574,6 +1574,7 @@ class Date(object): self.recalc_sort_value() ny = self.get_new_year() + year_delta = 0 if ny: # new year offset? if ny == Date.NEWYEAR_MAR1: split = (3, 1) @@ -1585,26 +1586,19 @@ class Date(object): split = ny else: split = (0, 0) - if (self.get_month(), self.get_day()) >= split: - d1 = Date(self.get_year(), 1, 1) + if (self.get_month(), self.get_day()) >= split and split != (0, 0): + year_delta = -1 + d1 = Date(self.get_year() + year_delta, self.get_month(), self.get_day()) 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 -= (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 - + self.sortval = d1.sortval + + if text: + self.text = text + if modifier != Date.MOD_TEXTONLY: sanity = Date(self) sanity.convert_calendar(self.calendar, known_valid = False) + # We don't do the roundtrip conversion on self, becaue # 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): # 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): log.debug("Sanity check failed - self: {}, sanity: {}".format( 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)) + # ignore slash difference - if text: - self.text = text def recalc_sort_value(self): """ diff --git a/gramps/gen/lib/test/date_test.py b/gramps/gen/lib/test/date_test.py index 3570ef3ad..7cb06c396 100644 --- a/gramps/gen/lib/test/date_test.py +++ b/gramps/gen/lib/test/date_test.py @@ -344,6 +344,9 @@ class MatchDateTest(BaseDateTest): ("1706-12-31 (Julian)", "1707-01-01 (Swedish)", True), ("1712-02-28 (Julian)", "1712-02-29 (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):