Statistics
| Branch: | Tag: | Revision:

humotion / src / timestamp.cpp @ e19ad051

History | View | Annotate | Download (1.965 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
    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 32327f15 Simon Schulz
void Timestamp::set(int32_t _sec, int32_t _nsec){
30
    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
bool Timestamp::operator< (Timestamp &cmp){
54
    if (sec < cmp.sec){
55
        return true;
56
    }else if (sec > cmp.sec){
57
        return false;
58
    }else{ //(a.sec == b.sec)
59
        //seconds are equal, check nsec:
60
        return (nsec < cmp.nsec);
61
    }
62
}
63
64
bool Timestamp::operator>= (Timestamp &cmp){
65
    if (sec > cmp.sec){
66
        return true;
67
    }else if (sec < cmp.sec){
68
        return false;
69
    }else{ //(a.sec == b.sec)
70
        //seconds are equal, check nsec:
71
        return (nsec >= cmp.nsec);
72
    }
73
}
74
75
bool Timestamp::operator> (Timestamp &cmp){
76
    if (sec > cmp.sec){
77
        return true;
78
    }else if (sec < cmp.sec){
79
        return false;
80
    }else{ //(a.sec == b.sec)
81
        //seconds are equal, check nsec:
82
        return (nsec > cmp.nsec);
83
    }
84
}
85
86
bool Timestamp::operator== (Timestamp &cmp){
87
    return (sec == cmp.sec) && (nsec == cmp.nsec);
88
}
89
90
bool Timestamp::operator!= (Timestamp &cmp){
91
    return !(*this == cmp);
92
}