38#include "blocxx/BLOCXX_config.h"
50 , m_start(getCurrentTime())
84template <
typename T,
typename U>
85void safeAssign(T& x, U y)
87 if (y < (std::numeric_limits<T>::min)())
89 x = (std::numeric_limits<T>::min)();
91 else if (y > (std::numeric_limits<T>::max)())
93 x = (std::numeric_limits<T>::max)();
97 x =
static_cast<T
>(y + 0.5);
101bool compareInterval(
const DateTime& first,
const DateTime& timeToTest,
double seconds)
105 DateTime diff = timeToTest - first;
106 double diff1 = diff.get() + diff.getMicrosecond() * 1e-6;
107 return diff1 >= seconds;
138#ifdef BLOCXX_HAVE_STRUCT_TIMEVAL
141TimeoutTimer::asTimeval(::timeval& tv,
double maxSeconds)
const
152 safeAssign(temp.tv_sec, maxSeconds);
154 safeAssign(temp.tv_usec, modf(maxSeconds, &dummy) * 1e6);
155 if (
infinite() || temp.tv_sec < tv.tv_sec)
159 else if (temp.tv_sec == tv.tv_sec && temp.tv_usec < tv.tv_usec)
161 tv.tv_usec = temp.tv_usec;
169TimeoutTimer::asTimeval(::timeval& tv)
const
183 tv.tv_sec = diff.
get();
184 tv.tv_usec = diff.getMicrosecond();
196 safeAssign(tv.tv_sec, timeTillExpiration);
198 safeAssign(tv.tv_usec, modf(timeTillExpiration, &dummy) * 1e6);
220 seconds =
static_cast<double>(diff.
get()) + (
static_cast<double>(diff.
getMicrosecond()) / 1e6);
236 seconds = seconds > maxSeconds ? maxSeconds : seconds;
266 const long MICROSECONDS_PER_SECOND = 1000000;
272 safeAssign(microSecs,
277 secs += microSecs / MICROSECONDS_PER_SECOND;
278 microSecs = microSecs % MICROSECONDS_PER_SECOND;
286TimeoutTimer::asDWORDMs()
const
321 safeAssign(rval,
calcSeconds(maxSeconds) * 1000.0);
325#ifdef BLOCXX_HAVE_STRUCT_TIMESPEC
327TimeoutTimer::asTimespec(::timespec& ts)
const
329 const long NANOSECONDS_PER_MICROSECOND = 1000;
330 const long NANOSECONDS_PER_MILLISECOND = 1000000;
331 const long NANOSECONDS_PER_SECOND = 1000000000;
339 ts.tv_sec = timeoutTime.
get();
340 ts.tv_nsec = timeoutTime.
getMicrosecond() * NANOSECONDS_PER_MICROSECOND;
353 safeAssign(ts.tv_nsec,
358 ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
359 ts.tv_nsec = ts.tv_nsec % NANOSECONDS_PER_SECOND;
The DateTime class is an abstraction for date time data.
UInt32 getMicrosecond() const
Get the microsecond of the second for this DateTime object.
static DateTime getCurrent()
Gets a DateTime instance set to the current system time.
A timeout can be absolute, which means that it will happen at the specified DateTime.
DateTime getAbsolute() const
float getRelative() const
ETimeoutType getType() const
static Timeout relative(float seconds)
static Timeout absolute(const DateTime &dt)
TimeoutTimer(const Timeout &x)
bool expired() const
Indicates whether the last loop time has exceeded the timeout.
void start()
Meant to be called by timeout functions which loop.
void loop()
Meant to be called by timeout functions which loop, but don't want to reset the interval.
double calcSeconds() const
Timeout asRelativeTimeout() const
Timeout asTimeout() const
Timeout asAbsoluteTimeout() const
Converts the timer to an absolute timeout.
virtual DateTime getCurrentTime() const
void resetOnLoop()
Meant to be called by timeout functions which loop, and that want to reset the interval.
bool infinite() const
Indicates whether the timeout will never expire.