]> git.tdb.fi Git - libs/core.git/blob - source/time/timestamp.h
Throw out anything polling related - they will go to libmspio eventually
[libs/core.git] / source / time / timestamp.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_TIMESTAMP_H_
7 #define MSP_TIME_TIMESTAMP_H_
8
9 #include <stdint.h>
10 #include "timedelta.h"
11
12 namespace Msp {
13 namespace Time {
14
15 /**
16 Represents a moment in time, such as the last tick.  This class is NOT intended
17 to be used for storing arbitary user-defined times, I'll add a DateTime class
18 if the need arises.
19 */
20 class TimeStamp
21 {
22 public:
23         /**
24         Construct a TimeStamp that represents an arbitarily distant point in the
25         past.  It's guaranteed to be less than any valid timestamp.
26         */
27         TimeStamp(): usec(0) { }
28
29         /**
30         Constructs a TimeStamp from a plain number.  The purpose of this is to allow
31         serialization together with the raw() function.
32         */
33         explicit TimeStamp(int64_t u): usec(u) { }
34
35         /**
36         Returns the raw number stored inside the TimeStamp.  This should only be used
37         for serialization and the result should not be interpreted in any way.
38         */
39         int64_t   raw() const { return usec; }
40
41         TimeStamp operator+(const TimeDelta &t) const  { return TimeStamp(usec+t.raw()); }
42         TimeStamp &operator+=(const TimeDelta &t)      { usec+=t.raw(); return *this; }
43         TimeStamp operator-(const TimeDelta &t) const  { return TimeStamp(usec-t.raw()); }
44         TimeStamp &operator-=(const TimeDelta &t)      { usec-=t.raw(); return *this; }
45         TimeDelta operator-(const TimeStamp &t) const  { return TimeDelta(usec-t.usec); }
46         bool      operator>=(const TimeStamp &t) const { return usec>=t.usec; }
47         bool      operator>(const TimeStamp &t) const  { return usec>t.usec; }
48         bool      operator<=(const TimeStamp &t) const { return usec<=t.usec; }
49         bool      operator<(const TimeStamp &t) const  { return usec<t.usec; }
50         bool      operator==(const TimeStamp &t) const { return usec==t.usec; }
51         bool      operator!=(const TimeStamp &t) const { return usec!=t.usec; }
52         operator bool() const                          { return usec>0; }
53
54         static TimeStamp from_unixtime(time_t t) { return TimeStamp(t*1000000LL); }
55 private:
56         int64_t usec;
57 };
58
59 } // namespace Time
60 } // namespace Msp
61
62 #endif