Time.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 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 
18 #ifndef _GAZEBO_COMMON_TIME_HH_
19 #define _GAZEBO_COMMON_TIME_HH_
20 
21 #include <string>
22 #include <stdlib.h>
23 #include <time.h>
24 #include <iostream>
25 
27 #include "gazebo/util/system.hh"
28 
29 namespace gazebo
30 {
31  namespace common
32  {
36  #define GZ_SEC_TO_HOUR(_s) ((_s) / 3600.0)
37 
40 
44  class GZ_COMMON_VISIBLE Time
45  {
47  public: static const Time Zero;
48 
50  public: static const Time Second;
51 
53  public: static const Time Hour;
54 
57  public: enum FormatOption
58  {
60  DAYS = 0,
62  HOURS = 1,
64  MINUTES = 2,
66  SECONDS = 3,
68  MILLISECONDS = 4
69  };
70 
72  public: Time();
73 
76  public: Time(const Time &_time);
77 
80  public: Time(const struct timeval &_tv);
81 
84  public: Time(const struct timespec &_tv);
85 
89  public: Time(int32_t _sec, int32_t _nsec);
90 
93  public: Time(double _time);
94 
96  public: virtual ~Time();
97 
100  public: static const Time &GetWallTime();
101 
104  public: static const std::string &GetWallTimeAsISOString();
105 
107  public: void SetToWallTime();
108 
112  public: void Set(int32_t _sec, int32_t _nsec);
113 
116  public: void Set(double _seconds);
117 
120  public: double Double() const;
121 
124  public: float Float() const;
125 
131  public: std::string FormattedString(FormatOption _start = DAYS,
132  FormatOption _end = MILLISECONDS) const;
133 
137  public: static Time Sleep(const common::Time &_time);
138 
142  public: static Time MSleep(unsigned int _ms);
143 
147  public: static Time NSleep(unsigned int _ns);
148 
152  public: Time &operator =(const struct timeval &_tv);
153 
157  public: Time &operator =(const struct timespec &_tv);
158 
162  public: Time &operator =(const Time &_time);
163 
167  public: Time operator +(const struct timeval &_tv) const;
168 
172  public: Time operator +(const struct timespec &_tv) const;
173 
177  public: const Time &operator +=(const struct timeval &_tv);
178 
182  public: const Time &operator +=(const struct timespec &_tv);
183 
187  public: Time operator +(const Time &_time) const;
188 
192  public: const Time &operator +=(const Time &_time);
193 
197  public: Time operator -(const struct timeval &_tv) const;
198 
202  public: const Time &operator -=(const struct timeval &_tv);
203 
207  public: Time operator -(const struct timespec &_tv) const;
208 
212  public: const Time &operator -=(const struct timespec &_tv);
213 
217  public: Time operator -(const Time &_time) const;
218 
222  public: const Time &operator -=(const Time &_time);
223 
227  public: Time operator *(const struct timeval &_tv) const;
228 
232  public: const Time &operator *=(const struct timeval &_tv);
233 
237  public: Time operator *(const struct timespec &_tv) const;
238 
242  public: const Time &operator *=(const struct timespec &_tv);
243 
247  public: Time operator *(const Time &_time) const;
248 
252  public: const Time &operator *=(const Time &_time);
253 
257  public: Time operator /(const struct timeval &_tv) const;
258 
262  public: const Time &operator /=(const struct timeval &_tv);
263 
267  public: Time operator /(const struct timespec &_tv) const;
268 
272  public: const Time &operator /=(const struct timespec &_tv);
273 
277  public: Time operator /(const Time &_time) const;
278 
282  public: const Time &operator /=(const Time &time);
283 
287  public: bool operator ==(const struct timeval &_tv) const;
288 
292  public: bool operator ==(const struct timespec &_tv) const;
293 
297  public: bool operator ==(const Time &_time) const;
298 
302  public: bool operator ==(double _time) const;
303 
307  public: bool operator!=(const struct timeval &_tv) const;
308 
312  public: bool operator!=(const struct timespec &_tv) const;
313 
317  public: bool operator!=(const Time &_time) const;
318 
322  public: bool operator!=(double _time) const;
323 
327  public: bool operator<(const struct timeval &_tv) const;
328 
332  public: bool operator<(const struct timespec &_tv) const;
333 
337  public: bool operator<(const Time &_time) const;
338 
342  public: bool operator<(double _time) const;
343 
347  public: bool operator<=(const struct timeval &_tv) const;
348 
352  public: bool operator<=(const struct timespec &_tv) const;
353 
357  public: bool operator<=(const Time &_time) const;
358 
362  public: bool operator<=(double _time) const;
363 
367  public: bool operator>(const struct timeval &_tv) const;
368 
372  public: bool operator>(const struct timespec &_tv) const;
373 
377  public: bool operator>(const Time &_time) const;
378 
382  public: bool operator>(double _time) const;
383 
387  public: bool operator>=(const struct timeval &_tv) const;
388 
392  public: bool operator>=(const struct timespec &_tv) const;
393 
397  public: bool operator>=(const Time &_time) const;
398 
402  public: bool operator>=(double _time) const;
403 
407  public: static inline double SecToNano(double _sec)
408  { return _sec * 1e9;}
409 
413  public: static inline double MilToNano(double _ms)
414  { return _ms * 1e6;}
415 
419  public: static inline double MicToNano(double _ms)
420  { return _ms * 1e3;}
421 
426  public: friend std::ostream &operator<<(std::ostream &_out,
427  const gazebo::common::Time &_time)
428  {
429  _out << _time.sec << " " << _time.nsec;
430  return _out;
431  }
432 
437  public: friend std::istream &operator>>(std::istream &_in,
438  gazebo::common::Time &_time)
439  {
440  // Skip white spaces
441  _in.setf(std::ios_base::skipws);
442  _in >> _time.sec >> _time.nsec;
443  return _in;
444  }
445 
447  public: int32_t sec;
448 
450  public: int32_t nsec;
451 
453  public: static const int32_t nsInSec;
454 
457  public: static const int32_t nsInMs;
458 
460  private: static Time wallTime;
461 
463  private: static std::string wallTimeISO;
464 
467  private: inline void Correct()
468  {
469  // In the case sec and nsec have different signs, normalize
470  if (this->sec > 0 && this->nsec < 0)
471  {
472  int32_t n = abs(this->nsec / this->nsInSec) + 1;
473  this->sec -= n;
474  this->nsec += n * this->nsInSec;
475  }
476  if (this->sec < 0 && this->nsec > 0)
477  {
478  int32_t n = abs(this->nsec / this->nsInSec) + 1;
479  this->sec += n;
480  this->nsec -= n * this->nsInSec;
481  }
482 
483  // Make any corrections
484  this->sec += this->nsec / this->nsInSec;
485  this->nsec = this->nsec % this->nsInSec;
486  }
487 
488  private: static struct timespec clockResolution;
489  };
491  }
492 }
493 #endif
static const int32_t nsInSec
Constant multiplier to convert from nanoseconds to seconds.
Definition: Time.hh:453
Forward declarations for the common classes.
Definition: Animation.hh:33
int32_t nsec
Nanoseconds.
Definition: Time.hh:450
static const Time Hour
A static time variable set to an hour: common::Time(3600, 0).
Definition: Time.hh:53
int32_t sec
Seconds.
Definition: Time.hh:447
friend std::ostream & operator<<(std::ostream &_out, const gazebo::common::Time &_time)
Stream insertion operator.
Definition: Time.hh:426
static const int32_t nsInMs
Constant multiplier to convert from nanoseconds to milliseconds.
Definition: Time.hh:457
static double MilToNano(double _ms)
Convert milliseconds to nanoseconds.
Definition: Time.hh:413
FormatOption
Definition: Time.hh:57
static double MicToNano(double _ms)
Convert microseconds to nanoseconds.
Definition: Time.hh:419
GAZEBO_VISIBLE void Set(common::Image &_img, const msgs::Image &_msg)
Convert a msgs::Image to a common::Image.
static double SecToNano(double _sec)
Convert seconds to nanoseconds.
Definition: Time.hh:407
friend std::istream & operator>>(std::istream &_in, gazebo::common::Time &_time)
Stream extraction operator.
Definition: Time.hh:437
static const Time Second
A static time variable set to a second: common::Time(1, 0).
Definition: Time.hh:50
static const Time Zero
A static zero time variable set to common::Time(0, 0).
Definition: Time.hh:47
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:44