All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Time.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Nate Koenig
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 /* Desc: External interfaces for Gazebo
18  * Author: Nate Koenig
19  * Date: 03 Apr 2007
20  */
21 
22 #ifndef TIME_HH
23 #define TIME_HH
24 
25 #include <stdlib.h>
26 #include <time.h>
27 #include <iostream>
28 
29 namespace gazebo
30 {
31  namespace common
32  {
35 
38  class Time
39  {
41  public: Time();
42 
45  public: Time(const Time &_time);
46 
49  public: Time(const struct timeval &_tv);
50 
53  public: Time(const struct timespec &_tv);
54 
58  public: Time(int32_t _sec, int32_t _nsec);
59 
62  public: Time(double _time);
63 
65  public: virtual ~Time();
66 
69  public: static const Time &GetWallTime();
70 
72  public: void SetToWallTime();
73 
77  public: void Set(int32_t _sec, int32_t _nsec);
78 
81  public: void Set(double _seconds);
82 
85  public: double Double() const;
86 
89  public: float Float() const;
90 
93  public: static Time MSleep(unsigned int _ms);
94 
97  public: static Time NSleep(unsigned int _ns);
98 
101  public: static Time NSleep(Time _time);
102 
106  public: Time &operator =(const struct timeval &tv);
107 
111  public: Time &operator =(const struct timespec &tv);
112 
116  public: Time &operator =(const Time &time);
117 
121  public: Time operator +(const struct timeval &tv) const;
122 
126  public: Time operator +(const struct timespec &tv) const;
127 
131  public: const Time &operator +=(const struct timeval &tv);
132 
136  public: const Time &operator +=(const struct timespec &tv);
137 
140  public: Time operator +(const Time &time) const;
141 
144  public: const Time &operator +=(const Time &time);
145 
148  public: Time operator -(const struct timeval &tv) const;
149 
152  public: const Time &operator -=(const struct timeval &tv);
153 
156  public: Time operator -(const struct timespec &tv) const;
157 
160  public: const Time &operator -=(const struct timespec &tv);
161 
164  public: Time operator -(const Time &time) const;
165 
168  public: const Time &operator -=(const Time &time);
169 
172  public: Time operator *(const struct timeval &tv) const;
173 
177  public: const Time &operator *=(const struct timeval &tv);
178 
181  public: Time operator *(const struct timespec &tv) const;
182 
186  public: const Time &operator *=(const struct timespec &tv);
187 
191  public: Time operator *(const Time &time) const;
192 
196  public: const Time &operator *=(const Time &time);
197 
201  public: Time operator /(const struct timeval &tv) const;
202 
206  public: const Time &operator /=(const struct timeval &tv);
207 
211  public: Time operator /(const struct timespec &tv) const;
212 
216  public: const Time &operator /=(const struct timespec &tv);
217 
221  public: Time operator /(const Time &time) const;
222 
226  public: const Time &operator /=(const Time &time);
227 
231  public: bool operator ==(const struct timeval &tv) const;
232 
236  public: bool operator ==(const struct timespec &tv) const;
237 
241  public: bool operator ==(const Time &time) const;
242 
246  public: bool operator ==(double time) const;
247 
251  public: bool operator!=(const struct timeval &tv) const;
252 
256  public: bool operator!=(const struct timespec &tv) const;
257 
261  public: bool operator!=(const Time &time) const;
262 
266  public: bool operator!=(double time) const;
267 
271  public: bool operator<(const struct timeval &tv) const;
272 
276  public: bool operator<(const struct timespec &tv) const;
277 
281  public: bool operator<(const Time &time) const;
282 
286  public: bool operator<(double time) const;
287 
291  public: bool operator<=(const struct timeval &tv) const;
292 
296  public: bool operator<=(const struct timespec &tv) const;
297 
301  public: bool operator<=(const Time &time) const;
302 
306  public: bool operator<=(double time) const;
307 
311  public: bool operator>(const struct timeval &tv) const;
312 
316  public: bool operator>(const struct timespec &tv) const;
317 
321  public: bool operator>(const Time &time) const;
322 
326  public: bool operator>(double time) const;
327 
331  public: bool operator>=(const struct timeval &tv) const;
332 
336  public: bool operator>=(const struct timespec &tv) const;
337 
341  public: bool operator>=(const Time &time) const;
342 
346  public: bool operator>=(double time) const;
347 
351  public: static inline double SecToNano(double _sec)
352  { return _sec * 1e-9;}
353 
357  public: static inline double MilToNano(double _ms)
358  { return _ms * 1e-6;}
359 
363  public: static inline double MicToNano(double _ms)
364  { return _ms * 1e-3;}
365 
370  public: friend std::ostream &operator<<(std::ostream &_out,
371  const gazebo::common::Time &_time)
372  {
373  _out << _time.sec << " " << _time.nsec;
374  return _out;
375  }
376 
381  public: friend std::istream &operator>>(std::istream &_in,
382  gazebo::common::Time &_time)
383  {
384  // Skip white spaces
385  _in.setf(std::ios_base::skipws);
386  _in >> _time.sec >> _time.nsec;
387  return _in;
388  }
389 
391  public: int32_t sec;
392 
394  public: int32_t nsec;
395 
397  private: static Time wallTime;
398 
401  private: inline void Correct()
402  {
403  // In the case sec and nsec have different signs, normalize
404  while (this->sec > 0 && this->nsec < 0)
405  {
406  this->sec--;
407  this->nsec += 1e9;
408  }
409  while (this->sec < 0 && this->nsec > 0)
410  {
411  this->sec++;
412  this->nsec -= 1e9;
413  }
414 
415  // Make any corrections
416  this->sec += this->nsec / static_cast<int32_t>(1e9);
417  this->nsec = this->nsec % static_cast<int32_t>(1e9);
418  }
419  private: static struct timespec clock_resolution;
420  };
422  }
423 }
424 #endif