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_
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; }
47 Fills in a timespec struct. To get a meaningful scalar value from the
48 TimeDelta, divide with one of the values in units.h.
50 void fill_timespec(timespec &ts) const { ts.tv_sec=usec/1000000; ts.tv_nsec=(usec%1000000)*1000; }
53 TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
54 TimeDelta &operator+=(const TimeDelta &t) { usec+=t.usec; return *this; }
55 TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
56 TimeDelta &operator-=(const TimeDelta &t) { usec-=t.usec; return *this; }
59 TimeDelta operator*(T a) const { return TimeDelta(RawTime(usec*a)); }
61 TimeDelta &operator*=(T a) { usec=RawTime(usec*a); return *this; }
64 TimeDelta operator/(T a) const { return TimeDelta(RawTime(usec/a)); }
66 TimeDelta &operator/=(T a) { usec=RawTime(usec/a); return *this; }
68 double operator/(const TimeDelta &t) const { return double(usec)/t.usec; }
70 bool operator>(const TimeDelta &t) const { return usec>t.usec; }
71 bool operator>=(const TimeDelta &t) const { return usec>=t.usec; }
72 bool operator<(const TimeDelta &t) const { return 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; }
77 operator const void *() const { return usec ? this : 0; }
81 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
83 extern std::ostream &operator<<(std::ostream &, const TimeDelta &);