humotion / src / timestamp.cpp @ 2aa96942
History | View | Annotate | Download (3.154 KB)
1 | 0c8d22a5 | sschulz | /*
|
---|---|---|---|
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 | 8db9ba4f | Simon Schulz | #include <math.h> |
29 | 32327f15 | Simon Schulz | |
30 | 0c8d22a5 | sschulz | #include "humotion/timestamp.h" |
31 | |||
32 | using humotion::Timestamp;
|
||
33 | 32327f15 | Simon Schulz | |
34 | 0c8d22a5 | sschulz | Timestamp::Timestamp(void) {
|
35 | // init of an empty timestamp will be assigned to the current system time
|
||
36 | 8db9ba4f | Simon Schulz | set(now()); |
37 | 32327f15 | Simon Schulz | } |
38 | |||
39 | 0c8d22a5 | sschulz | Timestamp::Timestamp(uint32_t _sec, uint32_t _nsec) { |
40 | 32327f15 | Simon Schulz | set(_sec, _nsec); |
41 | } |
||
42 | |||
43 | 0c8d22a5 | sschulz | Timestamp::Timestamp(double dsec) {
|
44 | 8db9ba4f | Simon Schulz | double fsec, fnsec;
|
45 | 0c8d22a5 | sschulz | fnsec = modf(dsec , &fsec); |
46 | 8db9ba4f | Simon Schulz | sec = fsec; |
47 | nsec = fnsec * 1000000000.0; |
||
48 | } |
||
49 | |||
50 | 0c8d22a5 | sschulz | Timestamp Timestamp::now(void) {
|
51 | 8db9ba4f | Simon Schulz | struct timespec tp;
|
52 | clock_gettime(CLOCK_REALTIME, &tp); |
||
53 | e19ad051 | Simon Schulz | return Timestamp(tp.tv_sec, tp.tv_nsec);
|
54 | 8db9ba4f | Simon Schulz | } |
55 | |||
56 | 0c8d22a5 | sschulz | void Timestamp::set(uint32_t _sec, uint32_t _nsec) {
|
57 | 32327f15 | Simon Schulz | sec = _sec; |
58 | nsec = _nsec; |
||
59 | } |
||
60 | |||
61 | 0c8d22a5 | sschulz | void Timestamp::set(Timestamp a) {
|
62 | 8db9ba4f | Simon Schulz | set(a.sec, a.nsec); |
63 | } |
||
64 | |||
65 | 0c8d22a5 | sschulz | double Timestamp::to_seconds(void) { |
66 | return sec + (static_cast<double>(nsec))/1000000000.0; |
||
67 | 32327f15 | Simon Schulz | } |
68 | |||
69 | 0c8d22a5 | sschulz | bool Timestamp::operator<= (const Timestamp &cmp) const { |
70 | if (sec < cmp.sec) {
|
||
71 | 32327f15 | Simon Schulz | return true; |
72 | 0c8d22a5 | sschulz | } else if (sec > cmp.sec) { |
73 | 32327f15 | Simon Schulz | return false; |
74 | 0c8d22a5 | sschulz | } else { // (a.sec == b.sec) |
75 | // seconds are equal, check nsec:
|
||
76 | 32327f15 | Simon Schulz | return (nsec <= cmp.nsec);
|
77 | } |
||
78 | } |
||
79 | |||
80 | 0c8d22a5 | sschulz | bool Timestamp::is_null(void) { |
81 | caf7373f | Simon Schulz | return (sec == 0) && (nsec == 0); |
82 | } |
||
83 | |||
84 | 0c8d22a5 | sschulz | bool Timestamp::operator< (const Timestamp &cmp) const { |
85 | if (sec < cmp.sec) {
|
||
86 | 32327f15 | Simon Schulz | return true; |
87 | 0c8d22a5 | sschulz | } else if (sec > cmp.sec) { |
88 | 32327f15 | Simon Schulz | return false; |
89 | 0c8d22a5 | sschulz | } else { // (a.sec == b.sec) |
90 | // seconds are equal, check nsec:
|
||
91 | 32327f15 | Simon Schulz | return (nsec < cmp.nsec);
|
92 | } |
||
93 | } |
||
94 | |||
95 | 0c8d22a5 | sschulz | bool Timestamp::operator>= (const Timestamp &cmp) const { |
96 | if (sec > cmp.sec) {
|
||
97 | 32327f15 | Simon Schulz | return true; |
98 | 0c8d22a5 | sschulz | } else if (sec < cmp.sec) { |
99 | 32327f15 | Simon Schulz | return false; |
100 | 0c8d22a5 | sschulz | } else { // (a.sec == b.sec) |
101 | // seconds are equal, check nsec:
|
||
102 | 32327f15 | Simon Schulz | return (nsec >= cmp.nsec);
|
103 | } |
||
104 | } |
||
105 | |||
106 | 0c8d22a5 | sschulz | bool Timestamp::operator> (const Timestamp &cmp) const { |
107 | if (sec > cmp.sec) {
|
||
108 | 32327f15 | Simon Schulz | return true; |
109 | 0c8d22a5 | sschulz | } else if (sec < cmp.sec) { |
110 | 32327f15 | Simon Schulz | return false; |
111 | 0c8d22a5 | sschulz | } else { // (a.sec == b.sec) |
112 | // seconds are equal, check nsec:
|
||
113 | 32327f15 | Simon Schulz | return (nsec > cmp.nsec);
|
114 | } |
||
115 | } |
||
116 | |||
117 | 0c8d22a5 | sschulz | bool Timestamp::operator== (const Timestamp &cmp) const { |
118 | 32327f15 | Simon Schulz | return (sec == cmp.sec) && (nsec == cmp.nsec);
|
119 | } |
||
120 | |||
121 | 0c8d22a5 | sschulz | bool Timestamp::operator!= (const Timestamp &cmp) const { |
122 | 32327f15 | Simon Schulz | return !(*this == cmp); |
123 | } |