Stream: dotnet
Topic: System.FormatException FhirDateTime --> ToDateTimeOffset()
Harald Sømnes Hanssen (Jun 07 2017 at 13:07):
Hi.
I'm having an issue with using FhirDateTime when time is represented by dots
The exception goes like this
"System.FormatException: 'The string '2016-05-05T12.00.00' is not a valid AllXsd value.'"
This value is in an FhirDateTime object.
var dateTime = new FhirDateTime(dateStr);
var dateTimeOffset = dateTime.ToDateTimeOffset() --> causes the exception
The problem here, is that my computer uses the format hh.mm.ss format instead of hh:mm:ss (which is very annoying). An easy fix would be to change the format on my computer, however, this is just a quickfix and does not solve the problem:
Making sure that the ToDateTimeOffset() gets a date in the expected format before it converts the date.
Is there a reasonable way of tackling it?
Michel Rutten (Jun 07 2017 at 21:22):
Hi @Harald Sømnes Hanssen, the challenge is that the FHIR datetime format does not map 1-1 to the C# DateTimeOffset datatype. For example, FHIR allows partial dates such as year-month, however you cannot express this in native C# datatypes. Therefore the API internally manages FHIR datetime values as strings, providing extension methods for mapping from/to C# datatypes - however that conversion is not always possible. Consequently, you have to do some additional work in your client logic. We're open to suggestions about improving the API, so please feel free to submit your ideas.
Harald Sømnes Hanssen (Jun 08 2017 at 07:20):
Hi Michael
Wouldn't it be better to map the date by properties and set the desired format when you created the FhirDateTime object?
For example,
public FhirDateTime(int year, int month, int day, int hr, int min, int sec = 0, string dateTimeXmlFormat, string dateTimeFormat)
Or at least have an outputmethod in the FhirDateTime?
public ToDateTime(string outputDateTimeFormat = null)
When you are transferring dates between systems, it is vital to be able to get the correct date without having to create temporary solutions.
In my case, the ToDateTime method would have probably solved the issue I'm having, where I'm transferring the date from FHIR service to a hl7 v3 connector.
Brian Postlethwaite (Jun 09 2017 at 04:17):
Not sure what you'd like us to do here.
We've provided helper methods to go to/from c# datetime and datetimeoffset classes to the FhirDateTime class. If you pass a string that isn't a FHIR string to the constructor, yes it breaks.
Brian Postlethwaite (Jun 09 2017 at 04:18):
There are extension methods in Hl7.Fhir.Support that go from the C# format to the fhirDate classes.
https://github.com/ewoutkramer/fhir-net-api/blob/master/src/Hl7.Fhir.Core/Support/DateExtensions.cs#L17
Can highly recommend using these going from internal models to FHIR models.
Brian Postlethwaite (Jun 09 2017 at 04:20):
While I'm talking about the handy extras, theres one for Guid too.
https://github.com/ewoutkramer/fhir-net-api/blob/master/src/Hl7.Fhir.Core/Support/GuidExtensions.cs#L17
string id = Guid.NewGuid().ToFhirId(); // much cleaner and consistent format
Last updated: Apr 12 2022 at 19:14 UTC