+ /** Set the time from a double value */
+ void setTime(const double& seconds)
+ {
+ sec_type wholeSecs = sec_type(floor(seconds));
+ nsec_type reminder;
+ reminder = nsec_type(floor((seconds - wholeSecs)*(1000*1000*1000)));
+ setTime(wholeSecs, reminder);
+ }
+ /** Set the time from a seconds/nanoseconds pair */
+ void setTime(sec_type sec, nsec_type nsec)
+ {
+ if (0 <= nsec) {
+ _sec = sec + nsec / (1000*1000*1000);
+ _nsec = nsec % (1000*1000*1000);
+ } else {
+ _sec = sec - 1 + nsec / (1000*1000*1000);
+ _nsec = (1000*1000*1000) + nsec % (1000*1000*1000);
+ }
+ }
+
+
+ /** @return the saved seconds of this time stamp */
+ long get_seconds() const { return _sec; }
+
+ /** @return the saved microseconds of this time stamp */
+ int get_usec() const { return _nsec/1000; }
+
+ /** @return the saved seconds of this time stamp */
+ const sec_type& getSeconds() const
+ { return _sec; }
+ /** @return the saved nanoseconds of this time stamp */
+ const nsec_type& getNanoSeconds() const
+ { return _nsec; }
+
+ /** @return the value of the timestamp in nanoseconds,
+ * use doubles to avoid overflow.
+ * If you need real nanosecond accuracy for time differences, build up a
+ * SGTimeStamp reference time and compare SGTimeStamps directly.
+ */
+ double toNSecs() const
+ { return _nsec + double(_sec)*1000*1000*1000; }
+
+ /** @return the value of the timestamp in microseconds,
+ * use doubles to avoid overflow.
+ * If you need real nanosecond accuracy for time differences, build up a
+ * SGTimeStamp reference time and compare SGTimeStamps directly.
+ */
+ double toUSecs() const
+ { return 1e-3*_nsec + double(_sec)*1000*1000; }
+
+ /** @return the value of the timestamp in milliseconds,
+ * use doubles to avoid overflow.
+ * If you need real nanosecond accuracy for time differences, build up a
+ * SGTimeStamp reference time and compare SGTimeStamps directly.
+ */
+ double toMSecs() const
+ { return 1e-6*_nsec + double(_sec)*1000; }
+
+ /** @return the value of the timestamp in seconds,
+ * use doubles to avoid overflow.
+ * If you need real nanosecond accuracy for time differences, build up a
+ * SGTimeStamp reference time and compare SGTimeStamps directly.
+ */
+ double toSecs() const
+ { return 1e-9*_nsec + _sec; }
+
+ /** Inplace addition.
+ */
+ SGTimeStamp& operator+=(const SGTimeStamp& c)
+ {
+ _sec += c._sec;
+ _nsec += c._nsec;
+ if ((1000*1000*1000) <= _nsec) {
+ _nsec -= (1000*1000*1000);
+ _sec += 1;
+ }
+ return *this;
+ }
+
+ /** Inplace subtraction.
+ */
+ SGTimeStamp& operator-=(const SGTimeStamp& c)
+ {
+ _sec -= c._sec;
+ _nsec -= c._nsec;
+ if (_nsec < 0) {
+ _nsec += (1000*1000*1000);
+ _sec -= 1;
+ }
+ return *this;
+ }
+
+ /**
+ * Create SGTimeStamps from input with given units
+ */
+ static SGTimeStamp fromSecMSec(sec_type sec, nsec_type msec)
+ { return SGTimeStamp(sec, 1000*1000*msec); }
+ static SGTimeStamp fromSecUSec(sec_type sec, nsec_type usec)
+ { return SGTimeStamp(sec, 1000*usec); }
+ static SGTimeStamp fromSecNSec(sec_type sec, nsec_type nsec)
+ { return SGTimeStamp(sec, nsec); }
+
+ static SGTimeStamp fromSec(int sec)
+ { SGTimeStamp ts; ts.setTime(sec); return ts; }
+ static SGTimeStamp fromSec(const double& sec)
+ { SGTimeStamp ts; ts.setTime(sec); return ts; }
+ static SGTimeStamp fromMSec(nsec_type msec)
+ { return SGTimeStamp(0, 1000*1000*msec); }
+ static SGTimeStamp fromUSec(nsec_type usec)
+ { return SGTimeStamp(0, 1000*usec); }
+ static SGTimeStamp fromNSec(nsec_type nsec)
+ { return SGTimeStamp(0, nsec); }
+
+ /**
+ * Return a timestamp with the current time.
+ */
+ static SGTimeStamp now()
+ { SGTimeStamp ts; ts.stamp(); return ts; }
+
+ /**
+ * Sleep until the time of abstime is passed.
+ */
+ static bool sleepUntil(const SGTimeStamp& abstime);
+
+ /**
+ * Sleep for reltime.
+ */
+ static bool sleepFor(const SGTimeStamp& reltime);
+
+ /**
+ * Alias for the most common use case with milliseconds.
+ */
+ static bool sleepForMSec(unsigned msec)
+ { return sleepFor(fromMSec(msec)); }
+
+ /**
+ * elapsed time since the stamp was taken, in msec
+ */
+ int elapsedMSec() const;