aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CommonLibs/Timeval.cpp29
-rw-r--r--CommonLibs/Timeval.h27
2 files changed, 28 insertions, 28 deletions
diff --git a/CommonLibs/Timeval.cpp b/CommonLibs/Timeval.cpp
index 991e241..21072fc 100644
--- a/CommonLibs/Timeval.cpp
+++ b/CommonLibs/Timeval.cpp
@@ -34,36 +34,33 @@ void Timeval::future(unsigned offset)
now();
unsigned sec = offset/1000;
unsigned msec = offset%1000;
- mTimeval.tv_usec += msec*1000;
- mTimeval.tv_sec += sec;
- if (mTimeval.tv_usec>1000000) {
- mTimeval.tv_usec -= 1000000;
- mTimeval.tv_sec += 1;
+ mTimespec.tv_nsec += msec*1000*1000;
+ mTimespec.tv_sec += sec;
+ if (mTimespec.tv_nsec > 1000*1000*1000) {
+ mTimespec.tv_nsec -= 1000*1000*1000;
+ mTimespec.tv_sec += 1;
}
}
struct timespec Timeval::timespec() const
{
- struct timespec retVal;
- retVal.tv_sec = mTimeval.tv_sec;
- retVal.tv_nsec = 1000 * (long)mTimeval.tv_usec;
- return retVal;
+ return mTimespec;
}
bool Timeval::passed() const
{
Timeval nowTime;
- if (nowTime.mTimeval.tv_sec < mTimeval.tv_sec) return false;
- if (nowTime.mTimeval.tv_sec > mTimeval.tv_sec) return true;
- if (nowTime.mTimeval.tv_usec >= mTimeval.tv_usec) return true;
+ if (nowTime.mTimespec.tv_sec < mTimespec.tv_sec) return false;
+ if (nowTime.mTimespec.tv_sec > mTimespec.tv_sec) return true;
+ if (nowTime.mTimespec.tv_nsec >= mTimespec.tv_nsec) return true;
return false;
}
double Timeval::seconds() const
{
- return ((double)mTimeval.tv_sec) + 1e-6*((double)mTimeval.tv_usec);
+ return ((double)mTimespec.tv_sec) + 1e-9*((double)mTimespec.tv_nsec);
}
@@ -72,8 +69,8 @@ long Timeval::delta(const Timeval& other) const
{
// 2^31 milliseconds is just over 4 years.
int32_t deltaS = other.sec() - sec();
- int32_t deltaUs = other.usec() - usec();
- return 1000*deltaS + deltaUs/1000;
+ int32_t deltaNs = other.nsec() - nsec();
+ return 1000*deltaS + deltaNs/1000000;
}
@@ -89,7 +86,7 @@ ostream& operator<<(ostream& os, const Timeval& tv)
ostream& operator<<(ostream& os, const struct timespec& ts)
{
- os << ts.tv_sec << "," << ts.tv_nsec;
+ os << ts.tv_sec << "," << ts.tv_nsec/1000;
return os;
}
diff --git a/CommonLibs/Timeval.h b/CommonLibs/Timeval.h
index c497864..b780af1 100644
--- a/CommonLibs/Timeval.h
+++ b/CommonLibs/Timeval.h
@@ -42,12 +42,12 @@ class Timeval {
private:
- struct timeval mTimeval;
+ struct timespec mTimespec;
public:
- /** Set the value to gettimeofday. */
- void now() { gettimeofday(&mTimeval,NULL); }
+ /** Set the value to current time. */
+ void now() { clock_gettime(CLOCK_REALTIME, &mTimespec); }
/** Set the value to gettimeofday plus an offset. */
void future(unsigned ms);
@@ -55,16 +55,18 @@ class Timeval {
//@{
Timeval(unsigned sec, unsigned usec)
{
- mTimeval.tv_sec = sec;
- mTimeval.tv_usec = usec;
+ mTimespec.tv_sec = sec;
+ mTimespec.tv_nsec = usec*1000;
}
Timeval(const struct timeval& wTimeval)
- :mTimeval(wTimeval)
- {}
+ {
+ mTimespec.tv_sec = wTimeval.tv_sec;
+ mTimespec.tv_nsec = wTimeval.tv_sec*1000;
+ }
/**
- Create a Timeval offset into the future.
+ Create a Timespec offset into the future.
@param offset milliseconds
*/
Timeval(unsigned offset=0) { future(offset); }
@@ -76,8 +78,9 @@ class Timeval {
/** Return total seconds. */
double seconds() const;
- uint32_t sec() const { return mTimeval.tv_sec; }
- uint32_t usec() const { return mTimeval.tv_usec; }
+ uint32_t sec() const { return mTimespec.tv_sec; }
+ uint32_t usec() const { return mTimespec.tv_nsec / 1000; }
+ uint32_t nsec() const { return mTimespec.tv_nsec; }
/** Return differnce from other (other-self), in ms. */
long delta(const Timeval& other) const;
@@ -88,11 +91,11 @@ class Timeval {
/** Remaining time in ms. */
long remaining() const { return -elapsed(); }
- /** Return true if the time has passed, as per gettimeofday. */
+ /** Return true if the time has passed, as per clock_gettime(CLOCK_REALTIME). */
bool passed() const;
/** Add a given number of minutes to the time. */
- void addMinutes(unsigned minutes) { mTimeval.tv_sec += minutes*60; }
+ void addMinutes(unsigned minutes) { mTimespec.tv_sec += minutes*60; }
};