2 #include "ace/Truncate.h"
4 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
6 /// Returns the value of the object as a timeval.
8 ACE_Time_Value::operator timeval () const
10 // ACE_OS_TRACE ("ACE_Time_Value::operator timeval");
11 #if defined (ACE_HAS_TIME_T_LONG_MISMATCH)
12 // Recall that on some Windows we substitute another type for timeval in tv_
13 ACE_Time_Value *me = const_cast<ACE_Time_Value*> (this);
14 me->ext_tv_.tv_sec = ACE_Utils::truncate_cast<long> (this->tv_.tv_sec);
15 me->ext_tv_.tv_usec = ACE_Utils::truncate_cast<long> (this->tv_.tv_usec);
19 #endif /* ACE_HAS_TIME_T_LONG_MISMATCH */
23 ACE_Time_Value::set (const timeval &tv)
25 // ACE_OS_TRACE ("ACE_Time_Value::set");
26 this->tv_.tv_sec = tv.tv_sec;
27 this->tv_.tv_usec = tv.tv_usec;
33 ACE_Time_Value::ACE_Time_Value (const struct timeval &tv)
35 // ACE_OS_TRACE ("ACE_Time_Value::ACE_Time_Value");
40 ACE_Time_Value::operator const timeval * () const
42 // ACE_OS_TRACE ("ACE_Time_Value::operator const timeval *");
43 #if defined (ACE_HAS_TIME_T_LONG_MISMATCH)
44 // Recall that on some Windows we substitute another type for timeval in tv_
45 ACE_Time_Value *me = const_cast<ACE_Time_Value*> (this);
46 me->ext_tv_.tv_sec = ACE_Utils::truncate_cast<long> (this->tv_.tv_sec);
47 me->ext_tv_.tv_usec = ACE_Utils::truncate_cast<long> (this->tv_.tv_usec);
48 return (const timeval *) &this->ext_tv_;
50 return (const timeval *) &this->tv_;
51 #endif /* ACE_HAS_TIME_T_LONG_MISMATCH */
55 ACE_Time_Value::set (time_t sec, suseconds_t usec)
57 // ACE_OS_TRACE ("ACE_Time_Value::set");
58 this->tv_.tv_sec = sec;
59 this->tv_.tv_usec = usec;
61 if ((__builtin_constant_p(sec) &
62 __builtin_constant_p(usec)) &&
63 (sec >= 0 && usec >= 0 && usec < ACE_ONE_SECOND_IN_USECS))
70 ACE_Time_Value::set (double d)
72 // ACE_OS_TRACE ("ACE_Time_Value::set");
73 if (d < ACE_Numeric_Limits<time_t>::min())
75 this->tv_.tv_sec = ACE_Numeric_Limits<time_t>::min();
76 this->tv_.tv_usec = -ACE_ONE_SECOND_IN_USECS + 1;
78 else if (d > ACE_Numeric_Limits<time_t>::max())
80 this->tv_.tv_sec = ACE_Numeric_Limits<time_t>::max();
81 this->tv_.tv_usec = ACE_ONE_SECOND_IN_USECS - 1;
85 time_t l = (time_t) d;
87 this->tv_.tv_usec = (suseconds_t) ((d - (double) l) * ACE_ONE_SECOND_IN_USECS + (d < 0 ? -0.5 : 0.5));
91 /// Initializes a timespec_t. Note that this approach loses precision
92 /// since it converts the nano-seconds into micro-seconds. But then
93 /// again, do any real systems have nano-second timer precision?!
95 ACE_Time_Value::set (const timespec_t &tv)
97 // ACE_OS_TRACE ("ACE_Time_Value::set");
100 tv.tv_nsec / 1000); // Convert nanoseconds into microseconds.
104 ACE_Time_Value::ACE_Time_Value ()
107 // ACE_OS_TRACE ("ACE_Time_Value::ACE_Time_Value");
112 ACE_Time_Value::ACE_Time_Value (time_t sec, suseconds_t usec)
114 // ACE_OS_TRACE ("ACE_Time_Value::ACE_Time_Value");
115 this->set (sec, usec);
118 /// Returns number of seconds.
120 ACE_Time_Value::sec () const
122 // ACE_OS_TRACE ("ACE_Time_Value::sec");
123 return this->tv_.tv_sec;
126 /// Sets the number of seconds.
128 ACE_Time_Value::sec (time_t sec)
130 // ACE_OS_TRACE ("ACE_Time_Value::sec");
131 this->tv_.tv_sec = sec;
134 /// Converts from Time_Value format into milli-seconds format.
135 ACE_INLINE unsigned long
136 ACE_Time_Value::msec () const
138 // ACE_OS_TRACE ("ACE_Time_Value::msec");
140 // Note - we're truncating a value here, which can lose data. This is
141 // called out in the user documentation for this with a recommendation to
142 // use msec(ACE_UINT64&) instead, so just go ahead and truncate.
143 time_t secs = this->tv_.tv_sec * 1000 + this->tv_.tv_usec / 1000;
144 return ACE_Utils::truncate_cast<unsigned long> (secs);
147 ACE_INLINE ACE_UINT64
148 ACE_Time_Value::get_msec () const
150 // ACE_OS_TRACE ("ACE_Time_Value::get_msec");
151 ACE_UINT64 ms = ACE_Utils::truncate_cast<ACE_UINT64> (this->tv_.tv_sec);
153 ms += (this->tv_.tv_usec / 1000);
158 ACE_Time_Value::msec (ACE_UINT64 &ms) const
160 // ACE_OS_TRACE ("ACE_Time_Value::msec");
161 ms = this->get_msec ();
165 ACE_Time_Value::msec (ACE_UINT64 &ms) /*const*/
167 // ACE_OS_TRACE ("ACE_Time_Value::msec");
168 const ACE_Time_Value *tv = this;
173 ACE_Time_Value::set_msec (const ACE_UINT64 &ms)
175 // ACE_OS_TRACE ("ACE_Time_Value::set_msec");
176 // Convert millisecond units to seconds;
177 ACE_UINT64 secs = ms / 1000;
178 this->tv_.tv_sec = static_cast<long> (secs);
179 // Convert remainder to microseconds;
180 this->tv_.tv_usec = static_cast<long>((ms - (secs * 1000)) * 1000);
183 /// Converts from milli-seconds format into Time_Value format.
185 ACE_Time_Value::msec (long milliseconds)
187 // ACE_OS_TRACE ("ACE_Time_Value::msec");
188 // Convert millisecond units to seconds;
189 long secs = milliseconds / 1000;
190 this->tv_.tv_sec = secs;
191 // Convert remainder to microseconds;
192 this->tv_.tv_usec = (milliseconds - (secs * 1000)) * 1000;
195 /// Converts from milli-seconds format into Time_Value format.
197 ACE_Time_Value::msec (int milliseconds)
199 ACE_Time_Value::msec (static_cast<long> (milliseconds));
202 /// Returns number of micro-seconds.
203 ACE_INLINE suseconds_t
204 ACE_Time_Value::usec () const
206 // ACE_OS_TRACE ("ACE_Time_Value::usec");
207 return this->tv_.tv_usec;
210 /// Sets the number of micro-seconds.
212 ACE_Time_Value::usec (suseconds_t usec)
214 // ACE_OS_TRACE ("ACE_Time_Value::usec");
215 this->tv_.tv_usec = usec;
219 ACE_Time_Value::to_usec (ACE_UINT64 & usec) const
221 // ACE_OS_TRACE ("ACE_Time_Value::to_usec");
222 usec = static_cast<ACE_UINT64> (this->tv_.tv_sec);
224 usec += this->tv_.tv_usec;
227 ACE_INLINE ACE_Time_Value
228 operator * (double d, const ACE_Time_Value &tv)
230 return ACE_Time_Value (tv) *= d;
233 ACE_INLINE ACE_Time_Value
234 operator * (const ACE_Time_Value &tv, double d)
236 return ACE_Time_Value (tv) *= d;
239 /// True if tv1 > tv2.
241 operator > (const ACE_Time_Value &tv1,
242 const ACE_Time_Value &tv2)
244 // ACE_OS_TRACE ("operator >");
245 if (tv1.sec () > tv2.sec ())
247 else if (tv1.sec () == tv2.sec ()
248 && tv1.usec () > tv2.usec ())
254 /// True if tv1 >= tv2.
256 operator >= (const ACE_Time_Value &tv1,
257 const ACE_Time_Value &tv2)
259 // ACE_OS_TRACE ("operator >=");
260 if (tv1.sec () > tv2.sec ())
262 else if (tv1.sec () == tv2.sec ()
263 && tv1.usec () >= tv2.usec ())
269 /// Returns the value of the object as a timespec_t.
271 ACE_Time_Value::operator timespec_t () const
273 // ACE_OS_TRACE ("ACE_Time_Value::operator timespec_t");
275 tv.tv_sec = this->sec ();
276 // Convert microseconds into nanoseconds.
277 tv.tv_nsec = this->tv_.tv_usec * 1000;
281 /// Initializes the ACE_Time_Value object from a timespec_t.
283 ACE_Time_Value::ACE_Time_Value (const timespec_t &tv)
286 // ACE_OS_TRACE ("ACE_Time_Value::ACE_Time_Value");
290 /// True if tv1 < tv2.
292 operator < (const ACE_Time_Value &tv1,
293 const ACE_Time_Value &tv2)
295 // ACE_OS_TRACE ("operator <");
299 /// True if tv1 >= tv2.
301 operator <= (const ACE_Time_Value &tv1,
302 const ACE_Time_Value &tv2)
304 // ACE_OS_TRACE ("operator <=");
308 /// True if tv1 == tv2.
310 operator == (const ACE_Time_Value &tv1,
311 const ACE_Time_Value &tv2)
313 // ACE_OS_TRACE ("operator ==");
314 return tv1.sec () == tv2.sec ()
315 && tv1.usec () == tv2.usec ();
318 /// True if tv1 != tv2.
320 operator != (const ACE_Time_Value &tv1,
321 const ACE_Time_Value &tv2)
323 // ACE_OS_TRACE ("operator !=");
324 return !(tv1 == tv2);
328 ACE_INLINE ACE_Time_Value &
329 ACE_Time_Value::operator+= (const ACE_Time_Value &tv)
331 // ACE_OS_TRACE ("ACE_Time_Value::operator+=");
332 this->sec (this->sec () + tv.sec ());
333 this->usec (this->usec () + tv.usec ());
338 ACE_INLINE ACE_Time_Value &
339 ACE_Time_Value::operator+= (time_t tv)
341 // ACE_OS_TRACE ("ACE_Time_Value::operator+=");
342 this->sec (this->sec () + tv);
346 ACE_INLINE ACE_Time_Value &
347 ACE_Time_Value::operator= (time_t tv)
349 // ACE_OS_TRACE ("ACE_Time_Value::operator=");
355 /// Subtract TV to this.
356 ACE_INLINE ACE_Time_Value &
357 ACE_Time_Value::operator-= (const ACE_Time_Value &tv)
359 // ACE_OS_TRACE ("ACE_Time_Value::operator-=");
360 this->sec (this->sec () - tv.sec ());
361 this->usec (this->usec () - tv.usec ());
366 ACE_INLINE ACE_Time_Value &
367 ACE_Time_Value::operator-= (time_t tv)
369 // ACE_OS_TRACE ("ACE_Time_Value::operator-=");
370 this->sec (this->sec () - tv);
374 /// Adds two ACE_Time_Value objects together, returns the sum.
375 ACE_INLINE ACE_Time_Value
376 operator + (const ACE_Time_Value &tv1,
377 const ACE_Time_Value &tv2)
379 // ACE_OS_TRACE ("operator +");
380 ACE_Time_Value sum (tv1);
386 /// Subtracts two ACE_Time_Value objects, returns the difference.
387 ACE_INLINE ACE_Time_Value
388 operator - (const ACE_Time_Value &tv1,
389 const ACE_Time_Value &tv2)
391 // ACE_OS_TRACE ("operator -");
392 ACE_Time_Value delta (tv1);
398 ACE_END_VERSIONED_NAMESPACE_DECL
400 // Additional chrono streaming operators.
405 ACE_INLINE nanoseconds&
406 operator <<(nanoseconds &ns, ACE_Time_Value const &tv)
408 ns = duration_cast<nanoseconds>(seconds{tv.sec ()}) +
409 duration_cast<nanoseconds>(microseconds{tv.usec()});
413 ACE_INLINE microseconds&
414 operator <<(microseconds &us, ACE_Time_Value const &tv)
416 us= duration_cast<microseconds>(seconds{tv.sec ()}) +
417 microseconds{tv.usec()};
421 ACE_INLINE milliseconds&
422 operator <<(milliseconds &ms, ACE_Time_Value const &tv)
424 ms = duration_cast<milliseconds>(seconds{tv.sec ()}) +
425 duration_cast<milliseconds>(microseconds{tv.usec()});
430 operator <<(seconds &s, ACE_Time_Value const &tv)
432 s = seconds{tv.sec ()} +
433 duration_cast<seconds>(microseconds{tv.usec()});
438 operator <<(minutes &m, ACE_Time_Value const &tv)
440 m = duration_cast<minutes>(seconds{tv.sec ()}) +
441 duration_cast<minutes>(microseconds{tv.usec()});
446 operator <<(hours &h, ACE_Time_Value const &tv)
448 h = duration_cast<hours>(seconds{tv.sec ()}) +
449 duration_cast<hours>(microseconds{tv.usec()});
454 ACE_INLINE nanoseconds&
455 operator +=(nanoseconds &ns, ACE_Time_Value const &tv)
457 ns += duration_cast<nanoseconds>(seconds{tv.sec ()}) +
458 duration_cast<nanoseconds>(microseconds{tv.usec()});
462 ACE_INLINE microseconds&
463 operator +=(microseconds &us, ACE_Time_Value const &tv)
465 us += duration_cast<microseconds>(seconds{tv.sec ()}) +
466 microseconds{tv.usec()};
470 ACE_INLINE milliseconds&
471 operator +=(milliseconds &ms, ACE_Time_Value const &tv)
473 ms += duration_cast<milliseconds>(seconds{tv.sec ()}) +
474 duration_cast<milliseconds>(microseconds{tv.usec()});
479 operator +=(seconds &s, ACE_Time_Value const &tv)
481 s += seconds{tv.sec ()} +
482 duration_cast<seconds>(microseconds{tv.usec()});
487 operator +=(minutes &m, ACE_Time_Value const &tv)
489 m += duration_cast<minutes>(seconds{tv.sec ()}) +
490 duration_cast<minutes>(microseconds{tv.usec()});
495 operator +=(hours &h, ACE_Time_Value const &tv)
497 h += duration_cast<hours>(seconds{tv.sec ()}) +
498 duration_cast<hours>(microseconds{tv.usec()});
503 ACE_INLINE nanoseconds&
504 operator -=(nanoseconds &ns, ACE_Time_Value const &tv)
506 ns -= duration_cast<nanoseconds>(seconds{tv.sec ()}) +
507 duration_cast<nanoseconds>(microseconds{tv.usec()});
511 ACE_INLINE microseconds&
512 operator -=(microseconds &us, ACE_Time_Value const &tv)
514 us -= duration_cast<microseconds>(seconds{tv.sec ()}) +
515 microseconds{tv.usec()};
519 ACE_INLINE milliseconds&
520 operator -=(milliseconds &ms, ACE_Time_Value const &tv)
522 ms -= duration_cast<milliseconds>(seconds{tv.sec ()}) +
523 duration_cast<milliseconds>(microseconds{tv.usec()});
528 operator -=(seconds &s, ACE_Time_Value const &tv)
530 s -= seconds{tv.sec ()} +
531 duration_cast<seconds>(microseconds{tv.usec()});
536 operator -=(minutes &m, ACE_Time_Value const &tv)
538 m -= duration_cast<minutes>(seconds{tv.sec ()}) +
539 duration_cast<minutes>(microseconds{tv.usec()});
544 operator -=(hours &h, ACE_Time_Value const &tv)
546 h -= duration_cast<hours>(seconds{tv.sec ()}) +
547 duration_cast<hours>(microseconds{tv.usec()});