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