Gazebo Common

API Reference

3.17.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 GZ_COMMON_TIME_HH_
18 #define GZ_COMMON_TIME_HH_
19 
20 #include <string>
21 #include <iostream>
22 
23 #include <gz/common/Export.hh>
24 #include <gz/common/Util.hh>
25 
26 namespace ignition
27 {
28  namespace common
29  {
33  class IGNITION_COMMON_VISIBLE Time
34  {
36  public: static const Time Zero;
37 
40  public: enum class FormatOption
41  {
43  DAYS = 0,
45  HOURS = 1,
47  MINUTES = 2,
49  SECONDS = 3,
51  MILLISECONDS = 4
52  };
53 
55  public: Time();
56 
59  public: Time(const Time &_time);
60 
63  public: explicit Time(const struct timespec &_tv);
64 
68  public: Time(int32_t _sec, int32_t _nsec);
69 
72  public: explicit Time(double _time);
73 
75  public: virtual ~Time();
76 
79  public: static const Time &SystemTime();
80 
84  public: void Set(int32_t _sec, int32_t _nsec);
85 
88  public: void Set(double _seconds);
89 
92  public: double Double() const;
93 
96  public: float Float() const;
97 
106  public: static Time Sleep(const common::Time &_time);
107 
113  public: std::string FormattedString(
114  FormatOption _start = FormatOption::DAYS,
115  FormatOption _end = FormatOption::MILLISECONDS) const;
116 
120  public: Time &operator=(const Time &_time);
121 
125  public: Time operator+(const Time &_time) const;
126 
130  public: const Time &operator +=(const Time &_time);
131 
135  public: Time operator -(const struct timespec &_tv) const;
136 
140  public: const Time &operator -=(const struct timespec &_tv);
141 
145  public: Time operator -(const Time &_time) const;
146 
150  public: const Time &operator -=(const Time &_time);
151 
155  public: Time operator *(const struct timespec &_tv) const;
156 
160  public: const Time &operator *=(const struct timespec &_tv);
161 
165  public: Time operator *(const Time &_time) const;
166 
170  public: const Time &operator *=(const Time &_time);
171 
175  public: const Time &operator /=(const struct timespec &_tv);
176 
180  public: Time operator /(const Time &_time) const;
181 
185  public: const Time &operator /=(const Time &time);
186 
190  public: bool operator==(const Time &_time) const;
191 
195  public: bool operator==(double _time) const;
196 
200  public: bool operator!=(const Time &_time) const;
201 
205  public: bool operator!=(double _time) const;
206 
210  public: bool operator<(const Time &_time) const;
211 
215  public: bool operator<(double _time) const;
216 
220  public: bool operator<=(const Time &_time) const;
221 
225  public: bool operator<=(double _time) const;
226 
230  public: bool operator>(const struct timespec &_tv) const;
231 
235  public: bool operator>(const Time &_time) const;
236 
240  public: bool operator>(double _time) const;
241 
245  public: bool operator>=(const Time &_time) const;
246 
250  public: bool operator>=(const struct timespec &_tv) const;
251 
255  public: bool operator>=(double _time) const;
256 
261  public: friend std::ostream &operator<<(std::ostream &_out,
262  const ignition::common::Time &_time)
263  {
264  _out << _time.sec << " " << _time.nsec;
265  return _out;
266  }
267 
272  public: friend std::istream &operator>>(std::istream &_in,
273  ignition::common::Time &_time)
274  {
275  // Skip white spaces
276  _in.setf(std::ios_base::skipws);
277  _in >> _time.sec >> _time.nsec;
278  return _in;
279  }
280 
282  public: int32_t sec;
283 
285  public: int32_t nsec;
286 
288  private: static Time wallTime;
289 
292  private: inline void Correct()
293  {
294  // In the case sec and nsec have different signs, normalize
295  if (this->sec > 0 && this->nsec < 0)
296  {
297  int32_t n = abs(this->nsec / this->nsInSec) + 1;
298  this->sec -= n;
299  this->nsec += n * this->nsInSec;
300  }
301  if (this->sec < 0 && this->nsec > 0)
302  {
303  int32_t n = abs(this->nsec / this->nsInSec) + 1;
304  this->sec += n;
305  this->nsec -= n * this->nsInSec;
306  }
307 
308  // Make any corrections
309  this->sec += this->nsec / this->nsInSec;
310  this->nsec = this->nsec % this->nsInSec;
311  }
312 
314  private: static const int32_t nsInSec;
315 
318  private: static const int32_t nsInMs;
319 
320  private: static struct timespec clockResolution;
321  };
322  }
323 }
324 #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:33
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:285
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:272
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:261
bool operator!=(const Time &_time) const
Equal to operator.
FormatOption
Definition: gz/common/Time.hh:40
const Time & operator+=(const Time &_time)
Addition assignemtn operator.
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:36
int32_t sec
Seconds.
Definition: gz/common/Time.hh:282