]> git.tdb.fi Git - libs/core.git/blob - source/time/timedelta.h
Clean up after the timedelta.h/units.h merge
[libs/core.git] / source / time / timedelta.h
1 #ifndef MSP_TIME_TIMEDELTA_H_
2 #define MSP_TIME_TIMEDELTA_H_
3
4 #include <ctime>
5 #include <msp/strings/lexicalcast.h>
6 #include "rawtime.h"
7
8 namespace Msp {
9 namespace Time {
10
11 /**
12 Represents a quantity of time, such as five seconds.
13 */
14 class TimeDelta
15 {
16 private:
17         RawTime usec;
18
19 public:
20         /** Constructs a zero TimeDelta. */
21         TimeDelta(): usec(0) { }
22
23         /** Constructs a TimeDelta from a plain number.  The purpose of this is to
24         allow serialization together with the raw() function. */
25         explicit TimeDelta(RawTime u): usec(u) { }
26
27         /** Returns the raw number stored inside the TimeDelta.  This should only be used
28         for serialization and the result should not be interpreted in any way. */
29         RawTime raw() const { return usec; }
30
31         TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
32         TimeDelta &operator+=(const TimeDelta &t) { usec += t.usec; return *this; }
33         TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
34         TimeDelta &operator-=(const TimeDelta &t) { usec -= t.usec; return *this; }
35
36         template<typename T>
37         TimeDelta operator*(T a) const { return TimeDelta(RawTime(usec*a)); }
38         template<typename T>
39         TimeDelta &operator*=(T a) { usec = RawTime(usec*a); return *this; }
40
41         template<typename T>
42         TimeDelta operator/(T a) const { return TimeDelta(RawTime(usec/a)); }
43         template<typename T>
44         TimeDelta &operator/=(T a) { usec = RawTime(usec/a); return *this; }
45
46         double operator/(const TimeDelta &t) const { return double(usec)/t.usec; }
47
48         bool operator>(const TimeDelta &t) const { return usec>t.usec; }
49         bool operator>=(const TimeDelta &t) const { return usec>=t.usec; }
50         bool operator<(const TimeDelta &t) const { return usec<t.usec; }
51         bool operator<=(const TimeDelta &t) const { return usec<=t.usec; }
52         bool operator==(const TimeDelta &t) const { return usec==t.usec; }
53         bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
54
55         operator const void *() const { return usec ? this : 0; }
56 };
57
58 template<typename T>
59 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
60
61 void operator<<(LexicalConverter &, const TimeDelta &);
62
63
64 // Constants to be used in creation of TimeDeltas
65 extern const TimeDelta zero;
66 extern const TimeDelta usec;
67 extern const TimeDelta msec;
68 extern const TimeDelta sec;
69 extern const TimeDelta min;
70 extern const TimeDelta hour;
71 extern const TimeDelta day;
72 extern const TimeDelta week;
73
74 } // namespace Time
75 } // namespace Msp
76
77 #endif