-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtime.pas
More file actions
778 lines (652 loc) · 20.4 KB
/
btime.pas
File metadata and controls
778 lines (652 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
{ Copyright (C) 2005 Bas Steendijk and Peter Green
For conditions of distribution and use, see copyright notice in zlib_license.txt
which is included in the package
----------------------------------------------------------------------------- }
{
this unit returns unix timestamp with seconds and microseconds (as float)
works on windows/delphi, and on freepascal on unix.
}
unit btime;
interface
{$ifdef mswindows}
uses
ltimevalstuff;
{$endif}
type
float=extended;
tunixtimeint={$ifdef ver100}longint;{$else}int64;{$endif}
const
colorburst=39375000/11; {3579545.4545....}
var
timezone:integer;
timezonestr:string;
irctime,unixtime:tunixtimeint;
tickcount:integer;
settimebias:tunixtimeint;
performancecountfreq:extended;
function irctimefloat:float;
function irctimeint:tunixtimeint;
//unix timestamp (UTC) float seconds
function unixtimefloat:float;
function unixtimeint:tunixtimeint;
//monotonic float seconds
function monotimefloat:float;
//monotonic (alias, old function name)
function wintimefloat:float;
procedure settime(newtime:tunixtimeint);
procedure gettimezone;
procedure timehandler;
procedure init;
function timestring(i:tunixtimeint):string; // Wednesday August 15 2012 -- 16:21:09 +02:00
function timestrshort(i:tunixtimeint):string; // Wed Aug 15 16:21:09 2012
function timestriso(i:tunixtimeint):string; // 2012-08-15 16:21:09
function timestrisoutc(i:float):string; // 2012-08-15T14:21:09.255553Z
{$ifdef mswindows}
function unixtimefloat_systemtime:float;
{$endif}
function oletounixfloat(t:float):float;
function oletounix(t:tdatetime):tunixtimeint;
function unixtoole(i:float):tdatetime;
{$ifdef mswindows}
function mmtimefloat:float;
function qpctimefloat:float;
{$endif}
{$ifdef mswindows}
procedure gettimeofday(var tv:ttimeval);
{$endif}
const
mmtime_driftavgsize=32;
mmtime_warmupnum=4;
mmtime_warmupcyclelength=15;
var
//this flag is to be set when btime has been running long enough to stabilise
warmup_finished:boolean;
timefloatbias:float;
ticks_freq:float=0;
ticks_freq2:float=0;
ticks_freq_known:boolean=false;
lastunixtimefloat:float=0;
lastsynctime:float=0;
lastsyncbias:float=0;
mmtime_last:integer=0;
mmtime_wrapadd:float;
mmtime_lastsyncmm:float=0;
mmtime_lastsyncqpc:float=0;
mmtime_drift:float=1;
mmtime_lastresult:float;
mmtime_nextdriftcorrection:float;
mmtime_driftavg:array[0..mmtime_driftavgsize] of float;
mmtime_synchedqpc:boolean;
mmtime_prev_drift:float;
mmtime_prev_lastsyncmm:float;
mmtime_prev_lastsyncqpc:float;
implementation
{$ifdef fpc}
{$mode delphi}
{$endif}
uses
{$ifdef UNIX}
{$ifdef VER1_0}
linux,
{$else}
baseunix,unix,unixutil,sockets, {unixutil and sockets needed by unixstuff.inc on some compiler versions}
{$endif}
{$ifdef linux}
dl,
{$endif}
{$else}
windows,unitsettc,mmsystem,
{$endif}
sysutils;
{$include unixstuff.inc}
const
daysdifference=25569;
function oletounixfloat(t:float):float;
begin
t := (t - daysdifference) * 86400;
result := t;
end;
function oletounix(t:tdatetime):tunixtimeint;
begin
result := round(oletounixfloat(t));
end;
function unixtoole(i:float):tdatetime;
begin
result := ((i)/86400)+daysdifference;
end;
const
highdwordconst=65536.0 * 65536.0;
function utrunc(f:float):integer;
{converts float to integer, in 32 bits unsigned range}
begin
if f >= (highdwordconst/2) then f := f - highdwordconst;
result := trunc(f);
end;
function uinttofloat(i:integer):float;
{converts 32 bits unsigned integer to float}
begin
result := i;
if result < 0 then result := result + highdwordconst;
end;
{$ifdef unix}
{-----------------------------------------*nix/freepascal code to read time }
function unixtimefloat:float;
var
tv:ttimeval;
begin
gettimeofday(tv);
result := tv.tv_sec+(tv.tv_usec/1000000);
end;
{$ifdef linux}
{$define monotimefloat_implemented}
const
CLOCK_MONOTONIC = 1;
type
ptimeval = ^ttimeval;
tclock_gettime = function(clk_id: integer; tp: ptimeval): integer; cdecl;
var
librt_handle:pointer;
librt_inited:boolean;
clock_gettime: tclock_gettime;
function monotimefloat:float;
var
ts: ttimeval;
begin
if not librt_inited then begin
librt_inited := true;
clock_gettime := nil;
librt_handle := dlopen('librt.so', RTLD_LAZY);
if assigned(librt_handle) then begin
clock_gettime := dlsym(librt_handle, 'clock_gettime');
end;
end;
if assigned(clock_gettime) then begin
if clock_gettime(CLOCK_MONOTONIC, @ts) = 0 then begin
//note this really returns nanoseconds
result := ts.tv_sec + ts.tv_usec / 1000000000.0;
exit;
end;
end;
//fallback
result := unixtimefloat;
end;
{$endif} {linux}
{$ifdef darwin} {mac OS X}
{$define monotimefloat_implemented}
type
tmach_timebase_info = packed record
numer: longint;
denom: longint;
end;
pmach_timebase_info = ^tmach_timebase_info;
function mach_absolute_time: int64; cdecl; external;
function mach_timebase_info(info: pmach_timebase_info): integer; cdecl; external;
var
timebase_info: tmach_timebase_info;
function monotimefloat:float;
var
i:int64;
begin
if timebase_info.denom = 0 then begin
mach_timebase_info(@timebase_info);
end;
i := mach_absolute_time;
result := (i * timebase_info.numer div timebase_info.denom) / 1000000000.0;
end;
{$endif} {darwin, mac OS X}
{$ifndef monotimefloat_implemented} {fallback}
function monotimefloat:extended;
begin
result := unixtimefloat;
end;
{$endif} {monotimefloat fallback}
function unixtimeint:tunixtimeint;
var
tv:ttimeval;
begin
gettimeofday(tv);
result := tv.tv_sec;
end;
{------------------------------ end of *nix/freepascal section}
{$else} {delphi 3}
{------------------------------ windows/delphi code to read time}
{simulate gettimeofday on windows so one can always use gettimeofday if preferred}
procedure gettimeofday(var tv:ttimeval);
var
e:extended;
begin
e := unixtimefloat;
tv.tv_sec := round(int(e));
tv.tv_usec := trunc(frac(e)*1000000);
{just in case}
if (tv.tv_usec < 0) then tv.tv_usec := 0;
if (tv.tv_usec > 999999) then tv.tv_usec := 999999;
end;
{
time float: gettickcount
resolution: 9x: ~55 ms NT: 1/64th of a second
guarantees: continuous without any jumps
frequency base: same as system clock.
epoch: system boot
note: if called more than once per 49.7 days, 32 bits wrapping is compensated for and it keeps going on.
note: i handle the timestamp as signed integer, but with the wrap compensation that works as well, and is faster
}
function mmtimefloat:float;
const
wrapduration=highdwordconst * 0.001;
var
i:integer;
temp:float;
begin
i := gettickcount; {timegettime}
if i < mmtime_last then begin
mmtime_wrapadd := mmtime_wrapadd + wrapduration;
end;
mmtime_last := i;
result := mmtime_wrapadd + i * 0.001;
if (ticks_freq <> 0) and ticks_freq_known then begin
{the value we get is rounded to 1 ms, but the ticks are not a multiple of 1 ms
this makes the value noisy. use the known ticks frequency to restore the original value}
temp := int((result / ticks_freq)+0.5) * ticks_freq;
{if the known ticks freq is wrong (can happen), disable the un-rounding behavior
this will be a bit less accurate but it prevents problems}
if abs(temp - result) > 0.002 then begin
ticks_freq := 0;
end else result := temp;
end;
end;
procedure measure_ticks_freq;
var
f,g:float;
o:tosversioninfo;
isnt:boolean;
{ is9x:boolean;}
adjust1,adjust2:cardinal;
adjustbool:longbool;
begin
if (performancecountfreq = 0) then qpctimefloat;
ticks_freq_known := false;
settc;
f := mmtimefloat;
repeat g := mmtimefloat until g > f;
unsettc;
f := g - f;
fillchar(o,sizeof(o),0);
o.dwOSVersionInfoSize := sizeof(o);
getversionex(o);
isnt := o.dwPlatformId = VER_PLATFORM_WIN32_NT;
{ is9x := o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;}
ticks_freq2 := f;
mmtime_synchedqpc := false;
if (isnt and (o.dwMajorVersion >= 5)) then begin
{windows 2000 and later: query tick rate from OS in 100 ns units
typical rates: XP: 156250 or 100144, windows 7: 156001}
if GetSystemTimeAdjustment(adjust1,adjust2,adjustbool) then begin
ticks_freq := adjust1 / 10000000.0;
ticks_freq_known := true;
mmtime_synchedqpc := false;
end;
end;
{9x}
if (performancecountfreq = 1193182) and (f >= 0.050) and (f <= 0.060) then begin
ticks_freq_known := true;
ticks_freq := 65536 / (colorburst / 3);
mmtime_synchedqpc := true;
end;
ticks_freq_known := true;
if ticks_freq <> 0 then ticks_freq2 := ticks_freq;
// writeln(formatfloat('0.000000',ticks_freq));
end;
{
time float: QueryPerformanceCounter
resolution: <1us
guarantees: can have forward jumps depending on hardware. can have forward and backwards jitter on dual core.
frequency base: on NT, not the system clock, drifts compared to it.
epoch: system boot
}
function qpctimefloat:extended;
var
p:packed record
lowpart:longint;
highpart:longint
end;
p2:tlargeinteger absolute p;
e:extended;
begin
if performancecountfreq = 0 then begin
QueryPerformancefrequency(p2);
e := p.lowpart;
if e < 0 then e := e + highdwordconst;
performancecountfreq := ((p.highpart*highdwordconst)+e);
end;
queryperformancecounter(p2);
e := p.lowpart;
if e < 0 then e := e + highdwordconst;
result := ((p.highpart*highdwordconst)+e)/performancecountfreq;
end;
{
time float: QPC locked to gettickcount
resolution: <1us
guarantees: continuous without any jumps
frequency base: same as system clock.
epoch: system boot
}
function mmqpctimefloat:float;
const
maxretries=5;
margin=0.002;
var
{ jump:float;}
mm,f,qpc,newdrift:float;
qpcjumped:boolean;
a,b:integer;
{ retrycount:integer;}
begin
if not ticks_freq_known then measure_ticks_freq;
{ retrycount := maxretries;}
qpc := qpctimefloat;
mm := mmtimefloat;
f := (qpc - mmtime_lastsyncqpc) * mmtime_drift + mmtime_lastsyncmm;
//writeln('XXXX ',formatfloat('0.000000',qpc-mm));
qpcjumped := ((f-mm) > ticks_freq2+margin) or ((f-mm) < -margin);
// if qpcjumped then writeln('qpc jumped ',(f-mm));
if ((qpc > mmtime_nextdriftcorrection) and not mmtime_synchedqpc) or qpcjumped then begin
mmtime_nextdriftcorrection := qpc + 1;
repeat
mmtime_prev_drift := mmtime_drift;
mmtime_prev_lastsyncmm := mmtime_lastsyncmm;
mmtime_prev_lastsyncqpc := mmtime_lastsyncqpc;
mm := mmtimefloat;
{ dec(retrycount);}
settc;
result := qpctimefloat;
f := mmtimefloat;
repeat
if f = mm then result := qpctimefloat;
f := mmtimefloat
until f > mm;
qpc := qpctimefloat;
unsettc;
if (qpc > result + 0.0001) then begin
continue;
end;
mm := f;
if (mmtime_lastsyncqpc <> 0) and not qpcjumped then begin
newdrift := (mm - mmtime_lastsyncmm) / (qpc - mmtime_lastsyncqpc);
mmtime_drift := newdrift;
{ writeln('raw drift: ',formatfloat('0.00000000',mmtime_drift));}
move(mmtime_driftavg[0],mmtime_driftavg[1],sizeof(mmtime_driftavg[0])*high(mmtime_driftavg));
mmtime_driftavg[0] := mmtime_drift;
{ write('averaging drift ',formatfloat('0.00000000',mmtime_drift),' -> ');}
{ mmtime_drift := 0;}
b := 0;
for a := 0 to high(mmtime_driftavg) do begin
if mmtime_driftavg[a] <> 0 then inc(b);
{ mmtime_drift := mmtime_drift + mmtime_driftavg[a];}
end;
{ mmtime_drift := mmtime_drift / b;}
a := 5;
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;
mmtime_nextdriftcorrection := qpc + a;
if (b >= 2) then warmup_finished := true;
{ writeln(formatfloat('0.00000000',mmtime_drift));}
if mmtime_synchedqpc then mmtime_drift := 1;
end;
mmtime_lastsyncqpc := qpc;
mmtime_lastsyncmm := mm;
{ writeln(formatfloat('0.00000000',mmtime_drift));}
break;
until false;
qpc := qpctimefloat;
result := (qpc - mmtime_lastsyncqpc) * mmtime_drift + mmtime_lastsyncmm;
{f := (qpc - mmtime_prev_lastsyncqpc) * mmtime_prev_drift + mmtime_prev_lastsyncmm;
jump := result-f;
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)));}
f := result;
end;
result := f;
if (result < mmtime_lastresult) then result := mmtime_lastresult;
mmtime_lastresult := result;
end;
{ free pascals tsystemtime is incompatible with windows api calls
so we declare it ourselves - plugwash
}
{$ifdef fpc}
type
TSystemTime = record
wYear: Word;
wMonth: Word;
wDayOfWeek: Word;
wDay: Word;
wHour: Word;
wMinute: Word;
wSecond: Word;
wMilliseconds: Word;
end;
{$endif}
function Date_utc: extended;
var
SystemTime: TSystemTime;
begin
{$ifdef fpc}
GetsystemTime(@SystemTime);
{$else}
GetsystemTime(SystemTime);
{$endif}
with SystemTime do Result := EncodeDate(wYear, wMonth, wDay);
end;
function Time_utc: extended;
var
SystemTime: TSystemTime;
begin
{$ifdef fpc}
GetsystemTime(@SystemTime);
{$else}
GetsystemTime(SystemTime);
{$endif}
with SystemTime do
Result := EncodeTime(wHour, wMinute, wSecond, wMilliSeconds);
end;
function Now_utc: extended;
begin
Result := round(Date_utc) + Time_utc;
end;
function unixtimefloat_systemtime:float;
begin
{result := oletounixfloat(now_utc);}
{this method gives exactly the same result with extended precision, but is less sensitive to float rounding in theory}
result := oletounixfloat(int(date_utc+0.5))+time_utc*86400;
end;
function monotimefloat:extended;
begin
result := mmqpctimefloat;
end;
var
GetSystemTimePreciseAsFileTime:procedure(var v:tfiletime); stdcall;
win8inited:boolean;
procedure initwin8;
var
dllhandle:thandle;
begin
win8inited := true;
dllhandle := loadlibrary('kernel32.dll');
if (dllhandle <> 0) then begin
GetSystemTimePreciseAsFileTime := getprocaddress(dllhandle,'GetSystemTimePreciseAsFileTime');
end;
end;
function unixtimefloat_win8:float;
var
ft:tfiletime;
i:int64 absolute ft;
begin
GetSystemTimePreciseAsFileTime(ft);
{change from windows 1601-01-01 to unix 1970-01-01.
use integer math for this, to preserve precision}
dec(i, 116444736000000000);
result := (i / 10000000);
end;
function unixtimefloat:float;
const
margin = 0.0012;
var
f,g,h:float;
begin
if not win8inited then initwin8;
if assigned(@GetSystemTimePreciseAsFileTime) then begin
result := unixtimefloat_win8;
exit;
end;
result := monotimefloat+timefloatbias;
f := result-unixtimefloat_systemtime;
if ((f > ticks_freq2+margin) or (f < -margin)) or (timefloatbias = 0) then begin
// writeln('unixtimefloat init');
f := unixtimefloat_systemtime;
settc;
repeat g := unixtimefloat_systemtime; h := monotimefloat until g > f;
unsettc;
timefloatbias := g-h;
result := unixtimefloat;
end;
{for small changes backwards, guarantee no steps backwards}
if (result <= lastunixtimefloat) and (result > lastunixtimefloat-1.5) then result := lastunixtimefloat + 0.0000001;
lastunixtimefloat := result;
end;
function unixtimeint:tunixtimeint;
begin
result := trunc(unixtimefloat);
end;
{$endif}
{-----------------------------------------------end of platform specific}
function wintimefloat:float;
begin
result := monotimefloat;
end;
function irctimefloat:float;
begin
result := unixtimefloat+settimebias;
end;
function irctimeint:tunixtimeint;
begin
result := unixtimeint+settimebias;
end;
procedure settime(newtime:tunixtimeint);
var
a:tunixtimeint;
begin
a := irctimeint-settimebias;
if newtime = 0 then settimebias := 0 else settimebias := newtime-a;
irctime := irctimeint;
end;
procedure timehandler;
begin
if unixtime = 0 then init;
unixtime := unixtimeint;
irctime := irctimeint;
if unixtime and 63 = 0 then begin
{update everything, apply timezone changes, clock changes, etc}
gettimezone;
timefloatbias := 0;
unixtime := unixtimeint;
irctime := irctimeint;
end;
end;
procedure gettimezone;
var
{$ifdef UNIX}
{$ifndef ver1_9_4}
{$ifndef ver1_0}
{$define above194}
{$endif}
{$endif}
{$ifndef above194}
hh,mm,ss:word;
{$endif}
{$endif}
l:integer;
begin
{$ifdef UNIX}
{$ifdef above194}
timezone := tzseconds;
{$else}
gettime(hh,mm,ss);
timezone := (longint(hh) * 3600 + mm * 60 + ss) - (unixtimeint mod 86400);
{$endif}
{$else}
timezone := round((now-now_utc)*86400);
{$endif}
while timezone > 43200 do dec(timezone,86400);
while timezone < -43200 do inc(timezone,86400);
if timezone >= 0 then timezonestr := '+' else timezonestr := '-';
l := abs(timezone) div 60;
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);
end;
function timestrshort(i:tunixtimeint):string;
const
weekday:array[0..6] of string[4]=('Thu','Fri','Sat','Sun','Mon','Tue','Wed');
month:array[0..11] of string[4]=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var
y,m,d,h,min,sec,ms:word;
t:tdatetime;
begin
t := unixtoole(i+timezone);
decodedate(t,y,m,d);
decodetime(t,h,min,sec,ms);
result := weekday[(i+timezone) div 86400 mod 7]+' '+month[m-1]+' '+inttostr(d)+' '+
inttostr(h div 10)+inttostr(h mod 10)+':'+inttostr(min div 10)+inttostr(min mod 10)+':'+inttostr(sec div 10)+inttostr(sec mod 10)+' '+
inttostr(y);
end;
function timestring(i:tunixtimeint):string;
const
weekday:array[0..6] of string[10]=('Thursday','Friday','Saturday','Sunday','Monday','Tuesday','Wednesday');
month:array[0..11] of string[10]=('January','February','March','April','May','June','July','August','September','October','November','December');
var
y,m,d,h,min,sec,ms:word;
t:tdatetime;
begin
t := unixtoole(i+timezone);
decodedate(t,y,m,d);
decodetime(t,h,min,sec,ms);
result := weekday[(i+timezone) div 86400 mod 7]+' '+month[m-1]+' '+inttostr(d)+' '+inttostr(y)+' -- '+
inttostr(h div 10)+inttostr(h mod 10)+':'+inttostr(min div 10)+inttostr(min mod 10)+':'+inttostr(sec div 10)+inttostr(sec mod 10)+' '+
timezonestr;
end;
function timestriso(i:tunixtimeint):string;
var
y,m,d,h,min,sec,ms:word;
t:tdatetime;
begin
t := unixtoole(i+timezone);
decodedate(t,y,m,d);
decodetime(t,h,min,sec,ms);
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);
end;
function timestrisoutc(i:float):string;
var
y,m,d,h,min,sec,ms:word;
t:tdatetime;
fr:float;
begin
t := unixtoole(i);
decodedate(t,y,m,d);
decodetime(t,h,min,sec,ms);
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);
fr := frac(i);
result := result + '.'+
inttostr(trunc(fr*10) mod 10)+
inttostr(trunc(fr*100) mod 10)+
inttostr(trunc(fr*1000) mod 10)+
inttostr(trunc(fr*10000) mod 10)+
inttostr(trunc(fr*100000) mod 10)+
inttostr(trunc(fr*1000000) mod 10)+'Z';
end;
procedure init;
begin
{$ifdef mswindows}timebeginperiod(1);{$endif} //ensure stable unchanging clock
fillchar(mmtime_driftavg,sizeof(mmtime_driftavg),0);
settimebias := 0;
gettimezone;
unixtime := unixtimeint;
irctime := irctimeint;
end;
initialization init;
end.