Skip to content

Commit

Permalink
Update compare years months in equival and equal
Browse files Browse the repository at this point in the history
  • Loading branch information
atuonufure committed Dec 4, 2023
1 parent 8d72545 commit 1158364
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions fhirpathpy/engine/invocations/equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def equivalence(ctx, x, y):
if isinstance(a, Decimal) or isinstance(b, Decimal):
return is_equivalent(a, b)

if isinstance(a, nodes.FP_Quantity) or isinstance(b, nodes.FP_Quantity):
return a.deep_equal(b)

return x == y


Expand Down
27 changes: 20 additions & 7 deletions fhirpathpy/engine/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,7 @@ def __hash__(self):
def __eq__(self, other):
if isinstance(other, FP_Quantity):
if self.unit in self._years_and_months and other.unit in self._years_and_months:
self_value_in_months = self.value
other_value_in_months = other.value
if self.unit in ["'a'", "year", "years"]:
self_value_in_months *= 12
if other.unit in ["'a'", "year", "years"]:
other_value_in_months *= 12
return self_value_in_months == other_value_in_months
return self._compare_years_and_months(other)
elif self.unit in self._weeks_days_and_time and other.unit in self._weeks_days_and_time:
self_value_in_seconds = self.value * self.datetime_multipliers[self.unit]
other_value_in_seconds = other.value * self.datetime_multipliers[other.unit]
Expand All @@ -172,6 +166,15 @@ def __eq__(self, other):
else:
return super().__eq__(other)

def deep_equal(self, other):
if isinstance(other, FP_Quantity):
if self.unit in self._years_and_months and other.unit in self._years_and_months:
return self._compare_years_and_months(other, year_units=["'a'", "year", "years"])
else:
return self.__eq__(other)
else:
return super().__eq__(other)

def conv_unit_to(fromUnit, value, toUnit):
## 1 Year <-> 12 Months
from_year_month_magnitude = FP_Quantity._year_month_conversion_factor.get(fromUnit)
Expand Down Expand Up @@ -202,6 +205,16 @@ def conv_unit_to(fromUnit, value, toUnit):

return None

def _compare_years_and_months(self, other, year_units=["year", "years"]):
self_value_in_months = self.value
other_value_in_months = other.value

if self.unit in year_units:
self_value_in_months *= 12
if other.unit in year_units:
other_value_in_months *= 12
return self_value_in_months == other_value_in_months


class FP_TimeBase(FP_Type):
datetime_multipliers = [
Expand Down

0 comments on commit 1158364

Please sign in to comment.