]> git.tdb.fi Git - libs/core.git/blob - source/time/timedelta.h
97c8cac03adf5020fb8e168d11d89c37fb653997
[libs/core.git] / source / time / timedelta.h
1 /*
2 This file is part of libmspcore     
3 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
5 */
6 #ifndef MSP_TIME_TIMEDELTA_H_
7 #define MSP_TIME_TIMEDELTA_H_
8
9 #include <stdint.h>
10 #include <time.h>
11 #include <ostream>
12 #include "types.h"
13
14 namespace Msp {
15 namespace Time {
16
17 /**
18 Represents a quantity of time, such as five seconds.
19 */
20 class TimeDelta
21 {
22 public:
23         /**
24         Constructs a zero TimeDelta.
25         */
26         TimeDelta(): usec(0) { }
27
28         /**
29         Constructs a TimeDelta from a plain number.  The purpose of this is to allow
30         serialization together with the raw() function.  For creating TimeDeltas
31         with a specific length, see units.h.
32         */
33         explicit TimeDelta(RawTime u): usec(u) { }
34
35         /**
36         Returns the raw number stored inside the TimeDelta.  This should only be used
37         for serialization and the result should not be interpreted in any way.
38         */
39         RawTime raw() const { return usec; }
40
41 #ifndef WIN32
42         /**
43         Fills in a timespec struct.  To get a meaningful scalar value from the
44         TimeDelta, divide with one of the values in units.h.
45         */
46         void fill_timespec(timespec &ts) const { ts.tv_sec=usec/1000000; ts.tv_nsec=(usec%1000000)*1000; }
47 #endif
48
49         TimeDelta operator+(const TimeDelta &t) const  { return TimeDelta(usec+t.usec); }
50         TimeDelta &operator+=(const TimeDelta &t)      { usec+=t.usec; return *this; }
51         TimeDelta operator-(const TimeDelta &t) const  { return TimeDelta(usec-t.usec); }
52         TimeDelta &operator-=(const TimeDelta &t)      { usec-=t.usec; return *this; }
53
54         template<typename T>
55         TimeDelta operator*(T a) const                 { return TimeDelta(int64_t(usec*a)); }
56         template<typename T>
57         TimeDelta &operator*=(T a)                     { usec=int64_t(usec*a); return *this; }
58
59         template<typename T>
60         TimeDelta operator/(T a) const                 { return TimeDelta(int64_t(usec/a)); }
61         template<typename T>
62         TimeDelta &operator/=(T a)                     { usec=int64_t(usec/a); return *this; }
63
64         double    operator/(const TimeDelta &t) const  { return double(usec)/t.usec; }
65
66         bool      operator>(const TimeDelta &t) const  { return usec>t.usec; }
67         bool      operator>=(const TimeDelta &t) const { return usec>=t.usec; }
68         bool      operator<(const TimeDelta &t) const  { return usec<t.usec; }
69         bool      operator<=(const TimeDelta &t) const { return usec<=t.usec; }
70         bool      operator==(const TimeDelta &t) const { return usec==t.usec; }
71         bool      operator!=(const TimeDelta &t) const { return usec!=t.usec; }
72
73         operator bool() const                          { return usec; }
74 private:
75         RawTime usec;
76 };
77
78 template<typename T>
79 inline TimeDelta operator*(T a, const TimeDelta &t)   { return t*a; }
80
81 extern std::ostream &operator<<(std::ostream &, const TimeDelta &);
82
83 } // namespace Time
84 } // namespace Msp
85
86 #endif