Skip to content

Commit

Permalink
added transform for converting datetimes to mountain time (#16542)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucero-v authored Nov 14, 2024
1 parent ec79b35 commit 8d3bdf8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gov.cdc.prime.router.fhirengine.translation.hl7.utils
import ca.uhn.fhir.model.api.TemporalPrecisionEnum
import fhirengine.translation.hl7.utils.FhirPathFunctions
import fhirengine.translation.hl7.utils.helpers.convertDateToAge
import gov.cdc.prime.router.common.DateUtilities
import gov.cdc.prime.router.fhirengine.translation.hl7.SchemaException
import org.hl7.fhir.r4.fhirpath.FHIRPathUtilityClasses.FunctionDetails
import org.hl7.fhir.r4.model.Base
Expand Down Expand Up @@ -445,11 +446,6 @@ object CustomFHIRFunctions : FhirPathFunctions {
throw SchemaException("Must call changeTimezone on a single element")
}

val inputDate = focus[0] as? BaseDateTimeType ?: throw SchemaException(
"Must call changeTimezone on a dateTime, instant, or date; " +
"was attempted on a ${focus[0].fhirType()}"
)

if (parameters == null || parameters[0].size != 1) {
throw SchemaException("Must pass a timezone as the parameter")
}
Expand All @@ -465,14 +461,41 @@ object CustomFHIRFunctions : FhirPathFunctions {
)
}

return when (inputDate.precision) {
TemporalPrecisionEnum.YEAR, TemporalPrecisionEnum.MONTH, TemporalPrecisionEnum.DAY, null -> mutableListOf(
inputDate
return if (focus[0] is StringType) {
if (focus[0].toString().length <= 8) { // we don't want to convert Date-only strings
return mutableListOf(StringType(focus[0].toString()))
}

// TODO: find a way to pass in these values from receiver settings

val dateTimeFormat = null
val convertPositiveDateTimeOffsetToNegative = null
val useHighPrecisionHeaderDateTimeFormat = null

val formattedDate = DateUtilities.formatDateForReceiver(
DateUtilities.parseDate((focus[0].toString())),
ZoneId.of(inputTimeZone),
dateTimeFormat ?: DateUtilities.DateTimeFormat.OFFSET,
convertPositiveDateTimeOffsetToNegative ?: false,
useHighPrecisionHeaderDateTimeFormat ?: false
)

TemporalPrecisionEnum.MINUTE, TemporalPrecisionEnum.SECOND, TemporalPrecisionEnum.MILLI -> mutableListOf(
DateTimeType(inputDate.value, inputDate.precision, timezonePassed)
mutableListOf(
StringType(formattedDate)
)
} else {
val inputDate = focus[0] as? BaseDateTimeType ?: throw SchemaException(
"Must call changeTimezone on a dateTime, instant, or date; " +
"was attempted on a ${focus[0].fhirType()}"
)

when (inputDate.precision) {
TemporalPrecisionEnum.YEAR, TemporalPrecisionEnum.MONTH, TemporalPrecisionEnum.DAY, null ->
mutableListOf(inputDate)

TemporalPrecisionEnum.MINUTE, TemporalPrecisionEnum.SECOND, TemporalPrecisionEnum.MILLI ->
mutableListOf(DateTimeType(inputDate.value, inputDate.precision, timezonePassed))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
elements:
- name: hl7-datetime-to-local
resource: "Bundle.descendants().where(url='https://reportstream.cdc.gov/fhir/StructureDefinition/hl7v2-date-time')"
bundleProperty: "%resource.value[x]"
value: ["%resource.value.changeTimezone('America/Denver')"]


- name: message-date-time-to-local
resource: 'Bundle.entry.resource.ofType(MessageHeader).extension("https://reportstream.cdc.gov/fhir/StructureDefinition/msh-message-header").extension("MSH.7")'
condition: '%resource.value.exists()'
bundleProperty: '%resource.value[x]'
value: ["%resource.value.changeTimezone('America/Denver')"]

0 comments on commit 8d3bdf8

Please sign in to comment.