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 <string>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <iostream>
29 
31 
32 namespace gazebo
33 {
34  namespace common
35  {
38 
42  class Time
43  {
45  public: static const Time Zero;
46 
48  public: Time();
49 
52  public: Time(const Time &_time);
53 
56  public: Time(const struct timeval &_tv);
57 
60  public: Time(const struct timespec &_tv);
61 
65  public: Time(int32_t _sec, int32_t _nsec);
66 
69  public: Time(double _time);
70 
72  public: virtual ~Time();
73 
76  public: static const Time &GetWallTime();
77 
80  public: static const std::string &GetWallTimeAsISOString();
81 
83  public: void SetToWallTime();
84 
88  public: void Set(int32_t _sec, int32_t _nsec);
89 
92  public: void Set(double _seconds);
93 
96  public: double Double() const;
97 
100  public: float Float() const;
101 
105  public: static Time Sleep(const common::Time &_time);
106 
110  public: static Time MSleep(unsigned int _ms);
111 
115  public: static Time NSleep(unsigned int _ns);
116 
120  public: Time &operator =(const struct timeval &_tv);
121 
125  public: Time &operator =(const struct timespec &_tv);
126 
130  public: Time &operator =(const Time &_time);
131 
135  public: Time operator +(const struct timeval &_tv) const;
136 
140  public: Time operator +(const struct timespec &_tv) const;
141 
145  public: const Time &operator +=(const struct timeval &_tv);
146 
150  public: const Time &operator +=(const struct timespec &_tv);
151 
155  public: Time operator +(const Time &_time) const;
156 
160  public: const Time &operator +=(const Time &_time);
161 
165  public: Time operator -(const struct timeval &_tv) const;
166 
170  public: const Time &operator -=(const struct timeval &_tv);
171 
175  public: Time operator -(const struct timespec &_tv) const;
176 
180  public: const Time &operator -=(const struct timespec &_tv);
181 
185  public: Time operator -(const Time &_time) const;
186 
190  public: const Time &operator -=(const Time &_time);
191 
195  public: Time operator *(const struct timeval &_tv) const;
196 
200  public: const Time &operator *=(const struct timeval &_tv);
201 
205  public: Time operator *(const struct timespec &_tv) const;
206 
210  public: const Time &operator *=(const struct timespec &_tv);
211 
215  public: Time operator *(const Time &_time) const;
216 
220  public: const Time &operator *=(const Time &_time);
221 
225  public: Time operator /(const struct timeval &_tv) const;
226 
230  public: const Time &operator /=(const struct timeval &_tv);
231 
235  public: Time operator /(const struct timespec &_tv) const;
236 
240  public: const Time &operator /=(const struct timespec &_tv);
241 
245  public: Time operator /(const Time &_time) const;
246 
250  public: const Time &operator /=(const Time &time);
251 
255  public: bool operator ==(const struct timeval &_tv) const;
256 
260  public: bool operator ==(const struct timespec &_tv) const;
261 
265  public: bool operator ==(const Time &_time) const;
266 
270  public: bool operator ==(double _time) const;
271 
275  public: bool operator!=(const struct timeval &_tv) const;
276 
280  public: bool operator!=(const struct timespec &_tv) const;
281 
285  public: bool operator!=(const Time &_time) const;
286 
290  public: bool operator!=(double _time) const;
291 
295  public: bool operator<(const struct timeval &_tv) const;
296 
300  public: bool operator<(const struct timespec &_tv) const;
301 
305  public: bool operator<(const Time &_time) const;
306 
310  public: bool operator<(double _time) const;
311 
315  public: bool operator<=(const struct timeval &_tv) const;
316 
320  public: bool operator<=(const struct timespec &_tv) const;
321 
325  public: bool operator<=(const Time &_time) const;
326 
330  public: bool operator<=(double _time) const;
331 
335  public: bool operator>(const struct timeval &_tv) const;
336 
340  public: bool operator>(const struct timespec &_tv) const;
341 
345  public: bool operator>(const Time &_time) const;
346 
350  public: bool operator>(double _time) const;
351 
355  public: bool operator>=(const struct timeval &_tv) const;
356 
360  public: bool operator>=(const struct timespec &_tv) const;
361 
365  public: bool operator>=(const Time &_time) const;
366 
370  public: bool operator>=(double _time) const;
371 
375  public: static inline double SecToNano(double _sec)
376  { return _sec * 1e9;}
377 
381  public: static inline double MilToNano(double _ms)
382  { return _ms * 1e6;}
383 
387  public: static inline double MicToNano(double _ms)
388  { return _ms * 1e3;}
389 
394  public: friend std::ostream &operator<<(std::ostream &_out,
395  const gazebo::common::Time &_time)
396  {
397  _out << _time.sec << " " << _time.nsec;
398  return _out;
399  }
400 
405  public: friend std::istream &operator>>(std::istream &_in,
406  gazebo::common::Time &_time)
407  {
408  // Skip white spaces
409  _in.setf(std::ios_base::skipws);
410  _in >> _time.sec >> _time.nsec;
411  return _in;
412  }
413 
415  public: int32_t sec;
416 
418  public: int32_t nsec;
419 
421  private: static Time wallTime;
422 
424  private: static std::string wallTimeISO;
425 
428  private: inline void Correct()
429  {
430  // In the case sec and nsec have different signs, normalize
431  if (this->sec > 0 && this->nsec < 0)
432  {
433  int32_t n = abs(this->nsec / 1e9) + 1;
434  this->sec -= n;
435  this->nsec += n * 1e9;
436  }
437  if (this->sec < 0 && this->nsec > 0)
438  {
439  int32_t n = abs(this->nsec / 1e9) + 1;
440  this->sec += n;
441  this->nsec -= n * 1e9;
442  }
443 
444  // Make any corrections
445  this->sec += this->nsec / static_cast<int32_t>(1e9);
446  this->nsec = this->nsec % static_cast<int32_t>(1e9);
447  }
448  private: static struct timespec clockResolution;
449  };
451  }
452 }
453 #endif