25 #include <core/exception.h> 26 #include <core/exceptions/software.h> 27 #include <utils/time/clock.h> 28 #include <utils/time/time.h> 106 time_.tv_sec = tv->tv_sec;
107 time_.tv_usec = tv->tv_usec;
122 time_.tv_usec = usec;
137 time_t sec = (time_t)(ms / 1000.0);
138 suseconds_t usec = (ms % 1000) * 1000;
141 time_.tv_usec = usec;
152 time_t sec = (time_t)s;
153 suseconds_t usec = (suseconds_t)roundf((s - sec) * 1000000.f);
156 time_.tv_usec = usec;
168 this->clock_ = clock;
178 time_.tv_sec = t.time_.tv_sec;
179 time_.tv_usec = t.time_.tv_usec;
194 time_.tv_sec = t->time_.tv_sec;
195 time_.tv_usec = t->time_.tv_usec;
221 return ((
double)time_.tv_sec + (
double)time_.tv_usec / 1000000.);
230 return (time_.tv_sec * 1000 + (
long)(time_.tv_usec / 1000));
239 return (time_.tv_sec * 1000000 + time_.tv_usec);
248 time_.tv_sec = tv->tv_sec;
249 time_.tv_usec = tv->tv_usec;
260 time_.tv_usec = usec;
269 time_.tv_sec = (time_t)(ms / 1000.0);
270 time_.tv_usec = (ms % 1000) * 1000;
279 time_.tv_sec = (time_t)floor(s);
280 time_.tv_usec = (suseconds_t)(s - time_.tv_sec) * 1000000;
300 time_.tv_sec = t->time_.tv_sec;
301 time_.tv_usec = t->time_.tv_usec;
335 if (time_.tv_usec + t.time_.tv_usec >= 1000000) {
336 ret.time_.tv_usec = time_.tv_usec + t.time_.tv_usec - 1000000;
337 ret.time_.tv_sec = time_.tv_sec + t.time_.tv_sec + 1;
339 ret.time_.tv_usec = time_.tv_usec + t.time_.tv_usec;
340 ret.time_.tv_sec = time_.tv_sec + t.time_.tv_sec;
364 time_t sec_only = (time_t)floor(sec);
365 suseconds_t usec_only = (suseconds_t)roundf((sec - sec_only) * 1000000);
366 if ((time_.tv_usec + usec_only) >= 1000000) {
367 ret.time_.tv_usec = time_.tv_usec + usec_only - 1000000;
368 ret.time_.tv_sec = time_.tv_sec + sec_only + 1;
370 ret.time_.tv_usec = time_.tv_usec + usec_only;
371 ret.time_.tv_sec = time_.tv_sec + sec_only;
385 if (time_.tv_usec + usec >= 1000000) {
387 long int tmp_usec = time_.tv_usec + usec;
388 ret.time_.tv_usec = tmp_usec % 1000000;
389 ret.time_.tv_sec = time_.tv_sec + (tmp_usec / 1000000);
391 ret.time_.tv_sec = time_.tv_sec;
392 ret.time_.tv_usec += usec;
406 if (time_.tv_usec < t.time_.tv_usec) {
407 ret.time_.tv_usec = 1000000 + time_.tv_usec - t.time_.tv_usec;
408 ret.time_.tv_sec = time_.tv_sec - t.time_.tv_sec - 1;
410 ret.time_.tv_usec = time_.tv_usec - t.time_.tv_usec;
411 ret.time_.tv_sec = time_.tv_sec - t.time_.tv_sec;
435 time_t sec_only = (time_t)floor(sec);
436 suseconds_t usec_only = (suseconds_t)roundf((sec - sec_only) * 1000000);
437 if (time_.tv_usec < usec_only) {
438 ret.time_.tv_usec = 1000000 + time_.tv_usec - usec_only;
439 ret.time_.tv_sec = time_.tv_sec - sec_only - 1;
441 ret.time_.tv_usec = time_.tv_usec - usec_only;
442 ret.time_.tv_sec = time_.tv_sec - sec_only;
456 time_t sec_only = usec / 1000000;
457 suseconds_t usec_only = usec % 1000000;
458 if (time_.tv_usec < usec_only) {
459 ret.time_.tv_usec = 1000000 + time_.tv_usec - usec_only;
460 ret.time_.tv_sec = time_.tv_sec - sec_only - 1;
462 ret.time_.tv_usec = time_.tv_usec - usec_only;
463 ret.time_.tv_sec = time_.tv_sec - sec_only;
476 if (time_.tv_usec + t.time_.tv_usec >= 1000000) {
477 time_.tv_usec += t.time_.tv_usec - 1000000;
478 time_.tv_sec += t.time_.tv_sec + 1;
480 time_.tv_usec += t.time_.tv_usec;
481 time_.tv_sec += t.time_.tv_sec;
494 if (time_.tv_usec + usec >= 1000000) {
496 long int tmp_usec = time_.tv_usec + usec;
497 time_.tv_usec = tmp_usec % 1000000;
498 time_.tv_sec += tmp_usec / 1000000;
500 time_.tv_usec += usec;
513 time_t sec_only = (time_t)floor(sec);
514 suseconds_t usec_only = (suseconds_t)roundf((sec - sec_only) * 1000000);
515 if ((time_.tv_usec + usec_only) >= 1000000) {
516 time_.tv_usec += usec_only - 1000000;
517 time_.tv_sec += sec_only + 1;
519 time_.tv_usec += usec_only;
520 time_.tv_sec += sec_only;
555 *
this = *
this - usec;
566 time_.tv_sec = t.time_.tv_sec;
567 time_.tv_usec = t.time_.tv_usec;
579 return (time_.tv_sec == t.time_.tv_sec) && (time_.tv_usec == t.time_.tv_usec);
589 return (time_.tv_sec == t->time_.tv_sec) && (time_.tv_usec == t->time_.tv_usec);
599 return (time_.tv_sec != t.time_.tv_sec) || (time_.tv_usec != t.time_.tv_usec);
609 return (time_.tv_sec != t->time_.tv_sec) || (time_.tv_usec != t->time_.tv_usec);
619 return (time_.tv_sec > t.time_.tv_sec)
620 || ((time_.tv_sec == t.time_.tv_sec) && (time_.tv_usec > t.time_.tv_usec));
630 return (time_.tv_sec > t->time_.tv_sec)
631 || ((time_.tv_sec == t->time_.tv_sec) && (time_.tv_usec > t->time_.tv_usec));
641 return (time_.tv_sec > t.time_.tv_sec)
642 || ((time_.tv_sec == t.time_.tv_sec) && (time_.tv_usec >= t.time_.tv_usec));
652 return (time_.tv_sec > t->time_.tv_sec)
653 || ((time_.tv_sec == t->time_.tv_sec) && (time_.tv_usec >= t->time_.tv_usec));
663 return (time_.tv_sec < t.time_.tv_sec)
664 || ((time_.tv_sec == t.time_.tv_sec) && (time_.tv_usec < t.time_.tv_usec));
674 return (time_.tv_sec < t->time_.tv_sec)
675 || ((time_.tv_sec == t->time_.tv_sec) && (time_.tv_usec < t->time_.tv_usec));
685 return (time_.tv_sec < t.time_.tv_sec)
686 || ((time_.tv_sec == t.time_.tv_sec) && (time_.tv_usec <= t.time_.tv_usec));
696 return (time_.tv_sec < t->time_.tv_sec)
697 || ((time_.tv_sec == t->time_.tv_sec) && (time_.tv_usec <= t->time_.tv_usec));
706 if (NULL != clock_) {
709 throw Exception(
"Clock not set, cannot stamp time");
722 if (NULL != clock_) {
725 throw Exception(
"Clock not set, cannot stamp time (systime)");
744 long int remaining_usec = (until - now).
in_usec();
745 while (remaining_usec > 0) {
746 usleep(remaining_usec);
748 remaining_usec = (until - now).
in_usec();
772 long int remaining_usec = (until - now).
in_usec();
773 while (remaining_usec > 0) {
774 usleep(remaining_usec);
776 remaining_usec = (until - now).
in_usec();
797 if (time_.tv_sec < 1000000000) {
798 snprintf(timestr_,
TIMESTR_SIZE,
"%li:%li", time_.tv_sec, (
long)time_.tv_usec);
802 gmtime_r(&(time_.tv_sec), &time_tm);
804 localtime_r(&(time_.tv_sec), &time_tm);
806 asctime_r(&time_tm, timestr_);
807 timestr_[strlen(timestr_) - 1] = 0;
822 if (time_.tv_sec < 1000000000) {
823 snprintf(s,
TIMESTR_SIZE,
"%li:%li", time_.tv_sec, (
long)time_.tv_usec);
827 gmtime_r(&(time_.tv_sec), &time_tm);
829 localtime_r(&(time_.tv_sec), &time_tm);
831 asctime_r(&time_tm, s);
832 s[strlen(s) - 1] = 0;
static const unsigned int TIMESTR_SIZE
Maximum size of string returned by str() and the minimum size of the string passwd to str_r().
Time & operator=(const Time &t)
Assign operator.
double in_sec() const
Convet time to seconds.
Time & operator-=(const Time &t)
-= operator.
static Clock * instance()
Clock initializer.
const char * str(bool utc=false) const
Output function.
void get_time(struct timeval *tv) const
Returns the time of the selected time source.
Time & stamp_systime()
Set this time to the current system time.
bool operator<=(const Time &t) const
Less than or equal to operator.
Fawkes library namespace.
void get_systime(struct timeval *tv) const
Returns the system time.
This is supposed to be the central clock in Fawkes.
Time & operator+=(const long int usec)
+= operator
A class for handling time.
A NULL pointer was supplied where not allowed.
bool operator<(const Time &t) const
Less than operator.
void str_r(char *s, bool utc=false)
Output function.
bool operator==(const Time &t) const
Check equality of times.
void wait()
Wait (sleep) for this time.
void wait_systime()
Wait (sleep) for this system time.
Time operator-(const Time &t) const
Operator that substracts one Time from another.
const Time TIME_MIN
Instance of Time denoting the minimum value possible.
long in_msec() const
Convert the stored time into milli-seconds.
void add(double seconds)
Add seconds.
Base class for exceptions in Fawkes.
void set_clock(Clock *clock)
Set clock for this instance.
bool operator>=(const Time &t) const
Greater than or equal to operator.
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.
const Time TIME_MAX
Instance of Time denoting the maximum value possible.
void set_time(const timeval *tv)
Sets the time.
bool operator>(const Time &t) const
Greater than operator.
Time operator+(const double sec) const
Operator that adds times.
long in_usec() const
Convert the stored time into micro-seconds.
Time & stamp()
Set this time to the current time.
bool operator!=(const Time &t) const
Check inequality of times.