humotion / src / timestamp.cpp @ 32327f15
History | View | Annotate | Download (1.696 KB)
1 |
#include "timestamp.h" |
---|---|
2 |
|
3 |
using namespace std; |
4 |
using namespace humotion; |
5 |
|
6 |
Timestamp::Timestamp(void){
|
7 |
//init of an empty timestamp will be assigned to the current system time
|
8 |
struct timespec tp;
|
9 |
clock_gettime(CLOCK_REALTIME, &tp); |
10 |
sec = tp.tv_sec; |
11 |
nsec = tp.tv_nsec; |
12 |
} |
13 |
|
14 |
Timestamp::Timestamp(int32_t _sec, int32_t _nsec){ |
15 |
set(_sec, _nsec); |
16 |
} |
17 |
|
18 |
void Timestamp::set(int32_t _sec, int32_t _nsec){
|
19 |
sec = _sec; |
20 |
nsec = _nsec; |
21 |
} |
22 |
|
23 |
double Timestamp::to_seconds(void){ |
24 |
return sec + ((double)nsec)/1000000000.0; |
25 |
} |
26 |
|
27 |
bool Timestamp::operator<= (Timestamp &cmp){ |
28 |
if (sec < cmp.sec){
|
29 |
return true; |
30 |
}else if (sec > cmp.sec){ |
31 |
return false; |
32 |
}else{ //(a.sec == b.sec) |
33 |
//seconds are equal, check nsec:
|
34 |
return (nsec <= cmp.nsec);
|
35 |
} |
36 |
} |
37 |
|
38 |
bool Timestamp::operator< (Timestamp &cmp){ |
39 |
if (sec < cmp.sec){
|
40 |
return true; |
41 |
}else if (sec > cmp.sec){ |
42 |
return false; |
43 |
}else{ //(a.sec == b.sec) |
44 |
//seconds are equal, check nsec:
|
45 |
return (nsec < cmp.nsec);
|
46 |
} |
47 |
} |
48 |
|
49 |
bool Timestamp::operator>= (Timestamp &cmp){ |
50 |
if (sec > cmp.sec){
|
51 |
return true; |
52 |
}else if (sec < cmp.sec){ |
53 |
return false; |
54 |
}else{ //(a.sec == b.sec) |
55 |
//seconds are equal, check nsec:
|
56 |
return (nsec >= cmp.nsec);
|
57 |
} |
58 |
} |
59 |
|
60 |
bool Timestamp::operator> (Timestamp &cmp){ |
61 |
if (sec > cmp.sec){
|
62 |
return true; |
63 |
}else if (sec < cmp.sec){ |
64 |
return false; |
65 |
}else{ //(a.sec == b.sec) |
66 |
//seconds are equal, check nsec:
|
67 |
return (nsec > cmp.nsec);
|
68 |
} |
69 |
} |
70 |
|
71 |
bool Timestamp::operator== (Timestamp &cmp){ |
72 |
return (sec == cmp.sec) && (nsec == cmp.nsec);
|
73 |
} |
74 |
|
75 |
bool Timestamp::operator!= (Timestamp &cmp){ |
76 |
return !(*this == cmp); |
77 |
} |
78 |
|