1 { Copyright (C) 2005 Bas Steendijk and Peter Green
\r 
   2   For conditions of distribution and use, see copyright notice in zlib_license.txt
\r 
   3   which is included in the package
\r 
   4   ----------------------------------------------------------------------------- }
\r 
   6 this unit returns unix timestamp with seconds and microseconds (as float)
\r 
   7 works on windows/delphi, and on freepascal on unix.
\r 
  16 {$include lcoreconfig.inc}
\r 
  27   tunixtimeint={$ifdef ver100}longint;{$else}int64;{$endif}
\r 
  30   colorburst=39375000/11;  {3579545.4545....}
\r 
  35   irctime,unixtime:tunixtimeint;
\r 
  37   settimebias:tunixtimeint;
\r 
  38   performancecountfreq:extended;
\r 
  39   btimenowin8:boolean;
\r 
  41 function irctimefloat:float;
\r 
  42 function irctimeint:tunixtimeint;
\r 
  44 //unix timestamp (UTC) float seconds
\r 
  45 function unixtimefloat:float;
\r 
  46 function unixtimeint:tunixtimeint;
\r 
  48 //monotonic float seconds
\r 
  49 function monotimefloat:float;
\r 
  51 //monotonic (alias, old function name)
\r 
  52 function wintimefloat:float;
\r 
  54 procedure settime(newtime:tunixtimeint);
\r 
  55 procedure gettimezone;
\r 
  56 procedure timehandler;
\r 
  59 function timestring(i:tunixtimeint):string;      // Wednesday August 15 2012 -- 16:21:09 +02:00
\r 
  60 function timestrshort(i:tunixtimeint):string;    // Wed Aug 15 16:21:09 2012
\r 
  61 function timestriso(i:tunixtimeint):string;      // 2012-08-15 16:21:09
\r 
  62 function timestrisoutc(i:float):string;          // 2012-08-15T14:21:09.255553Z
\r 
  64 procedure beginhightimerrate;
\r 
  65 procedure endhightimerrate;
\r 
  67 procedure tzinvalidate;
\r 
  70 function tzgetoffsetforts(ts:tunixtimeint):integer;
\r 
  74 function unixtimefloat_systemtime:float;
\r 
  77 function oletounixfloat(t:float):float;
\r 
  78 function oletounix(t:tdatetime):tunixtimeint;
\r 
  79 function unixtoole(i:float):tdatetime;
\r 
  82 function mmtimefloat:float;
\r 
  83 function qpctimefloat:float;
\r 
  87 procedure gettimeofday(var tv:ttimeval);
\r 
  92   mmtime_driftavgsize=32;
\r 
  94   mmtime_warmupcyclelength=15;
\r 
  96   //this flag is to be set when btime has been running long enough to stabilise
\r 
  97   warmup_finished:boolean;
\r 
  99   timefloatbias:float;
\r 
 100   ticks_freq:float=0;
\r 
 101   ticks_freq2:float=0;
\r 
 102   ticks_freq_known:boolean=false;
\r 
 103   lastunixtimefloat:float=0;
\r 
 104   lastsynctime:float=0;
\r 
 105   lastsyncbias:float=0;
\r 
 107   mmtime_last:integer=0;
\r 
 108   mmtime_wrapadd:float;
\r 
 109   mmtime_lastsyncmm:float=0;
\r 
 110   mmtime_lastsyncqpc:float=0;
\r 
 111   mmtime_drift:float=1;
\r 
 112   mmtime_lastresult:float;
\r 
 113   mmtime_nextdriftcorrection:float;
\r 
 114   mmtime_driftavg:array[0..mmtime_driftavgsize] of float;
\r 
 115   mmtime_synchedqpc:boolean;
\r 
 117   mmtime_prev_drift:float;
\r 
 118   mmtime_prev_lastsyncmm:float;
\r 
 119   mmtime_prev_lastsyncqpc:float;
\r 
 130       {$ifdef linux}linux,{$endif} //for clock_gettime
