1 #ifndef MSP_TIME_TIMEDELTA_H_
2 #define MSP_TIME_TIMEDELTA_H_
6 #include <msp/core/mspcore_api.h>
7 #include <msp/strings/lexicalcast.h>
14 Represents a quantity of time, such as five seconds.
16 class MSPCORE_API TimeDelta
22 /** Constructs a zero TimeDelta. */
23 constexpr TimeDelta() = default;
25 /** Constructs a TimeDelta from a plain number. The purpose of this is to
26 allow serialization together with the raw() function. */
27 explicit constexpr TimeDelta(RawTime u): usec(u) { }
29 /** Returns the raw number stored inside the TimeDelta. This should only be used
30 for serialization and the result should not be interpreted in any way. */
31 RawTime raw() const { return usec; }
33 TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
34 TimeDelta &operator+=(const TimeDelta &t) { usec += t.usec; return *this; }
35 TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
36 TimeDelta &operator-=(const TimeDelta &t) { usec -= t.usec; return *this; }
37 TimeDelta operator-() const { return TimeDelta(-usec); }
40 TimeDelta operator*(T a) const { return TimeDelta(RawTime(usec*a)); }
42 TimeDelta &operator*=(T a) { usec = RawTime(usec*a); return *this; }
45 TimeDelta operator/(T a) const { return TimeDelta(RawTime(usec/a)); }
47 TimeDelta &operator/=(T a) { usec = RawTime(usec/a); return *this; }
49 double operator/(const TimeDelta &t) const { return double(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; }
56 bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
58 explicit operator bool() const { return usec; }
62 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
64 MSPCORE_API void operator<<(LexicalConverter &, const TimeDelta &);
67 // Constants to be used in creation of TimeDeltas
68 constexpr TimeDelta zero(0);
69 constexpr TimeDelta usec(1);
70 constexpr TimeDelta msec(1000);
71 constexpr TimeDelta sec(1000000);
72 constexpr TimeDelta min(60*1000000);
73 constexpr TimeDelta hour(3600*1000000LL);
74 constexpr TimeDelta day(86400*1000000LL);
75 constexpr TimeDelta week(7*86400*1000000LL);
77 inline TimeDelta abs(const TimeDelta &t) { return t>=zero ? t : -t; }