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 // forward declaration
30 struct timeval;
31 
32 namespace gazebo
33 {
34  namespace common
35  {
39  #define GZ_SEC_TO_HOUR(_s) ((_s) / 3600.0)
40 
43 
47  class GZ_COMMON_VISIBLE Time
48  {
50  public: static const Time Zero;
51 
53  public: static const Time Second;
54 
56  public: static const Time Hour;
57 
60  public: enum FormatOption
61  {
63  DAYS = 0,
65  HOURS = 1,
67  MINUTES = 2,
69  SECONDS = 3,
71  MILLISECONDS = 4
72  };
73 
75  public: Time();
76 
79  public: Time(const Time &_time);
80 
83  // cppcheck-suppress noExplicitConstructor
84  public: Time(const struct timeval &_tv);
85 
88  // cppcheck-suppress noExplicitConstructor
89  public: Time(const struct timespec &_tv);
90 
94  public: Time(int32_t _sec, int32_t _nsec);
95 
98  // cppcheck-suppress noExplicitConstructor
99  public: Time(double _time);
100 
102  public: virtual ~Time();
103 
108  public: static Time Maximum();
109 
112  public: static const Time &GetWallTime();
113 
116  public: static const std::string &GetWallTimeAsISOString();
117 
119  public: void SetToWallTime();
120 
124  public: void Set(int32_t _sec, int32_t _nsec);
125 
128  public: void Set(double _seconds);
129 
132  public: double Double() const;
133 
136  public: float Float() const;
137 
143  public: std::string FormattedString(FormatOption _start = DAYS,
144  FormatOption _end = MILLISECONDS) const;
145 
149  public: static Time Sleep(const common::Time &_time);
150 
154  public: static Time MSleep(unsigned int _ms);
155 
159  public: static Time NSleep(unsigned int _ns);
160 
164  public: Time &operator =(const struct timeval &_tv);
165 
169  public: Time &operator =(const struct timespec &_tv);
170 
174  public: Time &operator =(const Time &_time);
175 
179  public: Time operator +(const struct timeval &_tv) const;
180 
184  public: Time operator +(const struct timespec &_tv) const;
185 
189  public: const Time &operator +=(const struct timeval &_tv);
190 
194  public: const Time &operator +=(const struct timespec &_tv);
195 
199  public: Time operator +(const Time &_time) const;
200 
204  public: const Time &operator +=(const Time &_time);
205 
209  public: Time operator -(const struct timeval &_tv) const;
210 
214  public: const Time &operator -=(const struct timeval &_tv);
215 
219  public: Time operator -(const struct timespec &_tv) const;
220 
224  public: const Time &operator -=(const struct timespec &_tv);
225 
229  public: Time operator -(const Time &_time) const;
230 
234  public: const Time &operator -=(const Time &_time);
235 
239  public: Time operator *(const struct timeval &_tv) const;
240 
244  public: const Time &operator *=(const struct timeval &_tv);
245 
249  public: Time operator *(const struct timespec &_tv) const;
250 
254  public: const Time &operator *=(const struct timespec &_tv);
255 
259  public: Time operator *(const Time &_time) const;
260 
264  public: const Time &operator *=(const Time &_time);
265 
269  public: Time operator /(const struct timeval &_tv) const;
270 
274  public: const Time &operator /=(const struct timeval &_tv);
275 
279  public: Time operator /(const struct timespec &_tv) const;
280 
284  public: const Time &operator /=(const struct timespec &_tv);
285 
289  public: Time operator /(const Time &_time) const;
290 
294  public: const Time &operator /=(const Time &time);
295 
299  public: bool operator ==(const struct timeval &_tv) const;
300 
304  public: bool operator ==(const struct timespec &_tv) const;
305 
309  public: bool operator ==(const Time &_time) const;
310 
314  public: bool operator ==(double _time) const;
315 
319  public: bool operator!=(const struct timeval &_tv) const;
320 
324  public: bool operator!=(const struct timespec &_tv) const;
325 
329  public: bool operator!=(const Time &_time) const;
330 
334  public: bool operator!=(double _time) const;
335 
339  public: bool operator<(const struct timeval &_tv) const;
340 
344  public: bool operator<(const struct timespec &_tv) const;
345 
349  public: bool operator<(const Time &_time) const;
350 
354  public: bool operator<(double _time) const;
355 
359  public: bool operator<=(const struct timeval &_tv) const;
360 
364  public: bool operator<=(const struct timespec &_tv) const;
365 
369  public: bool operator<=(const Time &_time) const;
370 
374  public: bool operator<=(double _time) const;
375 
379  public: bool operator>(const struct timeval &_tv) const;
380 
384  public: bool operator>(const struct timespec &_tv) const;
385 
389  public: bool operator>(const Time &_time) const;
390 
394  public: bool operator>(double _time) const;
395 
399  public: bool operator>=(const struct timeval &_tv) const;
400 
404  public: bool operator>=(const struct timespec &_tv) const;
405 
409  public: bool operator>=(const Time &_time) const;
410 
414  public: bool operator>=(double _time) const;
415 
419  public: static inline double SecToNano(double _sec)
420  { return _sec * 1e9;}
421 
425  public: static inline double MilToNano(double _ms)
426  { return _ms * 1e6;}
427 
431  public: static inline double MicToNano(double _ms)
432  { return _ms * 1e3;}
433 
438  public: friend std::ostream &operator<<(std::ostream &_out,
439  const gazebo::common::Time &_time)
440  {
441  _out << _time.sec << " " << _time.nsec;
442  return _out;
443  }
444 
449  public: friend std::istream &operator>>(std::istream &_in,
450  gazebo::common::Time &_time)
451  {
452  // Skip white spaces
453  _in.setf(std::ios_base::skipws);
454  _in >> _time.sec >> _time.nsec;
455  return _in;
456  }
457 
459  public: int32_t sec;
460 
462  public: int32_t nsec;
463 
465  public: static const int32_t nsInSec;
466 
469  public: static const int32_t nsInMs;
470 
472  private: static Time wallTime;
473 
475  private: static std::string wallTimeISO;
476 
479  private: inline void Correct()
480  {
481  // In the case sec and nsec have different signs, normalize
482  if (this->sec > 0 && this->nsec < 0)
483  {
484  int32_t n = abs(this->nsec / this->nsInSec) + 1;
485  this->sec -= n;
486  this->nsec += n * this->nsInSec;
487  }
488  if (this->sec < 0 && this->nsec > 0)
489  {
490  int32_t n = abs(this->nsec / this->nsInSec) + 1;
491  this->sec += n;
492  this->nsec -= n * this->nsInSec;
493  }
494 
495  // Make any corrections
496  this->sec += this->nsec / this->nsInSec;
497  this->nsec = this->nsec % this->nsInSec;
498  }
499 
500  private: static struct timespec clockResolution;
501  };
503  }
504 }
505 #endif
static const int32_t nsInSec
Constant multiplier to convert from nanoseconds to seconds.
Definition: Time.hh:465
Forward declarations for the common classes.
Definition: Animation.hh:26
int32_t nsec
Nanoseconds.
Definition: Time.hh:462
static const Time Hour
A static time variable set to an hour: common::Time(3600, 0).
Definition: Time.hh:56
int32_t sec
Seconds.
Definition: Time.hh:459
friend std::ostream & operator<<(std::ostream &_out, const gazebo::common::Time &_time)
Stream insertion operator.
Definition: Time.hh:438
static const int32_t nsInMs
Constant multiplier to convert from nanoseconds to milliseconds.
Definition: Time.hh:469
static double MilToNano(double _ms)
Convert milliseconds to nanoseconds.
Definition: Time.hh:425
FormatOption
Definition: Time.hh:60
static double MicToNano(double _ms)
Convert microseconds to nanoseconds.
Definition: Time.hh:431
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:419
common
Definition: FuelModelDatabase.hh:37
friend std::istream & operator>>(std::istream &_in, gazebo::common::Time &_time)
Stream extraction operator.
Definition: Time.hh:449
static const Time Second
A static time variable set to a second: common::Time(1, 0).
Definition: Time.hh:53
static const Time Zero
A static zero time variable set to common::Time(0, 0).
Definition: Time.hh:50
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:47