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_
20 Represents a quantity of time, such as five seconds.
29 Constructs a zero TimeDelta.
31 TimeDelta(): usec(0) { }
34 Constructs a TimeDelta from a plain number. The purpose of this is to allow
35 serialization together with the raw() function. For creating TimeDeltas
36 with a specific length, see units.h.
38 explicit TimeDelta(RawTime u): usec(u) { }
41 Returns the raw number stored inside the TimeDelta. This should only be used
42 for serialization and the result should not be interpreted in any way.
44 RawTime raw() const { return usec; }
48 Fills in a timespec struct. To get a meaningful scalar value from the
49 TimeDelta, divide with one of the values in units.h.
51 void fill_timespec(timespec &ts) const { ts.tv_sec=usec/1000000; ts.tv_nsec = (usec%1000000)*1000; }
53 void fill_timeval(timeval &tv) const { tv.tv_sec=usec/1000000; tv.tv_usec = usec%1000000; }
56 TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
57 TimeDelta &operator+=(const TimeDelta &t) { usec+=t.usec; return *this; }
58 TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
59 TimeDelta &operator-=(const TimeDelta &t) { usec-=t.usec; return *this; }
62 TimeDelta operator*(T a) const { return TimeDelta(RawTime(usec*a)); }
64 TimeDelta &operator*=(T a) { usec=RawTime(usec*a); return *this; }
67 TimeDelta operator/(T a) const { return TimeDelta(RawTime(usec/a)); }
69 TimeDelta &operator/=(T a) { usec=RawTime(usec/a); return *this; }
71 double operator/(const TimeDelta &t) const { return double(usec)/t.usec; }
73 bool operator>(const TimeDelta &t) const { return usec>t.usec; }
74 bool operator>=(const TimeDelta &t) const { return usec>=t.usec; }
75 bool operator<(const TimeDelta &t) const { return usec<t.usec; }
76 bool operator<=(const TimeDelta &t) const { return usec<=t.usec; }
77 bool operator==(const TimeDelta &t) const { return usec==t.usec; }
78 bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
80 operator const void *() const { return usec ? this : 0; }
84 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
86 extern std::ostream &operator<<(std::ostream &, const TimeDelta &);