\r 
 131       {$ifdef freebsd}freebsd,{$endif} //for clock_gettime
\r 
 132       baseunix,unix,unixutil,sockets, {unixutil and sockets needed by unixstuff.inc on some compiler versions}
\r 
 135     windows,unitsettc,mmsystem,
\r 
 139   {$include unixstuff.inc}
\r 
 143   daysdifference=25569;
\r 
 145 function oletounixfloat(t:float):float;
\r 
 147   t := (t - daysdifference) * 86400;
\r 
 151 function oletounix(t:tdatetime):tunixtimeint;
\r 
 153   result := round(oletounixfloat(t));
\r 
 156 function unixtoole(i:float):tdatetime;
\r 
 158   result := ((i)/86400)+daysdifference;
\r 
 162   highdwordconst=65536.0 * 65536.0;
\r 
 164 function utrunc(f:float):integer;
\r 
 165 {converts float to integer, in 32 bits unsigned range}
\r 
 167   if f >= (highdwordconst/2) then f := f - highdwordconst;
\r 
 168   result := trunc(f);
\r 
 171 function uinttofloat(i:integer):float;
\r 
 172 {converts 32 bits unsigned integer to float}
\r 
 175   if result < 0 then result := result + highdwordconst;
\r 
 179 {-----------------------------------------*nix/freepascal code to read time }
\r 
 181 function unixtimefloat:float;
\r 
 189   if (sec < -1) then inc(sec,$100000000); //tv_sec is 32 bits. allow -1 for invalid result
\r 
 191   result := sec+(tv.tv_usec/1000000);
\r 
 194 {$ifdef linux}{$define have_clock_gettime}{$endif}
\r 
 195 {$ifdef freebsd}{$define have_clock_gettime}{$endif}
\r 
 197 {$ifdef have_clock_gettime}
\r 
 198   {$define monotimefloat_implemented}
\r 
 200   function monotimefloat:float;
\r 
 204     if clock_gettime(CLOCK_MONOTONIC, @ts) = 0 then begin
\r 
 205       //note this really returns nanoseconds
\r 
 206       result := ts.tv_sec + ts.tv_nsec / 1000000000.0;
\r 
 210     result := unixtimefloat;
\r 
 216 {$ifdef darwin} {mac OS X}
\r 
 217 {$define monotimefloat_implemented}
\r 
 220     tmach_timebase_info = packed record
\r 
 224     pmach_timebase_info = ^tmach_timebase_info;
\r 
 226     function mach_absolute_time: int64; cdecl; external;
\r 
 227     function mach_timebase_info(info: pmach_timebase_info): integer; cdecl; external;
\r 
 230     timebase_info: tmach_timebase_info;
\r 
 232   function monotimefloat:float;
\r 
 236     if timebase_info.denom = 0 then begin
\r 
 237       mach_timebase_info(@timebase_info);
\r 
 239     i := mach_absolute_time;
\r 
 240     result := (i * timebase_info.numer div timebase_info.denom) / 1000000000.0;
\r 
 243 {$endif} {darwin, mac OS X}
\r 
 246 {$ifndef monotimefloat_implemented} {fallback}
\r 
 248   function monotimefloat:extended;
\r 
 250     result := unixtimefloat;
\r 
 253 {$endif} {monotimefloat fallback}
\r 
 256 function unixtimeint:tunixtimeint;
\r 
 264   if (sec < -1) then inc(sec,$100000000); //tv_sec is 32 bits. allow -1 for invalid result
\r 
 269 {------------------------------ end of *nix/freepascal section}
\r 
 272 {------------------------------ windows/delphi code to read time}
\r 
 275 procedure tzinvalidate;
\r 
 280 {simulate gettimeofday on windows so one can always use gettimeofday if preferred}
\r 
 282 procedure gettimeofday(var tv:ttimeval);
\r 
 286   e := unixtimefloat;
\r 
 287   tv.tv_sec := round(int(e));
