source: rrlib_util/tTime.h @ 43:0b04f7b6406e

Last change on this file since 43:0b04f7b6406e was 43:0b04f7b6406e, checked in by Tobias Föhst <foehst@…>, 9 years ago

Reformatted with astyle 2.02.1

File size: 11.8 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation; either version 2
10// of the License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20//
21//----------------------------------------------------------------------
22/*!\file    tTime.h
23 *
24 */
25//----------------------------------------------------------------------
26#ifndef _util_tTime_h_
27#define _util_tTime_h_
28
29#include <sys/time.h>
30#include <iostream>
31
32namespace rrlib
33{
34
35#ifdef _LIB_RRLIB_SERIALIZATION_PRESENT_
36namespace serialization
37{
38class tInputStream;
39class tOutputStream;
40}
41#endif
42
43namespace util
44{
45//! Repesents times (absolutes and differences)
46/*! Use this class whenever you want to deal with times,
47  as it provides a number of operators and functions.
48 */
49class  tTime
50{
51public:
52
53  //! standard constructor, creates a null-time
54  tTime()
55  {
56    tv_sec = 0;
57    tv_usec = 0;
58  };
59
60
61  //! constructor, takes a timeval for creation
62  tTime(const tTime & t)
63  {
64    tv_sec = t.tv_sec;
65    tv_usec = t.tv_usec;
66  }
67
68  //! constructor, takes a timeval for creation
69  tTime(timeval t)
70  {
71    tv_sec = t.tv_sec;
72    tv_usec = t.tv_usec;
73  }
74
75  //! constructor that gets a time in seconds plus microseconds
76  tTime(long sec, long usec)
77  {
78    tv_sec = sec;
79    tv_usec = usec;
80    NormalizeTime();
81  }
82
83  //! This static function returns a tTime that contains the current System-time
84  static inline tTime Now()
85  {
86    timeval ntime;
87    gettimeofday(&ntime, 0);
88    return tTime(ntime);
89  }
90
91  /*! Returns the time of the current thread (time elapsed since start
92    of thread. It exists only for internal MCA usage. You do not need
93    to call it directly. */
94  static inline tTime TaskTime()
95  {
96# if (CLOCKS_PER_SEC == 1000000)
97
98    return tTime(0, clock());
99# else
100
101    return tTime(0, clock() * (1000000 / CLOCKS_PER_SEC));
102# endif
103
104  }
105
106  /*! Returns a time that is calculated by tTime::Now()+tTime(0,usec)
107   */
108  static inline tTime FutureUSec(long usec)
109  {
110    tTime ntime(0, usec);
111    //FromUSec(usec);
112    ntime += tTime::Now();
113    return ntime;
114  }
115
116  /*! Returns a time that is calculated by
117    tTime::Now()+tTime.FromMSec(msec)
118  */
119  static inline tTime FutureMSec(long msec)
120  {
121    tTime ntime(0, msec * 1000);
122    //FromMSec(msec);
123    ntime += tTime::Now();
124    return ntime;
125  }
126
127  /*! Returns a time that is calculated by tTime::Now()+tTime(sec,0)
128   */
129  static inline tTime FutureSec(long sec)
130  {
131    tTime ntime(sec, 0);
132    //FromSec(sec);
133    ntime += tTime::Now();
134    return ntime;
135  }
136
137  /*! Sets tTime to zero. Return value is tTime itself. */
138  inline tTime FromZero()
139  {
140    tv_usec = 0;
141    tv_sec = 0;
142    return *this;
143  }
144
145  /*! Sets tTime to the current  Return value is tTime itself. */
146  inline tTime FromNow()
147  {
148    *this = Now();
149    return *this;
150  }
151
152  /*! Sets tTime to the current task  Return value is tTime itself. */
153  inline tTime FromTaskTime()
154  {
155    *this = TaskTime();
156    return *this;
157  }
158
159  /*! Sets tTime to tTime(sec,0). Return value is tTime itself. */
160  inline tTime FromSec(long sec)
161  {
162    *this = tTime(sec, 0);
163    return *this;
164  }
165
166  /*! Splits msec in sec and usec and sets tTime to tTime(sec,usec). Return value is tTime itsself. */
167  inline tTime FromMSec(long msec)
168  {
169    if (msec < 0)
170      *this = tTime(msec / 1000 - 1, ((msec % 1000) + 1000) * 1000);
171    else
172      *this = tTime(msec / 1000, (msec % 1000) * 1000);
173    return *this;
174  }
175  inline tTime FromUSec(long usec)
176  {
177    if (usec < 0)
178      *this = tTime(usec / 1000000 - 1, (usec % 1000000) + 1000000);
179    else
180      *this = tTime(usec / 1000000, usec % 1000000);
181    return *this;
182  }
183
184  /*! Compares a tTime with zero. */
185  inline bool IsZero() const
186  {
187    return ((tv_usec == 0) && (tv_sec == 0));
188  }
189
190  /*! Returns tTime in nanoseconds */
191  inline long long ToNSec()const
192  {
193    return ((long long)tv_usec + (long long)tv_sec * 1000000) * 1000;
194  }
195
196  /*! Returns tTime in microseconds */
197  inline long long ToUSec()const
198  {
199    return (long long)tv_usec + (long long)tv_sec * 1000000;
200  }
201
202  /*! Returns tTime in milli seconds rounded down to an long integer.*/
203  inline long long ToMSec()const
204  {
205    return (long long)tv_usec / 1000 + (long long)tv_sec * 1000;
206  }
207
208  /*! Returns tTime in seconds rounded down to an long integer.*/
209  inline long ToSec()const
210  {
211    return tv_sec;
212  }
213
214  /*! Don't use this function: Returns the tv_sec value if timeval which is the basis of tTime */
215  inline long TvSec()const
216  {
217    return tv_sec;
218  }
219
220  /*! Don't use this function: Returns the tv_usec value if timeval which is the basis of tTime */
221  inline long TvUSec() const
222  {
223    return tv_usec;
224  }
225
226  /*! Don't use this function: Returns the tv_usec value if timeval which is the basis of tTime */
227  inline long TvNSec() const
228  {
229    return tv_usec * 1000;
230  }
231
232  /*! Don't use this function: Sets the internal tv_sec variable of timeval */
233  inline void SetTvSec(long new_tv_sec)
234  {
235    tv_sec = new_tv_sec;
236  }
237
238  /*! Don't use this function: Sets the internal tv_usec variable of timeval */
239  inline void SetTvUSec(long new_tv_usec)
240  {
241    tv_usec = new_tv_usec;
242  }
243
244  /*! Use this function if you want to express the time in hours, minutes and seconds */
245  inline int Hours()const
246  {
247    return tv_sec / 3600 % 24;
248  }
249
250  /*! Use this function if you want to express the time in hours, minutes and seconds */
251  inline int Minutes()const
252  {
253    return (tv_sec % 3600) / 60;
254  }
255
256  /*! Use this function if you want to express the time in hours, minutes and seconds */
257  inline int Seconds()const
258  {
259    return (tv_sec % 3600) % 60;
260  }
261
262  /*! Use this function if you want to get the subseconds in milliseconds (rounded) */
263  inline int MSeconds()const
264  {
265    return tv_usec / 1000;
266  }
267
268  /*! Adds two times */
269  inline tTime operator+(const tTime& b)const
270  {
271    tTime a = *this;
272    return a += b;
273  }
274
275  /*! Adds a second time */
276  inline tTime operator+=(const tTime& b)
277  {
278    tv_usec += b.tv_usec;
279    tv_sec += b.tv_sec;
280    //NormalizeTimePositive();
281    NormalizeTime();
282    return *this;
283  }
284
285  /*! Substracts the second time from the first */
286  inline tTime operator-(const tTime& b)const
287  {
288    tTime a = *this;
289    return a -= b;
290  }
291
292  /*! Substracts a second time */
293  inline tTime operator-=(const tTime& b)
294  {
295    tv_usec -= b.tv_usec;
296    tv_sec -= b.tv_sec;
297    //NormalizeTimeNegative();
298    NormalizeTime();
299    return *this;
300  }
301
302  /*! sign operator */
303  inline tTime operator-()const
304  {
305    tTime a(-tv_sec, tv_usec);
306//     a.tv_usec = tv_usec;
307//     a.tv_sec = -tv_sec;
308    return a;
309  }
310
311  /*! Multiplies the time by a factor. Uses operator*= (see below). */
312  inline tTime operator*(double factor)const
313  {
314    tTime a = *this;
315    return a *= factor;
316  }
317
318  /*! Multiplies by a factor. */
319  inline tTime operator*=(double factor)
320  {
321    tv_usec = (long)(tv_usec * factor);
322    tv_sec = (long)(tv_sec * factor);
323    NormalizeTime();
324    return *this;
325  }
326
327  /*! Compares two variables of type tTime. Returns true if they are not equal*/
328  inline bool operator!=(const tTime& b)const
329  {
330    return (tv_usec != b.tv_usec) || (tv_sec != b.tv_sec);
331  }
332
333  /*! Compares two variables of type tTime. Returns true if they are equal*/
334  inline bool operator==(const tTime& b) const
335  {
336    return (tv_usec == b.tv_usec) && (tv_sec == b.tv_sec);
337  }
338
339  /*! Compares two variables of type tTime. Returns true if the first one is earlier than the second one*/
340  inline bool operator<(const tTime& b) const
341  {
342    return (tv_sec == b.tv_sec) ? (tv_usec < b.tv_usec) : (tv_sec < b.tv_sec);
343  }
344
345  /*! Compares two variables of type tTime. Returns true if the first one is later than the second one*/
346  inline bool operator>(const tTime& b) const
347  {
348    return (tv_sec == b.tv_sec) ? (tv_usec > b.tv_usec) : (tv_sec > b.tv_sec);
349  }
350
351  /*! Compares two variables of type tTime. Returns true if the first one is erlier than or equal to the second one*/
352  inline bool operator<=(const tTime& b) const
353  {
354    return (tv_sec == b.tv_sec) ? ((tv_usec == b.tv_usec) || (tv_usec < b.tv_usec)) : (tv_sec < b.tv_sec);
355  }
356
357  /*! Compares two variables of type tTime. Returns true if the first one is later than or equal to the second one*/
358  inline bool operator>=(const tTime& b) const
359  {
360    return (tv_sec == b.tv_sec) ? ((tv_usec == b.tv_usec) || (tv_usec > b.tv_usec)) : (tv_sec > b.tv_sec);
361  }
362
363  /*! Casts this tTime object into timespec object (consists of tv_sec/tv_nsec, see h)*/
364  operator timespec()
365  {
366    timespec t = {tv_sec, 1000 * tv_usec};
367    return t;
368  }
369
370  /*! Returns a formatted string for strftime-like-usage instead of getting "(sec, usec)", which is not really readable when used as global timestamp (date) */
371  inline std::string GetString(const std::string &format_string) const
372  {
373    char buffer[64];
374    strftime(buffer, sizeof(buffer), format_string.c_str(), localtime(&tv_sec));
375    return std::string(buffer);
376  }
377
378
379  // some standard time intervals to be used for timeouts etc
380  static const tTime time_forever;
381  static const tTime time_0ms;
382  static const tTime time_1us;
383  static const tTime time_1ms;
384  static const tTime time_5ms;
385  static const tTime time_10ms;
386  static const tTime time_20ms;
387  static const tTime time_25ms;
388  static const tTime time_30ms;
389  static const tTime time_40ms;
390  static const tTime time_50ms;
391  static const tTime time_100ms;
392  static const tTime time_200ms;
393  static const tTime time_250ms;
394  static const tTime time_300ms;
395  static const tTime time_400ms;
396  static const tTime time_500ms;
397  static const tTime time_1s;
398  static const tTime time_2s;
399  static const tTime time_5s;
400  static const tTime time_10s;
401  static const tTime time_30s;
402  static const tTime time_60s;
403  static const tTime time_120s;
404  static const tTime time_180s;
405  static const tTime time_240s;
406  static const tTime time_30000s;
407  static const tTime time_1year;
408
409private:
410  long tv_sec;
411  long tv_usec;
412
413  /*! Makes sure that tv_usec lies in the interval [0; 1000000] by:
414      - converting multiples of 1000000 usec into multiples of sec
415      - transforming tv_sec and tv_usec so that the usec part is positive
416  */
417  inline void NormalizeTime()
418  {
419    // test modulo
420    tv_sec = tv_sec + tv_usec / 1000000;
421    tv_usec = tv_usec % 1000000;
422
423    // ensure that usec part is positive
424    if (tv_usec < 0)
425    {
426      tv_sec --;
427      tv_usec += 1000000;
428    }
429  }
430};
431
432//! Overloading the << operator for ostream
433/*! Outputs a time as a pair of long integer representing (seconds, microseconds).
434 */
435inline std::ostream &operator<<(std::ostream &ost, const tTime& time)
436{
437  ost << "(" << time.TvSec() << ", " << time.TvUSec() << ")";
438  return ost;
439}
440
441//! Overloading the << operator for ostream
442/*! Outputs a time as a pair of long integer representing (seconds, microseconds).
443 */
444inline std::istream &operator>>(std::istream &str, tTime &time)
445{
446  char temp;
447  long int tv_sec, tv_usec;
448  str >> temp;
449  if (temp == '(')
450  {
451    str >> tv_sec >> temp >> tv_usec >> temp;
452  }
453  else
454  {
455    str.putback(temp);
456    str >> tv_sec >> tv_usec;
457  }
458  time = tTime(tv_sec, tv_usec);
459  return str;
460}
461
462#ifdef _LIB_RRLIB_SERIALIZATION_PRESENT_
463
464serialization::tOutputStream &operator << (serialization::tOutputStream &stream, const tTime &t);
465
466serialization::tInputStream &operator >> (serialization::tInputStream &stream, tTime &t);
467
468#endif
469
470}
471}
472#endif
Note: See TracBrowser for help on using the repository browser.