-
Notifications
You must be signed in to change notification settings - Fork 2
/
time.c
51 lines (41 loc) · 952 Bytes
/
time.c
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
#include "bolo.h"
#include <sys/time.h>
bolo_msec_t
bolo_ms(const struct timeval *tv)
{
struct timeval now;
if (!tv) {
if (gettimeofday(&now, NULL) != 0)
return INVALID_MS;
tv = &now;
}
return tv->tv_sec * 1000
+ tv->tv_usec / 1000;
}
bolo_msec_t
bolo_s(const struct timeval *tv)
{
struct timeval now;
if (!tv) {
if (gettimeofday(&now, NULL) != 0)
return INVALID_S;
tv = &now;
}
return tv->tv_sec;
}
#ifdef TEST
/* LCOV_EXCL_START */
TESTS {
struct timeval tv;
#define is_time(s,ms,real_ms,msg) do { \
tv.tv_sec = (s); tv.tv_usec = (ms) * 1000; \
is_unsigned(bolo_ms(&tv),(real_ms),msg ": bolo_ms()"); \
is_unsigned(bolo_s (&tv),(s), msg ": bolo_s()"); \
} while (0)
is_time(400, 0, 400000, "400s 0ms");
is_time(400, 700, 400700, "400s 700ms");
ok(bolo_ms(NULL) != INVALID_MS, "bolo_ms(NULL) returns now");
ok(bolo_s(NULL) != INVALID_S, "bolo_s(NULL) returns now");
}
/* LCOV_EXCL_STOP */
#endif