\r 
 288   tv.tv_usec := trunc(frac(e)*1000000);
\r 
 290   if (tv.tv_usec < 0) then tv.tv_usec := 0;
\r 
 291   if (tv.tv_usec > 999999) then tv.tv_usec := 999999;
\r 
 296 time float: gettickcount
\r 
 297 resolution: 9x: ~55 ms NT: 1/64th of a second
\r 
 298 guarantees: continuous without any jumps
\r 
 299 frequency base: same as system clock.
\r 
 301 note: if called more than once per 49.7 days, 32 bits wrapping is compensated for and it keeps going on.
\r 
 302 note: i handle the timestamp as signed integer, but with the wrap compensation that works as well, and is faster
\r 
 305 function mmtimefloat:float;
\r 
 307   wrapduration=highdwordconst * 0.001;
\r 
 312   i := gettickcount; {timegettime}
\r 
 313   if i < mmtime_last then begin
\r 
 314     mmtime_wrapadd := mmtime_wrapadd + wrapduration;
\r 
 317   result := mmtime_wrapadd + i * 0.001;
\r 
 319   if (ticks_freq <> 0) and ticks_freq_known then begin
\r 
 320     {the value we get is rounded to 1 ms, but the ticks are not a multiple of 1 ms
\r 
 321     this makes the value noisy. use the known ticks frequency to restore the original value}
\r 
 322     temp := int((result / ticks_freq)+0.5) * ticks_freq;
\r 
 324     {if the known ticks freq is wrong (can happen), disable the un-rounding behavior
\r 
 325     this will be a bit less accurate but it prevents problems}
\r 
 326     if abs(temp - result) > 0.002 then begin
\r 
 328     end else result := temp;
\r 
 332 procedure measure_ticks_freq;
\r 
 338   adjust1,adjust2:cardinal;
\r 
 339   adjustbool:longbool;
\r 
 341   if (performancecountfreq = 0) then qpctimefloat;
\r 
 342   ticks_freq_known := false;
\r 
 345   repeat g := mmtimefloat until g > f;
\r 
 348   fillchar(o,sizeof(o),0);
\r 
 349   o.dwOSVersionInfoSize := sizeof(o);
\r 
 351   isnt := o.dwPlatformId = VER_PLATFORM_WIN32_NT;
\r 
 352 {  is9x := o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;}
\r 
 355   mmtime_synchedqpc := false;
\r 
 357   if (isnt and (o.dwMajorVersion >= 5)) then begin
\r 
 358     {windows 2000 and later: query tick rate from OS in 100 ns units
\r 
 359     typical rates: XP: 156250 or 100144, windows 7: 156001}
\r 
 360     if GetSystemTimeAdjustment(adjust1,adjust2,adjustbool) then begin
\r 
 361       ticks_freq := adjust1 / 10000000.0;
\r 
 362       ticks_freq_known := true;
\r 
 363       mmtime_synchedqpc := false;
\r 
 368   if (performancecountfreq = 1193182) and (f >= 0.050) and (f <= 0.060) then begin
\r 
 369     ticks_freq_known := true;
\r 
 370     ticks_freq := 65536 / (colorburst / 3);
\r 
 371     mmtime_synchedqpc := true;
\r 
 373   ticks_freq_known := true;
\r 
 374   if ticks_freq <> 0 then ticks_freq2 := ticks_freq;
\r 
 375 //  writeln(formatfloat('0.000000',ticks_freq));
\r 
 379 time float: QueryPerformanceCounter
\r 
 381 guarantees: can have forward jumps depending on hardware. can have forward and backwards jitter on dual core.
\r 
 382 frequency base: on NT, not the system clock, drifts compared to it.
\r 
 385 function qpctimefloat:extended;
\r 
 391   p2:tlargeinteger absolute p;
\r 
 394   if performancecountfreq = 0 then begin
\r 
 395     QueryPerformancefrequency(p2);
\r 
 397     if e < 0 then e := e + highdwordconst;
