source: rrlib_util/tTime.h @ 14:ab5b300482aa

Last change on this file since 14:ab5b300482aa was 14:ab5b300482aa, checked in by Jens Wettach <wettach@…>, 9 years ago

added stream output for tTime

File size: 3.4 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) AG Robotersysteme TU Kaiserslautern
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{
34namespace util
35{
36class tTime
37{
38public:
39  tTime(): tv_sec(0), tv_usec(0)
40  {}
41
42  tTime(timeval time): tv_sec(time.tv_sec), tv_usec(time.tv_usec)
43  {}
44
45  //! constructor that gets a time in seconds plus microseconds
46  tTime(long sec, long usec)
47  {
48    tv_sec = sec;
49    tv_usec = usec;
50    NormalizeTime();
51  }
52
53
54  inline void SetTvSec(long new_tv_sec)
55  {
56    this->tv_sec = new_tv_sec;
57  }
58  inline void SetTvUSec(long new_tv_usec)
59  {
60    this->tv_usec = new_tv_usec;
61  }
62  inline long TvSec()const
63  {
64    return this->tv_sec;
65  }
66  inline long TvUSec()const
67  {
68    return this->tv_usec;
69  }
70  inline tTime FromUSec(long usec)
71  {
72    if (usec < 0)
73      *this = tTime(usec / 1000000 - 1, (usec % 1000000) + 1000000);
74    else
75      *this = tTime(usec / 1000000, usec % 1000000);
76    return *this;
77  }
78  //! This static function returns a tTime that contains the current System-time
79  static inline tTime Now()
80  {
81    timeval ntime;
82    gettimeofday(&ntime, 0);
83    return tTime(ntime);
84  }
85
86  /*! Returns tTime in seconds rounded down to an long integer.*/
87  inline long long ToSec()const
88  {
89    return this->tv_sec;
90  }
91
92  /*! Returns tTime in milli seconds rounded down to an long integer.*/
93  inline long long ToMSec()const
94  {
95    return (long long)tv_usec / 1000 + (long long)tv_sec*1000;
96  }
97
98  /*! Returns tTime in microseconds */
99  inline long long ToUSec()const
100  {
101    return (long long)tv_usec + (long long)tv_sec*1000000;
102  }
103
104  /*! Substracts the second time from the first */
105  inline tTime operator-(const tTime& b)const
106  {
107    tTime a = *this;
108    return a -= b;
109  }
110
111  /*! Substracts a second time */
112  inline tTime operator-=(const tTime& b)
113  {
114    tv_usec -= b.tv_usec;
115    tv_sec -= b.tv_sec;
116    NormalizeTime();
117    return *this;
118  }
119
120private:
121  long tv_sec;
122  long tv_usec;
123
124  /*! Makes sure that tv_usec lies in the interval [0; 1000000] by:
125      - converting multiples of 1000000 usec into multiples of sec
126      - transforming tv_sec and tv_usec so that the usec part is positive
127  */
128  inline void NormalizeTime()
129  {
130    // test modulo
131    tv_sec = tv_sec + tv_usec / 1000000;
132    tv_usec = tv_usec % 1000000;
133
134    // ensure that usec part is positive
135    if (tv_usec < 0)
136    {
137      tv_sec --;
138      tv_usec += 1000000;
139    }
140  }
141
142};
143
144std::ostream& operator<<(std::ostream& str, const tTime& time);
145}
146}
147
148#endif
Note: See TracBrowser for help on using the repository browser.