2

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.

  • 1
    What you show is not the code, but the schema/description the code is working with. Intersystems products were originally based on Mumps with APIs for other programing languages. – sqlab Aug 21 '16 at 06:36
  • You might want to build external validation procedure for DateTime stamps. – Sid Aug 22 '16 at 14:36
  • I understand that this is the schema. The project I'm working on is to remediate all schemas and enable validation alerting on all namespaces. Using the schema I can easily say "The value in this field can only be integers and 5 characters long". I'm simply asking if I can get more detailed than this. –  Aug 22 '16 at 15:42

3 Answers3

1

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])?$
Jaime
  • 5,770
  • 4
  • 23
  • 50
  • not sure why but these are not validating: 1234567890+0300, 202010011330+0300, the TZ part seems to be totally spearated from the rest. https://regex101.com/r/f0WHJj/1 by adding extra parentheses to isolate the datetime from the TZ they validate correctly ^(((\d{2}){2,7}|\d{14}.\d{1,4})([+-]\d{4})?)$ – Pablo Pazos Oct 15 '20 at 20:09
  • BTW the dot should be escaped: \. – Pablo Pazos Oct 15 '20 at 22:36
0

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})?

Grahame Grieve
  • 3,538
  • 3
  • 15
  • 17
0

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;
}
Nick Hatt
  • 357
  • 1
  • 7