-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeutils.h
62 lines (52 loc) · 1.6 KB
/
timeutils.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
/**
* @file time.h
* @brief Utilities for dealing with time.
*
*/
#ifndef __TIME_H
#define __TIME_H
#include <time.h>
class Time {
timespec t;
public:
static const long BILLION = 1000000000;
Time(){
clock_gettime(CLOCK_MONOTONIC,&t);
}
Time(double tt){
double secs;
double frac = modf(tt,&secs);
t.tv_sec = (long)secs;
t.tv_nsec = (long)(frac*BILLION);
}
inline friend Time operator+(Time lhs,const Time& rhs){
lhs.t.tv_sec += rhs.t.tv_sec;
lhs.t.tv_nsec += rhs.t.tv_nsec;
while(lhs.t.tv_nsec>BILLION){
lhs.t.tv_nsec-=BILLION;
lhs.t.tv_sec++;
}
return lhs;
}
inline friend double operator-(const Time& lhs, const Time& rhs){
timespec temp;
if ((lhs.t.tv_nsec-rhs.t.tv_nsec)<0) {
temp.tv_sec = lhs.t.tv_sec-rhs.t.tv_sec-1;
temp.tv_nsec = BILLION+lhs.t.tv_nsec-rhs.t.tv_nsec;
} else {
temp.tv_sec = lhs.t.tv_sec-rhs.t.tv_sec;
temp.tv_nsec = lhs.t.tv_nsec-rhs.t.tv_nsec;
}
double d = temp.tv_sec;
double ns = temp.tv_nsec;
d += ns*1e-9;
return d;
}
inline friend bool operator< (const Time& lhs, const Time& rhs){
return (lhs-rhs)<0;
}
inline friend bool operator> (const Time& lhs, const Time& rhs){ return rhs < lhs; }
inline friend bool operator<=(const Time& lhs, const Time& rhs){ return !(lhs > rhs); }
inline friend bool operator>=(const Time& lhs, const Time& rhs){ return !(lhs < rhs); }
};
#endif /* __TIME_H */