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