Time.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 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  {
35 
40  {
42  public: static const Time Zero;
43 
45  public: static const Time Second;
46 
48  public: static const Time Hour;
49 
52  public: enum FormatOption
53  {
55  DAYS = 0,
57  HOURS = 1,
59  MINUTES = 2,
61  SECONDS = 3,
63  MILLISECONDS = 4
64  };
65 
67  public: Time();
68 
71  public: Time(const Time &_time);
72 
75  public: Time(const struct timeval &_tv);
76 
79  public: Time(const struct timespec &_tv);
80 
84  public: Time(int32_t _sec, int32_t _nsec);
85 
88  public: Time(double _time);
89 
91  public: virtual ~Time();
92 
95  public: static const Time &GetWallTime();
96 
99  public: static const std::string &GetWallTimeAsISOString();
100 
102  public: void SetToWallTime();
103 
107  public: void Set(int32_t _sec, int32_t _nsec);
108 
111  public: void Set(double _seconds);
112 
115  public: double Double() const;
116 
119  public: float Float() const;
120 
126  public: std::string FormattedString(FormatOption _start = DAYS,
127  FormatOption _end = MILLISECONDS) const;
128 
132  public: static Time Sleep(const common::Time &_time);
133 
137  public: static Time MSleep(unsigned int _ms);
138 
142  public: static Time NSleep(unsigned int _ns);
143 
147  public: Time &operator =(const struct timeval &_tv);
148 
152  public: Time &operator =(const struct timespec &_tv);
153 
157  public: Time &operator =(const Time &_time);
158 
162  public: Time operator +(const struct timeval &_tv) const;
163 
167  public: Time operator +(const struct timespec &_tv) const;
168 
172  public: const Time &operator +=(const struct timeval &_tv);
173 
177  public: const Time &operator +=(const struct timespec &_tv);
178 
182  public: Time operator +(const Time &_time) const;
183 
187  public: const Time &operator +=(const Time &_time);
188 
192  public: Time operator -(const struct timeval &_tv) const;
193 
197  public: const Time &operator -=(const struct timeval &_tv);
198 
202  public: Time operator -(const struct timespec &_tv) const;
203 
207  public: const Time &operator -=(const struct timespec &_tv);
208 
212  public: Time operator -(const Time &_time) const;
213 
217  public: const Time &operator -=(const Time &_time);
218 
222  public: Time operator *(const struct timeval &_tv) const;
223 
227  public: const Time &operator *=(const struct timeval &_tv);
228 
232  public: Time operator *(const struct timespec &_tv) const;
233 
237  public: const Time &operator *=(const struct timespec &_tv);
238 
242  public: Time operator *(const Time &_time) const;
243 
247  public: const Time &operator *=(const Time &_time);
248 
252  public: Time operator /(const struct timeval &_tv) const;
253 
257  public: const Time &operator /=(const struct timeval &_tv);
258 
262  public: Time operator /(const struct timespec &_tv) const;
263 
267  public: const Time &operator /=(const struct timespec &_tv);
268 
272  public: Time operator /(const Time &_time) const;
273 
277  public: const Time &operator /=(const Time &time);
278 
282  public: bool operator ==(const struct timeval &_tv) const;
283 
287  public: bool operator ==(const struct timespec &_tv) const;
288 
292  public: bool operator ==(const Time &_time) const;
293 
297  public: bool operator ==(double _time) const;
298 
302  public: bool operator!=(const struct timeval &_tv) const;
303 
307  public: bool operator!=(const struct timespec &_tv) const;
308 
312  public: bool operator!=(const Time &_time) const;
313 
317  public: bool operator!=(double _time) const;
318 
322  public: bool operator<(const struct timeval &_tv) const;
323 
327  public: bool operator<(const struct timespec &_tv) const;
328 
332  public: bool operator<(const Time &_time) const;
333 
337  public: bool operator<(double _time) const;
338 
342  public: bool operator<=(const struct timeval &_tv) const;
343 
347  public: bool operator<=(const struct timespec &_tv) const;
348 
352  public: bool operator<=(const Time &_time) const;
353 
357  public: bool operator<=(double _time) const;
358 
362  public: bool operator>(const struct timeval &_tv) const;
363 
367  public: bool operator>(const struct timespec &_tv) const;
368 
372  public: bool operator>(const Time &_time) const;
373 
377  public: bool operator>(double _time) const;
378 
382  public: bool operator>=(const struct timeval &_tv) const;
383 
387  public: bool operator>=(const struct timespec &_tv) const;
388 
392  public: bool operator>=(const Time &_time) const;
393 
397  public: bool operator>=(double _time) const;
398 
402  public: static inline double SecToNano(double _sec)
403  { return _sec * 1e9;}
404 
408  public: static inline double MilToNano(double _ms)
409  { return _ms * 1e6;}
410 
414  public: static inline double MicToNano(double _ms)
415  { return _ms * 1e3;}
416 
421  public: friend std::ostream &operator<<(std::ostream &_out,
422  const gazebo::common::Time &_time)
423  {
424  _out << _time.sec << " " << _time.nsec;
425  return _out;
426  }
427 
432  public: friend std::istream &operator>>(std::istream &_in,
433  gazebo::common::Time &_time)
434  {
435  // Skip white spaces
436  _in.setf(std::ios_base::skipws);
437  _in >> _time.sec >> _time.nsec;
438  return _in;
439  }
440 
442  public: int32_t sec;
443 
445  public: int32_t nsec;
446 
448  private: static Time wallTime;
449 
451  private: static std::string wallTimeISO;
452 
455  private: inline void Correct()
456  {
457  // In the case sec and nsec have different signs, normalize
458  if (this->sec > 0 && this->nsec < 0)
459  {
460  int32_t n = abs(this->nsec / this->nsInSec) + 1;
461  this->sec -= n;
462  this->nsec += n * this->nsInSec;
463  }
464  if (this->sec < 0 && this->nsec > 0)
465  {
466  int32_t n = abs(this->nsec / this->nsInSec) + 1;
467  this->sec += n;
468  this->nsec -= n * this->nsInSec;
469  }
470 
471  // Make any corrections
472  this->sec += this->nsec / this->nsInSec;
473  this->nsec = this->nsec % this->nsInSec;
474  }
475 
476  private: static struct timespec clockResolution;
477 
479  private: static const int32_t nsInSec;
480 
483  private: static const int32_t nsInMs;
484  };
486  }
487 }
488 #endif
int32_t nsec
Nanoseconds.
Definition: Time.hh:445
static const Time Hour
A static time variable set to an hour: common::Time(3600, 0).
Definition: Time.hh:48
static double MilToNano(double _ms)
Convert milliseconds to nanoseconds.
Definition: Time.hh:408
friend std::ostream & operator<<(std::ostream &_out, const gazebo::common::Time &_time)
Stream insertion operator.
Definition: Time.hh:421
#define GZ_COMMON_VISIBLE
Definition: system.hh:91
FormatOption
Definition: Time.hh:52
static const Time Second
A static time variable set to a second: common::Time(1, 0).
Definition: Time.hh:45
static double MicToNano(double _ms)
Convert microseconds to nanoseconds.
Definition: Time.hh:414
int32_t sec
Seconds.
Definition: Time.hh:442
static const Time Zero
A static zero time variable set to common::Time(0, 0).
Definition: Time.hh:42
GAZEBO_VISIBLE void Set(common::Image &_img, const msgs::Image &_msg)
Convert a msgs::Image to a common::Image.
friend std::istream & operator>>(std::istream &_in, gazebo::common::Time &_time)
Stream extraction operator.
Definition: Time.hh:432
static double SecToNano(double _sec)
Convert seconds to nanoseconds.
Definition: Time.hh:402
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:39