appkit  1.5.1
RoadNarrows Robotics Application Kit
Time.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: Time.h
10 //
11 //
12 /*! \file
13  *
14  * \brief Time functions and class interfaces.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \par Copyright
19  * \h_copy 2015-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 /*
24  * @EulaBegin@
25  * @EulaEnd@
26  */
27 ////////////////////////////////////////////////////////////////////////////////
28 
29 #ifndef _RNR_TIME_H
30 #define _RNR_TIME_H
31 
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <time.h>
35 
36 #include <iostream>
37 
38 #include "rnr/rnrconfig.h"
39 #include "rnr/log.h"
40 
41 // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
42 // Global Namespace
43 // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44 
45 /*!
46  * \brief A timespec insertion operator.
47  *
48  * \param os Output stream.
49  * \param obj Object to insert.
50  *
51  * \return Reference to output stream.
52  */
53 extern std::ostream &operator<<(std::ostream &os, const timespec &obj);
54 
55 /*!
56  * \brief RoadNarrows Robotics
57  */
58 namespace rnr
59 {
60  /*!
61  * \brief Chronos - God of Time
62  */
63  namespace chronos
64  {
65  const long MILLION = 1000000; ///< 1,000,000
66  const long long BILLION = 1000000000; ///< 1,000,000,000
67 
68  /*!
69  * \brief Get the current time, indentified by CLOCK_REALTIME, since the
70  * last Epoch.
71  *
72  * \return Timespec.
73  */
74  extern timespec now();
75 
76  /*!
77  * \brief Get the current time, indentified by CLOCK_REALTIME, since the
78  * last Epoch.
79  *
80  * \param [out] ts Timespec.
81  *
82  * \copydoc doc_return_std
83  */
84  extern int now(timespec &ts);
85 
86  /*!
87  * \brief Convert timespec to floating point number equivalent.
88  *
89  * \param ts Timespec.
90  *
91  * \return Seconds and fractions of a second.
92  */
93  extern double toFp(const timespec &ts);
94 
95  /*!
96  * \brief Convert floating point seconds to timespec equivalent.
97  *
98  * \param Seconds and fractions of a second.
99  *
100  * \return ts Timespec.
101  */
102  extern timespec toTs(const double &t);
103 
104  /*!
105  * \brief Check if timespec is set.
106  *
107  * \param ts Timespec.
108  *
109  * \return Returns true or false.
110  */
111  extern bool isSet(const timespec &a);
112 
113  /*!
114  * \brief Clear this object's time.
115  *
116  * \param ts Timespec.
117  */
118  extern void clear(timespec &ts);
119 
120  /*!
121  * \brief Add two timespecs.
122  *
123  * a + b.
124  *
125  * \param a Timespec a.
126  * \param b Timespec b.
127  *
128  * \return Normalized timespec.
129  */
130  extern timespec add(const timespec &a, const timespec &b);
131 
132  /*!
133  * \brief a + b
134  *
135  * \param a Timespec a.
136  * \param b Timespec b.
137  *
138  * \return Normalized timespec.
139  */
140  inline timespec operator+(const timespec &a, const timespec &b)
141  {
142  return add(a, b);
143  }
144 
145  /*!
146  * \brief Subtract two timespecs.
147  *
148  * a - b
149  *
150  * \param a Timespec a.
151  * \param b Timespec b.
152  *
153  * \return Normalized timespec.
154  */
155  extern timespec sub(const timespec &a, const timespec &b);
156 
157  /*!
158  * \brief a - b
159  *
160  * \param a Timespec a.
161  * \param b Timespec b.
162  *
163  * \return Normalized timespec.
164  */
165  inline timespec operator-(const timespec &a, const timespec &b)
166  {
167  return sub(a, b);
168  }
169 
170  /*!
171  * \brief Normalize the timespec.
172  *
173  * \param a Timespec.
174  *
175  * \return Normalized timespec where tv_nsec has a value in the range
176  * 0 to 999,999,999.
177  */
178  extern timespec normalize(const timespec &a);
179 
180 
181  //--------------------------------------------------------------------------
182  // Class Time
183  //--------------------------------------------------------------------------
184 
185  /*!
186  * \brief Time class.
187  *
188  * The class uses timespec as the underlining interface to system calls.
189  * Some calls require the lower resolution timeval interface. This class
190  * will autobmaitcally down/up samples between the two interfaces
191  */
192  class Time
193  {
194  public:
195  /*!
196  * \brief Default constructor.
197  */
198  Time();
199 
200  /*!
201  * \brief Initialization constructor.
202  *
203  * \param ts Time in timespec format.
204  */
205  Time(const timespec &ts);
206 
207  /*!
208  * \brief Initialization constructor.
209  *
210  * \param t Seconds.
211  */
212  Time(const double &t);
213 
214  /*!
215  * \brief Copy constructor.
216  *
217  * \param src Source object.
218  */
219  Time(const Time &src);
220 
221  /*!
222  * \brief Destructor.
223  */
224  ~Time()
225  {
226  }
227 
228  /*!
229  * \brief Get this object's value as a floating point number of seconds
230  * and fractions of a second.
231  *
232  * \return Double.
233  */
234  double t()
235  {
236  return m_fpTime;
237  }
238 
239  /*!
240  * \brief Get this object's value in timespec format.
241  *
242  * \return timespec.
243  */
244  timespec ts()
245  {
246  return m_tsTime;
247  }
248 
249  /*!
250  * \brief Check if this object's time is set.
251  *
252  * \return Returns true or false.
253  */
254  bool isSet()
255  {
256  return chronos::isSet(m_tsTime);
257  }
258 
259  /*!
260  * \brief Clear this object's time.
261  */
262  void clear();
263 
264 
265  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
266  // Clock and Time Functions
267  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
268 
269  /*!
270  * \brief Get the CLOCK_REALTIME resolution.
271  *
272  * \return Returns a floating point number of the resolution in seconds
273  * and fractions of a second.
274  */
275  double getResolution();
276 
277  /*!
278  * \brief Get the current time, indentified by CLOCK_REALTIME, since the
279  * last Epoch.
280  *
281  * No internal time data are touched.
282  *
283  * \return Returns a floating point number of the current time in seconds
284  * and fractions of a second.
285  */
286  double now();
287 
288  /*!
289  * \brief Mark the current time, indentified by CLOCK_REALTIME, since the
290  * last Epoch.
291  *
292  * The internal time data are automatically updated.
293  *
294  * \return Returns a floating point number of the current time in seconds
295  * and fractions of a second.
296  */
297  double markNow();
298 
299 
300  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
301  // (Compound) Assignment Operators
302  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
303 
304  /*!
305  * \defgroup doc_t_op_assign
306  * \{
307  * \brief this = b
308  *
309  * \param b Rvalue object.
310  *
311  * \return Reference to this.
312  * \}
313  * \copydoc doc_t_op_assign
314  */
315  Time &operator=(const Time &b);
316 
317  /*! \copydoc doc_t_op_assign */
318  Time &operator=(const timespec &b);
319 
320  /*! \copydoc doc_t_op_assign */
321  Time &operator=(const double &b);
322 
323  /*!
324  * \defgroup doc_t_op_add_assign
325  * \{
326  * \brief this += b
327  *
328  * \param b Rvalue object.
329  *
330  * \return Reference to this.
331  * \}
332  * \copydoc doc_t_op_add_assign
333  */
334  Time &operator+=(const Time &b);
335 
336  /*! \copydoc doc_t_op_add_assign */
337  Time &operator+=(const timespec &b);
338 
339  /*! \copydoc doc_t_op_add_assign */
340  Time &operator+=(const double &b);
341 
342  /*!
343  * \defgroup doc_t_op_sub_assign
344  * \{
345  * \brief this -= b
346  *
347  * \param b Rvalue object.
348  *
349  * \return Reference to this.
350  * \}
351  * \copydoc doc_t_op_sub_assign
352  */
353  Time &operator-=(const Time &b);
354 
355  /*! \copydoc doc_t_op_sub_assign */
356  Time &operator-=(const timespec &b);
357 
358  /*! \copydoc doc_t_op_sub_assign */
359  Time &operator-=(const double &b);
360 
361  /*!
362  * \brief this *= b
363  *
364  * \param b Rvalue object.
365  *
366  * \return Reference to this.
367  */
368  Time &operator*=(const double &b);
369 
370 
371  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
372  // Arithmetic Operators
373  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
374 
375  /*!
376  * \defgroup doc_t_op_add
377  * \{
378  * \brief c = a + b
379  *
380  * \param a Time object.
381  * \param b Rvalue object.
382  *
383  * \return Time
384  * \}
385  * \copydoc doc_t_op_add
386  */
387  friend Time operator+(Time a, const Time &b)
388  {
389  return a += b;
390  }
391 
392  /*! \copydoc doc_t_op_add */
393  friend Time operator+(Time a, const timespec &b)
394  {
395  return a += b;
396  }
397 
398  /*! \copydoc doc_t_op_add */
399  friend Time operator+(Time a, const double &b)
400  {
401  return a += b;
402  }
403 
404  /*!
405  * \defgroup doc_t_op_sub
406  * \{
407  * \brief c = a - b
408  *
409  * \param a Time object.
410  * \param b Rvalue object.
411  *
412  * \return Time
413  * \}
414  * \copydoc doc_t_op_sub
415  */
416  friend Time operator-(Time a, const Time &b)
417  {
418  return a -= b;
419  }
420 
421  /*! \copydoc doc_t_op_sub */
422  friend Time operator-(Time a, const timespec &b)
423  {
424  return a -= b;
425  }
426 
427  /*! \copydoc doc_t_op_sub */
428  friend Time operator-(Time a, const double &b)
429  {
430  return a -= b;
431  }
432 
433  /*!
434  * \brief c = a * b
435  *
436  * \param a Time object.
437  * \param b Rvalue object.
438  *
439  * \return Time
440  */
441  friend Time operator*(Time a, const double &b)
442  {
443  return a *= b;
444  }
445 
446 
447  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
448  // Comparision Operators
449  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
450 
451  /*!
452  * \defgroup doc_t_op_eq
453  * \{
454  * \brief this == b
455  *
456  * \param b Rvalue object.
457  *
458  * \return
459  * Returns true if this object is the same time as b.
460  * Otherwise returns false.
461  * \}
462  * \copydoc doc_t_op_eq
463  */
464  bool operator==(const Time &b)
465  {
466  return operator==(b.m_tsTime);
467  }
468 
469  /*! \copydoc doc_t_op_eq */
470  bool operator==(const timespec &b)
471  {
472  return (m_tsTime.tv_sec == b.tv_sec) &&
473  (m_tsTime.tv_nsec == b.tv_nsec);
474  }
475 
476  /*! \copydoc doc_t_op_eq */
477  bool operator==(const double &b)
478  {
479  return m_fpTime == b;
480  }
481 
482  /*!
483  * \defgroup doc_t_op_lt
484  * \{
485  * \brief this < b
486  *
487  * \param b Rvalue object.
488  *
489  * \return
490  * Returns true if this object is an earlier time than b.
491  * Otherwise returns false.
492  * \}
493  * \copydoc doc_t_op_lt
494  */
495  bool operator<(const Time &b)
496  {
497  return operator<(b.m_tsTime);
498  }
499 
500  /*! \copydoc doc_t_op_lt */
501  bool operator<(const timespec &b);
502 
503  /*! \copydoc doc_t_op_lt */
504  bool operator<(const double &b)
505  {
506  return m_fpTime < b;
507  }
508 
509  /*!
510  * \defgroup doc_t_op_gt
511  * \{
512  * \brief this > b
513  *
514  * \param b Rvalue object.
515  *
516  * \return
517  * Returns true if this object is an later time than b.
518  * Otherwise returns false.
519  * \}
520  * \copydoc doc_t_op_gt
521  */
522  bool operator>(const Time &b)
523  {
524  return operator>(b.m_tsTime);
525  }
526 
527  /*! \copydoc doc_t_op_gt */
528  bool operator>(const timespec &b);
529 
530  /*! \copydoc doc_t_op_gt */
531  bool operator>(const double &b)
532  {
533  return m_fpTime > b;
534  }
535 
536  /*!
537  * \defgroup doc_t_op_le
538  * \{
539  * \brief this <= b
540  *
541  * \param b Rvalue object.
542  *
543  * \return
544  * Returns true if this object is an earlier or equal time to b.
545  * Otherwise returns false.
546  * \}
547  * \copydoc doc_t_op_le
548  */
549  bool operator<=(const Time &b)
550  {
551  return operator<(b.m_tsTime) || operator==(b.m_tsTime);
552  }
553 
554  /*! \copydoc doc_t_op_le */
555  bool operator<=(const timespec &b)
556  {
557  return operator<(b) || operator==(b);
558  }
559 
560  /*! \copydoc doc_t_op_le */
561  bool operator<=(const double &b)
562  {
563  return m_fpTime <= b;
564  }
565 
566  /*!
567  * \defgroup doc_t_op_ge
568  * \{
569  * \brief this >= b
570  *
571  * \param b Rvalue object.
572  *
573  * \return
574  * Returns true if this object is an later or equal time to b.
575  * Otherwise returns false.
576  * \}
577  * \copydoc doc_t_op_ge
578  */
579  bool operator>=(const Time &b)
580  {
581  return operator>(b.m_tsTime) || operator==(b.m_tsTime);
582  }
583 
584  /*! \copydoc doc_t_op_ge */
585  bool operator>=(const timespec &b)
586  {
587  return operator>(b) || operator==(b);
588  }
589 
590  /*! \copydoc doc_t_op_ge */
591  bool operator>=(const double &b)
592  {
593  return m_fpTime >= b;
594  }
595 
596 
597  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
598  // String Dates and Times
599  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
600 
601  /*
602  * \brief Convert this time to local calender time.
603  *
604  * Examples:
605  * Resolution = 0: "Wed Jun 30 21:49:08 2017" \n
606  * Resolution = 2: "Wed Jun 30 21:49:08.38 2017"
607  *
608  * \param resSec Resolution of the second field [0,9].
609  *
610  * \return String.
611  */
612  std::string calendarTime(const int resSec = 0) const;
613 
614  friend std::ostream &operator<<(std::ostream &os, const Time &obj);
615 
616  protected:
617  timespec m_tsTime; ///< time in timespec format
618  double m_fpTime; ///< time in floating point format
619  }; // class Time
620 
621  /*!
622  * \brief Time insertion operator.
623  *
624  * \param os Output stream.
625  * \param obj Object to insert.
626  *
627  * \return Reference to output stream.
628  */
629  extern std::ostream &operator<<(std::ostream &os, const Time &obj);
630 
631  } // namespace chronos
632 } // namespace rnr
633 
634 
635 #endif // _RNR_TIME_H
Time & operator=(const Time &b)
this = b
Definition: Time.cxx:227
~Time()
Destructor.
Definition: Time.h:224
bool isSet(const timespec &a)
Check if timespec is set.
Definition: Time.cxx:105
timespec m_tsTime
time in timespec format
Definition: Time.h:617
double m_fpTime
time in floating point format
Definition: Time.h:618
friend Time operator-(Time a, const timespec &b)
c = a - b
Definition: Time.h:422
bool operator<(const Time &b)
this < b
Definition: Time.h:495
friend Time operator+(Time a, const double &b)
c = a + b
Definition: Time.h:399
timespec ts()
Get this object&#39;s value in timespec format.
Definition: Time.h:244
bool operator<=(const Time &b)
this <= b
Definition: Time.h:549
bool operator<(const double &b)
this < b
Definition: Time.h:504
Time class.
Definition: Time.h:192
double getResolution()
Get the CLOCK_REALTIME resolution.
Definition: Time.cxx:196
friend std::ostream & operator<<(std::ostream &os, const Time &obj)
Time insertion operator.
double t()
Get this object&#39;s value as a floating point number of seconds and fractions of a second.
Definition: Time.h:234
friend Time operator+(Time a, const timespec &b)
c = a + b
Definition: Time.h:393
Time & operator-=(const Time &b)
this -= b
Definition: Time.cxx:269
void clear()
Clear this object&#39;s time.
Definition: Time.cxx:185
double toFp(const timespec &ts)
Convert timespec to floating point number equivalent.
Definition: Time.cxx:86
bool isSet()
Check if this object&#39;s time is set.
Definition: Time.h:254
bool operator<=(const double &b)
this <= b
Definition: Time.h:561
bool operator>=(const Time &b)
this >= b
Definition: Time.h:579
friend Time operator+(Time a, const Time &b)
c = a + b
Definition: Time.h:387
void clear(timespec &ts)
Clear this object&#39;s time.
Definition: Time.cxx:110
double now()
Get the current time, indentified by CLOCK_REALTIME, since the last Epoch.
Definition: Time.cxx:208
timespec sub(const timespec &a, const timespec &b)
Subtract two timespecs.
Definition: Time.cxx:126
bool operator==(const double &b)
this == b
Definition: Time.h:477
bool operator>=(const double &b)
this >= b
Definition: Time.h:591
timespec toTs(const double &t)
Convert floating point seconds to timespec equivalent.
Definition: Time.cxx:91
bool operator>(const double &b)
this > b
Definition: Time.h:531
double markNow()
Mark the current time, indentified by CLOCK_REALTIME, since the last Epoch.
Definition: Time.cxx:215
timespec add(const timespec &a, const timespec &b)
Add two timespecs.
Definition: Time.cxx:116
bool operator==(const timespec &b)
this == b
Definition: Time.h:470
bool operator>(const Time &b)
this > b
Definition: Time.h:522
bool operator==(const Time &b)
this == b
Definition: Time.h:464
timespec now()
Get the current time, indentified by CLOCK_REALTIME, since the last Epoch.
Definition: Time.cxx:70
Time()
Default constructor.
Definition: Time.cxx:162
Time & operator+=(const Time &b)
this += b
Definition: Time.cxx:248
friend Time operator-(Time a, const Time &b)
c = a - b
Definition: Time.h:416
Time & operator*=(const double &b)
this *= b
Definition: Time.cxx:290
timespec operator-(const timespec &a, const timespec &b)
a - b
Definition: Time.h:165
const long MILLION
1,000,000
Definition: Time.h:65
friend Time operator*(Time a, const double &b)
c = a * b
Definition: Time.h:441
RoadNarrows Robotics.
Definition: Camera.h:74
timespec normalize(const timespec &a)
Normalize the timespec.
Definition: Time.cxx:136
bool operator>=(const timespec &b)
this >= b
Definition: Time.h:585
const long long BILLION
1,000,000,000
Definition: Time.h:66
std::ostream & operator<<(std::ostream &os, const timespec &obj)
A timespec insertion operator.
bool operator<=(const timespec &b)
this <= b
Definition: Time.h:555
friend Time operator-(Time a, const double &b)
c = a - b
Definition: Time.h:428
timespec operator+(const timespec &a, const timespec &b)
a + b
Definition: Time.h:140