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.
21 Constructs a zero TimeDelta.
23 TimeDelta(): usec(0) { }
26 Constructs a TimeDelta from a plain number. The purpose of this is to allow
27 serialization together with the raw() function. For creating TimeDeltas
28 with a specific length, see units.h.
30 explicit TimeDelta(RawTime u): usec(u) { }
33 Returns the raw number stored inside the TimeDelta. This should only be used
34 for serialization and the result should not be interpreted in any way.
36 RawTime raw() const { return usec; }
38 TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
39 TimeDelta &operator+=(const TimeDelta &t) { usec+=t.usec; return *this; }
40 TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
41 TimeDelta &operator-=(const TimeDelta &t) { usec-=t.usec; return *this; }
44 TimeDelta operator*(T a) const { return TimeDelta(RawTime(usec*a)); }
46 TimeDelta &operator*=(T a) { usec=RawTime(usec*a); return *this; }
49 TimeDelta operator/(T a) const { return TimeDelta(RawTime(usec/a)); }
51 TimeDelta &operator/=(T a) { usec=RawTime(usec/a); return *this; }
53 double operator/(const TimeDelta &t) const { return double(usec)/t.usec; }
55 bool operator>(const TimeDelta &t) const { return usec>t.usec; }
56 bool operator>=(const TimeDelta &t) const { return usec>=t.usec; }
57 bool operator<(const TimeDelta &t) const { return usec<t.usec; }
58 bool operator<=(const TimeDelta &t) const { return usec<=t.usec; }
59 bool operator==(const TimeDelta &t) const { return usec==t.usec; }
60 bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
63 operator timeval() const { return rawtime_to_timeval(usec); }
64 operator timespec() const { return rawtime_to_timespec(usec); }
67 operator const void *() const { return usec ? this : 0; }
71 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
73 void operator<<(LexicalConverter &, const TimeDelta &);