]> git.tdb.fi Git - libs/core.git/blob - source/time/timedelta.h
Use vectors for storage in Poller
[libs/core.git] / source / time / timedelta.h
1 #ifndef MSP_TIME_TIMEDELTA_H_
2 #define MSP_TIME_TIMEDELTA_H_
3
4 #include <cstdlib>
5 #include <ctime>
6 #include <msp/strings/lexicalcast.h>
7 #include "rawtime.h"
8
9 namespace Msp {
10 namespace Time {
11
12 /**
13 Represents a quantity of time, such as five seconds.
14 */
15 class TimeDelta
16 {
17 private:
18         RawTime usec;
19
20 public:
21         /** Constructs a zero TimeDelta. */
22         TimeDelta(): usec(0) { }
23
24         /** Constructs a TimeDelta from a plain number.  The purpose of this is to
25         allow serialization together with the raw() function. */
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         TimeDelta operator-() const { return TimeDelta(-usec); }
37
38         template<typename T>
39         TimeDelta operator*(T a) const { return TimeDelta(RawTime(usec*a)); }
40         template<typename T>
41         TimeDelta &operator*=(T a) { usec = RawTime(usec*a); return *this; }
42
43         template<typename T>
44         TimeDelta operator/(T a) const { return TimeDelta(RawTime(usec/a)); }
45         template<typename T>
46         TimeDelta &operator/=(T a) { usec = RawTime(usec/a); return *this; }
47
48         double operator/(const TimeDelta &t) const { return double(usec)/t.usec; }
49
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         bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
56
57         operator const void *() const { return usec ? this : 0; }
58 };
59
60 template<typename T>
61 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
62
63 void operator<<(LexicalConverter &, const TimeDelta &);
64
65
66 // Constants to be used in creation of TimeDeltas
67 extern const TimeDelta zero;
68 extern const TimeDelta usec;
69 extern const TimeDelta msec;
70 extern const TimeDelta sec;
71 extern const TimeDelta min;
72 extern const TimeDelta hour;
73 extern const TimeDelta day;
74 extern const TimeDelta week;
75
76 inline TimeDelta abs(const TimeDelta &t) { return t>=zero ? t : -t; }
77 using std::abs;
78
79 } // namespace Time
80 } // namespace Msp
81
82 #endif