141 lines
3.6 KiB
C++
141 lines
3.6 KiB
C++
#ifndef TIMESTAMP_H
|
|
#define TIMESTAMP_H
|
|
|
|
#include <chrono>
|
|
|
|
/**
|
|
* helper-class to handle timestamps
|
|
*/
|
|
struct Timestamp {
|
|
|
|
private:
|
|
|
|
/** internal timestamp in milliseconds */
|
|
//int64_t _ms;
|
|
int64_t _us;
|
|
|
|
/** hidden ctor. for internal methods only */
|
|
Timestamp(int64_t us) : _us(us) {;}
|
|
|
|
public:
|
|
|
|
/** empty ctor */
|
|
explicit Timestamp() : _us(0) {;}
|
|
|
|
|
|
/** get timestamp from the given value which represents microseconds */
|
|
static inline Timestamp fromUS(const int64_t us) {
|
|
Timestamp ts;
|
|
ts._us = us;
|
|
return ts;
|
|
}
|
|
/** get timestamp from the given value which represents milliesconds */
|
|
static inline Timestamp fromMS(const int64_t ms) {
|
|
Timestamp ts;
|
|
ts._us = ms * 1000;
|
|
return ts;
|
|
}
|
|
|
|
/** get timestamp from the given value which represents seconds */
|
|
static inline Timestamp fromSec(const float sec) {
|
|
Timestamp ts;
|
|
ts._us = static_cast<int64_t>(sec * 1000 * 1000);
|
|
return ts;
|
|
}
|
|
|
|
/** get timestamp from the given value which represents a sample rate in hz */
|
|
static inline Timestamp fromHz(const float hz) {
|
|
const float sec = 1.0f / hz;
|
|
return Timestamp::fromSec(sec);
|
|
}
|
|
|
|
/** get timestamp for the current unix-time */
|
|
static inline Timestamp fromUnixTime() {
|
|
auto now = std::chrono::system_clock::now();
|
|
auto duration = now.time_since_epoch();
|
|
//auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
|
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
|
|
return Timestamp::fromUS(micros);
|
|
}
|
|
|
|
/** get timestamp for the current system-time */
|
|
static inline Timestamp fromRunningTime() {
|
|
static Timestamp startup = fromUnixTime();
|
|
return fromUnixTime() - startup;
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/** get finest available value */
|
|
inline int64_t finest() const {return _us;}
|
|
|
|
/** get timestamp in microseconds */
|
|
inline int64_t us() const {return _us;}
|
|
|
|
/** get timestamp in milliseconds */
|
|
inline int64_t ms() const {return _us/1000;}
|
|
|
|
/** get timestamp in seconds */
|
|
inline float sec() const {return _us/1000.0f/1000.0f;}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/** is this timestamp zero? */
|
|
bool isZero() const {return _us == 0;}
|
|
|
|
/** equal? */
|
|
bool operator == (const Timestamp& o) const {return _us == o._us;}
|
|
|
|
/** not equal? */
|
|
bool operator != (const Timestamp& o) const {return _us != o._us;}
|
|
|
|
/** smaller than the given one? */
|
|
bool operator < (const Timestamp& o) const {return _us < o._us;}
|
|
bool operator <= (const Timestamp& o) const {return _us <= o._us;}
|
|
|
|
/** greater than the given one? */
|
|
bool operator > (const Timestamp& o) const {return _us > o._us;}
|
|
bool operator >= (const Timestamp& o) const {return _us >= o._us;}
|
|
|
|
|
|
|
|
Timestamp operator - (const Timestamp& o) const {return Timestamp(_us - o._us);}
|
|
Timestamp& operator -= (const Timestamp& o) {_us += o._us; return *this;}
|
|
|
|
Timestamp operator + (const Timestamp& o) const {return Timestamp(_us + o._us);}
|
|
Timestamp& operator += (const Timestamp& o) {_us += o._us; return *this;}
|
|
|
|
template <typename T> Timestamp operator * (const T val) const {return Timestamp(_us * val);}
|
|
|
|
template <typename T> Timestamp operator / (const T val) const {return Timestamp(_us / val);}
|
|
|
|
// /** cast to float */
|
|
// operator float () const {return sec();}
|
|
|
|
|
|
};
|
|
|
|
namespace std {
|
|
template<> class numeric_limits<Timestamp> {
|
|
public:
|
|
static Timestamp min() {
|
|
const int64_t minVal = std::numeric_limits<int64_t>::min();
|
|
return Timestamp::fromMS(minVal);
|
|
}
|
|
static Timestamp lowest() {
|
|
const int64_t minVal = std::numeric_limits<int64_t>::min();
|
|
return Timestamp::fromMS(minVal);
|
|
}
|
|
static Timestamp max() {
|
|
const int64_t maxVal = std::numeric_limits<int64_t>::max();
|
|
return Timestamp::fromMS(maxVal);
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif // TIMESTAMP_H
|