\r 
 398     performancecountfreq := ((p.highpart*highdwordconst)+e);
\r 
 400   queryperformancecounter(p2);
\r 
 402   if e < 0 then e := e + highdwordconst;
\r 
 404   result := ((p.highpart*highdwordconst)+e)/performancecountfreq;
\r 
 408 time float: QPC locked to gettickcount
\r 
 410 guarantees: continuous without any jumps
\r 
 411 frequency base: same as system clock.
\r 
 415 function mmqpctimefloat:float;
\r 
 421   mm,f,qpc,newdrift:float;
\r 
 424 {  retrycount:integer;}
\r 
 426   if not ticks_freq_known then measure_ticks_freq;
\r 
 427 {  retrycount := maxretries;}
\r 
 429   qpc := qpctimefloat;
\r 
 431   f := (qpc - mmtime_lastsyncqpc) * mmtime_drift + mmtime_lastsyncmm;
\r 
 432   //writeln('XXXX ',formatfloat('0.000000',qpc-mm));
\r 
 433   qpcjumped := ((f-mm) > ticks_freq2+margin) or ((f-mm) < -margin);
\r 
 434 //  if qpcjumped then writeln('qpc jumped ',(f-mm));
\r 
 435   if ((qpc > mmtime_nextdriftcorrection) and not mmtime_synchedqpc) or qpcjumped then begin
\r 
 437     mmtime_nextdriftcorrection := qpc + 1;
\r 
 439       mmtime_prev_drift := mmtime_drift;
\r 
 440       mmtime_prev_lastsyncmm := mmtime_lastsyncmm;
\r 
 441       mmtime_prev_lastsyncqpc := mmtime_lastsyncqpc;
\r 
 444     {  dec(retrycount);}
\r 
 446       result := qpctimefloat;
\r 
 449         if f = mm then result := qpctimefloat;
\r 
 452       qpc := qpctimefloat;
\r 
 455       if (qpc > result + 0.0001) then begin
\r 
 460       if (mmtime_lastsyncqpc <> 0) and not qpcjumped then begin
\r 
 461         newdrift := (mm - mmtime_lastsyncmm) / (qpc - mmtime_lastsyncqpc);
\r 
 462         mmtime_drift := newdrift;
\r 
 463      {   writeln('raw drift: ',formatfloat('0.00000000',mmtime_drift));}
\r 
 464         move(mmtime_driftavg[0],mmtime_driftavg[1],sizeof(mmtime_driftavg[0])*high(mmtime_driftavg));
\r 
 465         mmtime_driftavg[0] := mmtime_drift;
\r 
 467 {        write('averaging drift ',formatfloat('0.00000000',mmtime_drift),' -> ');}
\r 
 468 {        mmtime_drift := 0;}
\r 
 470         for a := 0 to high(mmtime_driftavg) do begin
\r 
 471           if mmtime_driftavg[a] <> 0 then inc(b);
\r 
 472 {          mmtime_drift := mmtime_drift + mmtime_driftavg[a];}
\r 
 474 {        mmtime_drift := mmtime_drift / b;}
\r 
 476         if (b = 1) then a := 5 else if (b = 2) then a := 15 else if (b = 3) then a := 30 else if (b = 4) then a := 60 else if (b = 5) then a := 120 else if (b >= 5) then a := 120;
\r 
 477         mmtime_nextdriftcorrection := qpc + a;
\r 
 478         if (b >= 2) then warmup_finished := true;
\r 
 479 {        writeln(formatfloat('0.00000000',mmtime_drift));}
\r 
 480        if mmtime_synchedqpc then mmtime_drift := 1;
\r 
 483       mmtime_lastsyncqpc := qpc;
\r 
 484       mmtime_lastsyncmm := mm;
\r 
 485   {   writeln(formatfloat('0.00000000',mmtime_drift));}
\r 
 490     qpc := qpctimefloat;
\r 
 492     result := (qpc - mmtime_lastsyncqpc) * mmtime_drift + mmtime_lastsyncmm;
