-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeval.c
68 lines (56 loc) · 1.48 KB
/
timeval.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Copyright (c) Regents of The University of Michigan
* See COPYING.
*/
#include <sys/time.h>
#include "timeval.h"
int
tv_add(struct timeval *tp1, struct timeval *tp2, struct timeval *result) {
/* Add */
result->tv_sec = tp1->tv_sec + tp2->tv_sec;
result->tv_usec = tp1->tv_usec + tp2->tv_usec;
/* Check and correct usec overflow */
if (result->tv_usec >= 1000000) {
result->tv_sec += 1;
result->tv_usec -= 1000000;
}
return 0;
}
int
tv_sub(struct timeval *tp1, struct timeval *tp2, struct timeval *result) {
result->tv_sec = tp1->tv_sec;
result->tv_usec = tp1->tv_usec;
/* Borrow */
if (tp1->tv_usec < tp2->tv_usec) {
result->tv_sec -= 1;
result->tv_usec += 1000000;
}
/* Subtract */
result->tv_sec = result->tv_sec - tp2->tv_sec;
result->tv_usec = result->tv_usec - tp2->tv_usec;
/* Check for negative result */
if (result->tv_sec < 0) {
result->tv_sec = 0;
result->tv_usec = 0;
return (-1);
}
return 0;
}
int
tv_lt(struct timeval *tp1, struct timeval *tp2) {
if ((tp1->tv_sec < tp2->tv_sec) ||
((tp1->tv_sec == tp2->tv_sec) && (tp1->tv_usec < tp2->tv_usec))) {
return 1;
} else {
return 0;
}
}
int
tv_gt(struct timeval *tp1, struct timeval *tp2) {
if ((tp1->tv_sec < tp2->tv_sec) ||
((tp1->tv_sec == tp2->tv_sec) && (tp1->tv_usec < tp2->tv_usec))) {
return 0;
} else {
return 1;
}
}