2 This file is part of libmspcore
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
6 #ifndef MSP_TIME_TIMEDELTA_H_
7 #define MSP_TIME_TIMEDELTA_H_
17 Represents a quantity of time, such as five seconds.
23 Constructs a zero TimeDelta.
25 TimeDelta(): usec(0) { }
28 Constructs a TimeDelta from a plain number. The purpose of this is to allow
29 serialization together with the raw() function. For creating TimeDeltas
30 with a specific length, see units.h.
32 explicit TimeDelta(int64_t u): usec(u) { }
35 Returns the raw number stored inside the TimeDelta. This should only be used
36 for serialization and the result should not be interpreted in any way.
38 int64_t raw() const { return usec; }
42 Fills in a timespec struct. To get a meaningful scalar value from the
43 TimeDelta, divide with one of the values in units.h.
45 void fill_timespec(timespec &ts) const { ts.tv_sec=usec/1000000; ts.tv_nsec=(usec%1000000)*1000; }
48 TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
49 TimeDelta &operator+=(const TimeDelta &t) { usec+=t.usec; return *this; }
50 TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
51 TimeDelta &operator-=(const TimeDelta &t) { usec-=t.usec; return *this; }
54 TimeDelta operator*(T a) const { return TimeDelta(int64_t(usec*a)); }
56 TimeDelta &operator*=(T a) { usec=int64_t(usec*a); return *this; }
59 TimeDelta operator/(T a) const { return TimeDelta(int64_t(usec/a)); }
61 TimeDelta &operator/=(T a) { usec=int64_t(usec/a); return *this; }
63 double operator/(const TimeDelta &t) const { return double(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; }
68 bool operator<=(const TimeDelta &t) const { return usec<=t.usec; }
69 bool operator==(const TimeDelta &t) const { return usec==t.usec; }
70 bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
72 operator bool() const { return usec; }
78 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
80 extern std::ostream &operator<<(std::ostream &, const TimeDelta &);