Skip to content

Parse every HL7 DTM precision in TSComponentOne.GetAsDate (fixes #411) - #915

Open
iacsha wants to merge 2 commits into
nHapiNET:masterfrom
iacsha:fix/411-dtm-fractional-seconds
Open

iacsha wants to merge 2 commits into
nHapiNET:masterfrom
iacsha:fix/411-dtm-fractional-seconds

Conversation

@iacsha

@iacsha iacsha commented Nov 7, 2026

Copy link
Copy Markdown

Fixes #411.

TSComponentOne.GetAsDate() (the base of DTM in v2.5+ and the TS first component in earlier versions) only tried five ParseExact formats. Several valid DTM values, YYYY[MM[DD[HH[MM[SS[.S[S[S[S]]]]]]]]][+/-ZZZZ], therefore threw HL7Exception("Could not get field as dateTime"), including the Mindray V9 value from the issue:

Value Before After
20230211140114.0000+0800 (fraction + offset) throws parses
202302111401+0800, 2023021114+0800, 20230211+0800 throws parses
2023021114 (hour precision) throws parses
202302, 2023 throws parses

Change: the missing precisions are added as extra formats tried after the existing five, so every value that parsed before returns exactly what it did. An offset converts to local time, the same as the existing yyyyMMddHHmmsszzz path already does. Invalid values (5 fraction digits, odd lengths, month 13) still throw.

Tests: new DateTypeTest26 with the issue's full ORU^R01 (MSH-7 and OBX-14), offset/precision cases and invalid cases. All 10 valid cases fail on master and pass with the change. The time-dependent assertions compare against DateTimeOffset.LocalDateTime, and pass under TZ=America/New_York, Asia/Kolkata and UTC. Full NHapi.NUnit (978 passed, 2 skipped) and NHapi.Base.NUnit (321 passed, 3 skipped) are green on net8.0, and no new StyleCop warnings.

@franjom, I saw you sketched an approach here last year. Happy to fold in anything from yours, or to step aside if you had a PR in progress.

…iNET#411)

GetAsDate only accepted five ParseExact formats, so valid DTM values such as 20230211140114.0000+0800 (fractional seconds with a time zone offset, sent by Mindray V9 monitors) threw HL7Exception, as did hour, month and year precision and an offset on minute precision. Add the missing YYYY[MM[DD[HH[MM[SS[.S[S[S[S]]]]]]]]][+/-ZZZZ] forms after the existing formats, so values that already parsed are unchanged; an offset converts to local time as the existing offset format already does.
@codacy-production

codacy-production Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 4 complexity

Metric Results
Complexity 4

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@sonarqubecloud

sonarqubecloud Bot commented Nov 8, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mindray Datetime Format

1 participant