Statistics
| Branch: | Tag: | Revision:

humotion / src / timestamp.cpp @ 372eec67

History | View | Annotate | Download (2.039 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 50784a28 Simon Schulz
Timestamp::Timestamp(uint32_t _sec, uint32_t _nsec){
13 32327f15 Simon Schulz
    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
    struct timespec tp;
25
    clock_gettime(CLOCK_REALTIME, &tp);
26 e19ad051 Simon Schulz
    return Timestamp(tp.tv_sec, tp.tv_nsec);
27 8db9ba4f Simon Schulz
}
28
29 50784a28 Simon Schulz
void Timestamp::set(uint32_t _sec, uint32_t _nsec){
30 32327f15 Simon Schulz
    sec  = _sec;
31
    nsec = _nsec;
32
}
33
34 8db9ba4f Simon Schulz
void Timestamp::set(Timestamp a){
35
    set(a.sec, a.nsec);
36
}
37
38 32327f15 Simon Schulz
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 caf7373f Simon Schulz
bool Timestamp::is_null(void){
54
    return (sec == 0) && (nsec == 0);
55
}
56
57 32327f15 Simon Schulz
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
}