-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathltimevalstuff.pas
More file actions
78 lines (66 loc) · 1.97 KB
/
ltimevalstuff.pas
File metadata and controls
78 lines (66 loc) · 1.97 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
{ 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
----------------------------------------------------------------------------- }
{$ifdef fpc}
{$mode delphi}
{$endif}
unit ltimevalstuff;
interface
{$ifdef mswindows}
type
ttimeval = record
tv_sec : longint;
tv_usec : longint;
end;
{$else}
{$ifdef ver1_0}
uses linux;
{$else}
uses baseunix,unix,unixutil,sockets;
{$endif}
{$endif}
procedure tv_add(var tv:ttimeval;msec:integer);
function tv_compare(const tv1,tv2:ttimeval):boolean;
procedure tv_subtract(var tv:ttimeval;const tv2:ttimeval);
procedure msectotimeval(var tv:ttimeval;msec:integer);
//tv_invalidtimebig will always compare as greater than any valid timeval
//unfortunately unixstuff.inc hasn't worked it's magic yet so we
//have to ifdef this manually.
const
{$ifdef ver1_0}
tv_invalidtimebig : ttimeval = (sec:maxlongint;usec:maxlongint);
{$else}
tv_invalidtimebig : ttimeval = (tv_sec:maxlongint;tv_usec:maxlongint);
{$endif}
implementation
{$i unixstuff.inc}
{add nn msec to tv}
procedure tv_add(var tv:ttimeval;msec:integer);
begin
inc(tv.tv_usec,msec*1000);
inc(tv.tv_sec,tv.tv_usec div 1000000);
tv.tv_usec := tv.tv_usec mod 1000000;
end;
{tv1 >= tv2}
function tv_compare(const tv1,tv2:ttimeval):boolean;
begin
if tv1.tv_sec = tv2.tv_sec then begin
result := tv1.tv_usec >= tv2.tv_usec;
end else result := tv1.tv_sec > tv2.tv_sec;
end;
procedure tv_subtract(var tv:ttimeval;const tv2:ttimeval);
begin
dec(tv.tv_usec,tv2.tv_usec);
if tv.tv_usec < 0 then begin
inc(tv.tv_usec,1000000);
dec(tv.tv_sec)
end;
dec(tv.tv_sec,tv2.tv_sec);
end;
procedure msectotimeval(var tv:ttimeval;msec:integer);
begin
tv.tv_sec := msec div 1000;
tv.tv_usec := (msec mod 1000)*1000;
end;
end.