Line | Count | Source |
1 | | /* |
2 | | * Time calculation functions. |
3 | | * |
4 | | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU General Public License |
8 | | * as published by the Free Software Foundation; either version |
9 | | * 2 of the License, or (at your option) any later version. |
10 | | * |
11 | | */ |
12 | | |
13 | | #include <sys/time.h> |
14 | | |
15 | | #include <haproxy/api.h> |
16 | | #include <haproxy/time.h> |
17 | | |
18 | | |
19 | | /* |
20 | | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
21 | | */ |
22 | | struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms) |
23 | 0 | { |
24 | 0 | tv->tv_usec = from->tv_usec + (ms % 1000) * 1000; |
25 | 0 | tv->tv_sec = from->tv_sec + (ms / 1000); |
26 | 0 | while (tv->tv_usec >= 1000000) { |
27 | 0 | tv->tv_usec -= 1000000; |
28 | 0 | tv->tv_sec++; |
29 | 0 | } |
30 | 0 | return tv; |
31 | 0 | } |
32 | | |
33 | | /* |
34 | | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
35 | | * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that. |
36 | | */ |
37 | | int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2) |
38 | 0 | { |
39 | 0 | return __tv_ms_cmp(tv1, tv2); |
40 | 0 | } |
41 | | |
42 | | /* |
43 | | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
44 | | * assuming that TV_ETERNITY is greater than everything. |
45 | | */ |
46 | | int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2) |
47 | 0 | { |
48 | 0 | return __tv_ms_cmp2(tv1, tv2); |
49 | 0 | } |
50 | | |
51 | | /* |
52 | | * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2, |
53 | | * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is |
54 | | * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace |
55 | | * occurrences of (tv_ms_cmp2(tv,now) <= 0). |
56 | | */ |
57 | | int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2) |
58 | 0 | { |
59 | 0 | return __tv_ms_le2(tv1, tv2); |
60 | 0 | } |
61 | | |
62 | | /* |
63 | | * returns the remaining time between tv1=now and event=tv2 |
64 | | * if tv2 is passed, 0 is returned. |
65 | | * Must not be used when either argument is eternity. |
66 | | */ |
67 | | unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2) |
68 | 0 | { |
69 | 0 | return __tv_ms_remain(tv1, tv2); |
70 | 0 | } |
71 | | |
72 | | /* |
73 | | * returns the remaining time between tv1=now and event=tv2 |
74 | | * if tv2 is passed, 0 is returned. |
75 | | * Returns TIME_ETERNITY if tv2 is eternity. |
76 | | */ |
77 | | unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2) |
78 | 0 | { |
79 | 0 | if (tv_iseternity(tv2)) |
80 | 0 | return TIME_ETERNITY; |
81 | | |
82 | 0 | return __tv_ms_remain(tv1, tv2); |
83 | 0 | } |
84 | | |
85 | | /* |
86 | | * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2. |
87 | | * Must not be used when either argument is eternity. |
88 | | */ |
89 | | unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2) |
90 | 0 | { |
91 | 0 | return __tv_ms_elapsed(tv1, tv2); |
92 | 0 | } |
93 | | |
94 | | /* |
95 | | * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv> |
96 | | */ |
97 | | struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
98 | 0 | { |
99 | 0 | return __tv_add(tv, from, inc); |
100 | 0 | } |
101 | | |
102 | | /* |
103 | | * If <inc> is set, then add it to <from> and set the result to <tv>, then |
104 | | * return 1, otherwise return 0. It is meant to be used in if conditions. |
105 | | */ |
106 | | int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
107 | 0 | { |
108 | 0 | return __tv_add_ifset(tv, from, inc); |
109 | 0 | } |
110 | | |
111 | | /* |
112 | | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
113 | | * 0 is returned. The result is stored into tv. |
114 | | */ |
115 | | struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
116 | 0 | { |
117 | 0 | return __tv_remain(tv1, tv2, tv); |
118 | 0 | } |
119 | | |
120 | | /* |
121 | | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
122 | | * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is |
123 | | * eternity. |
124 | | */ |
125 | | struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
126 | 0 | { |
127 | 0 | return __tv_remain2(tv1, tv2, tv); |
128 | 0 | } |
129 | | |
130 | | /* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */ |
131 | | int _tv_isle(const struct timeval *tv1, const struct timeval *tv2) |
132 | 0 | { |
133 | 0 | return __tv_isle(tv1, tv2); |
134 | 0 | } |
135 | | |
136 | | /* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */ |
137 | | int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2) |
138 | 0 | { |
139 | 0 | return __tv_isgt(tv1, tv2); |
140 | 0 | } |
141 | | |
142 | | /* |
143 | | * Local variables: |
144 | | * c-indent-level: 8 |
145 | | * c-basic-offset: 8 |
146 | | * End: |
147 | | */ |