Validate timestamp in HL7 Schema?

3k views Asked by At

I see the TS and DT data types in the default 2.3 schema:

<SegmentStructure name='SMPL' description='Patient Identification'>
    <SegmentSubStructure piece='1' description='A sample datetime field' datatype='DT' max_length='12' required='O' ifrepeating='0'/>
</SegmentStructure>

<DataType name='TS' description='time stamp'>
    <DataSubType piece='1' description='time of an event' datatype='ST'/>
    <DataSubType piece='2' description='degree of precision' datatype='ST'/>
</DataType>

<DataType name='DT' description='Date (2.8.13)'>
    <DataSubType piece='1' description='Date (2.8.13)'/>
</DataType>

but it doesn't appear that it would actually validate the format. Is this possible? We had an instance recently where a customer was sending a timestamp with a missing digit (20160503120 for example). I would like to validate this with the schema if I can.

edit: Adding a some clarification hopefully

We use and when an ADT comes in it passes through a validator with the extension .hl7. By default this is 2.3.hl7 or similar (depending on the version). It looks just like the code above. I want to know if I can put regex in there somehow. Either in the segment structure definition (SMPL) or in the data type definition.

3

There are 3 answers

2
Grahame Grieve On

You could add a regex to the schema. This is the full TS regex from the v3 schemas:

[0-9]{1,8}|([0-9]{9,14}|[0-9]{14,14}.[0-9]+)([+-][0-9]{1,4})?

2
Jaime On

According to the specification for HL7 v2 (see http://www.hl7.eu/refactored/dtDTM.html ), the format of DT is: YYYY[MM[DD[HH[MM[SS[.S[S[S[S]]]]]]]]][+/-ZZZZ].

Thus, a proper regex expression would be:

^((\d{2}){2,7}|\d{14}.\d{1,4}([+-]\d{4})?)$

However, if you are talking about the TS datatype, it may include a caret and a precision indicator.

In that case, the regex expression may be:

^((\d{2}){2,7}|\d{14}.\d{1,4}([+-]\d{4})?)(\^[YLDHMS])?$

This is just a first screening, as the real date/time values are not being validated.

UPDATE:

I recently used the following regex expression for DT, which totally validates date, time and zone values, and return each component in groups:

^((?:19|20)[0-9]{2})(?:(1[0-2]|0[1-9])(?:(3[0-1]|[1-2][0-9]|0[1-9])(?:([0-1][0-9]|2[0-3])(?:([0-5][0-9])(?:([0-5][0-9](?:\.[0-9]{1,4})?)?)?)?)?)?)?([+-](?:[0-1][0-9]|2[0-3])[0-5][0-9])?$
3
Nick Hatt On

The v2 docs have this beauty:

YYYY[MM[DD[HH[MM[SS[.S[S[S[S]]]]]]]]][+/-ZZZZ]

The brackets indicate optional parts. Maybe just look at the breakpoints? Something like this? Assuming you split out the timezone first, then pass it off to whatever you're actually using to parse the dates.

switch (datePart.length) {
case 4:
  return parseDate(datePart, "YYYY");
case 6:
  return parseDate(datePart, "YYYYMM");
case 8:
  return parseDate(datePart, "YYYYMMDD");
case 10:
  return parseDate(datePart, "YYYYMMDDHH");
case 12:
  return parseDate(datePart, "YYYYMMDDHHmm");
case 14:
  return parseDate(datePart, "YYYYMMDDHHmmss");
case 16:
  return parseDate(datePart, "YYYYMMDDHHmmss.S");
case 17:
  return parseDate(datePart, "YYYYMMDDHHmmss.SS");
case 18:
  return parseDate(datePart, "YYYYMMDDHHmmss.SSS");
default:
  return undefined;
}