]> git.tdb.fi Git - libs/core.git/blob - source/time/timedelta.h
694136ec8e78477a0acbcb82227c607824491b67
[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         /**
41         Fills in a timespec struct.  To get a meaningful scalar value from the
42         TimeDelta, divide with one of the values in units.h.
43         */
44         void fill_timespec(timespec &ts) const { ts.tv_sec=usec/1000000; ts.tv_nsec=(usec%1000000)*1000; }
45
46         TimeDelta operator+(const TimeDelta &t) const  { return TimeDelta(usec+t.usec); }
47         TimeDelta &operator+=(const TimeDelta &t)      { usec+=t.usec; return *this; }
48         TimeDelta operator-(const TimeDelta &t) const  { return TimeDelta(usec-t.usec); }
49         TimeDelta &operator-=(const TimeDelta &t)      { usec-=t.usec; return *this; }
50
51         template<typename T>
52         TimeDelta operator*(T a) const                 { return TimeDelta((int64_t)(usec*a)); }
53         template<typename T>
54         TimeDelta &operator*=(T a)                     { usec=(int64_t)(usec*a); return *this; }
55
56         template<typename T>
57         TimeDelta operator/(T a) const                 { return TimeDelta((int64_t)(usec/a)); }
58         template<typename T>
59         TimeDelta &operator/=(T a)                     { usec=(int64_t)(usec/a); return *this; }
60
61         double    operator/(const TimeDelta &t) const  { return (double)usec/t.usec; }
62
63         bool      operator>(const TimeDelta &t) const  { return usec>t.usec; }
64         bool      operator>=(const TimeDelta &t) const { return usec>=t.usec; }
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
70         operator bool() const                          { return usec; }
71
72         friend std::ostream &operator<<(std::ostream &, const TimeDelta &);
73 private:
74         int64_t usec;
75 };
76
77 template<typename T>
78 inline TimeDelta operator*(T a, const TimeDelta &t)   { return t*a; }
79
80 } // namespace Time
81 } // namespace Msp
82
83 #endif