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 |
{$ifdef fpc}
|
7 |
{$mode delphi}
|
8 |
{$endif}
|
9 |
|
10 |
unit ltimevalstuff;
|
11 |
interface
|
12 |
|
13 |
{$ifdef mswindows}
|
14 |
type
|
15 |
ttimeval = record
|
16 |
tv_sec : longint;
|
17 |
tv_usec : longint;
|
18 |
end;
|
19 |
{$else}
|
20 |
{$ifdef ver1_0}
|
21 |
uses linux;
|
22 |
{$else}
|
23 |
uses baseunix,unix,unixutil,sockets;
|
24 |
{$endif}
|
25 |
{$endif}
|
26 |
|
27 |
|
28 |
procedure tv_add(var tv:ttimeval;msec:integer);
|
29 |
function tv_compare(const tv1,tv2:ttimeval):boolean;
|
30 |
procedure tv_subtract(var tv:ttimeval;const tv2:ttimeval);
|
31 |
procedure msectotimeval(var tv:ttimeval;msec:integer);
|
32 |
|
33 |
//tv_invalidtimebig will always compare as greater than any valid timeval
|
34 |
//unfortunately unixstuff.inc hasn't worked it's magic yet so we
|
35 |
//have to ifdef this manually.
|
36 |
const
|
37 |
{$ifdef ver1_0}
|
38 |
tv_invalidtimebig : ttimeval = (sec:maxlongint;usec:maxlongint);
|
39 |
{$else}
|
40 |
tv_invalidtimebig : ttimeval = (tv_sec:maxlongint;tv_usec:maxlongint);
|
41 |
{$endif}
|
42 |
implementation
|
43 |
|
44 |
{$i unixstuff.inc}
|
45 |
|
46 |
{add nn msec to tv}
|
47 |
procedure tv_add(var tv:ttimeval;msec:integer);
|
48 |
begin
|
49 |
inc(tv.tv_usec,msec*1000);
|
50 |
inc(tv.tv_sec,tv.tv_usec div 1000000);
|
51 |
tv.tv_usec := tv.tv_usec mod 1000000;
|
52 |
end;
|
53 |
|
54 |
{tv1 >= tv2}
|
55 |
function tv_compare(const tv1,tv2:ttimeval):boolean;
|
56 |
begin
|
57 |
if tv1.tv_sec = tv2.tv_sec then begin
|
58 |
result := tv1.tv_usec >= tv2.tv_usec;
|
59 |
end else result := tv1.tv_sec > tv2.tv_sec;
|
60 |
end;
|
61 |
|
62 |
procedure tv_subtract(var tv:ttimeval;const tv2:ttimeval);
|
63 |
begin
|
64 |
dec(tv.tv_usec,tv2.tv_usec);
|
65 |
if tv.tv_usec < 0 then begin
|
66 |
inc(tv.tv_usec,1000000);
|
67 |
dec(tv.tv_sec)
|
68 |
end;
|
69 |
dec(tv.tv_sec,tv2.tv_sec);
|
70 |
end;
|
71 |
|
72 |
procedure msectotimeval(var tv:ttimeval;msec:integer);
|
73 |
begin
|
74 |
tv.tv_sec := msec div 1000;
|
75 |
tv.tv_usec := (msec mod 1000)*1000;
|
76 |
end;
|
77 |
|
78 |
end. |