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 
103  public: static const Time &GetWallTime();
104 
107  public: static const std::string &GetWallTimeAsISOString();
108 
110  public: void SetToWallTime();
111 
115  public: void Set(int32_t _sec, int32_t _nsec);
116 
119  public: void Set(double _seconds);
120 
123  public: double Double() const;
124 
127  public: float Float() const;
128 
134  public: std::string FormattedString(FormatOption _start = DAYS,
135  FormatOption _end = MILLISECONDS) const;
136 
140  public: static Time Sleep(const common::Time &_time);
141 
145  public: static Time MSleep(unsigned int _ms);
146 
150  public: static Time NSleep(unsigned int _ns);
151 
155  public: Time &operator =(const struct timeval &_tv);
156 
160  public: Time &operator =(const struct timespec &_tv);
161 
165  public: Time &operator =(const Time &_time);
166 
170  public: Time operator +(const struct timeval &_tv) const;
171 
175  public: Time operator +(const struct timespec &_tv) const;
176 
180  public: const Time &operator +=(const struct timeval &_tv);
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: Time operator /(const struct timeval &_tv) const;
261 
265  public: const Time &operator /=(const struct timeval &_tv);
266 
270  public: Time operator /(const struct timespec &_tv) const;
271 
275  public: const Time &operator /=(const struct timespec &_tv);
276 
280  public: Time operator /(const Time &_time) const;
281 
285  public: const Time &operator /=(const Time &time);
286 
290  public: bool operator ==(const struct timeval &_tv) const;
291 
295  public: bool operator ==(const struct timespec &_tv) const;
296 
300  public: bool operator ==(const Time &_time) const;
301 
305  public: bool operator ==(double _time) const;
306 
310  public: bool operator!=(const struct timeval &_tv) const;
311 
315  public: bool operator!=(const struct timespec &_tv) const;
316 
320  public: bool operator!=(const Time &_time) const;
321 
325  public: bool operator!=(double _time) const;
326 
330  public: bool operator<(const struct timeval &_tv) const;
331 
335  public: bool operator<(const struct timespec &_tv) const;
336 
340  public: bool operator<(const Time &_time) const;
341 
345  public: bool operator<(double _time) const;
346 
350  public: bool operator<=(const struct timeval &_tv) const;
351 
355  public: bool operator<=(const struct timespec &_tv) const;
356 
360  public: bool operator<=(const Time &_time) const;
361 
365  public: bool operator<=(double _time) const;
366 
370  public: bool operator>(const struct timeval &_tv) const;
371 
375  public: bool operator>(const struct timespec &_tv) const;
376 
380  public: bool operator>(const Time &_time) const;
381 
385  public: bool operator>(double _time) const;
386 
390  public: bool operator>=(const struct timeval &_tv) const;
391 
395  public: bool operator>=(const struct timespec &_tv) const;
396 
400  public: bool operator>=(const Time &_time) const;
401 
405  public: bool operator>=(double _time) const;
406 
410  public: static inline double SecToNano(double _sec)
411  { return _sec * 1e9;}
412 
416  public: static inline double MilToNano(double _ms)
417  { return _ms * 1e6;}
418 
422  public: static inline double MicToNano(double _ms)
423  { return _ms * 1e3;}
424 
429  public: friend std::ostream &operator<<(std::ostream &_out,
430  const gazebo::common::Time &_time)
431  {
432  _out << _time.sec << " " << _time.nsec;
433  return _out;
434  }
435 
440  public: friend std::istream &operator>>(std::istream &_in,
441  gazebo::common::Time &_time)
442  {
443  // Skip white spaces
444  _in.setf(std::ios_base::skipws);
445  _in >> _time.sec >> _time.nsec;
446  return _in;
447  }
448 
450  public: int32_t sec;
451 
453  public: int32_t nsec;
454 
456  public: static const int32_t nsInSec;
457 
460  public: static const int32_t nsInMs;
461 
463  private: static Time wallTime;
464 
466  private: static std::string wallTimeISO;
467 
470  private: inline void Correct()
471  {
472  // In the case sec and nsec have different signs, normalize
473  if (this->sec > 0 && this->nsec < 0)
474  {
475  int32_t n = abs(this->nsec / this->nsInSec) + 1;
476  this->sec -= n;
477  this->nsec += n * this->nsInSec;
478  }
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 
486  // Make any corrections
487  this->sec += this->nsec / this->nsInSec;
488  this->nsec = this->nsec % this->nsInSec;
489  }
490 
491  private: static struct timespec clockResolution;
492  };
494  }
495 }
496 #endif
static const int32_t nsInSec
Constant multiplier to convert from nanoseconds to seconds.
Definition: Time.hh:456
int32_t nsec
Nanoseconds.
Definition: Time.hh:453
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:450
friend std::ostream & operator<<(std::ostream &_out, const gazebo::common::Time &_time)
Stream insertion operator.
Definition: Time.hh:429
static const int32_t nsInMs
Constant multiplier to convert from nanoseconds to milliseconds.
Definition: Time.hh:460
static double MilToNano(double _ms)
Convert milliseconds to nanoseconds.
Definition: Time.hh:416
FormatOption
Definition: Time.hh:57
static double MicToNano(double _ms)
Convert microseconds to nanoseconds.
Definition: Time.hh:422
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:410
friend std::istream & operator>>(std::istream &_in, gazebo::common::Time &_time)
Stream extraction operator.
Definition: Time.hh:440
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