1 #ifndef MSP_TIME_TIMEDELTA_H_
2 #define MSP_TIME_TIMEDELTA_H_
5 #include <msp/strings/lexicalcast.h>
12 Represents a quantity of time, such as five seconds.
20 /** Constructs a zero TimeDelta. */
21 TimeDelta(): usec(0) { }
23 /** Constructs a TimeDelta from a plain number. The purpose of this is to
24 allow serialization together with the raw() function. */
25 explicit TimeDelta(RawTime u): usec(u) { }
27 /** Returns the raw number stored inside the TimeDelta. This should only be used
28 for serialization and the result should not be interpreted in any way. */
29 RawTime raw() const { return usec; }
31 TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
32 TimeDelta &operator+=(const TimeDelta &t) { usec += t.usec; return *this; }
33 TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
34 TimeDelta &operator-=(const TimeDelta &t) { usec -= t.usec; return *this; }
37 TimeDelta operator*(T a) const { return TimeDelta(RawTime(usec*a)); }
39 TimeDelta &operator*=(T a) { usec = RawTime(usec*a); return *this; }
42 TimeDelta operator/(T a) const { return TimeDelta(RawTime(usec/a)); }
44 TimeDelta &operator/=(T a) { usec = RawTime(usec/a); return *this; }
46 double operator/(const TimeDelta &t) const { return double(usec)/t.usec; }
48 bool operator>(const TimeDelta &t) const { return usec>t.usec; }
49 bool operator>=(const TimeDelta &t) const { return 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; }
55 operator const void *() const { return usec ? this : 0; }
59 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
61 void operator<<(LexicalConverter &, const TimeDelta &);
64 // Constants to be used in creation of TimeDeltas
65 extern const TimeDelta zero;
66 extern const TimeDelta usec;
67 extern const TimeDelta msec;
68 extern const TimeDelta sec;
69 extern const TimeDelta min;
70 extern const TimeDelta hour;
71 extern const TimeDelta day;
72 extern const TimeDelta week;