Skip to content

Commit

Permalink
Update math - plus and minus
Browse files Browse the repository at this point in the history
  • Loading branch information
atuonufure committed Jan 10, 2024
1 parent 7605e09 commit 641f501
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
28 changes: 21 additions & 7 deletions fhirpathpy/engine/invocations/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,22 @@ def minus(ctx, xs_, ys_):
xs = remove_duplicate_extension(xs_)
ys = remove_duplicate_extension(ys_)

if len(xs) == 1 and len(ys) == 1:
x = util.get_data(util.val_data_converted(xs[0]))
y = util.get_data(util.val_data_converted(ys[0]))
if len(xs) != 1 or len(ys) != 1:
raise Exception("Cannot " + str(xs) + " + " + str(ys))

x = util.get_data(util.val_data_converted(xs[0]))
y = util.get_data(util.val_data_converted(ys[0]))

if util.is_number(x) and util.is_number(y):
return x - y
if util.is_number(x) and util.is_number(y):
return x - y

if isinstance(x, nodes.FP_TimeBase) and isinstance(y, nodes.FP_Quantity):
return x.plus(nodes.FP_Quantity(-y.value, y.unit))

if isinstance(x, nodes.FP_TimeBase) and isinstance(y, nodes.FP_Quantity):
return x.plus(nodes.FP_Quantity(-y.value, y.unit))
if isinstance(x, str) and isinstance(y, nodes.FP_Quantity):
x_ = nodes.FP_TimeBase.get_match_data(x)
if x_ is not None:
return x_.plus(nodes.FP_Quantity(-y.value, y.unit))

raise Exception("Cannot " + str(xs) + " - " + str(ys))

Expand Down Expand Up @@ -110,6 +117,13 @@ def plus(ctx, xs_, ys_):
if isinstance(x, nodes.FP_TimeBase) and isinstance(y, nodes.FP_Quantity):
return x.plus(y)

if isinstance(x, str) and isinstance(y, nodes.FP_Quantity):
x_ = nodes.FP_TimeBase.get_match_data(x)
if x_ is not None:
return x_.plus(y)

raise Exception("Cannot " + str(xs) + " + " + str(ys))


def abs(ctx, x):
if is_empty(x):
Expand Down
10 changes: 10 additions & 0 deletions fhirpathpy/engine/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,16 @@ def check_string(cls, str_val):
val = cls(str_val)
return val

@staticmethod
def get_match_data(str_val):
match = re.match(dateTimeRE, str_val)
if match:
return FP_DateTime(str_val)
match = re.match(timeRE, str_val)
if match:
return FP_Time(str_val)
return None


class FP_Time(FP_TimeBase):
matchGroupsIndices = [
Expand Down

0 comments on commit 641f501

Please sign in to comment.