38 #include "blocxx/BLOCXX_config.h" 50 , m_start(getCurrentTime())
84 template <
typename T,
typename U>
85 void 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);
101 bool compareInterval(
const DateTime& first,
const DateTime& timeToTest,
double seconds)
107 return diff1 >= seconds;
138 #ifdef BLOCXX_HAVE_STRUCT_TIMEVAL 141 TimeoutTimer::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;
169 TimeoutTimer::asTimeval(::timeval& tv)
const 183 tv.tv_sec = diff.
get();
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;
286 TimeoutTimer::asDWORDMs()
const 321 safeAssign(rval,
calcSeconds(maxSeconds) * 1000.0);
325 #ifdef BLOCXX_HAVE_STRUCT_TIMESPEC 327 TimeoutTimer::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;
Timeout asAbsoluteTimeout() const
Converts the timer to an absolute timeout.
virtual DateTime getCurrentTime() const
void loop()
Meant to be called by timeout functions which loop, but don't want to reset the interval.
UInt32 getMicrosecond() const
Get the microsecond of the second for this DateTime object.
bool infinite() const
Indicates whether the timeout will never expire.
bool expired() const
Indicates whether the last loop time has exceeded the timeout.
DateTime getAbsolute() const
static Timeout relative(float seconds)
ETimeoutType getType() const
A timeout can be absolute, which means that it will happen at the specified DateTime.
void start()
Meant to be called by timeout functions which loop.
double calcSeconds() const
The DateTime class is an abstraction for date time data.
Timeout asRelativeTimeout() const
static DateTime getCurrent()
Gets a DateTime instance set to the current system time.
float getRelative() const
TimeoutTimer(const Timeout &x)
void resetOnLoop()
Meant to be called by timeout functions which loop, and that want to reset the interval.
static Timeout absolute(const DateTime &dt)
Timeout asTimeout() const