--- /dev/null
+#include "rawtime.h"
+
+namespace Msp {
+namespace Time {
+
+#ifndef WIN32
+timeval rawtime_to_timeval(RawTime raw)
+{
+ timeval tv;
+ tv.tv_sec = raw/1000000;
+ tv.tv_usec = raw%1000000;
+ return tv;
+}
+
+timespec rawtime_to_timespec(RawTime raw)
+{
+ timespec ts;
+ ts.tv_sec = raw/1000000;
+ ts.tv_nsec = (raw%1000000)*1000;
+ return ts;
+}
+#endif
+
+} // namespace Time
+} // namespace Msp
#define MSP_TIME_RAWTIME_H_
#ifndef WIN32
-#include <stdint.h>
+#include <sys/time.h>
#endif
namespace Msp {
namespace Time {
-#ifdef WIN32
+#ifdef MSVC
typedef __int64 RawTime;
#else
-typedef int64_t RawTime;
+typedef long long RawTime;
+#endif
+
+#ifndef WIN32
+// Internal conversion utilities, not intended for public use
+timeval rawtime_to_timeval(RawTime);
+timespec rawtime_to_timespec(RawTime);
#endif
} // namespace Time
#define MSP_TIME_TIMEDELTA_H_
#include <time.h>
-#include <sys/time.h>
#include <msp/strings/lexicalcast.h>
#include "rawtime.h"
*/
RawTime raw() const { return usec; }
-#ifndef WIN32
- /**
- Fills in a timespec struct. To get a meaningful scalar value from the
- TimeDelta, divide with one of the values in units.h.
- */
- void fill_timespec(timespec &ts) const { ts.tv_sec=usec/1000000; ts.tv_nsec = (usec%1000000)*1000; }
-
- void fill_timeval(timeval &tv) const { tv.tv_sec=usec/1000000; tv.tv_usec = usec%1000000; }
-#endif
-
TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); }
TimeDelta &operator+=(const TimeDelta &t) { usec+=t.usec; return *this; }
TimeDelta operator-(const TimeDelta &t) const { return TimeDelta(usec-t.usec); }
bool operator==(const TimeDelta &t) const { return usec==t.usec; }
bool operator!=(const TimeDelta &t) const { return usec!=t.usec; }
+#ifndef WIN32
+ operator timeval() const { return rawtime_to_timeval(usec); }
+ operator timespec() const { return rawtime_to_timespec(usec); }
+#endif
+
operator const void *() const { return usec ? this : 0; }
};
operator const void *() const { return usec>0 ? this : 0; }
+#ifndef WIN32
+ operator timeval() const { return rawtime_to_timeval(usec); }
+ operator timespec() const { return rawtime_to_timespec(usec); }
+#endif
+
static TimeStamp from_unixtime(time_t t) { return TimeStamp(t*1000000LL); }
};
int sleep(const TimeDelta &d)
{
#ifndef WIN32
- timespec ts;
- d.fill_timespec(ts);
+ timespec ts = d;
return nanosleep(&ts, 0);
#else
Sleep((DWORD)(d/msec));