Gazebo Common

API Reference

4.7.0
gz/common/Time.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 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 #ifndef IGNITION_COMMON_TIME_HH_
18 #define IGNITION_COMMON_TIME_HH_
19 
20 #include <string>
21 #include <iostream>
22 
23 #include <gz/common/config.hh>
24 #include <gz/common/Export.hh>
25 #include <gz/common/Util.hh>
26 
27 namespace ignition
28 {
29  namespace common
30  {
34  class IGNITION_COMMON_VISIBLE Time
35  {
37  public: static const Time Zero;
38 
41  public: enum class FormatOption
42  {
44  DAYS = 0,
46  HOURS = 1,
48  MINUTES = 2,
50  SECONDS = 3,
52  MILLISECONDS = 4
53  };
54 
56  public: IGN_DEPRECATED(4) Time();
57 
60  public: IGN_DEPRECATED(4) Time(const Time &_time);
61 
64  public: explicit IGN_DEPRECATED(4) Time(const struct timespec &_tv);
65 
69  public: IGN_DEPRECATED(4) Time(int32_t _sec, int32_t _nsec);
70 
73  public: explicit IGN_DEPRECATED(4) Time(double _time);
74 
76  public: virtual ~Time();
77 
80  public: static const IGN_DEPRECATED(4) Time &SystemTime();
81 
85  public: void Set(int32_t _sec, int32_t _nsec);
86 
89  public: void Set(double _seconds);
90 
93  public: double Double() const;
94 
97  public: float Float() const;
98 
107  public: static Time IGN_DEPRECATED(4) Sleep(const common::Time &_time);
108 
114  public: std::string FormattedString(
115  FormatOption _start = FormatOption::DAYS,
116  FormatOption _end = FormatOption::MILLISECONDS) const;
117 
121  public: Time &operator=(const Time &_time);
122 
126  public: Time operator+(const Time &_time) const;
127 
131  public: const Time &operator +=(const Time &_time);
132 
136  public: Time operator -(const struct timespec &_tv) const;
137 
141  public: const Time &operator -=(const struct timespec &_tv);
142 
146  public: Time operator -(const Time &_time) const;
147 
151  public: const Time &operator -=(const Time &_time);
152 
156  public: Time operator *(const struct timespec &_tv) const;
157 
161  public: const Time &operator *=(const struct timespec &_tv);
162 
166  public: Time operator *(const Time &_time) const;
167 
171  public: const Time &operator *=(const Time &_time);
172 
176  public: const Time &operator /=(const struct timespec &_tv);
177 
181  public: Time operator /(const Time &_time) const;
182 
186  public: const Time &operator /=(const Time &time);
187 
191  public: bool operator==(const Time &_time) const;
192 
196  public: bool operator==(double _time) const;
197 
201  public: bool operator!=(const Time &_time) const;
202 
206  public: bool operator!=(double _time) const;
207 
211  public: bool operator<(const Time &_time) const;
212 
216  public: bool operator<(double _time) const;
217 
221  public: bool operator<=(const Time &_time) const;
222 
226  public: bool operator<=(double _time) const;
227 
231  public: bool operator>(const struct timespec &_tv) const;
232 
236  public: bool operator>(const Time &_time) const;
237 
241  public: bool operator>(double _time) const;
242 
246  public: bool operator>=(const Time &_time) const;
247 
251  public: bool operator>=(const struct timespec &_tv) const;
252 
256  public: bool operator>=(double _time) const;
257 
262  public: friend std::ostream &operator<<(std::ostream &_out,
263  const ignition::common::Time &_time)
264  {
265  _out << _time.sec << " " << _time.nsec;
266  return _out;
267  }
268 
273  public: friend std::istream &operator>>(std::istream &_in,
274  ignition::common::Time &_time)
275  {
276  // Skip white spaces
277  _in.setf(std::ios_base::skipws);
278  _in >> _time.sec >> _time.nsec;
279  return _in;
280  }
281 
283  public: int32_t sec;
284 
286  public: int32_t nsec;
287 
289  private: static Time wallTime;
290 
293  private: inline void Correct()
294  {
295  // In the case sec and nsec have different signs, normalize
296  if (this->sec > 0 && this->nsec < 0)
297  {
298  int32_t n = abs(this->nsec / this->nsInSec) + 1;
299  this->sec -= n;
300  this->nsec += n * this->nsInSec;
301  }
302  if (this->sec < 0 && this->nsec > 0)
303  {
304  int32_t n = abs(this->nsec / this->nsInSec) + 1;
305  this->sec += n;
306  this->nsec -= n * this->nsInSec;
307  }
308 
309  // Make any corrections
310  this->sec += this->nsec / this->nsInSec;
311  this->nsec = this->nsec % this->nsInSec;
312  }
313 
315  private: static const int32_t nsInSec;
316 
319  private: static const int32_t nsInMs;
320 
321  private: static struct timespec clockResolution;
322  };
323  }
324 }
325 #endif
static const Time & SystemTime()
Get the wall time.
static Time Sleep(const common::Time &_time)
Sleep for the specified time.
bool operator<=(const Time &_time) const
Less than or equal to operator.
Forward declarations for the common classes.
STL class.
Time operator-(const struct timespec &_tv) const
Subtraction operator.
A Time class, can be used to hold wall- or sim-time. stored as sec and nano-sec.
Definition: gz/common/Time.hh:34
bool operator==(const Time &_time) const
Equal to operator.
virtual ~Time()
Destructor.
bool operator>=(const Time &_time) const
Greater than or equal operator.
void Set(int32_t _sec, int32_t _nsec)
Set to sec and nsec.
Time operator*(const struct timespec &_tv) const
Multiplication operator.
bool operator<(const Time &_time) const
Less than operator.
int32_t nsec
Nanoseconds.
Definition: gz/common/Time.hh:286
const Time & operator*=(const struct timespec &_tv)
Multiplication assignment operator.
Time & operator=(const Time &_time)
Assignment operator.
STL class.
std::string FormattedString(FormatOption _start=FormatOption::DAYS, FormatOption _end=FormatOption::MILLISECONDS) const
Get the time as a string formatted as "DD hh:mm:ss.mmm", with the option to choose the start/end.
friend std::istream & operator>>(std::istream &_in, ignition::common::Time &_time)
Stream extraction operator.
Definition: gz/common/Time.hh:273
float Float() const
Get the time as a float.
T setf(T... args)
Time()
Constructors.
friend std::ostream & operator<<(std::ostream &_out, const ignition::common::Time &_time)
Stream insertion operator.
Definition: gz/common/Time.hh:262
bool operator!=(const Time &_time) const
Equal to operator.
FormatOption
Definition: gz/common/Time.hh:41
const Time & operator+=(const Time &_time)
Addition assignemtn operator.
STL namespace.
const Time & operator/=(const struct timespec &_tv)
Division assignment operator.
Time operator/(const Time &_time) const
Division operator.
Time operator+(const Time &_time) const
Addition operators.
STL class.
bool operator>(const struct timespec &_tv) const
Greater than operator.
const Time & operator-=(const struct timespec &_tv)
Subtraction assignment operator.
double Double() const
Get the time as a double.
static const Time Zero
A static zero time variable set to common::Time(0, 0).
Definition: gz/common/Time.hh:37
int32_t sec
Seconds.
Definition: gz/common/Time.hh:283