]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/timestamp.h
Rename to libmspcore
[libs/core.git] / source / time / timestamp.h
diff --git a/source/time/timestamp.h b/source/time/timestamp.h
new file mode 100644 (file)
index 0000000..da4e457
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+This file is part of libmspframework     
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+#ifndef MSP_TIME_TIMESTAMP_H_
+#define MSP_TIME_TIMESTAMP_H_
+
+#include <stdint.h>
+#include "timedelta.h"
+
+namespace Msp {
+namespace Time {
+
+/**
+Represents a moment in time, such as the last tick.  This class is NOT intended
+to be used for storing arbitary user-defined times, I'll add a DateTime class
+if the need arises.
+*/
+class TimeStamp
+{
+public:
+       /**
+       Construct a TimeStamp that represents an arbitarily distant point in the
+       past.  It's guaranteed to be less than any valid timestamp.
+       */
+       TimeStamp(): usec(0) { }
+
+       /**
+       Constructs a TimeStamp from a plain number.  The purpose of this is to allow
+       serialization together with the raw() function.
+       */
+       explicit TimeStamp(int64_t u): usec(u) { }
+
+       /**
+       Returns the raw number stored inside the TimeStamp.  This should only be used
+       for serialization and the result should not be interpreted in any way.
+       */
+       int64_t   raw() const { return usec; }
+
+       TimeStamp operator+(const TimeDelta &t) const  { return TimeStamp(usec+t.raw()); }
+       TimeStamp &operator+=(const TimeDelta &t)      { usec+=t.raw(); return *this; }
+       TimeStamp operator-(const TimeDelta &t) const  { return TimeStamp(usec-t.raw()); }
+       TimeStamp &operator-=(const TimeDelta &t)      { usec-=t.raw(); return *this; }
+       TimeDelta operator-(const TimeStamp &t) const  { return TimeDelta(usec-t.usec); }
+       bool      operator>=(const TimeStamp &t) const { return usec>=t.usec; }
+       bool      operator>(const TimeStamp &t) const  { return usec>t.usec; }
+       bool      operator<=(const TimeStamp &t) const { return usec<=t.usec; }
+       bool      operator<(const TimeStamp &t) const  { return usec<t.usec; }
+       bool      operator==(const TimeStamp &t) const { return usec==t.usec; }
+       bool      operator!=(const TimeStamp &t) const { return usec!=t.usec; }
+       operator bool() const                          { return usec>0; }
+
+       static TimeStamp from_unixtime(time_t t) { return TimeStamp(t*1000000LL); }
+private:
+       int64_t usec;
+};
+
+} // namespace Time
+} // namespace Msp
+
+#endif