From: Mikko Rasa Date: Sat, 28 May 2011 12:57:54 +0000 (+0300) Subject: Select RawTime definition based on compiler, not platform X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=f5951e7166dfd7028b95d7c780406f65afc5cdfc Select RawTime definition based on compiler, not platform Put time(val|spec) conversion functions to rawtime.h Use conversion operators in favor of fill_* methods Add conversions to TimeStamp as well --- diff --git a/source/time/rawtime.cpp b/source/time/rawtime.cpp new file mode 100644 index 0000000..427eb33 --- /dev/null +++ b/source/time/rawtime.cpp @@ -0,0 +1,25 @@ +#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 diff --git a/source/time/rawtime.h b/source/time/rawtime.h index afa3c94..fb6a383 100644 --- a/source/time/rawtime.h +++ b/source/time/rawtime.h @@ -9,16 +9,22 @@ Distributed under the LGPL #define MSP_TIME_RAWTIME_H_ #ifndef WIN32 -#include +#include #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 diff --git a/source/time/timedelta.h b/source/time/timedelta.h index 0a9ddd9..3a6e8ea 100644 --- a/source/time/timedelta.h +++ b/source/time/timedelta.h @@ -9,7 +9,6 @@ Distributed under the LGPL #define MSP_TIME_TIMEDELTA_H_ #include -#include #include #include "rawtime.h" @@ -43,16 +42,6 @@ public: */ 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); } @@ -77,6 +66,11 @@ public: 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; } }; diff --git a/source/time/timestamp.h b/source/time/timestamp.h index 5dcb076..8c16ee3 100644 --- a/source/time/timestamp.h +++ b/source/time/timestamp.h @@ -61,6 +61,11 @@ public: 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); } }; diff --git a/source/time/utils.cpp b/source/time/utils.cpp index 1d0b8d0..cd2e70f 100644 --- a/source/time/utils.cpp +++ b/source/time/utils.cpp @@ -81,8 +81,7 @@ Sleeps for the given time. 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));