Skip to content

Commit

Permalink
Correctly assign sortval = 0 when a date is EMPTY
Browse files Browse the repository at this point in the history
The is_valid() method for a Date simply checks the sortval for a zero value.
However, if an empty date is passed to _calc_sort_value() it first adjusts
to 1 ALL y,m,d values that are zero.  Thus, an empty date would return a
sortval for the year 1-1-1 as a valid date.

The problematic part of the test calculates an age for somebody with a birth
date of "" (an empty string) and a death date given by a year, such as 1760.
The previous test code required it to report "greater than 110 years",
which should not be a suitable response. The previous code also reported an
age of negative 1999 years for a birth year of 2000 and a death date of the
empty string. The unit test code now tests against a reply of "unknown".

Fixes #13415, #13423.
  • Loading branch information
CameronD73 authored and Nick-Hall committed Dec 19, 2024
1 parent 9aacb07 commit e898c78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
27 changes: 12 additions & 15 deletions gramps/gen/lib/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -1765,15 +1765,7 @@ def set(
self.calendar = calendar
self.dateval = value
self.set_new_year(newyear)
year, month, day = self._zero_adjust_ymd(
value[Date._POS_YR], value[Date._POS_MON], value[Date._POS_DAY]
)

if year == month == day == 0:
self.sortval = 0
else:
func = Date._calendar_convert[calendar]
self.sortval = func(year, month, day)
self._calc_sort_value()

if self.get_slash() and self.get_calendar() != Date.CAL_JULIAN:
self.set_calendar(Date.CAL_JULIAN)
Expand Down Expand Up @@ -1848,14 +1840,19 @@ def _calc_sort_value(self):
"""
Calculate the numerical sort value associated with the date.
"""
year, month, day = self._zero_adjust_ymd(
self.dateval[Date._POS_YR],
self.dateval[Date._POS_MON],
self.dateval[Date._POS_DAY],
)
if year == month == 0 and day == 0:
if (
self.dateval[Date._POS_YR]
== self.dateval[Date._POS_MON]
== self.dateval[Date._POS_DAY]
== 0
):
self.sortval = 0
else:
year, month, day = self._zero_adjust_ymd(
self.dateval[Date._POS_YR],
self.dateval[Date._POS_MON],
self.dateval[Date._POS_DAY],
)
func = Date._calendar_convert[self.calendar]
self.sortval = func(year, month, day)

Expand Down
10 changes: 5 additions & 5 deletions gramps/gen/lib/test/date_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,11 +1055,11 @@ class AgeTest(BaseDateTest):
"2000",
"40 years",
),
("", "1760", "greater than 110 years"),
("", "1960", "greater than 110 years"),
("", "2020", "greater than 110 years"),
("", "3020", "greater than 110 years"),
("2000", "", "(1999 years)"),
("", "1760", "unknown"),
("", "1960", "unknown"),
("", "2020", "unknown"),
("", "3020", "unknown"),
("2000", "", "unknown"),
]

def convert_to_date(self, d):
Expand Down

0 comments on commit e898c78

Please sign in to comment.