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 2012 Open Source Robotics Foundation
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 
39  class Time
40  {
42  public: Time();
43 
46  public: Time(const Time &_time);
47 
50  public: Time(const struct timeval &_tv);
51 
54  public: Time(const struct timespec &_tv);
55 
59  public: Time(int32_t _sec, int32_t _nsec);
60 
63  public: Time(double _time);
64 
66  public: virtual ~Time();
67 
70  public: static const Time &GetWallTime();
71 
73  public: void SetToWallTime();
74 
78  public: void Set(int32_t _sec, int32_t _nsec);
79 
82  public: void Set(double _seconds);
83 
86  public: double Double() const;
87 
90  public: float Float() const;
91 
94  public: static Time MSleep(unsigned int _ms);
95 
98  public: static Time NSleep(unsigned int _ns);
99 
102  public: static Time NSleep(Time _time);
103 
107  public: Time &operator =(const struct timeval &_tv);
108 
112  public: Time &operator =(const struct timespec &_tv);
113 
117  public: Time &operator =(const Time &_time);
118 
122  public: Time operator +(const struct timeval &_tv) const;
123 
127  public: Time operator +(const struct timespec &_tv) const;
128 
132  public: const Time &operator +=(const struct timeval &_tv);
133 
137  public: const Time &operator +=(const struct timespec &_tv);
138 
142  public: Time operator +(const Time &_time) const;
143 
147  public: const Time &operator +=(const Time &_time);
148 
152  public: Time operator -(const struct timeval &_tv) const;
153 
157  public: const Time &operator -=(const struct timeval &_tv);
158 
162  public: Time operator -(const struct timespec &_tv) const;
163 
167  public: const Time &operator -=(const struct timespec &_tv);
168 
172  public: Time operator -(const Time &_time) const;
173 
177  public: const Time &operator -=(const Time &_time);
178 
182  public: Time operator *(const struct timeval &_tv) const;
183 
187  public: const Time &operator *=(const struct timeval &_tv);
188 
192  public: Time operator *(const struct timespec &_tv) const;
193 
197  public: const Time &operator *=(const struct timespec &_tv);
198 
202  public: Time operator *(const Time &_time) const;
203 
207  public: const Time &operator *=(const Time &_time);
208 
212  public: Time operator /(const struct timeval &_tv) const;
213 
217  public: const Time &operator /=(const struct timeval &_tv);
218 
222  public: Time operator /(const struct timespec &_tv) const;
223 
227  public: const Time &operator /=(const struct timespec &_tv);
228 
232  public: Time operator /(const Time &_time) const;
233 
237  public: const Time &operator /=(const Time &time);
238 
242  public: bool operator ==(const struct timeval &_tv) const;
243 
247  public: bool operator ==(const struct timespec &_tv) const;
248 
252  public: bool operator ==(const Time &_time) const;
253 
257  public: bool operator ==(double _time) const;
258 
262  public: bool operator!=(const struct timeval &_tv) const;
263 
267  public: bool operator!=(const struct timespec &_tv) const;
268 
272  public: bool operator!=(const Time &_time) const;
273 
277  public: bool operator!=(double _time) const;
278 
282  public: bool operator<(const struct timeval &_tv) const;
283 
287  public: bool operator<(const struct timespec &_tv) const;
288 
292  public: bool operator<(const Time &_time) const;
293 
297  public: bool operator<(double _time) const;
298 
302  public: bool operator<=(const struct timeval &_tv) const;
303 
307  public: bool operator<=(const struct timespec &_tv) const;
308 
312  public: bool operator<=(const Time &_time) const;
313 
317  public: bool operator<=(double _time) const;
318 
322  public: bool operator>(const struct timeval &_tv) const;
323 
327  public: bool operator>(const struct timespec &_tv) const;
328 
332  public: bool operator>(const Time &_time) const;
333 
337  public: bool operator>(double _time) const;
338 
342  public: bool operator>=(const struct timeval &_tv) const;
343 
347  public: bool operator>=(const struct timespec &_tv) const;
348 
352  public: bool operator>=(const Time &_time) const;
353 
357  public: bool operator>=(double _time) const;
358 
362  public: static inline double SecToNano(double _sec)
363  { return _sec * 1e-9;}
364 
368  public: static inline double MilToNano(double _ms)
369  { return _ms * 1e-6;}
370 
374  public: static inline double MicToNano(double _ms)
375  { return _ms * 1e-3;}
376 
381  public: friend std::ostream &operator<<(std::ostream &_out,
382  const gazebo::common::Time &_time)
383  {
384  _out << _time.sec << " " << _time.nsec;
385  return _out;
386  }
387 
392  public: friend std::istream &operator>>(std::istream &_in,
393  gazebo::common::Time &_time)
394  {
395  // Skip white spaces
396  _in.setf(std::ios_base::skipws);
397  _in >> _time.sec >> _time.nsec;
398  return _in;
399  }
400 
402  public: int32_t sec;
403 
405  public: int32_t nsec;
406 
408  private: static Time wallTime;
409 
412  private: inline void Correct()
413  {
414  // In the case sec and nsec have different signs, normalize
415  if (this->sec > 0 && this->nsec < 0)
416  {
417  int32_t n = abs(this->nsec / 1e9) + 1;
418  this->sec -= n;
419  this->nsec += n * 1e9;
420  }
421  if (this->sec < 0 && this->nsec > 0)
422  {
423  int32_t n = abs(this->nsec / 1e9) + 1;
424  this->sec += n;
425  this->nsec -= n * 1e9;
426  }
427 
428  // Make any corrections
429  this->sec += this->nsec / static_cast<int32_t>(1e9);
430  this->nsec = this->nsec % static_cast<int32_t>(1e9);
431  }
432  private: static struct timespec clock_resolution;
433  };
435  }
436 }
437 #endif