diff options
author | Pau Espin Pedrol <pespin@sysmocom.de> | 2018-12-12 15:58:24 +0100 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2018-12-13 13:58:24 +0000 |
commit | 0646b3ce751bc485a466cda1580eaee786bead87 (patch) | |
tree | 66681a0ec1fee9a9c564ed52811683758210d0d1 /CommonLibs/Timeval.h | |
parent | 46cf9efc8ee0b006750f90023079bc02ebc17bc1 (diff) |
Timeval: Move implementation to use clock_gettime and timespec
According to gettimeofday manual:
"Applications should use the clock_gettime() function instead of the
obsolescent gettimeofday() function."
Furthermore, it may be desirable in the future to use other clocks such
as monotonic.
Change-Id: I2286998c5eefbf3c3dfb105c223daec7a1083803
Diffstat (limited to 'CommonLibs/Timeval.h')
-rw-r--r-- | CommonLibs/Timeval.h | 27 |
1 files changed, 15 insertions, 12 deletions
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; } }; |