humotion / src / timestamp.cpp @ 8db9ba4f
History | View | Annotate | Download (1.99 KB)
1 | 32327f15 | Simon Schulz | #include "timestamp.h" |
---|---|---|---|
2 | 8db9ba4f | Simon Schulz | #include <math.h> |
3 | 32327f15 | Simon Schulz | |
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 | 8db9ba4f | Simon Schulz | set(now()); |
10 | 32327f15 | Simon Schulz | } |
11 | |||
12 | Timestamp::Timestamp(int32_t _sec, int32_t _nsec){ |
||
13 | set(_sec, _nsec); |
||
14 | } |
||
15 | |||
16 | 8db9ba4f | Simon Schulz | 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 | Timestamp res; |
||
25 | struct timespec tp;
|
||
26 | clock_gettime(CLOCK_REALTIME, &tp); |
||
27 | res.set(tp.tv_sec, tp.tv_nsec); |
||
28 | return res;
|
||
29 | } |
||
30 | |||
31 | 32327f15 | Simon Schulz | void Timestamp::set(int32_t _sec, int32_t _nsec){
|
32 | sec = _sec; |
||
33 | nsec = _nsec; |
||
34 | } |
||
35 | |||
36 | 8db9ba4f | Simon Schulz | void Timestamp::set(Timestamp a){
|
37 | set(a.sec, a.nsec); |
||
38 | } |
||
39 | |||
40 | 32327f15 | Simon Schulz | double Timestamp::to_seconds(void){ |
41 | return sec + ((double)nsec)/1000000000.0; |
||
42 | } |
||
43 | |||
44 | bool Timestamp::operator<= (Timestamp &cmp){ |
||
45 | if (sec < cmp.sec){
|
||
46 | return true; |
||
47 | }else if (sec > cmp.sec){ |
||
48 | return false; |
||
49 | }else{ //(a.sec == b.sec) |
||
50 | //seconds are equal, check nsec:
|
||
51 | return (nsec <= cmp.nsec);
|
||
52 | } |
||
53 | } |
||
54 | |||
55 | bool Timestamp::operator< (Timestamp &cmp){ |
||
56 | if (sec < cmp.sec){
|
||
57 | return true; |
||
58 | }else if (sec > cmp.sec){ |
||
59 | return false; |
||
60 | }else{ //(a.sec == b.sec) |
||
61 | //seconds are equal, check nsec:
|
||
62 | return (nsec < cmp.nsec);
|
||
63 | } |
||
64 | } |
||
65 | |||
66 | bool Timestamp::operator>= (Timestamp &cmp){ |
||
67 | if (sec > cmp.sec){
|
||
68 | return true; |
||
69 | }else if (sec < cmp.sec){ |
||
70 | return false; |
||
71 | }else{ //(a.sec == b.sec) |
||
72 | //seconds are equal, check nsec:
|
||
73 | return (nsec >= cmp.nsec);
|
||
74 | } |
||
75 | } |
||
76 | |||
77 | bool Timestamp::operator> (Timestamp &cmp){ |
||
78 | if (sec > cmp.sec){
|
||
79 | return true; |
||
80 | }else if (sec < cmp.sec){ |
||
81 | return false; |
||
82 | }else{ //(a.sec == b.sec) |
||
83 | //seconds are equal, check nsec:
|
||
84 | return (nsec > cmp.nsec);
|
||
85 | } |
||
86 | } |
||
87 | |||
88 | bool Timestamp::operator== (Timestamp &cmp){ |
||
89 | return (sec == cmp.sec) && (nsec == cmp.nsec);
|
||
90 | } |
||
91 | |||
92 | bool Timestamp::operator!= (Timestamp &cmp){ |
||
93 | return !(*this == cmp); |
||
94 | } |