16 #include "osdep/osdep.h" /* For win32 gettimeofday() stub */
17 #include "util/error.h"
18 #include "util/time.h"
20 /* Get the current time.
21 * It attempts to use available functions, granularity
22 * may be as worse as 1 second if time() is used. */
24 timeval_now(timeval_T
*t
)
26 #ifdef HAVE_GETTIMEOFDAY
29 gettimeofday(&tv
, NULL
);
30 t
->sec
= (long) tv
.tv_sec
;
31 t
->usec
= (long) tv
.tv_usec
;
33 #ifdef HAVE_CLOCK_GETTIME
36 clock_gettime(CLOCK_REALTIME
, &ts
);
37 t
->sec
= (long) ts
.tv_sec
;
38 t
->usec
= (long) ts
.tv_nsec
/ 1000;
40 t
->sec
= (long) time(NULL
);
48 /* Subtract an interval to a timeval, it ensures that
49 * result is never negative. */
51 timeval_sub_interval(timeval_T
*t
, timeval_T
*interval
)
53 t
->sec
-= interval
->sec
;
60 t
->usec
-= interval
->usec
;
76 timeval_sub(timeval_T
*res
, timeval_T
*older
, timeval_T
*newer
)
78 res
->sec
= newer
->sec
- older
->sec
;
79 res
->usec
= newer
->usec
- older
->usec
;
81 while (res
->usec
< 0) {
90 timeval_add(timeval_T
*res
, timeval_T
*base
, timeval_T
*t
)
92 res
->sec
= base
->sec
+ t
->sec
;
93 res
->usec
= base
->usec
+ t
->usec
;
95 while (res
->usec
>= 1000000) {
104 timeval_add_interval(timeval_T
*t
, timeval_T
*interval
)
106 t
->sec
+= interval
->sec
;
107 t
->usec
+= interval
->usec
;
109 while (t
->usec
>= 1000000) {
118 timeval_from_double(timeval_T
*t
, double x
)
121 t
->usec
= (long) ((x
- (double) t
->sec
) * 1000000);
127 timeval_from_milliseconds(timeval_T
*t
, milliseconds_T milliseconds
)
129 long ms
= (long) milliseconds
;
132 t
->usec
= (ms
% 1000) * 1000;
138 timeval_from_seconds(timeval_T
*t
, long seconds
)
149 assert(sec
>= 0 && sec
< LONG_MAX
/ 1000L);
150 if_assert_failed
return (milliseconds_T
) (LONG_MAX
/ 1000L);
152 return (milliseconds_T
) (sec
* 1000L);
156 add_ms_to_ms(milliseconds_T a
, milliseconds_T b
)
161 assert(la
>= 0 && lb
>= 0 && lb
< LONG_MAX
- la
);
162 if_assert_failed
return (milliseconds_T
) (LONG_MAX
/ 1000L);
164 return (milliseconds_T
) (la
+ lb
);
168 mult_ms(milliseconds_T a
, long lb
)
172 assert(la
>= 0 && lb
>= 0 && la
< LONG_MAX
/ lb
);
173 if_assert_failed
return (milliseconds_T
) (LONG_MAX
/ 1000L);
175 return (milliseconds_T
) (la
* lb
);
179 timeval_to_milliseconds(timeval_T
*t
)
181 milliseconds_T a
= sec_to_ms(t
->sec
);
182 milliseconds_T b
= (milliseconds_T
) (t
->usec
/ 1000L);
184 return add_ms_to_ms(a
, b
);
188 timeval_to_seconds(timeval_T
*t
)
190 return t
->sec
+ t
->usec
/ 1000000L;
194 timeval_is_positive(timeval_T
*t
)
196 return (t
->sec
> 0 || (t
->sec
== 0 && t
->usec
> 0));
199 /* Be sure timeval is not negative. */
201 timeval_limit_to_zero_or_one(timeval_T
*t
)
203 if (t
->sec
< 0) t
->sec
= 0;
204 if (t
->usec
< 0) t
->usec
= 0;
205 /* Under Windows I got 300 seconds timeout, so 1 second should not hurt --witekfl */
206 if (t
->sec
> 1) t
->sec
= 1;
209 /* Returns 1 if t1 > t2
213 timeval_cmp(timeval_T
*t1
, timeval_T
*t2
)
215 if (t1
->sec
> t2
->sec
) return 1;
216 if (t1
->sec
< t2
->sec
) return -1;
218 return t1
->usec
- t2
->usec
;
222 timeval_div_off_t(off_t n
, timeval_T
*t
)
224 longlong ln
= 1000 * (longlong
) n
; /* FIXME: off_t -> longlong ??? Find a better way. --Zas */
225 longlong lsec
= 1000 * (longlong
) t
->sec
;
226 int lusec
= t
->usec
/ 1000;
229 return (ln
/ (lsec
+ lusec
));