Question
· Jun 2, 2020

How to get offset for a given timestamp, allowing for DST

Hello All

I'm not sure what the best practice is for this. I have a DTL for returning ORUs back to a hospital in a different time zone. They have requested that various timestamps have the time zone offset added to the HL7 format (%q%k, I think). First, I just wanted to be sure the system was aware it was DST. I was able to determine if DST is in effect like this:

<assign property='tzOffset' action='set' value='"-0500"' />
<code><![CDATA[ 
         SET dst=$SYSTEM.Util.IsDST()
         IF dst=1 {SET tzOffset = "-0400"}
         ]]>
    </code>
<assign property='target.{MSH:7}' action='set' value=' source.{MSH:7}_tzOffset' />

This presents the timestamp in MSH:7 as 20200602123015-0400, so I know the system is at least aware that it is DST. Now the part I don't understand is how to convert any given timestamp (%q) to include the correct offset for that time. I tried the following, but only got "-0000" for the offset.

 <assign property='target.{MSH:7}' action='set' value='..ConvertDateTime(source.{MSH:7},"%q","%q%z")' />

Any help would be greatly appreciated!

Discussion (5)1
Log in or sign up to continue

Hello Jonathan,

This is the method I wrote to do this:

/// This method will append an offset from UTC to a datetime passed in as HL7 Format
/// e.g. "20190711083705" returns "20190711083705+0100"
/// - NB BST was introduced in 1970 so dates passed in before 1970 will return +0000 offsets for all dates
ClassMethod GetLocalTimeWithUTCOffset(pHL7DateTime As %String) As %String
{
    // If blank datetime passed in return blank
    Quit:pHL7DateTime="" ""
    // If the datetime passed in is without a time then return the date unmodified
    Quit:$Length(pHL7DateTime)<=8 pHL7DateTime
        
    // Otherwise convert pHL7DateTime to Local Time with offset from utc
    Set tLocalTimeStamp = $ZDT(..ConvertDateTime(pHL7DateTime,"%q(0)","%q(3)"),1,5)
    // Seperate offset from this and remove the : between hours and seconds
    Set tOffset="+"_$PIECE(tLocalTimeStamp,"+",2)
    Set:tOffset="+" tOffset="-"_$PIECE(tLocalTimeStamp,"-",2)
    Set tOffset=$ZSTRIP(tOffset,"*",":")
    
    // Append Offset to pHL7DateTime
    Quit pHL7DateTime_tOffset
}

Hello Jonathan,

Assuming you have a class which overrides Ens.Rule.FunctionSet

e.g. 

Class LDH.Lib.FunctionSet Extends Ens.Rule.FunctionSet
    /// This method will append an offset from UTC to a datetime passed in as HL7 Format
    /// e.g. "20190711083705" returns "20190711083705+0100"
    /// - NB BST was introduced in 1970 so dates passed in before 1970 will return +0000 offsets for all dates
    ClassMethod GetLocalTimeWithUTCOffset(pHL7DateTime As %String) As %String
    {
        // If blank datetime passed in return blank
        Quit:pHL7DateTime="" ""
        // If the datetime passed in is without a time then return the date unmodified
        Quit:$Length(pHL7DateTime)<=8 pHL7DateTime
            
        // Otherwise convert pHL7DateTime to Local Time with offset from utc
        Set tLocalTimeStamp = $ZDT(..ConvertDateTime(pHL7DateTime,"%q(0)","%q(3)"),1,5)
        // Seperate offset from this and remove the : between hours and seconds
        Set tOffset="+"_$PIECE(tLocalTimeStamp,"+",2)
        Set:tOffset="+" tOffset="-"_$PIECE(tLocalTimeStamp,"-",2)
        Set tOffset=$ZSTRIP(tOffset,"*",":")
        
        // Append Offset to pHL7DateTime
        Quit pHL7DateTime_tOffset
    }

}

Then you can use it like this:

<assign value='##class(LDH.Lib.FunctionSet).GetLocalTimeWithUTCOffset(source.{DateTimeOfMessage.timeofanevent})' property='target.{DateTimeOfMessage.timeofanevent}' action='set' />