Revision 0c8d22a5 src/timestamp.cpp
src/timestamp.cpp | ||
---|---|---|
1 |
#include "timestamp.h" |
|
1 |
/* |
|
2 |
* This file is part of humotion |
|
3 |
* |
|
4 |
* Copyright(c) sschulz <AT> techfak.uni-bielefeld.de |
|
5 |
* http://opensource.cit-ec.de/projects/humotion |
|
6 |
* |
|
7 |
* This file may be licensed under the terms of the |
|
8 |
* GNU Lesser General Public License Version 3 (the ``LGPL''), |
|
9 |
* or (at your option) any later version. |
|
10 |
* |
|
11 |
* Software distributed under the License is distributed |
|
12 |
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either |
|
13 |
* express or implied. See the LGPL for the specific language |
|
14 |
* governing rights and limitations. |
|
15 |
* |
|
16 |
* You should have received a copy of the LGPL along with this |
|
17 |
* program. If not, go to http://www.gnu.org/licenses/lgpl.html |
|
18 |
* or write to the Free Software Foundation, Inc., |
|
19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* The development of this software was supported by the |
|
22 |
* Excellence Cluster EXC 277 Cognitive Interaction Technology. |
|
23 |
* The Excellence Cluster EXC 277 is a grant of the Deutsche |
|
24 |
* Forschungsgemeinschaft (DFG) in the context of the German |
|
25 |
* Excellence Initiative. |
|
26 |
*/ |
|
27 |
|
|
2 | 28 |
#include <math.h> |
3 | 29 |
|
4 |
using namespace std; |
|
5 |
using namespace humotion; |
|
30 |
#include "humotion/timestamp.h" |
|
31 |
|
|
32 |
using humotion::Timestamp; |
|
6 | 33 |
|
7 |
Timestamp::Timestamp(void){ |
|
8 |
//init of an empty timestamp will be assigned to the current system time |
|
34 |
Timestamp::Timestamp(void) {
|
|
35 |
// init of an empty timestamp will be assigned to the current system time
|
|
9 | 36 |
set(now()); |
10 | 37 |
} |
11 | 38 |
|
12 |
Timestamp::Timestamp(uint32_t _sec, uint32_t _nsec){ |
|
39 |
Timestamp::Timestamp(uint32_t _sec, uint32_t _nsec) {
|
|
13 | 40 |
set(_sec, _nsec); |
14 | 41 |
} |
15 | 42 |
|
16 |
Timestamp::Timestamp(double dsec){ |
|
43 |
Timestamp::Timestamp(double dsec) {
|
|
17 | 44 |
double fsec, fnsec; |
18 |
fnsec = modf (dsec , &fsec);
|
|
45 |
fnsec = modf(dsec , &fsec); |
|
19 | 46 |
sec = fsec; |
20 | 47 |
nsec = fnsec * 1000000000.0; |
21 | 48 |
} |
22 | 49 |
|
23 |
Timestamp Timestamp::now(void){ |
|
50 |
Timestamp Timestamp::now(void) {
|
|
24 | 51 |
struct timespec tp; |
25 | 52 |
clock_gettime(CLOCK_REALTIME, &tp); |
26 | 53 |
return Timestamp(tp.tv_sec, tp.tv_nsec); |
27 | 54 |
} |
28 | 55 |
|
29 |
void Timestamp::set(uint32_t _sec, uint32_t _nsec){ |
|
56 |
void Timestamp::set(uint32_t _sec, uint32_t _nsec) {
|
|
30 | 57 |
sec = _sec; |
31 | 58 |
nsec = _nsec; |
32 | 59 |
} |
33 | 60 |
|
34 |
void Timestamp::set(Timestamp a){ |
|
61 |
void Timestamp::set(Timestamp a) {
|
|
35 | 62 |
set(a.sec, a.nsec); |
36 | 63 |
} |
37 | 64 |
|
38 |
double Timestamp::to_seconds(void){ |
|
39 |
return sec + ((double)nsec)/1000000000.0;
|
|
65 |
double Timestamp::to_seconds(void) {
|
|
66 |
return sec + (static_cast<double>(nsec))/1000000000.0;
|
|
40 | 67 |
} |
41 | 68 |
|
42 |
bool Timestamp::operator<= (Timestamp &cmp){
|
|
43 |
if (sec < cmp.sec){ |
|
69 |
bool Timestamp::operator<= (const Timestamp &cmp) const {
|
|
70 |
if (sec < cmp.sec) {
|
|
44 | 71 |
return true; |
45 |
}else if (sec > cmp.sec){
|
|
72 |
} else if (sec > cmp.sec) {
|
|
46 | 73 |
return false; |
47 |
}else{ //(a.sec == b.sec)
|
|
48 |
//seconds are equal, check nsec: |
|
74 |
} else { // (a.sec == b.sec)
|
|
75 |
// seconds are equal, check nsec:
|
|
49 | 76 |
return (nsec <= cmp.nsec); |
50 | 77 |
} |
51 | 78 |
} |
52 | 79 |
|
53 |
bool Timestamp::is_null(void){ |
|
80 |
bool Timestamp::is_null(void) {
|
|
54 | 81 |
return (sec == 0) && (nsec == 0); |
55 | 82 |
} |
56 | 83 |
|
57 |
bool Timestamp::operator< (Timestamp &cmp){
|
|
58 |
if (sec < cmp.sec){ |
|
84 |
bool Timestamp::operator< (const Timestamp &cmp) const {
|
|
85 |
if (sec < cmp.sec) {
|
|
59 | 86 |
return true; |
60 |
}else if (sec > cmp.sec){
|
|
87 |
} else if (sec > cmp.sec) {
|
|
61 | 88 |
return false; |
62 |
}else{ //(a.sec == b.sec)
|
|
63 |
//seconds are equal, check nsec: |
|
89 |
} else { // (a.sec == b.sec)
|
|
90 |
// seconds are equal, check nsec:
|
|
64 | 91 |
return (nsec < cmp.nsec); |
65 | 92 |
} |
66 | 93 |
} |
67 | 94 |
|
68 |
bool Timestamp::operator>= (Timestamp &cmp){
|
|
69 |
if (sec > cmp.sec){ |
|
95 |
bool Timestamp::operator>= (const Timestamp &cmp) const {
|
|
96 |
if (sec > cmp.sec) {
|
|
70 | 97 |
return true; |
71 |
}else if (sec < cmp.sec){
|
|
98 |
} else if (sec < cmp.sec) {
|
|
72 | 99 |
return false; |
73 |
}else{ //(a.sec == b.sec)
|
|
74 |
//seconds are equal, check nsec: |
|
100 |
} else { // (a.sec == b.sec)
|
|
101 |
// seconds are equal, check nsec:
|
|
75 | 102 |
return (nsec >= cmp.nsec); |
76 | 103 |
} |
77 | 104 |
} |
78 | 105 |
|
79 |
bool Timestamp::operator> (Timestamp &cmp){
|
|
80 |
if (sec > cmp.sec){ |
|
106 |
bool Timestamp::operator> (const Timestamp &cmp) const {
|
|
107 |
if (sec > cmp.sec) {
|
|
81 | 108 |
return true; |
82 |
}else if (sec < cmp.sec){
|
|
109 |
} else if (sec < cmp.sec) {
|
|
83 | 110 |
return false; |
84 |
}else{ //(a.sec == b.sec)
|
|
85 |
//seconds are equal, check nsec: |
|
111 |
} else { // (a.sec == b.sec)
|
|
112 |
// seconds are equal, check nsec:
|
|
86 | 113 |
return (nsec > cmp.nsec); |
87 | 114 |
} |
88 | 115 |
} |
89 | 116 |
|
90 |
bool Timestamp::operator== (Timestamp &cmp){
|
|
117 |
bool Timestamp::operator== (const Timestamp &cmp) const {
|
|
91 | 118 |
return (sec == cmp.sec) && (nsec == cmp.nsec); |
92 | 119 |
} |
93 | 120 |
|
94 |
bool Timestamp::operator!= (Timestamp &cmp){
|
|
121 |
bool Timestamp::operator!= (const Timestamp &cmp) const {
|
|
95 | 122 |
return !(*this == cmp); |
96 | 123 |
} |
97 | 124 |
|
Also available in: Unified diff