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 bool IsZero()
const {
return !m_interval.IsSet(); }
267 void AsTimeval(
struct timeval *tv)
const { m_interval.AsTimeval(tv); }
269 time_t Seconds()
const {
return m_interval.Seconds(); }
270 suseconds_t MicroSeconds()
const {
return m_interval.MicroSeconds(); }
272 int64_t InMilliSeconds()
const {
return m_interval.InMilliSeconds(); }
273 int64_t AsInt()
const {
return m_interval.AsInt(); }
275 std::string ToString()
const {
return m_interval.ToString(); }
277 friend std::ostream& operator<< (std::ostream &out,
279 return out << interval.m_interval.ToString();
297 explicit TimeStamp(
const struct timeval ×tamp) : m_tv(timestamp) {}
303 if (
this != &other) {
309 TimeStamp& operator=(
const struct timeval &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; }
318 bool operator>=(
const TimeStamp &other)
const {
return m_tv >= other.m_tv; }
319 bool operator<(
const TimeStamp &other)
const {
return m_tv < other.m_tv; }
320 bool operator<=(
const TimeStamp &other)
const {
return m_tv <= other.m_tv; }
324 m_tv += interval.m_interval;
329 m_tv -= interval.m_interval;
334 return TimeStamp(m_tv + interval.m_interval);
342 return TimeStamp(m_tv - interval.m_interval);
346 bool IsSet()
const {
return m_tv.IsSet(); }
348 time_t Seconds()
const {
return m_tv.Seconds(); }
349 suseconds_t MicroSeconds()
const {
return m_tv.MicroSeconds(); }
351 std::string ToString()
const {
return m_tv.ToString(); }
353 friend std::ostream& operator<< (std::ostream &out,
355 return out << timestamp.m_tv.ToString();
372 virtual void CurrentTime(
TimeStamp *timestamp)
const {
374 gettimeofday(&tv, NULL);
392 m_offset += interval;
395 void AdvanceTime(int32_t sec, int32_t usec) {
397 m_offset += interval;
400 void CurrentTime(
TimeStamp *timestamp)
const {
402 gettimeofday(&tv, NULL);
404 *timestamp += m_offset;
410 #endif // INCLUDE_OLA_CLOCK_H_