Skip to content

Commit

Permalink
timezone fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jalbinson committed Nov 15, 2024
1 parent 77f3aed commit a91a0f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import ca.uhn.hl7v2.model.v251.message.ACK
import gov.cdc.prime.router.common.Environment
import gov.cdc.prime.router.fhirengine.utils.HL7Reader
import java.time.Clock
import java.util.Calendar
import java.util.Date
import java.util.TimeZone
import java.util.UUID

/**
Expand All @@ -25,7 +27,7 @@ class HL7ACKUtils(
ackMsh.msh4_SendingFacility.parse("CDC")
ackMsh.msh5_ReceivingApplication.parse(HL7Reader.getSendingApplication(incomingACKMessage))
ackMsh.msh6_ReceivingFacility.parse(HL7Reader.getSendingFacility(incomingACKMessage))
ackMsh.msh7_DateTimeOfMessage.time.setValueToSecond(Date.from(clock.instant()))
ackMsh.msh7_DateTimeOfMessage.time.setValue(getTimestamp())
ackMsh.msh9_MessageType.parse("ACK")
ackMsh.msh10_MessageControlID.parse(UUID.randomUUID().toString())
ackMsh.msh11_ProcessingID.parse(if (Environment.isProd()) "P" else "T")
Expand All @@ -39,4 +41,21 @@ class HL7ACKUtils(

return outgoingAck.toString()
}

/**
* HL7 library requires old Java date libraries, so we do the conversion here.
*
* We must directly specify the UTC timezone or else the HL7 library will use
* your machines local timezone.
*/
private fun getTimestamp(): Calendar {
val instant = clock.instant()
val date = Date.from(instant)

val calendar = Calendar.getInstance()
calendar.time = date
calendar.timeZone = TimeZone.getTimeZone("UTC")

return calendar
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import io.mockk.mockkStatic
import io.mockk.unmockkAll
import org.junit.jupiter.api.AfterEach
import java.time.Clock
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.util.UUID
import kotlin.test.Test

Expand All @@ -21,10 +22,9 @@ class HL7ACKUtilsTest {
val hl7Reader = HL7Reader(ActionLogger())
val hl7DiffHelper = HL7DiffHelper()

private val utc = ZoneId.of("UTC")
private val clock = Clock.fixed(
LocalDate.of(2024, 9, 21).atStartOfDay(utc).toInstant(),
utc
LocalDateTime.of(2024, 9, 21, 0, 0).toInstant(ZoneOffset.UTC),
ZoneId.of("UTC")
)
val utils = HL7ACKUtils(clock)
}
Expand All @@ -51,7 +51,7 @@ class HL7ACKUtilsTest {

val expected = f.hl7Reader.getMessages(
"""
MSH|^~\&|ReportStream|CDC|Epic|Hospital|20240920200000||ACK|$id|T|2.5.1|||NE|NE
MSH|^~\&|ReportStream|CDC|Epic|Hospital|20240921000000+0000||ACK|$id|T|2.5.1|||NE|NE
MSA|CA|4AFA57FE-D41D-4631-9500-286AAAF797E4
"""
).first()
Expand Down

0 comments on commit a91a0f7

Please sign in to comment.