nazrulworld / fhir.resources

FHIR Resources https://www.hl7.org/fhir/resourcelist.html
https://pypi.org/project/fhir.resources/
Other
372 stars 104 forks source link

DateTime is not FHIR spec compliant #72

Closed rectalogic closed 3 years ago

rectalogic commented 3 years ago

Description

The fhir.resources DateTime object is not spec compliant. The spec (and the doc comments) both require a +zz:zz or Z timezone suffix, it is using datetime.isoformat() which does not do this. Python isoformat() violates ISO8601 https://stackoverflow.com/a/23705687/1480205

See DateTime.to_string https://github.com/nazrulworld/fhir.resources/blob/main/fhir/resources/fhirtypes.py#L511

We are trying to interoperate with a partner and they are unable to consume the FHIR JSON we generate since the DateTimes are all wrong. We think a workaround will be to correctly pre-format the datetime and use the str instead of the actual datetime object, it looks like to_string will leave it alone in that case.

What I Did

All DateTimes in FHIR objects are of the form 2021-05-07T21:41:57.120530 This is not compliant with the doc comment or the spec https://www.hl7.org/fhir/datatypes.html#dateTime

>>> from fhir.resources.annotation import Annotation
>>> import datetime
>>> a = Annotation(text="abc", time=datetime.datetime.utcnow())
>>> a.dict()
{'text': 'abc', 'time': datetime.datetime(2021, 5, 7, 22, 33, 55, 557605)}
>>> a.json()
'{"text": "abc", "time": "2021-05-07T22:33:55.557605"}'
rectalogic commented 3 years ago

I think the fix is to always specify a timezone on our datetime - with no timezone isoformat() is not ISO8601 compliant