Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: DIUTC

DIUTC.m

Go to the documentation of this file.
  1. DIUTC ;O-OIFO/ALA/LG - UTC Timezone API ;Dec 16, 2015 10:00 AM
  1. ;;22.2;VA FileMan;**2,21**;Jan 05, 2016;Build 4
  1. ;;Per VA Directive 6402, this routine should not be modified.
  1. ;
  1. UTC(DTM,TMZ,INST,CNTRY,EXT) ;PEP - Return UTC value
  1. ;
  1. ; Input Parameters - DATE/TIME (DTM) is (*REQUIRED*) all other parameters are (Optional)
  1. ;
  1. ; DTM - Standard Date/Time; FileMan Internal and External; FileMan-specific e.g. NOW, T+1@12A
  1. ; TMZ - TimeZone ? Either Full Name (e.g., EASTERN; EASTERN EUPOREAN; ESTONIA; YUKON)
  1. ; or Pointer to the WORLD TIMEZONE (#1.71) file.
  1. ; Note: Time Zone Abbreviation (e.g. EST) can be ambiguous so is not used
  1. ; INST - Pointer to Institution (#4) file
  1. ; CNTRY - Country Name or Pointer to the COUNTRY CODE (#779.004) file.
  1. ; If Country Name is passed in, it must match the name in the HL7 COUNTRY file
  1. ; EXT - Extended return value
  1. ; Null=Default returns GMT with offset (internal FM format)
  1. ; 1 returns GMT with offset (internal FM format) with additional external values (multi-part)
  1. ;
  1. ; Output
  1. ; returns GMT with offset (internal FM format), if EXT=1 returns additional external values
  1. ;
  1. ;
  1. N %DT,XX,H,M,X,Y
  1. N CODE,DTME,GMT,GMTE,GMTO,INSTE,OFF,TMFRM,TMZE,CNTRYE,TMZEXC
  1. ;
  1. ; get values for all possible parameters
  1. F XX="DTM","TMZ","INST","CNTRY","EXT" S @XX=$G(@XX)
  1. ;
  1. I DTM="" Q -1_"^Date/Time parameter is missing."
  1. I '$P(DTM,".",2) Q -1_"^Date/Time parameter missing time." ;p21
  1. I INST]"",(TMZ]""!(CNTRY]"")) Q -1_"^Institution parameter cannot include Country or Timezone parameter."
  1. I TMZ]"",CNTRY="" Q -1_"^Timezone parameter must include Country parameter."
  1. I CNTRY]"",TMZ="" Q -1_"^Country parameter must include Timezone parameter."
  1. I EXT'="",(EXT'=1) Q -1_"^Extended parameter must be NULL or a 1 for extended output."
  1. ;
  1. S %DT="RSTX",X=DTM D ^%DT
  1. I Y<0 Q Y_"^Invalid Date/Time"
  1. S DTM=$E(Y_"00000",1,14)
  1. S Y=DTM D DD^%DT S DTME=Y ; External Date/Time
  1. ;
  1. ; Find offset if timezone and country parameteres are passed
  1. I TMZ]"" D ;
  1. . I +TMZ'=TMZ S TMZ=$$FIND1^DIC(1.71,"","X",TMZ,"","","ERROR")
  1. . S TMZE=$$GET1^DIQ(1.71,TMZ_",",.01,"E")
  1. I CNTRY]"" D ;
  1. . I +CNTRY'=CNTRY S CNTRY=$$FIND1^DIC(779.004,"","MX",CNTRY,"D","","ERROR")
  1. ;
  1. ; If no timezone param, no country param, look for institution param
  1. ; If the institution param exists get values:
  1. ; institution (external), timezone (internal/external), country (internal), timezone exception (internal)
  1. ; If no institution param, try to get data from default instituion stored in DUZ(2)
  1. I TMZ="",CNTRY="" N MSG D I $D(MSG) Q -1_MSG
  1. . I INST D Q ; if Institution is passed get data from Institution file #4
  1. . . S INSTE=$$GET1^DIQ(4,INST_",",.01,"E")
  1. . . S TMZ=$$GET1^DIQ(4,INST_",",800,"I")
  1. . . S TMZE=$$GET1^DIQ(4,INST_",",800,"E")
  1. . . S CNTRY=$$GET1^DIQ(4,INST_",",801,"I")
  1. . . S TMZEXC=$$GET1^DIQ(4,INST_",",802,"I")
  1. . . I 'TMZ!'CNTRY S MSG="^Cannot determine Country and Timezone from the Institution."
  1. . ; if Institution not passed try default Institution in DUZ(2)
  1. . I +$G(DUZ(2))=0 S MSG="^Cannot determine User location." Q ; quit if DUZ(2) not defined with message
  1. . S INSTE=$$GET1^DIQ(4,+DUZ(2)_",",.01,"E")
  1. . S TMZ=$$GET1^DIQ(4,+DUZ(2)_",",800,"I")
  1. . S TMZE=$$GET1^DIQ(4,+DUZ(2)_",",800,"E")
  1. . S CNTRY=$$GET1^DIQ(4,+DUZ(2)_",",801,"I")
  1. . S TMZEXC=$$GET1^DIQ(4,+DUZ(2)_",",802,"I")
  1. . I 'TMZ!'CNTRY S MSG="^Cannot determine Country and Timezone from User location."
  1. ;
  1. ; uppercase the external value for Country from Description field (#2) in file (#779.004)
  1. I CNTRY S CNTRYE=$$UP^XLFSTR($$GET1^DIQ(779.004,CNTRY_",",2,"E"))
  1. ;
  1. I 'TMZ Q -1_"^Invalid Timezone."
  1. I 'CNTRY Q -1_"^Invalid Country."
  1. I '$D(^DIT(1.71,"AB",CNTRY,TMZ)) Q -1_"^Mismatch of Country and Timezone."
  1. ;
  1. D CHECKDST ; check for daylight savings, standard, or summer timeframe
  1. D GMT ; convert input time to Greenwich Mean Time based on Offset
  1. ;
  1. ; Quit with GMT+Offset only (internal value)
  1. I 'EXT Q GMT_GMTO
  1. ;
  1. ; Quit with "Extended" output
  1. Q GMT_GMTO_U_GMTE_" (UTC"_OFF_")"_U_OFF_U_$G(TMZE)_U_$G(CNTRYE)_U_$G(CODE)_U_$G(TMFRM)_U_$G(INSTE)
  1. ;
  1. ;GMT+OFFSET ^ GMT+OFFSET EXT ^ OFFSET ^ TIMEZONE EXT ^ COUTRY EXT ^ TIMEZONE ABBREV ^ TIMEFRAME EXT (DAYLIGHT,STANDARD,SUMMER) ^ INSTITUTION EXT
  1. ;
  1. GMT ; calculate GMT time
  1. ;
  1. I '$D(OFF) Q ; quit if we don't have the Offset
  1. ;
  1. S H=$E(OFF,2,3),M=$E(OFF,4,5) ; get hour and minutes from GMT offset
  1. S H=$TR($E(OFF),"-+","+-")_H
  1. S M=$TR($E(OFF),"-+","+-")_M
  1. S GMT=$$FMADD^XLFDT(DTM,,H,M,) ; GMT internal Date/Time value
  1. S GMT=$E(GMT_"00000",1,14)
  1. S Y=GMT D DD^%DT S GMTE=Y ; GMT External Date/Time value
  1. S GMTO=OFF,M=$E(GMTO,4,5)
  1. S GMTO=$E(GMTO,1,3)*60+M/5+500
  1. ;(+/-hours x 60 + minutes) / 5 = value then 500 +/- value = offset
  1. ;
  1. ; massage offset for output
  1. S OFF=$E(OFF,1,3)_":"_$E(OFF,4,5)
  1. Q
  1. ;
  1. CHECKDST ;
  1. ; Check if this timezone changes to daylight (or summer) time or not
  1. ; (TDST=1, DOES NOT CHANGE, TDST=0, does change)
  1. ;
  1. N TDST,SIEN,DIEN,CIEN,DSIEN,YEAR,YIEN,STAN,SDTM,EDTM,DST,DSAV,SAME
  1. ;
  1. S TDST=1
  1. S SIEN=$O(^DIT(1.71,TMZ,1,"B","SST",""))
  1. S DIEN=$O(^DIT(1.71,TMZ,1,"B","DST",""))
  1. I 'DIEN S DIEN=$O(^DIT(1.71,TMZ,1,"B","SUM",""))
  1. I DIEN S CIEN=$O(^DIT(1.71,TMZ,1,DIEN,1,"B",CNTRY,""))
  1. I $G(CIEN) S TDST=0,DST=1
  1. I '$G(CIEN) S TDST=1
  1. ; If the timezone does not change, then DST would be zero for standard time (SST)
  1. I $G(TDST) S DST=0
  1. ; retrieve the offset for daylight savings (DST=1) OR standard (DST=0)
  1. S STAN=^DIT(1.71,TMZ,1,SIEN,0) I DIEN'="" S DSAV=^DIT(1.71,TMZ,1,DIEN,0)
  1. I 'TDST D
  1. . S DSIEN=$O(^DIT(1.72,"B",CNTRY,""))
  1. . I 'DSIEN Q ; quit if country timeframes not found in world daylight savings file #1.72
  1. . S SAME=$P(^DIT(1.72,DSIEN,0),"^",3)
  1. . I SAME'="" S DSIEN=$O(^DIT(1.72,"B",SAME,""))
  1. ;
  1. ; using the year from the date, determine the start and end date/time of world daylight savings from file #1.72
  1. I $G(DSIEN) D
  1. . S YEAR=$P($$FMTE^XLFDT(DTM\1,5),"/",3)
  1. . S YIEN=$O(^DIT(1.72,DSIEN,1,"B",YEAR,""))
  1. . I YIEN'="" S SDTM=$P(^DIT(1.72,DSIEN,1,YIEN,0),"^",2),EDTM=$P(^(0),"^",3)
  1. . I YIEN="" Q ; quit if year data not found
  1. . ; if DST=1, daylight savings in affect, DST=0, standard time in affect
  1. . I DTM<SDTM!(DTM'<EDTM) S DST=0
  1. . I DTM'<SDTM,DTM<EDTM S DST=1
  1. ;
  1. I $G(TMZEXC)=0 S DST=0 ; Timezone Exception from Institution file #4
  1. ;
  1. I 'DST S OFF=$P(STAN,"^",2),CODE=$P(STAN,"^",3),TMFRM=$P(STAN,U)
  1. I DST S OFF=$P(DSAV,"^",2),CODE=$P(DSAV,"^",3),TMFRM=$P(DSAV,U)
  1. S TMFRM=$S(TMFRM="SST":"STANDARD",TMFRM="DST":"DAYLIGHT SAVINGS",TMFRM="SUM":"SUMMER",1:"") ; set Timeframe
  1. Q