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 (C) 2012-2014 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 <string>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <iostream>
29 
31 #include "gazebo/util/system.hh"
32 
33 namespace gazebo
34 {
35  namespace common
36  {
39 
44  {
46  public: static const Time Zero;
47 
49  public: Time();
50 
53  public: Time(const Time &_time);
54 
57  public: Time(const struct timeval &_tv);
58 
61  public: Time(const struct timespec &_tv);
62 
66  public: Time(int32_t _sec, int32_t _nsec);
67 
70  public: Time(double _time);
71 
73  public: virtual ~Time();
74 
77  public: static const Time &GetWallTime();
78 
81  public: static const std::string &GetWallTimeAsISOString();
82 
84  public: void SetToWallTime();
85 
89  public: void Set(int32_t _sec, int32_t _nsec);
90 
93  public: void Set(double _seconds);
94 
97  public: double Double() const;
98 
101  public: float Float() const;
102 
106  public: static Time Sleep(const common::Time &_time);
107 
111  public: static Time MSleep(unsigned int _ms);
112 
116  public: static Time NSleep(unsigned int _ns);
117 
121  public: Time &operator =(const struct timeval &_tv);
122 
126  public: Time &operator =(const struct timespec &_tv);
127 
131  public: Time &operator =(const Time &_time);
132 
136  public: Time operator +(const struct timeval &_tv) const;
137 
141  public: Time operator +(const struct timespec &_tv) const;
142 
146  public: const Time &operator +=(const struct timeval &_tv);
147 
151  public: const Time &operator +=(const struct timespec &_tv);
152 
156  public: Time operator +(const Time &_time) const;
157 
161  public: const Time &operator +=(const Time &_time);
162 
166  public: Time operator -(const struct timeval &_tv) const;
167 
171  public: const Time &operator -=(const struct timeval &_tv);
172 
176  public: Time operator -(const struct timespec &_tv) const;
177 
181  public: const Time &operator -=(const struct timespec &_tv);
182 
186  public: Time operator -(const Time &_time) const;
187 
191  public: const Time &operator -=(const Time &_time);
192 
196  public: Time operator *(const struct timeval &_tv) const;
197 
201  public: const Time &operator *=(const struct timeval &_tv);
202 
206  public: Time operator *(const struct timespec &_tv) const;
207 
211  public: const Time &operator *=(const struct timespec &_tv);
212 
216  public: Time operator *(const Time &_time) const;
217 
221  public: const Time &operator *=(const Time &_time);
222 
226  public: Time operator /(const struct timeval &_tv) const;
227 
231  public: const Time &operator /=(const struct timeval &_tv);
232 
236  public: Time operator /(const struct timespec &_tv) const;
237 
241  public: const Time &operator /=(const struct timespec &_tv);
242 
246  public: Time operator /(const Time &_time) const;
247 
251  public: const Time &operator /=(const Time &time);
252 
256  public: bool operator ==(const struct timeval &_tv) const;
257 
261  public: bool operator ==(const struct timespec &_tv) const;
262 
266  public: bool operator ==(const Time &_time) const;
267 
271  public: bool operator ==(double _time) const;
272 
276  public: bool operator!=(const struct timeval &_tv) const;
277 
281  public: bool operator!=(const struct timespec &_tv) const;
282 
286  public: bool operator!=(const Time &_time) const;
287 
291  public: bool operator!=(double _time) const;
292 
296  public: bool operator<(const struct timeval &_tv) const;
297 
301  public: bool operator<(const struct timespec &_tv) const;
302 
306  public: bool operator<(const Time &_time) const;
307 
311  public: bool operator<(double _time) const;
312 
316  public: bool operator<=(const struct timeval &_tv) const;
317 
321  public: bool operator<=(const struct timespec &_tv) const;
322 
326  public: bool operator<=(const Time &_time) const;
327 
331  public: bool operator<=(double _time) const;
332 
336  public: bool operator>(const struct timeval &_tv) const;
337 
341  public: bool operator>(const struct timespec &_tv) const;
342 
346  public: bool operator>(const Time &_time) const;
347 
351  public: bool operator>(double _time) const;
352 
356  public: bool operator>=(const struct timeval &_tv) const;
357 
361  public: bool operator>=(const struct timespec &_tv) const;
362 
366  public: bool operator>=(const Time &_time) const;
367 
371  public: bool operator>=(double _time) const;
372 
376  public: static inline double SecToNano(double _sec)
377  { return _sec * 1e9;}
378 
382  public: static inline double MilToNano(double _ms)
383  { return _ms * 1e6;}
384 
388  public: static inline double MicToNano(double _ms)
389  { return _ms * 1e3;}
390 
395  public: friend std::ostream &operator<<(std::ostream &_out,
396  const gazebo::common::Time &_time)
397  {
398  _out << _time.sec << " " << _time.nsec;
399  return _out;
400  }
401 
406  public: friend std::istream &operator>>(std::istream &_in,
407  gazebo::common::Time &_time)
408  {
409  // Skip white spaces
410  _in.setf(std::ios_base::skipws);
411  _in >> _time.sec >> _time.nsec;
412  return _in;
413  }
414 
416  public: int32_t sec;
417 
419  public: int32_t nsec;
420 
422  private: static Time wallTime;
423 
425  private: static std::string wallTimeISO;
426 
429  private: inline void Correct()
430  {
431  // In the case sec and nsec have different signs, normalize
432  if (this->sec > 0 && this->nsec < 0)
433  {
434  int32_t n = abs(this->nsec / 1e9) + 1;
435  this->sec -= n;
436  this->nsec += n * 1e9;
437  }
438  if (this->sec < 0 && this->nsec > 0)
439  {
440  int32_t n = abs(this->nsec / 1e9) + 1;
441  this->sec += n;
442  this->nsec -= n * 1e9;
443  }
444 
445  // Make any corrections
446  this->sec += this->nsec / static_cast<int32_t>(1e9);
447  this->nsec = this->nsec % static_cast<int32_t>(1e9);
448  }
449  private: static struct timespec clockResolution;
450  };
452  }
453 }
454 #endif