Fawkes API  Fawkes Development Version
time.cpp
00001 
00002 /***************************************************************************
00003  *  time.c - A time class
00004  *
00005  *  Created: Wed Jun 06 16:50:11 2007
00006  *  Copyright  2007       Daniel Beck
00007  *             2007-2009  Tim Niemueller [www.niemueller.de]
00008  *
00009  ****************************************************************************/
00010 
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version. A runtime exception applies to
00015  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00016  *
00017  *  This program is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU Library General Public License for more details.
00021  *
00022  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00023  */
00024 
00025 #include <utils/time/time.h>
00026 #include <utils/time/clock.h>
00027 
00028 #include <core/exception.h>
00029 #include <core/exceptions/software.h>
00030 
00031 #include <time.h>
00032 #include <cmath>
00033 #include <cstdio>
00034 #include <cstdlib>
00035 #include <cstring>
00036 #include <unistd.h>
00037 #include <limits>
00038 
00039 namespace fawkes {
00040 #if 0 /* just to make Emacs auto-indent happy */
00041 }
00042 #endif
00043 
00044 /** Instance of Time denoting the maximum value possible.
00045  * This is particularly useful when initializing a minimization in time.
00046  */
00047 const Time TIME_MAX = Time(std::numeric_limits<time_t>::max(), 999999);
00048 
00049 /** Instance of Time denoting the minimum value possible.
00050  * This is particularly useful when initializing a maximization in time.
00051  */
00052 const Time TIME_MIN = Time(0, 1);
00053 
00054 /** @class Time <utils/time/time.h>
00055  * A class for handling time.
00056  * @author Daniel Beck
00057  * @author Tim Niemueller
00058  *
00059  * @fn const timeval * Time::get_timeval() const
00060  * Obtain the timeval where the time is stored.
00061  * @return a const pointer to the timeval where the time is stored
00062  *
00063  * @fn long Time::get_sec() const
00064  * Get seconds.
00065  * @return seconds stored in time stamp
00066  *
00067  * @fn long Time::get_msec() const
00068  * Get milliseconds.
00069  * @return milliseconds stored in time stamp
00070  *
00071  * @fn long Time::get_usec() const
00072  * Get microseconds.
00073  * @return microseconds stored in time stamp
00074  *
00075  * @fn long Time::get_nsec() const
00076  * Get nanoseconds.
00077  * @return microsecons converted to nanoseconds
00078  *
00079  * @fn bool Time::is_zero() const
00080  * Check if time is zero.
00081  * @return true if time is zero, i.e. sec = usec = 0, false otherwise
00082  *
00083  * @fn void Time::get_timestamp(long &sec, long &usec) const
00084  * Get time stamp.
00085  * @param sec upon return contains seconds stored in time stamp
00086  * @param usec upon return contains microseconds stored in time stamp
00087  */
00088 
00089 /** Maximum size of string returned by str() and the minimum size
00090  * of the string passwd to str_r(). */
00091 // as recommened in asctime_r() docs
00092 const unsigned int Time::TIMESTR_SIZE = 26;
00093 
00094 
00095 /** Constructor.
00096  * Sets time to the current time.
00097  */
00098 Time::Time()
00099 {
00100   __clock = Clock::instance();
00101   __clock->get_time(&__time);
00102   __timestr = NULL;
00103 }
00104 
00105 
00106 /** Constructor.
00107  * Sets time to the given time.
00108  * @param tv the Time object is initialized with the time given in this timeval
00109  */
00110 Time::Time(const timeval* tv)
00111 {
00112   __time.tv_sec = tv->tv_sec;
00113   __time.tv_usec = tv->tv_usec;
00114   __clock = Clock::instance();
00115   __timestr = NULL;
00116 }
00117 
00118 
00119 /** Constructor.
00120  * Sets time to the given time. Basically the same as setting from a timeval struct
00121  * but the components are given separately.
00122  * @param sec time in seconds since the epoch (or time range)
00123  * @param usec fractions in microseconds added to sec
00124  * @param clock optional clock to use, if NULL Clock::instance() will be used
00125  */
00126 Time::Time(long sec, long usec, Clock *clock)
00127 {
00128   __time.tv_sec  = sec;
00129   __time.tv_usec = usec;
00130   if (clock) {
00131     __clock = clock;
00132   } else {
00133     __clock = Clock::instance();
00134   }
00135   __timestr = NULL;
00136 }
00137 
00138 
00139 /** Constructor.
00140  * Sets time to given number of ms, use for time range.
00141  * @param ms the Time object is initialized to the time given in milli-seconds
00142  */
00143 Time::Time(long ms)
00144 {
00145   time_t sec = (time_t) (ms / 1000.0);
00146   suseconds_t usec =  (ms % 1000) * 1000;
00147 
00148   __time.tv_sec = sec;
00149   __time.tv_usec = usec;
00150   __clock = Clock::instance();
00151   __timestr = NULL;
00152 }
00153 
00154 
00155 /** Constructor.
00156  * Sets time to given number of ms, use for time range.
00157  * @param s the Time object is initialized to the time given in seconds
00158  */
00159 Time::Time(double s)
00160 {
00161   time_t sec = (time_t) s;
00162   suseconds_t usec = (suseconds_t)roundf((s - sec) * 1000000.f);
00163 
00164   __time.tv_sec = sec;
00165   __time.tv_usec = usec;
00166   __clock = Clock::instance();
00167   __timestr = NULL;
00168 }
00169 
00170 
00171 /** Constructor.
00172  * This constructor uses the supplied clock for setting the time. The
00173  * time is set to the current time.
00174  * @param clock clock
00175  */
00176 Time::Time(Clock *clock)
00177 {
00178   this->__clock = clock;
00179   __clock->get_time(&__time);
00180   __timestr = NULL;
00181 }
00182 
00183 
00184 /** Copy constructor.
00185  * @param t time to copy
00186  */
00187 Time::Time(const Time &t)
00188 {
00189   __time.tv_sec  = t.__time.tv_sec;
00190   __time.tv_usec = t.__time.tv_usec;
00191   __clock        = t.__clock;
00192   if (t.__timestr) {
00193     __timestr = (char *)malloc(TIMESTR_SIZE);
00194     strncpy(__timestr, t.__timestr, TIMESTR_SIZE);
00195   } else {
00196     __timestr = NULL;
00197   }
00198 }
00199 
00200 
00201 /** Copy constructor.
00202  * @param t time to copy
00203  */
00204 Time::Time(const Time *t)
00205 {
00206   __time.tv_sec  = t->__time.tv_sec;
00207   __time.tv_usec = t->__time.tv_usec;
00208   __clock        = t->__clock;
00209   if (t->__timestr) {
00210     __timestr = (char *)malloc(TIMESTR_SIZE);
00211     strncpy(__timestr, t->__timestr, TIMESTR_SIZE);
00212   } else {
00213     __timestr = NULL;
00214   }
00215 }
00216 
00217 
00218 /** Destructor. */
00219 Time::~Time()
00220 {
00221   if (__timestr)  free(__timestr);
00222 }
00223 
00224 
00225 /** Convet time to seconds.
00226  * Convert the stored time in a floating point number representing the
00227  * number of seconds. For a time the integral part is the number of seconds
00228  * since the epoch, for ranges you get the value as a float second.
00229  * @return the time in seconds
00230  */
00231 double
00232 Time::in_sec() const
00233 {
00234   return ((double)__time.tv_sec + (double)__time.tv_usec / 1000000.);
00235 }
00236 
00237 
00238 /** Convert the stored time into milli-seconds.
00239  * @return the time in milli-seconds
00240  */
00241 long
00242 Time::in_msec() const
00243 {
00244   return (__time.tv_sec * 1000 + (long) (__time.tv_usec / 1000));
00245 }
00246 
00247 
00248 /** Convert the stored time into micro-seconds.
00249  * @return the time in micro-seconds
00250  */
00251 long
00252 Time::in_usec() const
00253 {
00254   return (__time.tv_sec * 1000000 + __time.tv_usec);
00255 }
00256 
00257 
00258 /** Sets the time.
00259  * @param tv set the time to this value
00260  */
00261 void
00262 Time::set_time(const timeval* tv)
00263 {
00264   __time.tv_sec = tv->tv_sec;
00265   __time.tv_usec = tv->tv_usec;
00266 }
00267 
00268 
00269 /** Sets the time.
00270  * @param sec seconds part of the time
00271  * @param usec microseconds part of the time
00272  */
00273 void
00274 Time::set_time(long int sec, long int usec)
00275 {
00276   __time.tv_sec  = sec;
00277   __time.tv_usec = usec;
00278 }
00279 
00280 
00281 /** Sets the time.
00282  * @param ms set the time to this value
00283  */
00284 void
00285 Time::set_time(long ms)
00286 {
00287   __time.tv_sec  = (time_t) (ms / 1000.0);
00288   __time.tv_usec = (ms % 1000) * 1000;
00289 }
00290 
00291 
00292 /** Sets the time.
00293  * @param s set the time to this value
00294  */
00295 void
00296 Time::set_time(double s)
00297 {
00298   __time.tv_sec  = (time_t)floor(s);
00299   __time.tv_usec = (suseconds_t)(s - __time.tv_sec) * 1000000;
00300 }
00301 
00302 /** Set time to given time.
00303  * this is equivalent to operator+, but can be used in situations where
00304  * the operator cannot be used (for example in Lua).
00305  * @param t time to set to
00306  */
00307 void
00308 Time::set_time(const Time &t)
00309 {
00310   *this = t;
00311 }
00312 
00313 
00314 /** Set time to given time.
00315  * @param t time to set to
00316  */
00317 void
00318 Time::set_time(const Time *t)
00319 {
00320   __time.tv_sec  = t->__time.tv_sec;
00321   __time.tv_usec = t->__time.tv_usec;
00322 }
00323 
00324 
00325 /** Set clock for this instance.
00326  * @param clock clock to use from now on
00327  */
00328 void
00329 Time::set_clock(Clock *clock)
00330 {
00331   if (clock == NULL) throw NullPointerException("Clock may not be NULL");
00332   __clock = clock;
00333 }
00334 
00335 
00336 /** Add seconds.
00337  * The effect is equivalent to operator+=(const double sec), but this
00338  * can be used when the operator is not available (i.e. wrapper languages)
00339  * and it does not return itself.
00340  * @param seconds time in seconds to add
00341  */
00342 void
00343 Time::add(double seconds)
00344 {
00345   *this += seconds;
00346 }
00347 
00348 /** Operator that adds times.
00349  * @param t the other summand
00350  * @return the sum
00351  */
00352 Time
00353 Time::operator+(const Time& t) const
00354 {
00355   Time ret;
00356   if (__time.tv_usec + t.__time.tv_usec >= 1000000)
00357   {
00358     ret.__time.tv_usec = __time.tv_usec + t.__time.tv_usec - 1000000;
00359     ret.__time.tv_sec = __time.tv_sec + t.__time.tv_sec + 1;
00360   }
00361   else
00362   {
00363     ret.__time.tv_usec = __time.tv_usec + t.__time.tv_usec;
00364     ret.__time.tv_sec = __time.tv_sec + t.__time.tv_sec;
00365   }
00366 
00367   return ret;
00368 }
00369 
00370 
00371 /** Operator that adds times.
00372  * @param t the other summand
00373  * @return the sum
00374  */
00375 Time
00376 Time::operator+(const Time* t) const
00377 {
00378   return *this + *t;
00379 }
00380 
00381 
00382 /** Operator that adds times.
00383  * @param sec number of seconds to add
00384  * @return the sum
00385  */
00386 Time
00387 Time::operator+(const double sec) const
00388 {
00389   Time ret;
00390   time_t sec_only = (time_t)floor(sec);
00391   suseconds_t usec_only = (suseconds_t)roundf((sec - sec_only) * 1000000);
00392   if ((__time.tv_usec + usec_only) >= 1000000)
00393   {
00394     ret.__time.tv_usec = __time.tv_usec + usec_only - 1000000;
00395     ret.__time.tv_sec = __time.tv_sec + sec_only + 1;
00396   }
00397   else
00398   {
00399     ret.__time.tv_usec = __time.tv_usec + usec_only;
00400     ret.__time.tv_sec = __time.tv_sec + sec_only;
00401   }
00402 
00403   return ret;
00404 }
00405 
00406 
00407 /** Operator to add usec value.
00408  * @param usec microseconds to add
00409  * @return new resulting instance
00410  */
00411 Time
00412 Time::operator+(const long int usec) const
00413 {
00414   Time ret;
00415   if ( __time.tv_usec + usec >= 1000000 )
00416   {
00417     //usec + __time.tv_usec might be more than 1 second
00418     long int tmp_usec = __time.tv_usec + usec;
00419     ret.__time.tv_usec  = tmp_usec % 1000000;
00420     ret.__time.tv_sec   = __time.tv_sec + (tmp_usec / 1000000);
00421   }
00422   else
00423   {
00424     ret.__time.tv_sec   = __time.tv_sec;
00425     ret.__time.tv_usec += usec;
00426   }
00427 
00428   return ret;
00429 }
00430 
00431 
00432 /** Operator that substracts one Time from another.
00433  * @param t the Time that is substracted
00434  * @return the difference
00435  */
00436 Time
00437 Time::operator-(const Time& t) const
00438 {
00439   Time ret;
00440   if (__time.tv_usec < t.__time.tv_usec)
00441   {
00442     ret.__time.tv_usec = 1000000 + __time.tv_usec - t.__time.tv_usec;
00443     ret.__time.tv_sec = __time.tv_sec - t.__time.tv_sec - 1;
00444   }
00445   else
00446   {
00447     ret.__time.tv_usec = __time.tv_usec - t.__time.tv_usec;
00448     ret.__time.tv_sec = __time.tv_sec - t.__time.tv_sec;
00449   }
00450 
00451   return ret;
00452 }
00453 
00454 
00455 /** Operator that substracts one Time from another.
00456  * @param t the Time that is substracted
00457  * @return the difference
00458  */
00459 double
00460 Time::operator-(const Time* t) const
00461 {
00462   return time_diff_sec(__time, t->__time);
00463 }
00464 
00465 
00466 /** Operator that subtracts some time.
00467  * @param sec number of seconds to subtract
00468  * @return the difference
00469  */
00470 Time
00471 Time::operator-(const double sec) const
00472 {
00473   Time ret;
00474   time_t sec_only = (time_t)floor(sec);
00475   suseconds_t usec_only = (suseconds_t)roundf((sec - sec_only) * 1000000);
00476   if (__time.tv_usec < usec_only)
00477   {
00478     ret.__time.tv_usec = 1000000 + __time.tv_usec - usec_only;
00479     ret.__time.tv_sec = __time.tv_sec - sec_only - 1;
00480   }
00481   else
00482   {
00483     ret.__time.tv_usec = __time.tv_usec - usec_only;
00484     ret.__time.tv_sec = __time.tv_sec - sec_only;
00485   }
00486 
00487   return ret;
00488 }
00489 
00490 
00491 /** Operator that subtracts some time.
00492  * @param usec number of microseconds to subtract
00493  * @return the difference
00494  */
00495 Time
00496 Time::operator-(const long int usec) const
00497 {
00498   Time ret;
00499   time_t sec_only = usec / 1000000;
00500   suseconds_t usec_only = usec % 1000000;
00501   if (__time.tv_usec < usec_only)
00502   {
00503     ret.__time.tv_usec = 1000000 + __time.tv_usec - usec_only;
00504     ret.__time.tv_sec = __time.tv_sec - sec_only - 1;
00505   }
00506   else
00507   {
00508     ret.__time.tv_usec = __time.tv_usec - usec_only;
00509     ret.__time.tv_sec = __time.tv_sec - sec_only;
00510   }
00511 
00512   return ret;
00513 }
00514 
00515 
00516 /** += operator
00517  * @param t the other time
00518  * @return reference to this instance
00519  */
00520 Time &
00521 Time::operator+=(const Time& t)
00522 {
00523   if (__time.tv_usec + t.__time.tv_usec >= 1000000)
00524   {
00525     __time.tv_usec += t.__time.tv_usec - 1000000;
00526     __time.tv_sec  += t.__time.tv_sec + 1;
00527   }
00528   else
00529   {
00530     __time.tv_usec += t.__time.tv_usec;
00531     __time.tv_sec  += t.__time.tv_sec;
00532   }
00533 
00534   return *this;
00535 }
00536 
00537 
00538 /** += operator
00539  * @param usec microseconds to add
00540  * @return reference to this instance
00541  */
00542 Time &
00543 Time::operator+=(const long int usec)
00544 {
00545   if ( __time.tv_usec + usec >= 1000000 )
00546   {
00547     //usec + __time.tv_usec might be more than 1 second
00548     long int tmp_usec = __time.tv_usec + usec;
00549     __time.tv_usec = tmp_usec % 1000000;
00550     __time.tv_sec  += tmp_usec / 1000000;
00551   }
00552   else
00553   {
00554     __time.tv_usec += usec;
00555   }
00556 
00557   return *this;
00558 }
00559 
00560 
00561 /** += operator for double seconds
00562  * @param sec number of seconds to add
00563  * @return the sum
00564  */
00565 Time &
00566 Time::operator+=(const double sec)
00567 {
00568   time_t sec_only = (time_t)floor(sec);
00569   suseconds_t usec_only = (suseconds_t)roundf((sec - sec_only) * 1000000);
00570   if ((__time.tv_usec + usec_only) >= 1000000)
00571   {
00572     __time.tv_usec += usec_only - 1000000;
00573     __time.tv_sec  += sec_only + 1;
00574   }
00575   else
00576   {
00577     __time.tv_usec += usec_only;
00578     __time.tv_sec  += sec_only;
00579   }
00580 
00581   return *this;
00582 }
00583 
00584 
00585 /** -= operator.
00586  * @param t the other time
00587  * @return reference to this instance  after subtraction 
00588  */
00589 Time &
00590 Time::operator-=(const Time& t)
00591 {
00592   *this = *this - t;
00593   return *this;
00594 }
00595 
00596 
00597 /** -= operator.
00598  * @param sec seconds to subtract
00599  * @return reference to this instance  after subtraction 
00600  */
00601 Time &
00602 Time::operator-=(const double sec)
00603 {
00604   *this = *this - sec;
00605   return *this;
00606 }
00607 
00608 
00609 /** -= operator.
00610  * @param usec microseconds to subtract
00611  * @return reference to this instance after subtraction 
00612  */
00613 Time &
00614 Time::operator-=(const long int usec)
00615 {
00616   *this = *this - usec;
00617   return *this;
00618 }
00619 
00620 
00621 /** Assign operator.
00622  * @param t time to assign to this instance
00623  * @return reference to this instance
00624  */
00625 Time &
00626 Time::operator=(const Time &t)
00627 {
00628   __time.tv_sec  = t.__time.tv_sec;
00629   __time.tv_usec = t.__time.tv_usec;
00630   __clock        = t.__clock;
00631   return *this;
00632 }
00633 
00634 
00635 /** Check equality of times.
00636  * @param t time to compare to
00637  * @return true if sec and usec of both times are the same, false otherwise
00638  */
00639 bool
00640 Time::operator==(const Time& t) const
00641 {
00642   return (__time.tv_sec == t.__time.tv_sec) &&
00643          (__time.tv_usec == t.__time.tv_usec);
00644 }
00645 
00646 
00647 /** Check equality of times.
00648  * @param t time to compare to
00649  * @return true if sec and usec of both times are the same, false otherwise
00650  */
00651 bool
00652 Time::operator==(const Time* t) const
00653 {
00654   return (__time.tv_sec == t->__time.tv_sec) &&
00655          (__time.tv_usec == t->__time.tv_usec);
00656 }
00657 
00658 
00659 /** Check inequality of times.
00660  * @param t time to compare to
00661  * @return true if sec or usec of both times are different, false otherwise
00662  */
00663 bool
00664 Time::operator!=(const Time& t) const
00665 {
00666   return (__time.tv_sec != t.__time.tv_sec) ||
00667          (__time.tv_usec != t.__time.tv_usec);
00668 }
00669 
00670 
00671 /** Check inequality of times.
00672  * @param t time to compare to
00673  * @return true if sec or usec of both times are different, false otherwise
00674  */
00675 bool
00676 Time::operator!=(const Time* t) const
00677 {
00678   return (__time.tv_sec != t->__time.tv_sec) ||
00679          (__time.tv_usec != t->__time.tv_usec);
00680 }
00681 
00682 
00683 /** Greater than operator.
00684  * @param t time to compare to
00685  * @return true if this time is greater than @p t, false otherwise
00686  */
00687 bool
00688 Time::operator>(const Time& t) const
00689 {
00690   return (__time.tv_sec > t.__time.tv_sec) ||
00691     ((__time.tv_sec == t.__time.tv_sec) && (__time.tv_usec > t.__time.tv_usec));
00692 }
00693 
00694 
00695 /** Greater than operator.
00696  * @param t time to compare to
00697  * @return true if this time is greater than @p t, false otherwise
00698  */
00699 bool
00700 Time::operator>(const Time* t) const
00701 {
00702   return (__time.tv_sec > t->__time.tv_sec) ||
00703     ((__time.tv_sec == t->__time.tv_sec) && (__time.tv_usec > t->__time.tv_usec));
00704 }
00705 
00706 
00707 /** Greater than or equal to operator.
00708  * @param t time to compare to
00709  * @return true if this time is greater than @p t, false otherwise
00710  */
00711 bool
00712 Time::operator>=(const Time& t) const
00713 {
00714   return (__time.tv_sec > t.__time.tv_sec) ||
00715     ((__time.tv_sec == t.__time.tv_sec) && (__time.tv_usec >= t.__time.tv_usec));
00716 }
00717 
00718 
00719 /** Greater than or equal to operator.
00720  * @param t time to compare to
00721  * @return true if this time is greater than @p t, false otherwise
00722  */
00723 bool
00724 Time::operator>=(const Time* t) const
00725 {
00726   return (__time.tv_sec > t->__time.tv_sec) ||
00727     ((__time.tv_sec == t->__time.tv_sec) && (__time.tv_usec >= t->__time.tv_usec));
00728 }
00729 
00730 
00731 /** Less than operator.
00732  * @param t time to compare to
00733  * @return true if this time is less than @p t, false otherwise
00734  */
00735 bool
00736 Time::operator<(const Time& t) const
00737 {
00738   return (__time.tv_sec < t.__time.tv_sec) ||
00739     ((__time.tv_sec == t.__time.tv_sec) && (__time.tv_usec < t.__time.tv_usec));
00740 }
00741 
00742 
00743 /** Less than operator.
00744  * @param t time to compare to
00745  * @return true if this time is less than @p t, false otherwise
00746  */
00747 bool
00748 Time::operator<(const Time* t) const
00749 {
00750   return (__time.tv_sec < t->__time.tv_sec) ||
00751     ((__time.tv_sec == t->__time.tv_sec) && (__time.tv_usec < t->__time.tv_usec));
00752 }
00753 
00754 
00755 /** Less than or equal to operator.
00756  * @param t time to compare to
00757  * @return true if this time is less than @p t, false otherwise
00758  */
00759 bool
00760 Time::operator<=(const Time& t) const
00761 {
00762   return (__time.tv_sec < t.__time.tv_sec) ||
00763     ((__time.tv_sec == t.__time.tv_sec) && (__time.tv_usec <= t.__time.tv_usec));
00764 }
00765 
00766 
00767 /** Less than or equal to operator.
00768  * @param t time to compare to
00769  * @return true if this time is less than @p t, false otherwise
00770  */
00771 bool
00772 Time::operator<=(const Time* t) const
00773 {
00774   return (__time.tv_sec < t->__time.tv_sec) ||
00775     ((__time.tv_sec == t->__time.tv_sec) && (__time.tv_usec <= t->__time.tv_usec));
00776 }
00777 
00778 
00779 /** Set this time to the current time.
00780  * @return reference to this instance
00781  */
00782 Time &
00783 Time::stamp()
00784 {
00785   if ( NULL != __clock ) {
00786     __clock->get_time(&__time);
00787   } else {
00788     throw Exception("Clock not set, cannot stamp time");
00789   }
00790   return *this;
00791 }
00792 
00793 
00794 /** Set this time to the current system time.
00795  * This bypasses any possibly registered time source. Use with care and only
00796  * where you really need the system time.
00797  * @return reference to this instance
00798  */
00799 Time &
00800 Time::stamp_systime()
00801 {
00802   if ( NULL != __clock ) {
00803     __clock->get_systime(&__time);
00804   } else {
00805     throw Exception("Clock not set, cannot stamp time (systime)");
00806   }
00807   return *this;
00808 }
00809 
00810 
00811 /** Wait (sleep) for this time.
00812  * This waits for as much time as this instance provides. Note that you have to
00813  * make sure that you call this on a sensible time range. You probably do not want
00814  * to wait for almost 40 years when passing a time point...
00815  */
00816 void
00817 Time::wait()
00818 {
00819   Time until, now;
00820   until += *this;
00821 
00822   // we want to release run status at least shortly
00823   usleep(0);
00824 
00825   long int remaining_usec = (until - now).in_usec();
00826   while ( remaining_usec > 0 ) {
00827     usleep(remaining_usec);
00828     now.stamp();
00829     remaining_usec = (until - now).in_usec();
00830   }
00831 }
00832 
00833 
00834 /** Wait (sleep) for this system time.
00835  * This waits for as much time as this instance provides. Unlike wait() this
00836  * method calculates the time in system time, althouh the main clock may run
00837  * slower for example in a simulation. Note that you have to make sure that you
00838  * call this on a sensible time range. You probably do not want to wait for
00839  * almost 40 years when passing a time point...
00840  */
00841 void
00842 Time::wait_systime()
00843 {
00844   Time until, now;
00845 
00846   __clock->get_systime(until);
00847   until += *this;
00848 
00849   __clock->get_systime(now);
00850 
00851   // we want to release run status at least shortly
00852   usleep(0);
00853 
00854   long int remaining_usec = (until - now).in_usec();
00855   while ( remaining_usec > 0 ) {
00856     usleep(remaining_usec);
00857     __clock->get_systime(now);
00858     remaining_usec = (until - now).in_usec();
00859   }
00860 }
00861 
00862 /** Output function.
00863  * @return a pointer to a member containing a string represenation of
00864  * the given time. If seconds is smaller than 1 billion it is assumed that
00865  * this time represents a time range rather than a point in time and
00866  * the time is formatted as seconds.microseconds, otherwise the time
00867  * is formatted either via localtime() (if utc is false) or gmtime (if utc
00868  * is true).
00869  * @param utc true to get type formatted in UTC, otherwise local time
00870  */
00871 const char *
00872 Time::str(bool utc) const
00873 {
00874   // allocate time string if not done yet
00875   if ( ! __timestr )  __timestr = (char *)malloc(TIMESTR_SIZE);
00876 
00877   // heuristic to distinguish times and time ranges
00878   if (__time.tv_sec < 1000000000) {
00879     snprintf(__timestr, TIMESTR_SIZE, "%li:%li", __time.tv_sec, (long)__time.tv_usec);
00880   } else {
00881     tm time_tm;
00882     if ( utc ) {
00883       gmtime_r( &(__time.tv_sec), &time_tm );
00884     } else {
00885       localtime_r( &(__time.tv_sec), &time_tm );
00886     }
00887     asctime_r(&time_tm, __timestr);
00888     __timestr[strlen(__timestr) - 1] = 0;
00889   }
00890 
00891   return __timestr;
00892 }
00893 
00894 
00895 /** Output function.
00896  * This is the thread-safe version of str().
00897  * @param s pointer to a string of at least TIMESTR_SIZE bytes.
00898  * @param utc true to get type formatted in UTC, otherwise local time
00899  */
00900 void
00901 Time::str_r(char *s, bool utc)
00902 {
00903   // heuristic to distinguish times and time ranges
00904   if (__time.tv_sec < 1000000000) {
00905     snprintf(s, TIMESTR_SIZE, "%li:%li", __time.tv_sec, (long)__time.tv_usec);
00906   } else {
00907     tm time_tm;
00908     if ( utc ) {
00909       gmtime_r( &(__time.tv_sec), &time_tm );
00910     } else {
00911       localtime_r( &(__time.tv_sec), &time_tm );
00912     }
00913     asctime_r(&time_tm, s);
00914     s[strlen(s) - 1] = 0;
00915   }
00916 }
00917 
00918 } // end namespace fawkes