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