This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Indoor/data/Timestamp.h
frank da477866c1 worked on wifi-scanner for linux
new time-grouping for vap grouper
adjusted test-cases
minor changes/fixes/improvements
2017-10-11 14:00:24 +02:00

113 lines
2.9 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;
/** HIDDEN ctor. use factory methods */
explicit Timestamp(const int64_t ms) : _ms(ms) {;}
public:
/** empty ctor */
explicit Timestamp() : _ms(0) {;}
/** get timestamp from the given value which represents milliesconds */
static inline Timestamp fromMS(const int64_t ms) {return Timestamp(ms);}
/** get timestamp from the given value which represents seconds */
static inline Timestamp fromSec(const float sec) {return Timestamp(sec*1000);}
/** 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();
return Timestamp(millis);
}
/** get timestamp for the current system-time */
static inline Timestamp fromRunningTime() {
static Timestamp startup = fromUnixTime();
return fromUnixTime() - startup;
}
public:
/** get timestamp in milliseconds */
inline int64_t ms() const {return _ms;}
/** get timestamp in seconds */
inline float sec() const {return _ms/1000.0f;}
public:
/** is this timestamp zero? */
bool isZero() const {return _ms == 0;}
/** equal? */
bool operator == (const Timestamp& o) const {return _ms == o._ms;}
/** not equal? */
bool operator != (const Timestamp& o) const {return _ms != o._ms;}
/** smaller than the given one? */
bool operator < (const Timestamp& o) const {return _ms < o._ms;}
bool operator <= (const Timestamp& o) const {return _ms <= o._ms;}
/** greater than the given one? */
bool operator > (const Timestamp& o) const {return _ms > o._ms;}
bool operator >= (const Timestamp& o) const {return _ms >= o._ms;}
Timestamp operator - (const Timestamp& o) const {return Timestamp(_ms - o._ms);}
Timestamp& operator -= (const Timestamp& o) {_ms += o._ms; return *this;}
Timestamp operator + (const Timestamp& o) const {return Timestamp(_ms + o._ms);}
Timestamp& operator += (const Timestamp& o) {_ms += o._ms; return *this;}
template <typename T> Timestamp operator * (const T val) const {return Timestamp(_ms * val);}
template <typename T> Timestamp operator / (const T val) const {return Timestamp(_ms / 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