Statistics
| Branch: | Tag: | Revision:

humotion / src / timestamp.cpp @ 6491eeeb

History | View | Annotate | Download (3.154 KB)

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

    
28
#include <math.h>
29

    
30
#include "humotion/timestamp.h"
31

    
32
using humotion::Timestamp;
33

    
34
Timestamp::Timestamp(void) {
35
    // init of an empty timestamp will be assigned to the current system time
36
    set(now());
37
}
38

    
39
Timestamp::Timestamp(uint32_t _sec, uint32_t _nsec) {
40
    set(_sec, _nsec);
41
}
42

    
43
Timestamp::Timestamp(double dsec) {
44
    double fsec, fnsec;
45
    fnsec = modf(dsec , &fsec);
46
    sec  = fsec;
47
    nsec = fnsec * 1000000000.0;
48
}
49

    
50
Timestamp Timestamp::now(void) {
51
    struct timespec tp;
52
    clock_gettime(CLOCK_REALTIME, &tp);
53
    return Timestamp(tp.tv_sec, tp.tv_nsec);
54
}
55

    
56
void Timestamp::set(uint32_t _sec, uint32_t _nsec) {
57
    sec  = _sec;
58
    nsec = _nsec;
59
}
60

    
61
void Timestamp::set(Timestamp a) {
62
    set(a.sec, a.nsec);
63
}
64

    
65
double Timestamp::to_seconds(void) {
66
    return sec + (static_cast<double>(nsec))/1000000000.0;
67
}
68

    
69
bool Timestamp::operator<= (const Timestamp &cmp) const {
70
    if (sec < cmp.sec) {
71
        return true;
72
    } else if (sec > cmp.sec) {
73
        return false;
74
    } else {  // (a.sec == b.sec)
75
        // seconds are equal, check nsec:
76
        return (nsec <= cmp.nsec);
77
    }
78
}
79

    
80
bool Timestamp::is_null(void) {
81
    return (sec == 0) && (nsec == 0);
82
}
83

    
84
bool Timestamp::operator< (const Timestamp &cmp) const {
85
    if (sec < cmp.sec) {
86
        return true;
87
    } else if (sec > cmp.sec) {
88
        return false;
89
    } else {  // (a.sec == b.sec)
90
        // seconds are equal, check nsec:
91
        return (nsec < cmp.nsec);
92
    }
93
}
94

    
95
bool Timestamp::operator>= (const Timestamp &cmp) const {
96
    if (sec > cmp.sec) {
97
        return true;
98
    } else if (sec < cmp.sec) {
99
        return false;
100
    } else {  // (a.sec == b.sec)
101
        // seconds are equal, check nsec:
102
        return (nsec >= cmp.nsec);
103
    }
104
}
105

    
106
bool Timestamp::operator> (const Timestamp &cmp) const {
107
    if (sec > cmp.sec) {
108
        return true;
109
    } else if (sec < cmp.sec) {
110
        return false;
111
    } else {  // (a.sec == b.sec)
112
        // seconds are equal, check nsec:
113
        return (nsec > cmp.nsec);
114
    }
115
}
116

    
117
bool Timestamp::operator== (const Timestamp &cmp) const {
118
    return (sec == cmp.sec) && (nsec == cmp.nsec);
119
}
120

    
121
bool Timestamp::operator!= (const Timestamp &cmp) const {
122
    return !(*this == cmp);
123
}
124