]> git.tdb.fi Git - libs/core.git/blob - source/time/timedelta.h
Move the time constants to timedelta.h
[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.  For creating
25         TimeDeltas with a specific length, see units.h. */
26         explicit TimeDelta(RawTime u): usec(u) { }
27
28         /** Returns the raw number stored inside the TimeDelta.  This should only be used
29         for serialization and the result should not be interpreted in any way. */
30         RawTime raw() const { return usec; }
31
32         TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
33         TimeDelta &operator+=(const TimeDelta &t) { usec += t.usec; return *this; }
34         TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
35         TimeDelta &operator-=(const TimeDelta &t) { usec -= t.usec; return *this; }
36
37         template<typename T>
38         TimeDelta operator*(T a) const { return TimeDelta(RawTime(usec*a)); }
39         template<typename T>
40         TimeDelta &operator*=(T a) { usec = RawTime(usec*a); return *this; }
41
42         template<typename T>
43         TimeDelta operator/(T a) const { return TimeDelta(RawTime(usec/a)); }
44         template<typename T>
45         TimeDelta &operator/=(T a) { usec = RawTime(usec/a); return *this; }
46
47         double operator/(const TimeDelta &t) const { return double(usec)/t.usec; }
48
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         bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
55
56         operator const void *() const { return usec ? this : 0; }
57 };
58
59 template<typename T>
60 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
61
62 void operator<<(LexicalConverter &, const TimeDelta &);
63
64
65 // Constants to be used in creation of TimeDeltas
66 extern const TimeDelta zero;
67 extern const TimeDelta usec;
68 extern const TimeDelta msec;
69 extern const TimeDelta sec;
70 extern const TimeDelta min;
71 extern const TimeDelta hour;
72 extern const TimeDelta day;
73 extern const TimeDelta week;
74
75 } // namespace Time
76 } // namespace Msp
77
78 #endif