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  // cppcheck-suppress noExplicitConstructor
81  public: Time(const struct timeval &_tv);
82 
85  // cppcheck-suppress noExplicitConstructor
86  public: Time(const struct timespec &_tv);
87 
91  public: Time(int32_t _sec, int32_t _nsec);
92 
95  // cppcheck-suppress noExplicitConstructor
96  public: Time(double _time);
97 
99  public: virtual ~Time();
100 
105  public: static Time Maximum();
106 
109  public: static const Time &GetWallTime();
110 
113  public: static const std::string &GetWallTimeAsISOString();
114 
116  public: void SetToWallTime();
117 
121  public: void Set(int32_t _sec, int32_t _nsec);
122 
125  public: void Set(double _seconds);
126 
129  public: double Double() const;
130 
133  public: float Float() const;
134 
140  public: std::string FormattedString(FormatOption _start = DAYS,
141  FormatOption _end = MILLISECONDS) const;
142 
146  public: static Time Sleep(const common::Time &_time);
147 
151  public: static Time MSleep(unsigned int _ms);
152 
156  public: static Time NSleep(unsigned int _ns);
157 
161  public: Time &operator =(const struct timeval &_tv);
162 
166  public: Time &operator =(const struct timespec &_tv);
167 
171  public: Time &operator =(const Time &_time);
172 
176  public: Time operator +(const struct timeval &_tv) const;
177 
181  public: Time operator +(const struct timespec &_tv) const;
182 
186  public: const Time &operator +=(const struct timeval &_tv);
187 
191  public: const Time &operator +=(const struct timespec &_tv);
192 
196  public: Time operator +(const Time &_time) const;
197 
201  public: const Time &operator +=(const Time &_time);
202 
206  public: Time operator -(const struct timeval &_tv) const;
207 
211  public: const Time &operator -=(const struct timeval &_tv);
212 
216  public: Time operator -(const struct timespec &_tv) const;
217 
221  public: const Time &operator -=(const struct timespec &_tv);
222 
226  public: Time operator -(const Time &_time) const;
227 
231  public: const Time &operator -=(const Time &_time);
232 
236  public: Time operator *(const struct timeval &_tv) const;
237 
241  public: const Time &operator *=(const struct timeval &_tv);
242 
246  public: Time operator *(const struct timespec &_tv) const;
247 
251  public: const Time &operator *=(const struct timespec &_tv);
252 
256  public: Time operator *(const Time &_time) const;
257 
261  public: const Time &operator *=(const Time &_time);
262 
266  public: Time operator /(const struct timeval &_tv) const;
267 
271  public: const Time &operator /=(const struct timeval &_tv);
272 
276  public: Time operator /(const struct timespec &_tv) const;
277 
281  public: const Time &operator /=(const struct timespec &_tv);
282 
286  public: Time operator /(const Time &_time) const;
287 
291  public: const Time &operator /=(const Time &time);
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: bool operator>(const struct timeval &_tv) const;
377 
381  public: bool operator>(const struct timespec &_tv) const;
382 
386  public: bool operator>(const Time &_time) const;
387 
391  public: bool operator>(double _time) const;
392 
396  public: bool operator>=(const struct timeval &_tv) const;
397 
401  public: bool operator>=(const struct timespec &_tv) const;
402 
406  public: bool operator>=(const Time &_time) const;
407 
411  public: bool operator>=(double _time) const;
412 
416  public: static inline double SecToNano(double _sec)
417  { return _sec * 1e9;}
418 
422  public: static inline double MilToNano(double _ms)
423  { return _ms * 1e6;}
424 
428  public: static inline double MicToNano(double _ms)
429  { return _ms * 1e3;}
430 
435  public: friend std::ostream &operator<<(std::ostream &_out,
436  const gazebo::common::Time &_time)
437  {
438  _out << _time.sec << " " << _time.nsec;
439  return _out;
440  }
441 
446  public: friend std::istream &operator>>(std::istream &_in,
447  gazebo::common::Time &_time)
448  {
449  // Skip white spaces
450  _in.setf(std::ios_base::skipws);
451  _in >> _time.sec >> _time.nsec;
452  return _in;
453  }
454 
456  public: int32_t sec;
457 
459  public: int32_t nsec;
460 
462  public: static const int32_t nsInSec;
463 
466  public: static const int32_t nsInMs;
467 
469  private: static Time wallTime;
470 
472  private: static std::string wallTimeISO;
473 
476  private: inline void Correct()
477  {
478  // In the case sec and nsec have different signs, normalize
479  if (this->sec > 0 && this->nsec < 0)
480  {
481  int32_t n = abs(this->nsec / this->nsInSec) + 1;
482  this->sec -= n;
483  this->nsec += n * this->nsInSec;
484  }
485  if (this->sec < 0 && this->nsec > 0)
486  {
487  int32_t n = abs(this->nsec / this->nsInSec) + 1;
488  this->sec += n;
489  this->nsec -= n * this->nsInSec;
490  }
491 
492  // Make any corrections
493  this->sec += this->nsec / this->nsInSec;
494  this->nsec = this->nsec % this->nsInSec;
495  }
496 
497  private: static struct timespec clockResolution;
498  };
500  }
501 }
502 #endif
static const int32_t nsInSec
Constant multiplier to convert from nanoseconds to seconds.
Definition: Time.hh:462
Forward declarations for the common classes.
Definition: Animation.hh:26
int32_t nsec
Nanoseconds.
Definition: Time.hh:459
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:456
friend std::ostream & operator<<(std::ostream &_out, const gazebo::common::Time &_time)
Stream insertion operator.
Definition: Time.hh:435
static const int32_t nsInMs
Constant multiplier to convert from nanoseconds to milliseconds.
Definition: Time.hh:466
static double MilToNano(double _ms)
Convert milliseconds to nanoseconds.
Definition: Time.hh:422
FormatOption
Definition: Time.hh:57
static double MicToNano(double _ms)
Convert microseconds to nanoseconds.
Definition: Time.hh:428
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:416
friend std::istream & operator>>(std::istream &_in, gazebo::common::Time &_time)
Stream extraction operator.
Definition: Time.hh:446
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