1 #ifndef MSP_TIME_TIMEDELTA_H_
2 #define MSP_TIME_TIMEDELTA_H_
6 #include <msp/strings/lexicalcast.h>
13 Represents a quantity of time, such as five seconds.
21 /** Constructs a zero TimeDelta. */
22 TimeDelta() = default;
24 /** Constructs a TimeDelta from a plain number. The purpose of this is to
25 allow serialization together with the raw() function. */
26 explicit TimeDelta(RawTime u): usec(u) { }
28 /** Returns the raw number stored inside the TimeDelta. This should only be used
29 for serialization and the result should not be interpreted in any way. */
30 RawTime raw() const { return usec; }
32 TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
33 TimeDelta &operator+=(const TimeDelta &t) { usec += t.usec; return *this; }
34 TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
35 TimeDelta &operator-=(const TimeDelta &t) { usec -= t.usec; return *this; }
36 TimeDelta operator-() const { return TimeDelta(-usec); }
39 TimeDelta operator*(T a) const { return TimeDelta(RawTime(usec*a)); }
41 TimeDelta &operator*=(T a) { usec = RawTime(usec*a); return *this; }
44 TimeDelta operator/(T a) const { return TimeDelta(RawTime(usec/a)); }
46 TimeDelta &operator/=(T a) { usec = RawTime(usec/a); return *this; }
48 double operator/(const TimeDelta &t) const { return double(usec)/t.usec; }
50 bool operator>(const TimeDelta &t) const { return usec>t.usec; }
51 bool operator>=(const TimeDelta &t) const { return usec>=t.usec; }
52 bool operator<(const TimeDelta &t) const { return usec<t.usec; }
53 bool operator<=(const TimeDelta &t) const { return usec<=t.usec; }
54 bool operator==(const TimeDelta &t) const { return usec==t.usec; }
55 bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
57 operator const void *() const { return usec ? this : 0; }
61 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
63 void operator<<(LexicalConverter &, const TimeDelta &);
66 // Constants to be used in creation of TimeDeltas
67 extern const TimeDelta zero;
68 extern const TimeDelta usec;
69 extern const TimeDelta msec;
70 extern const TimeDelta sec;
71 extern const TimeDelta min;
72 extern const TimeDelta hour;
73 extern const TimeDelta day;
74 extern const TimeDelta week;
76 inline TimeDelta abs(const TimeDelta &t) { return t>=zero ? t : -t; }