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: static Time NSleep(Time _time) GAZEBO_DEPRECATED;
121 
125  public: Time &operator =(const struct timeval &_tv);
126 
130  public: Time &operator =(const struct timespec &_tv);
131 
135  public: Time &operator =(const Time &_time);
136 
140  public: Time operator +(const struct timeval &_tv) const;
141 
145  public: Time operator +(const struct timespec &_tv) const;
146 
150  public: const Time &operator +=(const struct timeval &_tv);
151 
155  public: const Time &operator +=(const struct timespec &_tv);
156 
160  public: Time operator +(const Time &_time) const;
161 
165  public: const Time &operator +=(const Time &_time);
166 
170  public: Time operator -(const struct timeval &_tv) const;
171 
175  public: const Time &operator -=(const struct timeval &_tv);
176 
180  public: Time operator -(const struct timespec &_tv) const;
181 
185  public: const Time &operator -=(const struct timespec &_tv);
186 
190  public: Time operator -(const Time &_time) const;
191 
195  public: const Time &operator -=(const Time &_time);
196 
200  public: Time operator *(const struct timeval &_tv) const;
201 
205  public: const Time &operator *=(const struct timeval &_tv);
206 
210  public: Time operator *(const struct timespec &_tv) const;
211 
215  public: const Time &operator *=(const struct timespec &_tv);
216 
220  public: Time operator *(const Time &_time) const;
221 
225  public: const Time &operator *=(const Time &_time);
226 
230  public: Time operator /(const struct timeval &_tv) const;
231 
235  public: const Time &operator /=(const struct timeval &_tv);
236 
240  public: Time operator /(const struct timespec &_tv) const;
241 
245  public: const Time &operator /=(const struct timespec &_tv);
246 
250  public: Time operator /(const Time &_time) const;
251 
255  public: const Time &operator /=(const Time &time);
256 
260  public: bool operator ==(const struct timeval &_tv) const;
261 
265  public: bool operator ==(const struct timespec &_tv) const;
266 
270  public: bool operator ==(const Time &_time) const;
271 
275  public: bool operator ==(double _time) const;
276 
280  public: bool operator!=(const struct timeval &_tv) const;
281 
285  public: bool operator!=(const struct timespec &_tv) const;
286 
290  public: bool operator!=(const Time &_time) const;
291 
295  public: bool operator!=(double _time) const;
296 
300  public: bool operator<(const struct timeval &_tv) const;
301 
305  public: bool operator<(const struct timespec &_tv) const;
306 
310  public: bool operator<(const Time &_time) const;
311 
315  public: bool operator<(double _time) const;
316 
320  public: bool operator<=(const struct timeval &_tv) const;
321 
325  public: bool operator<=(const struct timespec &_tv) const;
326 
330  public: bool operator<=(const Time &_time) const;
331 
335  public: bool operator<=(double _time) const;
336 
340  public: bool operator>(const struct timeval &_tv) const;
341 
345  public: bool operator>(const struct timespec &_tv) const;
346 
350  public: bool operator>(const Time &_time) const;
351 
355  public: bool operator>(double _time) const;
356 
360  public: bool operator>=(const struct timeval &_tv) const;
361 
365  public: bool operator>=(const struct timespec &_tv) const;
366 
370  public: bool operator>=(const Time &_time) const;
371 
375  public: bool operator>=(double _time) const;
376 
380  public: static inline double SecToNano(double _sec)
381  { return _sec * 1e-9;}
382 
386  public: static inline double MilToNano(double _ms)
387  { return _ms * 1e-6;}
388 
392  public: static inline double MicToNano(double _ms)
393  { return _ms * 1e-3;}
394 
399  public: friend std::ostream &operator<<(std::ostream &_out,
400  const gazebo::common::Time &_time)
401  {
402  _out << _time.sec << " " << _time.nsec;
403  return _out;
404  }
405 
410  public: friend std::istream &operator>>(std::istream &_in,
411  gazebo::common::Time &_time)
412  {
413  // Skip white spaces
414  _in.setf(std::ios_base::skipws);
415  _in >> _time.sec >> _time.nsec;
416  return _in;
417  }
418 
420  public: int32_t sec;
421 
423  public: int32_t nsec;
424 
426  private: static Time wallTime;
427 
429  private: static std::string wallTimeISO;
430 
433  private: inline void Correct()
434  {
435  // In the case sec and nsec have different signs, normalize
436  if (this->sec > 0 && this->nsec < 0)
437  {
438  int32_t n = abs(this->nsec / 1e9) + 1;
439  this->sec -= n;
440  this->nsec += n * 1e9;
441  }
442  if (this->sec < 0 && this->nsec > 0)
443  {
444  int32_t n = abs(this->nsec / 1e9) + 1;
445  this->sec += n;
446  this->nsec -= n * 1e9;
447  }
448 
449  // Make any corrections
450  this->sec += this->nsec / static_cast<int32_t>(1e9);
451  this->nsec = this->nsec % static_cast<int32_t>(1e9);
452  }
453  private: static struct timespec clockResolution;
454  };
456  }
457 }
458 #endif