Skip to content

Commit

Permalink
Use system datetime alias to avoid name clashing
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rl committed Feb 10, 2024
1 parent feef6ed commit 5102f22
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 4 additions & 4 deletions fhirpathpy/engine/invocations/datetime.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from datetime import datetime
from datetime import datetime as systemtime

from fhirpathpy.engine.invocations.constants import constants
from fhirpathpy.engine.nodes import FP_DateTime, FP_Time


def now(ctx, data):
if not constants.now:
now = datetime.now()
now = systemtime.now()
if not now.tzinfo:
now = now.astimezone()
isoStr = now.isoformat() # YYYY-MM-DDThh:mm:ss.ffffff+zz:zz
Expand All @@ -16,15 +16,15 @@ def now(ctx, data):

def today(ctx, data):
if not constants.today:
now = datetime.now()
now = systemtime.now()
isoStr = now.date().isoformat() # YYYY-MM-DD
constants.today = str(FP_DateTime(isoStr))
return constants.today


def timeOfDay(ctx, data):
if not constants.timeOfDay:
now = datetime.now()
now = systemtime.now()
isoStr = now.time().isoformat() # hh:mm:ss.ffffff
constants.timeOfDay = str(FP_Time(isoStr))
return constants.timeOfDay
8 changes: 6 additions & 2 deletions tests/test_evaluators.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,19 @@ def misc_functions_test(resource, path, expected):
],
)
def datetime_json_serialization_test(expression: str, expected_json: str):
with mock.patch("fhirpathpy.engine.invocations.datetime.datetime") as datetime_mock:
with mock.patch(
"fhirpathpy.engine.invocations.datetime.systemtime"
) as datetime_mock:
datetime_mock.now.return_value = datetime(
2020, 8, 20, 17, 52, 15, 123000, tzinfo=timezone.utc
)
assert json.dumps(evaluate({}, expression)) == expected_json


def now_function_test():
with mock.patch("fhirpathpy.engine.invocations.datetime.datetime") as datetime_mock:
with mock.patch(
"fhirpathpy.engine.invocations.datetime.systemtime"
) as datetime_mock:
datetime_mock.now.side_effect = [
datetime(2020, 8, 20, 17, 52, 15, 123000, tzinfo=timezone.utc),
datetime(2020, 8, 20, 17, 52, 16, 123000, tzinfo=timezone.utc),
Expand Down

0 comments on commit 5102f22

Please sign in to comment.