/[lcore]/trunk/btime.pas
ViewVC logotype

Contents of /trunk/btime.pas

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style CRLF

No admin address has been configured">No admin address has been configured
ViewVC Help
Powered by ViewVC 1.1.22