29 #ifndef INCLUDE_OLA_CLOCK_H_
30 #define INCLUDE_OLA_CLOCK_H_
48 static const int USEC_IN_SECONDS = 1000000;
49 static const int ONE_THOUSAND = 1000;
65 explicit BaseTimeVal(
const struct timeval ×tamp) { m_tv = timestamp; }
66 explicit BaseTimeVal(int64_t interval_useconds) { Set(interval_useconds); }
85 return timercmp(&m_tv, &other.m_tv, ==);
89 return !(*
this == other);
93 return timercmp(&m_tv, &other.m_tv, >);
97 return timercmp(&m_tv, &other.m_tv, >=);
101 return timercmp(&m_tv, &other.m_tv, <);
105 return timercmp(&m_tv, &other.m_tv, <=);
110 if (
this != &other) {
111 timeradd(&m_tv, &other.m_tv, &m_tv);
117 if (
this != &other) {
118 TimerSub(m_tv, other.m_tv, &m_tv);
131 TimerSub(m_tv, other.m_tv, &result.m_tv);
136 int64_t as_int = (*this).AsInt();
143 return timerisset(&m_tv);
146 void AsTimeval(
struct timeval *tv)
const {
151 time_t Seconds()
const {
return m_tv.tv_sec; }
153 suseconds_t MicroSeconds()
const {
return m_tv.tv_usec; }
156 int64_t InMilliSeconds()
const {
157 return (m_tv.tv_sec * static_cast<int64_t>(ONE_THOUSAND) +
158 m_tv.tv_usec / ONE_THOUSAND);
162 int64_t AsInt()
const {
163 return (m_tv.tv_sec * static_cast<int64_t>(USEC_IN_SECONDS) +
167 std::string ToString()
const {
168 std::stringstream str;
169 str << m_tv.tv_sec <<
"." << std::setfill(
'0') << std::setw(6)
180 void TimerSub(
const struct timeval &tv1,
const struct timeval &tv2,
181 struct timeval *result)
const {
182 result->tv_sec = tv1.tv_sec - tv2.tv_sec;
183 result->tv_usec = tv1.tv_usec - tv2.tv_usec;
184 if (result->tv_usec < 0) {
186 result->tv_usec += USEC_IN_SECONDS;
190 void Set(int64_t interval_useconds) {
192 m_tv.tv_sec =
static_cast<time_t
>(
193 interval_useconds / USEC_IN_SECONDS);
195 m_tv.tv_sec = interval_useconds / USEC_IN_SECONDS;
198 #ifdef HAVE_SUSECONDS_T
199 m_tv.tv_usec =
static_cast<suseconds_t
>(
200 interval_useconds % USEC_IN_SECONDS);
202 m_tv.tv_usec = interval_useconds % USEC_IN_SECONDS;
214 TimeInterval(int32_t sec, int32_t usec) : m_interval(sec, usec) {}
215 explicit TimeInterval(int64_t usec) : m_interval(usec) {}
221 if (
this != &other) {
222 m_interval = other.m_interval;
229 return m_interval == other.m_interval;
233 return m_interval != other.m_interval;
237 return m_interval > other.m_interval;
241 return m_interval >= other.m_interval;
245 return m_interval < other.m_interval;
249 return m_interval <= other.m_interval;
254 if (
this != &other) {
255 m_interval += other.m_interval;
265 void AsTimeval(
struct timeval *tv)
const { m_interval.AsTimeval(tv); }
267 time_t Seconds()
const {
return m_interval.Seconds(); }
268 suseconds_t MicroSeconds()
const {
return m_interval.MicroSeconds(); }
270 int64_t InMilliSeconds()
const {
return m_interval.InMilliSeconds(); }
271 int64_t AsInt()
const {
return m_interval.AsInt(); }
273 std::string ToString()
const {
return m_interval.ToString(); }
275 friend ostream& operator<< (ostream &out,
const TimeInterval &interval) {
276 return out << interval.m_interval.ToString();
294 explicit TimeStamp(
const struct timeval ×tamp) : m_tv(timestamp) {}
300 if (
this != &other) {
306 TimeStamp& operator=(
const struct timeval &tv) {
312 bool operator==(
const TimeStamp &other)
const {
return m_tv == other.m_tv; }
313 bool operator!=(
const TimeStamp &other)
const {
return m_tv != other.m_tv; }
314 bool operator>(
const TimeStamp &other)
const {
return m_tv > other.m_tv; }
315 bool operator>=(
const TimeStamp &other)
const {
return m_tv >= other.m_tv; }
316 bool operator<(
const TimeStamp &other)
const {
return m_tv < other.m_tv; }
317 bool operator<=(
const TimeStamp &other)
const {
return m_tv <= other.m_tv; }
321 m_tv += interval.m_interval;
326 m_tv -= interval.m_interval;
331 return TimeStamp(m_tv + interval.m_interval);
339 return TimeStamp(m_tv - interval.m_interval);
343 bool IsSet()
const {
return m_tv.IsSet(); }
345 time_t Seconds()
const {
return m_tv.Seconds(); }
346 suseconds_t MicroSeconds()
const {
return m_tv.MicroSeconds(); }
348 std::string ToString()
const {
return m_tv.ToString(); }
350 friend ostream& operator<< (ostream &out,
const TimeStamp ×tamp) {
351 return out << timestamp.m_tv.ToString();
368 virtual void CurrentTime(
TimeStamp *timestamp)
const {
370 gettimeofday(&tv, NULL);
389 m_offset += interval;
392 void AdvanceTime(int32_t sec, int32_t usec) {
394 m_offset += interval;
397 void CurrentTime(
TimeStamp *timestamp)
const {
399 gettimeofday(&tv, NULL);
401 *timestamp += m_offset;
407 #endif // INCLUDE_OLA_CLOCK_H_