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_
18 Represents a quantity of time, such as five seconds.
24 Constructs a zero TimeDelta.
26 TimeDelta(): usec(0) { }
29 Constructs a TimeDelta from a plain number. The purpose of this is to allow
30 serialization together with the raw() function. For creating TimeDeltas
31 with a specific length, see units.h.
33 explicit TimeDelta(RawTime u): usec(u) { }
36 Returns the raw number stored inside the TimeDelta. This should only be used
37 for serialization and the result should not be interpreted in any way.
39 RawTime raw() const { return usec; }
43 Fills in a timespec struct. To get a meaningful scalar value from the
44 TimeDelta, divide with one of the values in units.h.
46 void fill_timespec(timespec &ts) const { ts.tv_sec=usec/1000000; ts.tv_nsec=(usec%1000000)*1000; }
49 TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
50 TimeDelta &operator+=(const TimeDelta &t) { usec+=t.usec; return *this; }
51 TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
52 TimeDelta &operator-=(const TimeDelta &t) { usec-=t.usec; return *this; }
55 TimeDelta operator*(T a) const { return TimeDelta(int64_t(usec*a)); }
57 TimeDelta &operator*=(T a) { usec=int64_t(usec*a); return *this; }
60 TimeDelta operator/(T a) const { return TimeDelta(int64_t(usec/a)); }
62 TimeDelta &operator/=(T a) { usec=int64_t(usec/a); return *this; }
64 double operator/(const TimeDelta &t) const { return double(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; }
71 bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
73 operator const void *() const { return usec ? this : 0; }
79 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
81 extern std::ostream &operator<<(std::ostream &, const TimeDelta &);