\r 
 494     {f := (qpc - mmtime_prev_lastsyncqpc) * mmtime_prev_drift + mmtime_prev_lastsyncmm;
\r 
 496     writeln('jump ',formatfloat('0.000000',jump),'   drift ',formatfloat('0.00000000',mmtime_drift),' duration ',formatfloat('0.000',(mmtime_lastsyncqpc-mmtime_prev_lastsyncqpc)),' ',formatfloat('0.00000000',jump/(mmtime_lastsyncqpc-mmtime_prev_lastsyncqpc)));}
\r 
 503   if (result < mmtime_lastresult) then result := mmtime_lastresult;
\r 
 504   mmtime_lastresult := result;
\r 
 507 { free pascals tsystemtime is incompatible with windows api calls
\r 
 508  so we declare it ourselves - plugwash
\r 
 512   TSystemTime = record
\r 
 520      wMilliseconds: Word;
\r 
 523 function Date_utc: extended;
\r 
 525   SystemTime: TSystemTime;
\r 
 528     GetsystemTime(@SystemTime);
\r 
 530     GetsystemTime(SystemTime);
\r 
 532   with SystemTime do Result := EncodeDate(wYear, wMonth, wDay);
\r 
 535 function Time_utc: extended;
\r 
 537   SystemTime: TSystemTime;
\r 
 540     GetsystemTime(@SystemTime);
\r 
 542     GetsystemTime(SystemTime);
\r 
 545     Result := EncodeTime(wHour, wMinute, wSecond, wMilliSeconds);
\r 
 548 function Now_utc: extended;
\r 
 550   Result := round(Date_utc) + Time_utc;
\r 
 553 function unixtimefloat_systemtime:float;
\r 
 555   {result := oletounixfloat(now_utc);}
\r 
 557   {this method gives exactly the same result with extended precision, but is less sensitive to float rounding in theory}
\r 
 558   result := oletounixfloat(int(date_utc+0.5))+time_utc*86400;
\r 
 561 function monotimefloat:extended;
\r 
 563   result := mmqpctimefloat;
\r 
 569   GetSystemTimePreciseAsFileTime:procedure(var v:tfiletime); stdcall;
\r 
 570   win8inited:boolean;
\r 
 572 procedure initwin8;
\r 
 577   win8inited := true;
\r 
 578   dllhandle := loadlibrary('kernel32.dll');
\r 
 579   if (dllhandle <> 0) then begin
\r 
 580     GetSystemTimePreciseAsFileTime := getprocaddress(dllhandle,'GetSystemTimePreciseAsFileTime');
\r 
 585 function unixtimefloat_win8:float;
\r 
 588   i:int64 absolute ft;
\r 
 590   GetSystemTimePreciseAsFileTime(ft);
\r 
 591   {change from windows 1601-01-01 to unix 1970-01-01.
\r 
 592   use integer math for this, to preserve precision}
\r 
 593   dec(i, 116444736000000000);
\r 
 594   result := (i / 10000000);
\r 
 599 function unixtimefloat:float;
\r 
 605   if not btimenowin8 then begin
\r 
 606     if not win8inited then initwin8;
\r 
 607     if assigned(@GetSystemTimePreciseAsFileTime) then begin
\r 
 608       result := unixtimefloat_win8;
\r 
 613   result := monotimefloat+timefloatbias;
\r 
 614   f := result-unixtimefloat_systemtime;
\r 
 615   if ((f > ticks_freq2+margin) or (f < -margin)) or (timefloatbias = 0) then begin
\r 
 616 //    writeln('unixtimefloat init');
\r 
 617     f := unixtimefloat_systemtime;
\r 
 619     repeat g := unixtimefloat_systemtime; h := monotimefloat until g > f;
\r 
 621     timefloatbias := g-h;
\r 
 622     result := unixtimefloat;
\r 
 625   {for small changes backwards, guarantee no steps backwards}
\r 
 626   if (result <= lastunixtimefloat) and (result > lastunixtimefloat-1.5) then result := lastunixtimefloat + 0.0000001;
\r 
 627   lastunixtimefloat := result;
\r 
 630 function unixtimeint:tunixtimeint;
\r 
 632   result := trunc(unixtimefloat);
\r 
 636 {-----------------------------------------------end of platform specific}
\r 
 638 function wintimefloat:float;
\r 
 640   result := monotimefloat;
\r 
 643 function irctimefloat:float;
\r 
 645   result := unixtimefloat+settimebias;
\r 
 648 function irctimeint:tunixtimeint;
\r 
 650   result := unixtimeint+settimebias;
\r 
 654 procedure settime(newtime:tunixtimeint);
\r 
 658   a := irctimeint-settimebias;
\r 
 659   if newtime = 0 then settimebias := 0 else settimebias := newtime-a;
\r 
 661   irctime := irctimeint;
\r 
 664 procedure timehandler;
\r 
 666   if unixtime = 0 then init;
\r 
 667   unixtime := unixtimeint;
\r 
 668   irctime := irctimeint;
\r 
 669   if unixtime and 63 = 0 then begin
\r 
 670     {update everything, apply timezone changes, clock changes, etc}
\r 
 672     timefloatbias := 0;
\r 
 673     unixtime := unixtimeint;
\r 
 674     irctime := irctimeint;
\r 
 685 function tzgetfilename:ansistring;
\r 
 689   s,tz,tzdir:ansistring;
\r 
 694   tz := getenv('TZ');
\r 
 696   if (copy(tz,1,1) = ':') then begin
\r 
 697     tz := copy(tz,2,99999);
\r 
 699     if (copy(tz,1,1) <> '/') then begin
\r 
 700       tzdir := getenv('TZDIR');
\r 
 701       if (tzdir = '') then begin
\r 
 702         tzdir := '/usr/share/zoneinfo/';
\r 
 704         if (copy(tzdir,length(tzdir),1) <> '/') then tzdir := tzdir + '/';
\r 
 710     {$i-}reset(t);{$i+}
\r 
 711     if (ioresult = 0) then begin
\r 
 720   assignfile(t,'/etc/localtime');
\r 
 721   {$i-}reset(t);{$i+}
\r 
 722   if (ioresult = 0) then begin
\r 
 724     result := '/etc/localtime';
\r 
 728   assignfile(t,'/etc/timezone');
\r 
 731   {$i-}reset(t);{$i+}
\r 
 732   if (ioresult = 0) then begin
\r 
 735     if (s <> '') then begin
\r 
 736       result := '/usr/share/zoneinfo/'+s;
\r 
 743   dvar=array[0..65535] of byte;
\r 
 750 procedure tzinvalidate;
\r 
 752   if assigned(tzcache) then freemem(tzcache);
\r 
 760 function tzgetoffsetforts(ts:tunixtimeint):integer;
\r 
 769   //tzstrofs:integer;
\r 
 771   tzh_ttisgmtcnt:integer;
\r 
 772   tzh_ttisstdcnt:integer;
\r 
 773   tzh_leapcnt:integer;
\r 
 774   tzh_timecnt:integer;
\r 
 775   tzh_typecnt:integer;
\r 
 776   tzh_charcnt:integer;
\r 
 779 function getint:integer;
\r 
 781   if (ofs < 0) or ((ofs + 4) > fs) then raise exception.create('getint');
\r 
 782   result := (buf[ofs] shl 24) + (buf[ofs+1] shl 16) + (buf[ofs+2] shl 8) + buf[ofs+3];
\r 
 786 function getint64:int64;
\r 
 788   if (ofs < 0) or ((ofs + 8) > fs) then raise exception.create('getint64');
\r 
 789   result := int64(getint) shl 32;
\r 
 790   inc(result,cardinal(getint));
\r 
 794 function getbyte:byte;
\r 
 796   if (ofs < 0) or ((ofs + 1) > fs) then raise exception.create('getbyte');
\r 
 797   result := buf[ofs];
\r 
 805   if not assigned(tzcache) then begin
\r 
 807     if (tzfile = '') then tzfile := tzgetfilename;
\r 
 809     if (tzfile = '') then exit;
\r 
 811     assignfile(f,tzfile);
\r 
 813     {$i-}reset(f,1);{$i+}
\r 
 814     if (ioresult <> 0) then begin
\r 
 817     tzsize := filesize(f);
\r 
 818     if (tzsize > 65536) then tzsize := 65536;
\r 
 819     getmem(tzcache,tzsize);
\r 
 820     blockread(f,tzcache^,tzsize);
\r 
 830      if (getint <> $545a6966) then exit; // 'TZif'
\r 
 831      has64 := getbyte >= $32; //  '2'
\r 
 835      tzh_ttisgmtcnt := getint;
\r 
 836      tzh_ttisstdcnt := getint;
\r 
 837      tzh_leapcnt := getint;
\r 
 838      tzh_timecnt := getint;
\r 
 839      tzh_typecnt := getint;
\r 
 840      tzh_charcnt := getint;
\r 
 842      if mode64 or (not has64) then break;
\r 
 843      inc(ofs, 5 * tzh_timecnt + 6 * tzh_typecnt + 8 * tzh_leapcnt + tzh_ttisstdcnt + tzh_ttisgmtcnt + tzh_charcnt);
\r 
 848    if (tzh_timecnt < 0) or (tzh_timecnt > fs) then raise exception.create('tzh_timecnt');
\r 
 851    for a := 0 to tzh_timecnt -1 do begin
\r 
 852      if mode64 then t := getint64 else t := getint;
\r 
 853      if (t > ts) then begin
\r 
 857      if (a = tzh_timecnt -1) and (ts >= t) then index := a;
\r 
 859    ofs := ofs2 + tzh_timecnt * (1 + ord(mode64)) * 4;
\r 
 861    if (cardinal(ofs + index) >= fs) or (index < 0) then raise exception.create('index');
\r 
 862    index := buf[ofs+index];
\r 
 863    inc(ofs,tzh_timecnt);
\r 
 865    if (index >= tzh_typecnt) then raise exception.create('type');
\r 
 867   // writeln('ofs2 ',inttohex(ofs2,8));
\r 
 868    inc(ofs,6 * index);
\r 
 871    //tzisdst := getbyte;
\r 
 873   //the abbreviation string
\r 
 874   { tzstrofs := getbyte;
\r 
 876    ofs := ofs2 + 6 * tzh_typecnt;
\r 
 877    inc(ofs, tzstrofs);
\r 
 881      if (a <> 0) then tzstr := tzstr + chr(a);
\r 
 890 function tzgetoffset:integer;
\r 
 892   tzgetoffsetforts(unixtimeint);
\r 
 899 procedure gettimezone;
\r 
 915       timezone := tzgetoffset;
\r 
 916       //freepascal tzseconds is not 2038 safe
\r 
 919       timezone := (longint(hh) * 3600 + mm * 60 + ss) - (unixtimeint mod 86400);
\r 
 922   timezone := round((now-now_utc)*86400);
\r 
 925   while timezone > 50400 do dec(timezone,86400);
\r 
 926   while timezone < -50400 do inc(timezone,86400);
\r 
 928   if timezone >= 0 then timezonestr := '+' else timezonestr := '-';
\r 
 929   l := abs(timezone) div 60;
\r 
 930   timezonestr := timezonestr + char(l div 600 mod 10+48)+char(l div 60 mod 10+48)+':'+char(l div 10 mod 6+48)+char(l mod 10+48);
\r 
 933 function timestrshort(i:tunixtimeint):string;
\r 
 935   weekday:array[0..6] of string[4]=('Thu','Fri','Sat','Sun','Mon','Tue','Wed');
\r 
 936   month:array[0..11] of string[4]=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
\r 
 938   y,m,d,h,min,sec,ms:word;
\r 
 941   t := unixtoole(i+timezone);
\r 
 942   decodedate(t,y,m,d);
\r 
 943   decodetime(t,h,min,sec,ms);
\r 
 944   result := weekday[(i+timezone) div 86400 mod 7]+' '+month[m-1]+' '+inttostr(d)+' '+
\r 
 945   inttostr(h div 10)+inttostr(h mod 10)+':'+inttostr(min div 10)+inttostr(min mod 10)+':'+inttostr(sec div 10)+inttostr(sec mod 10)+' '+
\r 
 949 function timestring(i:tunixtimeint):string;
\r 
 951   weekday:array[0..6] of string[10]=('Thursday','Friday','Saturday','Sunday','Monday','Tuesday','Wednesday');
\r 
 952   month:array[0..11] of string[10]=('January','February','March','April','May','June','July','August','September','October','November','December');
\r 
 954   y,m,d,h,min,sec,ms:word;
\r 
 957   t := unixtoole(i+timezone);
\r 
 958   decodedate(t,y,m,d);
\r 
 959   decodetime(t,h,min,sec,ms);
\r 
 960   result := weekday[(i+timezone) div 86400 mod 7]+' '+month[m-1]+' '+inttostr(d)+' '+inttostr(y)+' -- '+
\r 
 961   inttostr(h div 10)+inttostr(h mod 10)+':'+inttostr(min div 10)+inttostr(min mod 10)+':'+inttostr(sec div 10)+inttostr(sec mod 10)+' '+
\r 
 965 function timestriso(i:tunixtimeint):string;
\r 
 967   y,m,d,h,min,sec,ms:word;
\r 
 970   t := unixtoole(i+timezone);
\r 
 971   decodedate(t,y,m,d);
\r 
 972   decodetime(t,h,min,sec,ms);
\r 
 973   result := inttostr(y)+'-'+inttostr(m div 10)+inttostr(m mod 10)+'-'+inttostr(d div 10)+inttostr(d mod 10)+' '+inttostr(h div 10)+inttostr(h mod 10)+':'+inttostr(min div 10)+inttostr(min mod 10)+':'+inttostr(sec div 10)+inttostr(sec mod 10);
\r 
 976 function timestrisoutc(i:float):string;
\r 
 978   y,m,d,h,min,sec,ms:word;
\r 
 983   decodedate(t,y,m,d);
\r 
 984   decodetime(t,h,min,sec,ms);
\r 
 985   result := inttostr(y)+'-'+inttostr(m div 10)+inttostr(m mod 10)+'-'+inttostr(d div 10)+inttostr(d mod 10)+'T'+inttostr(h div 10)+inttostr(h mod 10)+':'+inttostr(min div 10)+inttostr(min mod 10)+':'+inttostr(sec div 10)+inttostr(sec mod 10);
\r 
 988   result := result + '.'+
\r 
 989   inttostr(trunc(fr*10) mod 10)+
\r 
 990   inttostr(trunc(fr*100) mod 10)+
\r 
 991   inttostr(trunc(fr*1000) mod 10)+
\r 
 992   inttostr(trunc(fr*10000) mod 10)+
\r 
 993   inttostr(trunc(fr*100000) mod 10)+
\r 
 994   inttostr(trunc(fr*1000000) mod 10)+'Z';
\r 
 998 procedure beginhightimerrate;
\r 
1000   {$ifdef mswindows}timebeginperiod(1);{$endif}
\r 
1003 procedure endhightimerrate;
\r 
1005   {$ifdef mswindows}timeendperiod(1);{$endif}
\r 
1010   {$ifdef btimehighrate}beginhightimerrate;{$endif}
\r 
1011   fillchar(mmtime_driftavg,sizeof(mmtime_driftavg),0);
\r 
1014   unixtime := unixtimeint;
\r 
1015   irctime := irctimeint;
\r 
1018 initialization init;
\r