#ifndef _H_TIMEFLOW
#define _H_TIMEFLOW
#include <sys/time.h>
#include <math.h> // for MAXFLOAT
namespace Security {
namespace Time {
class Interval {
friend class Absolute;
public:
Interval() { }
Interval(int seconds) { mValue = seconds; }
Interval(double seconds) { mValue = seconds; }
explicit Interval(time_t seconds) { mValue = seconds; }
Interval &operator += (Interval rel) { mValue += rel.mValue; return *this; }
Interval &operator -= (Interval rel) { mValue -= rel.mValue; return *this; }
Interval &operator *= (double f) { mValue *= f; return *this; }
Interval &operator /= (double f) { mValue /= f; return *this; }
bool operator < (Interval other) const { return mValue < other.mValue; }
bool operator <= (Interval other) const { return mValue <= other.mValue; }
bool operator > (Interval other) const { return mValue > other.mValue; }
bool operator >= (Interval other) const { return mValue >= other.mValue; }
bool operator == (Interval other) const { return mValue == other.mValue; }
bool operator != (Interval other) const { return mValue != other.mValue; }
double seconds() const { return mValue; }
double mSeconds() const { return mValue * 1E3; }
double uSeconds() const { return mValue * 1E6; }
struct timeval timevalInterval() const;
private:
double mValue;
};
class Absolute {
friend class Interval;
friend Interval operator - (Absolute, Absolute);
friend Absolute now();
friend Absolute bigBang();
friend Absolute heatDeath();
public:
Absolute() { } Absolute(time_t t) { mValue = t; } Absolute(const struct timeval &tv);
Absolute &operator += (Interval rel) { mValue += rel.mValue; return *this; }
Absolute &operator -= (Interval rel) { mValue -= rel.mValue; return *this; }
bool operator < (Absolute other) const { return mValue < other.mValue; }
bool operator <= (Absolute other) const { return mValue <= other.mValue; }
bool operator > (Absolute other) const { return mValue > other.mValue; }
bool operator >= (Absolute other) const { return mValue >= other.mValue; }
bool operator == (Absolute other) const { return mValue == other.mValue; }
bool operator != (Absolute other) const { return mValue != other.mValue; }
operator struct timeval() const;
operator time_t () const { return time_t(mValue); }
double internalForm() const { return mValue; }
private:
double mValue;
Absolute(double value) : mValue(value) { }
};
Absolute now();
Interval resolution(Absolute at);
inline Absolute bigBang() { return -MAXFLOAT; }
inline Absolute heatDeath() { return +MAXFLOAT; }
inline Interval operator + (Interval r, Interval r2) { r += r2; return r; }
inline Interval operator - (Interval r, Interval r2) { r -= r2; return r; }
inline Interval operator * (Interval r, double f) { r *= f; return r; }
inline Interval operator / (Interval r, double f) { r /= f; return r; }
inline Absolute operator + (Absolute a, Interval r) { return a += r; }
inline Absolute operator + (Interval r, Absolute a) { return a += r; }
inline Absolute operator - (Absolute a, Interval r) { return a -= r; }
inline Interval operator - (Absolute t1, Absolute t0)
{ return t1.mValue - t0.mValue; }
} }
#endif //_H_TIMEFLOW