Fawkes API  Fawkes Development Version
time.cpp
1 
2 /***************************************************************************
3  * time.c - A time class
4  *
5  * Created: Wed Jun 06 16:50:11 2007
6  * Copyright 2007 Daniel Beck
7  * 2007-2009 Tim Niemueller [www.niemueller.de]
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #include <core/exception.h>
26 #include <core/exceptions/software.h>
27 #include <utils/time/clock.h>
28 #include <utils/time/time.h>
29 
30 #include <cmath>
31 #include <cstdio>
32 #include <cstdlib>
33 #include <cstring>
34 #include <limits>
35 #include <time.h>
36 #include <unistd.h>
37 
38 namespace fawkes {
39 
40 /** Instance of Time denoting the maximum value possible.
41  * This is particularly useful when initializing a minimization in time.
42  */
43 const Time TIME_MAX = Time(std::numeric_limits<time_t>::max(), 999999);
44 
45 /** Instance of Time denoting the minimum value possible.
46  * This is particularly useful when initializing a maximization in time.
47  */
48 const Time TIME_MIN = Time(0, 1);
49 
50 /** @class Time <utils/time/time.h>
51  * A class for handling time.
52  * @author Daniel Beck
53  * @author Tim Niemueller
54  *
55  * @fn const timeval * Time::get_timeval() const
56  * Obtain the timeval where the time is stored.
57  * @return a const pointer to the timeval where the time is stored
58  *
59  * @fn long Time::get_sec() const
60  * Get seconds.
61  * @return seconds stored in time stamp
62  *
63  * @fn long Time::get_msec() const
64  * Get milliseconds.
65  * @return milliseconds stored in time stamp
66  *
67  * @fn long Time::get_usec() const
68  * Get microseconds.
69  * @return microseconds stored in time stamp
70  *
71  * @fn long Time::get_nsec() const
72  * Get nanoseconds.
73  * @return microsecons converted to nanoseconds
74  *
75  * @fn bool Time::is_zero() const
76  * Check if time is zero.
77  * @return true if time is zero, i.e. sec = usec = 0, false otherwise
78  *
79  * @fn void Time::get_timestamp(long &sec, long &usec) const
80  * Get time stamp.
81  * @param sec upon return contains seconds stored in time stamp
82  * @param usec upon return contains microseconds stored in time stamp
83  */
84 
85 /** Maximum size of string returned by str() and the minimum size
86  * of the string passwd to str_r(). */
87 // as recommened in asctime_r() docs
88 const unsigned int Time::TIMESTR_SIZE = 26;
89 
90 /** Constructor.
91  * Sets time to the current time.
92  */
93 Time::Time()
94 {
95  clock_ = Clock::instance();
96  clock_->get_time(&time_);
97  timestr_ = NULL;
98 }
99 
100 /** Constructor.
101  * Sets time to the given time.
102  * @param tv the Time object is initialized with the time given in this timeval
103  */
104 Time::Time(const timeval *tv)
105 {
106  time_.tv_sec = tv->tv_sec;
107  time_.tv_usec = tv->tv_usec;
108  clock_ = Clock::instance();
109  timestr_ = NULL;
110 }
111 
112 /** Constructor.
113  * Sets time to the given time. Basically the same as setting from a timeval struct
114  * but the components are given separately.
115  * @param sec time in seconds since the epoch (or time range)
116  * @param usec fractions in microseconds added to sec
117  * @param clock optional clock to use, if NULL Clock::instance() will be used
118  */
119 Time::Time(long sec, long usec, Clock *clock)
120 {
121  time_.tv_sec = sec;
122  time_.tv_usec = usec;
123  if (clock) {
124  clock_ = clock;
125  } else {
126  clock_ = Clock::instance();
127  }
128  timestr_ = NULL;
129 }
130 
131 /** Constructor.
132  * Sets time to given number of ms, use for time range.
133  * @param ms the Time object is initialized to the time given in milli-seconds
134  */
135 Time::Time(long ms)
136 {
137  time_t sec = (time_t)(ms / 1000.0);
138  suseconds_t usec = (ms % 1000) * 1000;
139 
140  time_.tv_sec = sec;
141  time_.tv_usec = usec;
142  clock_ = Clock::instance();
143  timestr_ = NULL;
144 }
145 
146 /** Constructor.
147  * Sets time to given number of ms, use for time range.
148  * @param s the Time object is initialized to the time given in seconds
149  */
150 Time::Time(double s)
151 {
152  time_t sec = (time_t)s;
153  suseconds_t usec = (suseconds_t)roundf((s - sec) * 1000000.f);
154 
155  time_.tv_sec = sec;
156  time_.tv_usec = usec;
157  clock_ = Clock::instance();
158  timestr_ = NULL;
159 }
160 
161 /** Constructor.
162  * This constructor uses the supplied clock for setting the time. The
163  * time is set to the current time.
164  * @param clock clock
165  */
166 Time::Time(Clock *clock)
167 {
168  this->clock_ = clock;
169  clock_->get_time(&time_);
170  timestr_ = NULL;
171 }
172 
173 /** Copy constructor.
174  * @param t time to copy
175  */
176 Time::Time(const Time &t)
177 {
178  time_.tv_sec = t.time_.tv_sec;
179  time_.tv_usec = t.time_.tv_usec;
180  clock_ = t.clock_;
181  if (t.timestr_) {
182  timestr_ = (char *)malloc(TIMESTR_SIZE);
183  strncpy(timestr_, t.timestr_, TIMESTR_SIZE);
184  } else {
185  timestr_ = NULL;
186  }
187 }
188 
189 /** Copy constructor.
190  * @param t time to copy
191  */
192 Time::Time(const Time *t)
193 {
194  time_.tv_sec = t->time_.tv_sec;
195  time_.tv_usec = t->time_.tv_usec;
196  clock_ = t->clock_;
197  if (t->timestr_) {
198  timestr_ = (char *)malloc(TIMESTR_SIZE);
199  strncpy(timestr_, t->timestr_, TIMESTR_SIZE);
200  } else {
201  timestr_ = NULL;
202  }
203 }
204 
205 /** Destructor. */
206 Time::~Time()
207 {
208  if (timestr_)
209  free(timestr_);
210 }
211 
212 /** Convet time to seconds.
213  * Convert the stored time in a floating point number representing the
214  * number of seconds. For a time the integral part is the number of seconds
215  * since the epoch, for ranges you get the value as a float second.
216  * @return the time in seconds
217  */
218 double
219 Time::in_sec() const
220 {
221  return ((double)time_.tv_sec + (double)time_.tv_usec / 1000000.);
222 }
223 
224 /** Convert the stored time into milli-seconds.
225  * @return the time in milli-seconds
226  */
227 long
228 Time::in_msec() const
229 {
230  return (time_.tv_sec * 1000 + (long)(time_.tv_usec / 1000));
231 }
232 
233 /** Convert the stored time into micro-seconds.
234  * @return the time in micro-seconds
235  */
236 long
237 Time::in_usec() const
238 {
239  return (time_.tv_sec * 1000000 + time_.tv_usec);
240 }
241 
242 /** Sets the time.
243  * @param tv set the time to this value
244  */
245 void
246 Time::set_time(const timeval *tv)
247 {
248  time_.tv_sec = tv->tv_sec;
249  time_.tv_usec = tv->tv_usec;
250 }
251 
252 /** Sets the time.
253  * @param sec seconds part of the time
254  * @param usec microseconds part of the time
255  */
256 void
257 Time::set_time(long int sec, long int usec)
258 {
259  time_.tv_sec = sec;
260  time_.tv_usec = usec;
261 }
262 
263 /** Sets the time.
264  * @param ms set the time to this value
265  */
266 void
267 Time::set_time(long ms)
268 {
269  time_.tv_sec = (time_t)(ms / 1000.0);
270  time_.tv_usec = (ms % 1000) * 1000;
271 }
272 
273 /** Sets the time.
274  * @param s set the time to this value
275  */
276 void
277 Time::set_time(double s)
278 {
279  time_.tv_sec = (time_t)floor(s);
280  time_.tv_usec = (suseconds_t)(s - time_.tv_sec) * 1000000;
281 }
282 
283 /** Set time to given time.
284  * this is equivalent to operator+, but can be used in situations where
285  * the operator cannot be used (for example in Lua).
286  * @param t time to set to
287  */
288 void
289 Time::set_time(const Time &t)
290 {
291  *this = t;
292 }
293 
294 /** Set time to given time.
295  * @param t time to set to
296  */
297 void
298 Time::set_time(const Time *t)
299 {
300  time_.tv_sec = t->time_.tv_sec;
301  time_.tv_usec = t->time_.tv_usec;
302 }
303 
304 /** Set clock for this instance.
305  * @param clock clock to use from now on
306  */
307 void
308 Time::set_clock(Clock *clock)
309 {
310  if (clock == NULL)
311  throw NullPointerException("Clock may not be NULL");
312  clock_ = clock;
313 }
314 
315 /** Add seconds.
316  * The effect is equivalent to operator+=(const double sec), but this
317  * can be used when the operator is not available (i.e. wrapper languages)
318  * and it does not return itself.
319  * @param seconds time in seconds to add
320  */
321 void
322 Time::add(double seconds)
323 {
324  *this += seconds;
325 }
326 
327 /** Operator that adds times.
328  * @param t the other summand
329  * @return the sum
330  */
331 Time
332 Time::operator+(const Time &t) const
333 {
334  Time ret(0, 0);
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;
338  } else {
339  ret.time_.tv_usec = time_.tv_usec + t.time_.tv_usec;
340  ret.time_.tv_sec = time_.tv_sec + t.time_.tv_sec;
341  }
342 
343  return ret;
344 }
345 
346 /** Operator that adds times.
347  * @param t the other summand
348  * @return the sum
349  */
350 Time
351 Time::operator+(const Time *t) const
352 {
353  return *this + *t;
354 }
355 
356 /** Operator that adds times.
357  * @param sec number of seconds to add
358  * @return the sum
359  */
360 Time
361 Time::operator+(const double sec) const
362 {
363  Time ret(0, 0);
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;
369  } else {
370  ret.time_.tv_usec = time_.tv_usec + usec_only;
371  ret.time_.tv_sec = time_.tv_sec + sec_only;
372  }
373 
374  return ret;
375 }
376 
377 /** Operator to add usec value.
378  * @param usec microseconds to add
379  * @return new resulting instance
380  */
381 Time
382 Time::operator+(const long int usec) const
383 {
384  Time ret(0, 0);
385  if (time_.tv_usec + usec >= 1000000) {
386  //usec + time_.tv_usec might be more than 1 second
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);
390  } else {
391  ret.time_.tv_sec = time_.tv_sec;
392  ret.time_.tv_usec += usec;
393  }
394 
395  return ret;
396 }
397 
398 /** Operator that substracts one Time from another.
399  * @param t the Time that is substracted
400  * @return the difference
401  */
402 Time
403 Time::operator-(const Time &t) const
404 {
405  Time ret(0, 0);
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;
409  } else {
410  ret.time_.tv_usec = time_.tv_usec - t.time_.tv_usec;
411  ret.time_.tv_sec = time_.tv_sec - t.time_.tv_sec;
412  }
413 
414  return ret;
415 }
416 
417 /** Operator that substracts one Time from another.
418  * @param t the Time that is substracted
419  * @return the difference
420  */
421 double
422 Time::operator-(const Time *t) const
423 {
424  return time_diff_sec(time_, t->time_);
425 }
426 
427 /** Operator that subtracts some time.
428  * @param sec number of seconds to subtract
429  * @return the difference
430  */
431 Time
432 Time::operator-(const double sec) const
433 {
434  Time ret(0, 0);
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;
440  } else {
441  ret.time_.tv_usec = time_.tv_usec - usec_only;
442  ret.time_.tv_sec = time_.tv_sec - sec_only;
443  }
444 
445  return ret;
446 }
447 
448 /** Operator that subtracts some time.
449  * @param usec number of microseconds to subtract
450  * @return the difference
451  */
452 Time
453 Time::operator-(const long int usec) const
454 {
455  Time ret(0, 0);
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;
461  } else {
462  ret.time_.tv_usec = time_.tv_usec - usec_only;
463  ret.time_.tv_sec = time_.tv_sec - sec_only;
464  }
465 
466  return ret;
467 }
468 
469 /** += operator
470  * @param t the other time
471  * @return reference to this instance
472  */
473 Time &
474 Time::operator+=(const Time &t)
475 {
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;
479  } else {
480  time_.tv_usec += t.time_.tv_usec;
481  time_.tv_sec += t.time_.tv_sec;
482  }
483 
484  return *this;
485 }
486 
487 /** += operator
488  * @param usec microseconds to add
489  * @return reference to this instance
490  */
491 Time &
492 Time::operator+=(const long int usec)
493 {
494  if (time_.tv_usec + usec >= 1000000) {
495  //usec + time_.tv_usec might be more than 1 second
496  long int tmp_usec = time_.tv_usec + usec;
497  time_.tv_usec = tmp_usec % 1000000;
498  time_.tv_sec += tmp_usec / 1000000;
499  } else {
500  time_.tv_usec += usec;
501  }
502 
503  return *this;
504 }
505 
506 /** += operator for double seconds
507  * @param sec number of seconds to add
508  * @return the sum
509  */
510 Time &
511 Time::operator+=(const double sec)
512 {
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;
518  } else {
519  time_.tv_usec += usec_only;
520  time_.tv_sec += sec_only;
521  }
522 
523  return *this;
524 }
525 
526 /** -= operator.
527  * @param t the other time
528  * @return reference to this instance after subtraction
529  */
530 Time &
531 Time::operator-=(const Time &t)
532 {
533  *this = *this - t;
534  return *this;
535 }
536 
537 /** -= operator.
538  * @param sec seconds to subtract
539  * @return reference to this instance after subtraction
540  */
541 Time &
542 Time::operator-=(const double sec)
543 {
544  *this = *this - sec;
545  return *this;
546 }
547 
548 /** -= operator.
549  * @param usec microseconds to subtract
550  * @return reference to this instance after subtraction
551  */
552 Time &
553 Time::operator-=(const long int usec)
554 {
555  *this = *this - usec;
556  return *this;
557 }
558 
559 /** Assign operator.
560  * @param t time to assign to this instance
561  * @return reference to this instance
562  */
563 Time &
564 Time::operator=(const Time &t)
565 {
566  time_.tv_sec = t.time_.tv_sec;
567  time_.tv_usec = t.time_.tv_usec;
568  clock_ = t.clock_;
569  return *this;
570 }
571 
572 /** Check equality of times.
573  * @param t time to compare to
574  * @return true if sec and usec of both times are the same, false otherwise
575  */
576 bool
577 Time::operator==(const Time &t) const
578 {
579  return (time_.tv_sec == t.time_.tv_sec) && (time_.tv_usec == t.time_.tv_usec);
580 }
581 
582 /** Check equality of times.
583  * @param t time to compare to
584  * @return true if sec and usec of both times are the same, false otherwise
585  */
586 bool
587 Time::operator==(const Time *t) const
588 {
589  return (time_.tv_sec == t->time_.tv_sec) && (time_.tv_usec == t->time_.tv_usec);
590 }
591 
592 /** Check inequality of times.
593  * @param t time to compare to
594  * @return true if sec or usec of both times are different, false otherwise
595  */
596 bool
597 Time::operator!=(const Time &t) const
598 {
599  return (time_.tv_sec != t.time_.tv_sec) || (time_.tv_usec != t.time_.tv_usec);
600 }
601 
602 /** Check inequality of times.
603  * @param t time to compare to
604  * @return true if sec or usec of both times are different, false otherwise
605  */
606 bool
607 Time::operator!=(const Time *t) const
608 {
609  return (time_.tv_sec != t->time_.tv_sec) || (time_.tv_usec != t->time_.tv_usec);
610 }
611 
612 /** Greater than operator.
613  * @param t time to compare to
614  * @return true if this time is greater than @p t, false otherwise
615  */
616 bool
617 Time::operator>(const Time &t) const
618 {
619  return (time_.tv_sec > t.time_.tv_sec)
620  || ((time_.tv_sec == t.time_.tv_sec) && (time_.tv_usec > t.time_.tv_usec));
621 }
622 
623 /** Greater than operator.
624  * @param t time to compare to
625  * @return true if this time is greater than @p t, false otherwise
626  */
627 bool
628 Time::operator>(const Time *t) const
629 {
630  return (time_.tv_sec > t->time_.tv_sec)
631  || ((time_.tv_sec == t->time_.tv_sec) && (time_.tv_usec > t->time_.tv_usec));
632 }
633 
634 /** Greater than or equal to operator.
635  * @param t time to compare to
636  * @return true if this time is greater than @p t, false otherwise
637  */
638 bool
639 Time::operator>=(const Time &t) const
640 {
641  return (time_.tv_sec > t.time_.tv_sec)
642  || ((time_.tv_sec == t.time_.tv_sec) && (time_.tv_usec >= t.time_.tv_usec));
643 }
644 
645 /** Greater than or equal to operator.
646  * @param t time to compare to
647  * @return true if this time is greater than @p t, false otherwise
648  */
649 bool
650 Time::operator>=(const Time *t) const
651 {
652  return (time_.tv_sec > t->time_.tv_sec)
653  || ((time_.tv_sec == t->time_.tv_sec) && (time_.tv_usec >= t->time_.tv_usec));
654 }
655 
656 /** Less than operator.
657  * @param t time to compare to
658  * @return true if this time is less than @p t, false otherwise
659  */
660 bool
661 Time::operator<(const Time &t) const
662 {
663  return (time_.tv_sec < t.time_.tv_sec)
664  || ((time_.tv_sec == t.time_.tv_sec) && (time_.tv_usec < t.time_.tv_usec));
665 }
666 
667 /** Less than operator.
668  * @param t time to compare to
669  * @return true if this time is less than @p t, false otherwise
670  */
671 bool
672 Time::operator<(const Time *t) const
673 {
674  return (time_.tv_sec < t->time_.tv_sec)
675  || ((time_.tv_sec == t->time_.tv_sec) && (time_.tv_usec < t->time_.tv_usec));
676 }
677 
678 /** Less than or equal to operator.
679  * @param t time to compare to
680  * @return true if this time is less than @p t, false otherwise
681  */
682 bool
683 Time::operator<=(const Time &t) const
684 {
685  return (time_.tv_sec < t.time_.tv_sec)
686  || ((time_.tv_sec == t.time_.tv_sec) && (time_.tv_usec <= t.time_.tv_usec));
687 }
688 
689 /** Less than or equal to operator.
690  * @param t time to compare to
691  * @return true if this time is less than @p t, false otherwise
692  */
693 bool
694 Time::operator<=(const Time *t) const
695 {
696  return (time_.tv_sec < t->time_.tv_sec)
697  || ((time_.tv_sec == t->time_.tv_sec) && (time_.tv_usec <= t->time_.tv_usec));
698 }
699 
700 /** Set this time to the current time.
701  * @return reference to this instance
702  */
703 Time &
704 Time::stamp()
705 {
706  if (NULL != clock_) {
707  clock_->get_time(&time_);
708  } else {
709  throw Exception("Clock not set, cannot stamp time");
710  }
711  return *this;
712 }
713 
714 /** Set this time to the current system time.
715  * This bypasses any possibly registered time source. Use with care and only
716  * where you really need the system time.
717  * @return reference to this instance
718  */
719 Time &
721 {
722  if (NULL != clock_) {
723  clock_->get_systime(&time_);
724  } else {
725  throw Exception("Clock not set, cannot stamp time (systime)");
726  }
727  return *this;
728 }
729 
730 /** Wait (sleep) for this time.
731  * This waits for as much time as this instance provides. Note that you have to
732  * make sure that you call this on a sensible time range. You probably do not want
733  * to wait for almost 40 years when passing a time point...
734  */
735 void
736 Time::wait()
737 {
738  Time until, now;
739  until += *this;
740 
741  // we want to release run status at least shortly
742  usleep(0);
743 
744  long int remaining_usec = (until - now).in_usec();
745  while (remaining_usec > 0) {
746  usleep(remaining_usec);
747  now.stamp();
748  remaining_usec = (until - now).in_usec();
749  }
750 }
751 
752 /** Wait (sleep) for this system time.
753  * This waits for as much time as this instance provides. Unlike wait() this
754  * method calculates the time in system time, althouh the main clock may run
755  * slower for example in a simulation. Note that you have to make sure that you
756  * call this on a sensible time range. You probably do not want to wait for
757  * almost 40 years when passing a time point...
758  */
759 void
761 {
762  Time until, now;
763 
764  clock_->get_systime(until);
765  until += *this;
766 
767  clock_->get_systime(now);
768 
769  // we want to release run status at least shortly
770  usleep(0);
771 
772  long int remaining_usec = (until - now).in_usec();
773  while (remaining_usec > 0) {
774  usleep(remaining_usec);
775  clock_->get_systime(now);
776  remaining_usec = (until - now).in_usec();
777  }
778 }
779 
780 /** Output function.
781  * @return a pointer to a member containing a string represenation of
782  * the given time. If seconds is smaller than 1 billion it is assumed that
783  * this time represents a time range rather than a point in time and
784  * the time is formatted as seconds.microseconds, otherwise the time
785  * is formatted either via localtime() (if utc is false) or gmtime (if utc
786  * is true).
787  * @param utc true to get type formatted in UTC, otherwise local time
788  */
789 const char *
790 Time::str(bool utc) const
791 {
792  // allocate time string if not done yet
793  if (!timestr_)
794  timestr_ = (char *)malloc(TIMESTR_SIZE);
795 
796  // heuristic to distinguish times and time ranges
797  if (time_.tv_sec < 1000000000) {
798  snprintf(timestr_, TIMESTR_SIZE, "%li:%li", time_.tv_sec, (long)time_.tv_usec);
799  } else {
800  tm time_tm;
801  if (utc) {
802  gmtime_r(&(time_.tv_sec), &time_tm);
803  } else {
804  localtime_r(&(time_.tv_sec), &time_tm);
805  }
806  asctime_r(&time_tm, timestr_);
807  timestr_[strlen(timestr_) - 1] = 0;
808  }
809 
810  return timestr_;
811 }
812 
813 /** Output function.
814  * This is the thread-safe version of str().
815  * @param s pointer to a string of at least TIMESTR_SIZE bytes.
816  * @param utc true to get type formatted in UTC, otherwise local time
817  */
818 void
819 Time::str_r(char *s, bool utc)
820 {
821  // heuristic to distinguish times and time ranges
822  if (time_.tv_sec < 1000000000) {
823  snprintf(s, TIMESTR_SIZE, "%li:%li", time_.tv_sec, (long)time_.tv_usec);
824  } else {
825  tm time_tm;
826  if (utc) {
827  gmtime_r(&(time_.tv_sec), &time_tm);
828  } else {
829  localtime_r(&(time_.tv_sec), &time_tm);
830  }
831  asctime_r(&time_tm, s);
832  s[strlen(s) - 1] = 0;
833  }
834 }
835 
836 } // end namespace fawkes
fawkes::Time::in_msec
long in_msec() const
Convert the stored time into milli-seconds.
Definition: time.cpp:235
fawkes::Time::set_time
void set_time(const timeval *tv)
Sets the time.
Definition: time.cpp:253
fawkes::Time::str
const char * str(bool utc=false) const
Output function.
Definition: time.cpp:797
fawkes::TIME_MIN
const Time TIME_MIN
Instance of Time denoting the minimum value possible.
Definition: time.cpp:55
fawkes::Time::in_usec
long in_usec() const
Convert the stored time into micro-seconds.
Definition: time.cpp:244
fawkes::Time::operator<
bool operator<(const Time &t) const
Less than operator.
Definition: time.cpp:668
fawkes::Time::operator+=
Time & operator+=(const long int usec)
+= operator
Definition: time.cpp:499
fawkes::Clock::get_time
void get_time(struct timeval *tv) const
Returns the time of the selected time source.
Definition: clock.cpp:168
fawkes::TIME_MAX
const Time TIME_MAX
Instance of Time denoting the maximum value possible.
Definition: time.cpp:50
fawkes::Time::Time
Time()
Constructor.
Definition: time.cpp:100
fawkes::Time::stamp_systime
Time & stamp_systime()
Set this time to the current system time.
Definition: time.cpp:727
fawkes::time_diff_sec
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.
Definition: time.h:47
fawkes::Time::str_r
void str_r(char *s, bool utc=false)
Output function.
Definition: time.cpp:826
fawkes::Time::wait
void wait()
Wait (sleep) for this time.
Definition: time.cpp:743
fawkes::Time::operator>
bool operator>(const Time &t) const
Greater than operator.
Definition: time.cpp:624
fawkes::Time::~Time
~Time()
Destructor.
Definition: time.cpp:213
fawkes::Time::operator<=
bool operator<=(const Time &t) const
Less than or equal to operator.
Definition: time.cpp:690
fawkes
fawkes::Time::wait_systime
void wait_systime()
Wait (sleep) for this system time.
Definition: time.cpp:767
fawkes::Time::set_clock
void set_clock(Clock *clock)
Set clock for this instance.
Definition: time.cpp:315
fawkes::Clock::get_systime
void get_systime(struct timeval *tv) const
Returns the system time.
Definition: clock.cpp:222
fawkes::Time
Definition: time.h:98
fawkes::Time::add
void add(double seconds)
Add seconds.
Definition: time.cpp:329
fawkes::Time::operator-=
Time & operator-=(const Time &t)
-= operator.
Definition: time.cpp:538
fawkes::Time::operator+
Time operator+(const double sec) const
Operator that adds times.
Definition: time.cpp:368
fawkes::Clock::instance
static Clock * instance()
Clock initializer.
Definition: clock.cpp:70
fawkes::Time::stamp
Time & stamp()
Set this time to the current time.
Definition: time.cpp:711
fawkes::Time::operator>=
bool operator>=(const Time &t) const
Greater than or equal to operator.
Definition: time.cpp:646
fawkes::Time::operator-
Time operator-(const Time &t) const
Operator that substracts one Time from another.
Definition: time.cpp:410
fawkes::NullPointerException
Definition: software.h:37
fawkes::Time::TIMESTR_SIZE
static const unsigned int TIMESTR_SIZE
Maximum size of string returned by str() and the minimum size of the string passwd to str_r().
Definition: time.h:202
fawkes::Time::in_sec
double in_sec() const
Convet time to seconds.
Definition: time.cpp:226
fawkes::Time::operator=
Time & operator=(const Time &t)
Assign operator.
Definition: time.cpp:571
fawkes::Time::operator==
bool operator==(const Time &t) const
Check equality of times.
Definition: time.cpp:584
fawkes::Clock
Definition: clock.h:41
fawkes::Time::operator!=
bool operator!=(const Time &t) const
Check inequality of times.
Definition: time.cpp:604
fawkes::Exception
Definition: exception.h:41