2015-03-02 66 views
0

我有以下代碼解析字段從HL7 2.1消息,它將它轉換爲JSON對象在Logstash/ElasticSearch/Kibana進一步處理我的問題是如何在變壓器中正確解析從本地時間HL7到UTC ISO8691日期時間的日期時間戳。我已經包含了變壓器的整個代碼。在歡樂連接轉換hl7 datetime在本地時間爲ISO日期iso

ConvertToISODate函數是簡單地創建一個ISO日期時間字符串,但它不會將它轉換爲我需要的UTC。

function ConvertToISODate(hl7Date) { 
    // this function will return an ISO formatted date string from the insanity that is hl7 dates 
    // this function is also insane but the sane ways didn't seem to work 
    var outdate = ''; 
    //4 year 
    outdate += hl7Date.substring(0,4) + '-'; 
    //2 month 
    outdate += hl7Date.substring(4,6) + '-'; 
  //2 day 
    outdate += hl7Date.substring(6,8) + 'T'; 
    //2 hour 
    outdate += hl7Date.substring(8,10) + ':'; 
    //2 minute 
    outdate += hl7Date.substring(10,12) + ':'; 
    //2 second assume start of minute as not precise 
    outdate += '00.000Z'; 
    return outdate; 

} 

function Group(obrseg) { 
    this.seqno = obrseg['OBR.1']['OBR.1.1'].toString(); 
    if (this.seqno == '') { 
     this.seqno = '1'; 
    } else { 
     this.seqno = this.seqno; 
    } 
    this.reptno = obrseg['OBR.3']['OBR.3.1'].toString(); 
    this.reqno = obrseg['OBR.3']['OBR.3.1'].toString().substr(0, 10); 
    this.test = obrseg['OBR.4']['OBR.4.1'].toString(); // TODO what about s/a's ? 
    this.groupname = obrseg['OBR.4']['OBR.4.2'].toString(); 
    this.priority = obrseg['OBR.5']['OBR.5.1'].toString(); 
    this.reg_dt = ConvertToISODate(obrseg['OBR.6']['OBR.6.1'].toString()); 
    this.requester_id = obrseg['OBR.16']['OBR.16.1'].toString(); 
    this.requester_surname = obrseg['OBR.16']['OBR.16.2'].toString(); 
    this.requester_firstname = obrseg['OBR.16']['OBR.16.3'].toString(); 
    this.rept_dt = ConvertToISODate(obrseg['OBR.22']['OBR.22.1'].toString()); 
    this.status = obrseg['OBR.25']['OBR.25.1'].toString(); 
} 
function Constit(obxseg) { 
    this.seqno = obxseg['OBX.1']['OBX.1.1'].toString(); 
    if (this.seqno == '') { 
     this.seqno = '1'; 
    } else { 
     this.seqno = this.seqno; 
    } 
    this.valtype =obxseg['OBX.2']['OBX.2.1'].toString(); 
    this.test = obxseg['OBX.3']['OBX.3.1'].toString(); // TODO what about s/a's ? 
    this.testname = obxseg['OBX.3']['OBX.3.2'].toString(); 
    this.result = obxseg['OBX.5']['OBX.5.1'].toString(); 
    this.abn_flags = obxseg['OBX.8']['OBX.8.1'].toString(); 
    this.status = obxseg['OBX.11']['OBX.11.1'].toString(); 
} 
// FIXME what about NTE ??? 

var curGroup = null; 
var lastReqno = null; 
// this is the main output string 
var eventOut = {}; 
// eventOut.obr = {}; 
var obrIndex = 0; 
// get the orginating lab from the msh header 
eventOut["originatinglab"] = msg['MSH']['MSH.4']['MSH.4.1'].toString(); 
eventOut.groups = []; 
for each (seg in msg.children()) { 
    switch(seg.name().toString()) { 
     case "OBR": 
      curGroup = new Group(seg); 
      if (lastReqno == null || lastReqno != curGroup.reqno) { 
       lastReqno = curGroup.reqno; 
      } 
      // add current group to eventOut Object 

      eventOut["reptno"] = curGroup.reptno; 
      //eventOut.obr[curGroup.seqno] = curGroup; 
      //eventOut.obr[curGroup.seqno]["obx"] = {}; 
      eventOut.groups[obrIndex] = curGroup; 
      eventOut.groups[obrIndex++]["tests"] = []; 
      var obxIndex = 0; 
      break; 
     case "OBX": 
      var constit = new Constit(seg); 
      //eventOut.obr[curGroup.seqno]["obx"][constit.seqno] = constit; 
      eventOut.groups[obrIndex - 1]["tests"][obxIndex++] = constit; 
      break; 
     default: 
      break; // ignore 
    } 
} 

    // now to serilize the object and save to the channel map 
    // var eventOutString = JSON.stringify(eventOut); 

json = JSON.stringify(eventOut, null); // Convert XML HL7 to json don't pretty print 

// json = JSON.parse(json); // turn it back into a javascript object 
// json.orignal_message = original.toString(); 
// json = JSON.stringify(json); 

channelMap.put('xml', msg); 
channelMap.put('json', json); 

回答

0

我知道,這是不是你要找的答案,但,這可能幫助: 在歡樂編寫JavaScript代碼時,可以使用任何Java類:

//get DoB 
    var rawDate = msg['PID']['PID.7']['PID.7.1'].toString() 
    var formatter = java.text.SimpleDateFormat("yyyyMMdd"); 
    var date = formatter.parse(rawDate); 

    //format to MM/dd/yyyy 
    formatter = java.text.SimpleDateFormat("MM/dd/yyyy"); 
    var pretty_time = formatter.format(date); 
0

這我落得這樣做:

function ConvertToISODate(hl7Date) { 
// this function will return an ISO formatted date string in UTC from the insanity that is hl7 dates 
// the function is now more sane 

var utcDate = new Date(); 
    utcDate.setFullYear(hl7Date.substring(0,4), hl7Date.substring(4,6)-1, hl7Date.substring(6,8)); 
    utcDate.setHours(hl7Date.substring(8,10),hl7Date.substring(10,12),'00'); 
    return utcDate.toISOString(); 
} 

這似乎是現在處理的事實,在HL7消息時間戳是本地時間,但我希望有一個ISO格式的UTC時間字符串返回。

相關問題