unit util;

  interface (* util *)

    function asciiz_to_string(var s):string;
    function hex_byte(b:byte;klein:boolean):string;
    function hex_word(w:word;klein:boolean):string;
    function secs2time(secs:longint):string;
    function time2secs(time:string):longint;
    procedure timedate(var time,date:string);
    function zeitdifferenz(time1,time2:string):string;

  implementation (* util *)

    uses dos;

    type t_string2=string[2];

    function zwei(b:byte):t_string2;
      var temp:t_string2;
          lt:byte absolute temp;
      begin (* f *)
        str(b,temp);
        if lt=1 then
          temp:='0'+temp;
        zwei:=temp
      end; (* zwei *)

    (***********************************************************)

    function asciiz_to_string(var s):string;
      var a:array[byte] of byte absolute s;
          temp:string;
          lt:byte absolute temp;
      begin (* asciiz_to_string *)
        lt:=0; (* temp:=''; *)
        while (lt<255) and (a[lt]<>0) do
          inc(lt);
        if lt>0 then
          move(s,temp[1],lt);
        asciiz_to_string:=temp
      end; (* asciiz_to_string *)

    function hex_byte(b:byte;klein:boolean):string;
      function hex_digit(b:byte;klein:boolean):char;
        begin (* hex_digit *)
          if b<10 then
            hex_digit:=char(b or $30)
          else begin (* also: b>=10 *)
            if klein then
              hex_digit:=char(b+87)
            else (* also: not klein *)
              hex_digit:=char(b+55)
          end (* b>=10 *)
        end; (* hex_digit *)
      begin (* hex_byte *)
        hex_byte:=hex_digit((b shr 4),klein)+hex_digit((b and 15),klein)
      end; (* hex_byte *)

    function hex_word(w:word;klein:boolean):string;
      begin (* hex_word *)
        hex_word:=hex_byte(hi(w),klein)+hex_byte(lo(w),klein)
      end; (* hex_word *)

    function secs2time(secs:longint):string;
      var s:longint;
          stunden,minuten,sekunden,code:word;
      begin (* secs2time *)
        s:=secs;
        stunden:=trunc(1.0*s/3600.0);
        s:=s-3600*stunden;
        minuten:=trunc(1.0*s/60.0);
        s:=s-60*minuten;
        sekunden:=s;
        secs2time:=zwei(stunden)+':'+zwei(minuten)+':'+zwei(sekunden)
      end; (* secs2time *)

    function time2secs(time:string):longint;
      var stunden,minuten,sekunden,code:word;
      begin (* time2secs *)
        val(copy(time,1,2),stunden,code);
        val(copy(time,4,2),minuten,code);
        val(copy(time,7,2),sekunden,code);
        time2secs:=(stunden*60+minuten)*60+sekunden
      end; (* time2secs *)

    procedure timedate(var time,date:string);
      var t,d:string;

      procedure td(var t,d:string);
        const ascii:array[0..9] of char='0123456789';
        var regs:dos.registers;
        begin (* td *)
          regs.ah:=$2C;
          msdos(regs);
          with regs do
            t:=ascii[ch div 10]+ascii[ch mod 10]+':'+
               ascii[cl div 10]+ascii[cl mod 10]+':'+
               ascii[dh div 10]+ascii[dh mod 10];
          regs.ah:=$2a;
          msdos(regs);
          with regs do
            d:=ascii[dl div 10]+ascii[dl mod 10]+'.'+
               ascii[dh div 10]+ascii[dh mod 10]+'.'+
            {  ascii[cx div 1000]+ascii[cx mod 1000 div 100]+  }
               ascii[cx mod 100 div 10]+ascii[cx mod 10]
        end; (* td *)

      begin (* timedate *)
        td(t,d);
        td(time,date);
        while (time<>t) or (date<>d) do begin
          t:=time;
          d:=date;
          td(time,date)
        end (* while (time<>t) or (date<>d) *)
      end; (* timedate *)

    function zeitdifferenz(time1,time2:string):string;
      var h0,m0,s0,h1,m1,s1,h2,m2,s2:integer; (* nicht word! *)
      begin (* zeitdifferenz *)
        h1:=(byte(time1[1]) and 15)*10+(byte(time1[2]) and 15); (* stunden *)
        m1:=(byte(time1[4]) and 15)*10+(byte(time1[5]) and 15); (* minuten *)
        s1:=(byte(time1[7]) and 15)*10+(byte(time1[8]) and 15); (* sekunden *)
        h2:=(byte(time2[1]) and 15)*10+(byte(time2[2]) and 15); (* stunden *)
        m2:=(byte(time2[4]) and 15)*10+(byte(time2[5]) and 15); (* minuten *)
        s2:=(byte(time2[7]) and 15)*10+(byte(time2[8]) and 15); (* sekunden *)
        h0:=h2-h1;
        m0:=m2-m1;
        s0:=s2-s1;
        while s0<0 do begin m0:=pred(m0);s0:=s0+60 end;
        while m0<0 do begin h0:=pred(h0);m0:=m0+60 end;
        while h0<0 do h0:=h0+24;
        zeitdifferenz:=zwei(h0)+':'+zwei(m0)+':'+zwei(s0)
      end; (* zeitdifferenz *)

  end. (* util *)