-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_util.h
65 lines (54 loc) · 1.82 KB
/
time_util.h
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
//
// Created by jeffrey on 3/16/15.
//
#ifndef _EPOLL_TCP_TIME_UTIL_H_
#define _EPOLL_TCP_TIME_UTIL_H_
#include <sys/time.h>
int inline compare_time(timeval t1, timeval t2);
bool inline operator<(timeval t1, timeval t2);
bool inline operator<=(timeval t1, timeval t2);
bool inline operator>(timeval t1, timeval t2);
bool inline operator>=(timeval t1, timeval t2);
bool inline operator==(timeval t1, timeval t2);
timeval inline operator+(timeval t1, timeval t2);
timeval inline operator-(timeval t1, timeval t2);
int inline compare_time(timeval t1, timeval t2) {
if (t1.tv_sec > t2.tv_sec) return 1;
else if (t1.tv_sec < t2.tv_sec) return -1;
else if (t1.tv_usec > t2.tv_usec) return 1;
else if (t1.tv_usec < t2.tv_usec) return -1;
else return 0;
}
bool inline operator<(timeval t1, timeval t2) {
return -1 == compare_time(t1, t2);
}
inline bool operator>(timeval t1, timeval t2) {
return 1 == compare_time(t1, t2);
}
inline bool operator>=(timeval t1, timeval t2) {
return -1 != compare_time(t1, t2);
}
inline bool operator<=(timeval t1, timeval t2) {
return 1 != compare_time(t1, t2);
}
inline bool operator==(timeval t1, timeval t2) {
return (t1.tv_usec == t2.tv_usec) && (t1.tv_sec == t2.tv_sec);
}
timeval inline operator+(timeval t1, timeval t2) {
if (t1.tv_usec + t2.tv_usec >= 1000000) {
return timeval{t1.tv_sec + t2.tv_sec + 1, t1.tv_usec + t2.tv_usec - 1000000};
}
else {
return timeval{t1.tv_sec + t2.tv_sec, t1.tv_usec + t1.tv_usec};
}
}
// This is undefined if t1 < t2
timeval inline operator-(timeval t1, timeval t2) {
if (t1.tv_usec < t2.tv_usec) {
return timeval{t1.tv_sec - t2.tv_sec - 1, t1.tv_usec - t2.tv_usec + 1000000};
}
else {
return timeval{t1.tv_sec - t2.tv_sec, t1.tv_usec - t1.tv_usec};
}
}
#endif //_EPOLL_TCP_TIME_UTIL_H_