7197: back-port the fix 68c0615b from gramps40

The failing test now passes.

Back-porting work done:
1) use numbers in format strings for str.format, for py2.6 compat
2) downgrade to_struct to dateval as there's no to_struct in gramps34

Original commit:
	commit 68c0615bc44c5f0278e78fc2dfc7665ec2ea9775
	Author: Vassilii Khachaturov <vassilii@tarunz.org>
	Date:   Fri Nov 15 15:43:27 2013 +0200

		7197: merge the fix from master
This commit is contained in:
Vassilii Khachaturov 2013-11-15 18:30:53 +02:00
parent 5e78d54c9c
commit 62cd754ef4

View File

@ -3,6 +3,7 @@
# #
# Copyright (C) 2000-2007 Donald N. Allingham # Copyright (C) 2000-2007 Donald N. Allingham
# Copyright (C) 2009 Douglas S. Blank # Copyright (C) 2009 Douglas S. Blank
# Copyright (C) 2013 Vassilii Khachaturov
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -1497,6 +1498,31 @@ class Date(object):
day = max(d, 1) day = max(d, 1)
return (year, month, day) return (year, month, day)
def _adjust_newyear(self):
"""
Returns year adjustment performed (0 or -1).
"""
ny = self.get_new_year()
year_delta = 0
if ny: # new year offset?
if ny == Date.NEWYEAR_MAR1:
split = (3, 1)
elif ny == Date.NEWYEAR_MAR25:
split = (3, 25)
elif ny == Date.NEWYEAR_SEP1:
split = (9, 1)
elif isinstance(ny, (list, tuple)):
split = ny
else:
split = (0, 0)
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.recalc_sort_value()
self.sortval = d1.sortval
return year_delta
def set(self, quality=None, modifier=None, calendar=None, def set(self, quality=None, modifier=None, calendar=None,
value=None, value=None,
text=None, newyear=0): text=None, newyear=0):
@ -1577,24 +1603,7 @@ class Date(object):
self.set_calendar(Date.CAL_JULIAN) self.set_calendar(Date.CAL_JULIAN)
self.recalc_sort_value() self.recalc_sort_value()
ny = self.get_new_year() year_delta = self._adjust_newyear()
year_delta = 0
if ny: # new year offset?
if ny == Date.NEWYEAR_MAR1:
split = (3, 1)
elif ny == Date.NEWYEAR_MAR25:
split = (3, 25)
elif ny == Date.NEWYEAR_SEP1:
split = (9, 1)
elif isinstance(ny, (list, tuple)):
split = ny
else:
split = (0, 0)
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)
self.sortval = d1.sortval
if text: if text:
self.text = text self.text = text
@ -1602,35 +1611,46 @@ class Date(object):
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)
# convert_calendar resets slash and new year, restore these as needed
if sanity.get_slash() != self.get_slash():
sanity.set_slash(self.get_slash())
if self.is_compound() and sanity.get_slash2() != self.get_slash2():
sanity.set_slash2(self.get_slash2())
if sanity.get_new_year() != self.get_new_year():
sanity.set_new_year(self.get_new_year())
sanity._adjust_newyear()
# 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
# Did the roundtrip change the date value?! # Did the roundtrip change the date value?!
if sanity.dateval != value: if sanity.dateval != value:
# Maybe it is OK because of undetermined value adjustment? try:
zl = zip(sanity.dateval, value) # Maybe it is OK because of undetermined value adjustment?
# Loop over all values present, whether compound or not zl = zip(sanity.dateval, value)
for d,m,y,sl in zip(*[iter(zl)]*4): # Loop over all values present, whether compound or not
# each of d,m,y,sl is a pair from dateval and value, to compare 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
adjusted,original = sl
if adjusted != original:
raise DateError("Invalid date value {0}".
format(value))
for adjusted,original in d,m: for adjusted,original in d,m:
if adjusted != original and not(original == 0 and adjusted == 1):
raise DateError("Invalid day/month {0} passed in value {1}".
format(original, value))
adjusted,original = y
adjusted -= year_delta
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: {0}, sanity: {1}".format( raise DateError("Invalid year {0} passed in value {1}".
self.dateval, sanity.dateval))
raise DateError("Invalid day/month {} passed in value {}".
format(original, value)) format(original, value))
adjusted,original = y except DateError:
adjusted -= year_delta log.debug("Sanity check failed - self: {0}, sanity: {1}".format(
if adjusted != original and not(original == 0 and adjusted == 1): self.dateval, sanity.dateval))
log.debug("Sanity check failed - self: {0}, sanity: {1}".format( raise
self.dateval, sanity.dateval))
raise DateError("Invalid year {} passed in value {}".
format(original, value))
# ignore slash difference
def recalc_sort_value(self): def recalc_sort_value(self):
""" """