3 This file is part of libmspcore
4 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #ifndef MSP_TIME_TIMEDELTA_H_
9 #define MSP_TIME_TIMEDELTA_H_
12 #include <msp/strings/lexicalcast.h>
19 Represents a quantity of time, such as five seconds.
28 Constructs a zero TimeDelta.
30 TimeDelta(): usec(0) { }
33 Constructs a TimeDelta from a plain number. The purpose of this is to allow
34 serialization together with the raw() function. For creating TimeDeltas
35 with a specific length, see units.h.
37 explicit TimeDelta(RawTime u): usec(u) { }
40 Returns the raw number stored inside the TimeDelta. This should only be used
41 for serialization and the result should not be interpreted in any way.
43 RawTime raw() const { return usec; }
45 TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
46 TimeDelta &operator+=(const TimeDelta &t) { usec+=t.usec; return *this; }
47 TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
48 TimeDelta &operator-=(const TimeDelta &t) { usec-=t.usec; return *this; }
51 TimeDelta operator*(T a) const { return TimeDelta(RawTime(usec*a)); }
53 TimeDelta &operator*=(T a) { usec=RawTime(usec*a); return *this; }
56 TimeDelta operator/(T a) const { return TimeDelta(RawTime(usec/a)); }
58 TimeDelta &operator/=(T a) { usec=RawTime(usec/a); return *this; }
60 double operator/(const TimeDelta &t) const { return double(usec)/t.usec; }
62 bool operator>(const TimeDelta &t) const { return usec>t.usec; }
63 bool operator>=(const TimeDelta &t) const { return usec>=t.usec; }
64 bool operator<(const TimeDelta &t) const { return usec<t.usec; }
65 bool operator<=(const TimeDelta &t) const { return usec<=t.usec; }
66 bool operator==(const TimeDelta &t) const { return usec==t.usec; }
67 bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
70 operator timeval() const { return rawtime_to_timeval(usec); }
71 operator timespec() const { return rawtime_to_timespec(usec); }
74 operator const void *() const { return usec ? this : 0; }
78 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
80 void operator<<(LexicalConverter &, const TimeDelta &);