]> git.tdb.fi Git - libs/core.git/blob - source/time/timedelta.h
Add move semantics to Variant
[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/core/mspcore_api.h>
7 #include <msp/strings/lexicalcast.h>
8 #include "rawtime.h"
9
10 namespace Msp {
11 namespace Time {
12
13 /**
14 Represents a quantity of time, such as five seconds.
15 */
16 class MSPCORE_API TimeDelta
17 {
18 private:
19         RawTime usec = 0;
20
21 public:
22         /** Constructs a zero TimeDelta. */
23         constexpr TimeDelta() = default;
24
25         /** Constructs a TimeDelta from a plain number.  The purpose of this is to
26         allow serialization together with the raw() function. */
27         explicit constexpr TimeDelta(RawTime u): usec(u) { }
28
29         /** Returns the raw number stored inside the TimeDelta.  This should only be used
30         for serialization and the result should not be interpreted in any way. */
31         RawTime raw() const { return usec; }
32
33         TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
34         TimeDelta &operator+=(const TimeDelta &t) { usec += t.usec; return *this; }
35         TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
36         TimeDelta &operator-=(const TimeDelta &t) { usec -= t.usec; return *this; }
37         TimeDelta operator-() const { return TimeDelta(-usec); }
38
39         template<typename T>
40         TimeDelta operator*(T a) const { return TimeDelta(RawTime(usec*a)); }
41         template<typename T>
42         TimeDelta &operator*=(T a) { usec = RawTime(usec*a); return *this; }
43
44         template<typename T>
45         TimeDelta operator/(T a) const { return TimeDelta(RawTime(usec/a)); }
46         template<typename T>
47         TimeDelta &operator/=(T a) { usec = RawTime(usec/a); return *this; }
48
49         double operator/(const TimeDelta &t) const { return double(usec)/t.usec; }
50
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         bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
57
58         explicit operator bool() const { return usec; }
59 };
60
61 template<typename T>
62 inline TimeDelta operator*(T a, const TimeDelta &t) { return t*a; }
63
64 MSPCORE_API void operator<<(LexicalConverter &, const TimeDelta &);
65
66
67 // Constants to be used in creation of TimeDeltas
68 constexpr TimeDelta zero(0);
69 constexpr TimeDelta usec(1);
70 constexpr TimeDelta msec(1000);
71 constexpr TimeDelta sec(1000000);
72 constexpr TimeDelta min(60*1000000);
73 constexpr TimeDelta hour(3600*1000000LL);
74 constexpr TimeDelta day(86400*1000000LL);
75 constexpr TimeDelta week(7*86400*1000000LL);
76
77 inline TimeDelta abs(const TimeDelta &t) { return t>=zero ? t : -t; }
78 using std::abs;
79
80 } // namespace Time
81 } // namespace Msp
82
83 #endif