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