]> git.tdb.fi Git - libs/core.git/blob - source/time/timedelta.h
Drop copyright and license notices from source files
[libs/core.git] / source / time / timedelta.h
1 #ifndef MSP_TIME_TIMEDELTA_H_
2 #define MSP_TIME_TIMEDELTA_H_
3
4 #include <time.h>
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         /**
21         Constructs a zero TimeDelta.
22         */
23         TimeDelta(): usec(0) { }
24
25         /**
26         Constructs a TimeDelta from a plain number.  The purpose of this is to allow
27         serialization together with the raw() function.  For creating TimeDeltas
28         with a specific length, see units.h.
29         */
30         explicit TimeDelta(RawTime u): usec(u) { }
31
32         /**
33         Returns the raw number stored inside the TimeDelta.  This should only be used
34         for serialization and the result should not be interpreted in any way.
35         */
36         RawTime raw() const { return usec; }
37
38         TimeDelta operator+(const TimeDelta &t) const  { return TimeDelta(usec+t.usec); }
39         TimeDelta &operator+=(const TimeDelta &t)      { usec+=t.usec; return *this; }
40         TimeDelta operator-(const TimeDelta &t) const  { return TimeDelta(usec-t.usec); }
41         TimeDelta &operator-=(const TimeDelta &t)      { usec-=t.usec; 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         template<typename T>
49         TimeDelta operator/(T a) const                 { return TimeDelta(RawTime(usec/a)); }
50         template<typename T>
51         TimeDelta &operator/=(T a)                     { usec=RawTime(usec/a); return *this; }
52
53         double    operator/(const TimeDelta &t) const  { return double(usec)/t.usec; }
54
55         bool      operator>(const TimeDelta &t) const  { return usec>t.usec; }
56         bool      operator>=(const TimeDelta &t) const { return usec>=t.usec; }
57         bool      operator<(const TimeDelta &t) const  { return usec<t.usec; }
58         bool      operator<=(const TimeDelta &t) const { return usec<=t.usec; }
59         bool      operator==(const TimeDelta &t) const { return usec==t.usec; }
60         bool      operator!=(const TimeDelta &t) const { return usec!=t.usec; }
61
62 #ifndef WIN32
63         operator timeval() const { return rawtime_to_timeval(usec); }
64         operator timespec() const { return rawtime_to_timespec(usec); }
65 #endif
66
67         operator const void *() const                  { return usec ? this : 0; }
68 };
69
70 template<typename T>
71 inline TimeDelta operator*(T a, const TimeDelta &t)   { return t*a; }
72
73 void operator<<(LexicalConverter &, const TimeDelta &);
74
75 } // namespace Time
76 } // namespace Msp
77
78 #endif