Duration.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <cstdint>
5 #include <ostream>
6 #include <string>
7 
8 
9 namespace armarx::core::time
10 {
11 
12  /**
13  * @brief Represents a duration.
14  *
15  * API and implementation to match IceUtil::Time (where applicable).
16  */
17  class Duration
18  {
19  // Public API.
20  public:
21  /**
22  * @brief Constructs a zero-duration.
23  */
24  Duration();
25 
26  /**
27  * @brief Constructs a duration in microseconds.
28  * @param microSeconds Amount of microseconds.
29  * @return Duration instance.
30  */
31  static Duration MicroSeconds(std::int64_t microSeconds);
32 
33  /**
34  * @brief Constructs a duration in microseconds.
35  * @param microSeconds Amount of microseconds.
36  * @return Duration instance.
37  */
38  static Duration MicroSecondsDouble(double microSeconds);
39 
40  /**
41  * @brief Constructs a duration in milliseconds.
42  * @param milliSeconds Amount of milliseconds.
43  * @return Duration instance.
44  */
45  static Duration MilliSeconds(std::int64_t milliSeconds);
46 
47  /**
48  * @brief Constructs a duration in milliseconds.
49  * @param milliSeconds Amount of milliseconds.
50  * @return Duration instance.
51  */
52  static Duration MilliSecondsDouble(double milliSeconds);
53 
54  /**
55  * @brief Constructs a duration in seconds.
56  * @param seconds Amount of seconds.
57  * @return Duration instance.
58  */
59  static Duration Seconds(std::int64_t seconds);
60 
61  /**
62  * @brief Constructs a duration in seconds.
63  * @param seconds Amount of seconds.
64  * @return Duration instance.
65  */
66  static Duration SecondsDouble(double seconds);
67 
68  /**
69  * @brief Constructs a duration in minutes.
70  * @param minutes Amount of minutes.
71  * @return Duration instance.
72  */
73  static Duration Minutes(std::int64_t minutes);
74 
75  /**
76  * @brief Constructs a duration in minutes.
77  * @param minutes Amount of minutes.
78  * @return Duration instance.
79  */
80  static Duration MinutesDouble(double minutes);
81 
82  /**
83  * @brief Constructs a duration in hours.
84  * @param hours Amount of hours.
85  * @return Duration instance.
86  */
87  static Duration Hours(std::int64_t hours);
88 
89  /**
90  * @brief Constructs a duration in hours.
91  * @param hours Amount of hours.
92  * @return Duration instance.
93  */
94  static Duration HoursDouble(double hours);
95 
96  /**
97  * @brief Constructs a duration in days.
98  * @param days Amount of days.
99  * @return Duration instance.
100  */
101  static Duration Days(std::int64_t days);
102 
103  /**
104  * @brief Constructs a duration in days.
105  * @param days Amount of days.
106  * @return Duration instance.
107  */
108  static Duration DaysDouble(double days);
109 
110  /**
111  * @brief Returns the amount of microseconds.
112  * @return Amount of microseconds.
113  */
114  std::int64_t toMicroSeconds() const;
115 
116  /**
117  * @brief Returns the amount of microseconds.
118  * @return Amount of microseconds.
119  */
120  double toMicroSecondsDouble() const;
121 
122  /**
123  * @brief Returns the amount of milliseconds.
124  * @return Amount of milliseconds.
125  */
126  std::int64_t toMilliSeconds() const;
127 
128  /**
129  * @brief Returns the amount of milliseconds.
130  * @return Amount of milliseconds.
131  */
132  double toMilliSecondsDouble() const;
133 
134  /**
135  * @brief Returns the amount of seconds.
136  * @return Amount of seconds.
137  */
138  std::int64_t toSeconds() const;
139 
140  /**
141  * @brief Returns the amount of seconds.
142  * @return Amount of seconds.
143  */
144  double toSecondsDouble() const;
145 
146  /**
147  * @brief Returns the amount of minutes.
148  * @return Amount of minutes.
149  */
150  std::int64_t toMinutes() const;
151 
152  /**
153  * @brief Returns the amount of minutes.
154  * @return Amount of minutes.
155  */
156  double toMinutesDouble() const;
157 
158  /**
159  * @brief Returns the amount of hours.
160  * @return Amount of hours.
161  */
162  std::int64_t toHours() const;
163 
164  /**
165  * @brief Returns the amount of hours.
166  * @return Amount of hours.
167  */
168  double toHoursDouble() const;
169 
170  /**
171  * @brief Returns the amount of days.
172  * @return Amount of days.
173  */
174  std::int64_t toDays() const;
175 
176  /**
177  * @brief Returns the amount of days.
178  * @return Amount of days.
179  */
180  double toDaysDouble() const;
181 
182  /**
183  * @brief Tests whether the duration is positive (value in µs > 0).
184  * @return True if duration is positive, else otherwise.
185  */
186  bool isPositive() const;
187 
188  bool isZero() const;
189 
190  /**
191  * @brief String representation of the current duration in minimal/default format.
192  *
193  * The minimal representation is a float representation with max. 3 decimals. The unit will
194  * be determined by the highest unit whose value is non-zero. For example, 3 seconds and 500
195  * milliseconds => "3.5s".
196  *
197  * @return Formatted duration.
198  */
199  std::string toDurationString() const;
200 
201  /**
202  * @brief String representation of the current duration according to given format string.
203  *
204  * The format is according to https://en.cppreference.com/w/cpp/chrono/c/strftime. For
205  * milli seconds and micro seconds, special specifiers "%%msec" and "%%usec" were added
206  * respectively.
207  *
208  * Example format string for "10m 10.987s": "%Mm %S.%%msecs".
209  *
210  * @param format Format string.
211  * @return Formatted duration.
212  */
213  std::string toDurationString(const std::string& format) const;
214 
215  // Operators.
216  public:
217  Duration operator+(const Duration& rhs) const;
218 
219  Duration& operator+=(const Duration& rhs);
220 
221  Duration operator-() const;
222 
223  Duration operator-(const Duration& rhs) const;
224 
225  Duration& operator-=(const Duration& rhs);
226 
227  Duration operator*(double rhs) const;
228 
229  Duration operator*(int rhs) const;
230 
231  Duration operator*(std::int64_t rhs) const;
232 
233  Duration& operator*=(double rhs);
234 
235  Duration& operator*=(int rhs);
236 
237  Duration& operator*=(std::int64_t rhs);
238 
239  double operator/(const Duration& rhs) const;
240 
241  Duration operator/(double rhs) const;
242 
243  Duration operator/(int rhs) const;
244 
245  Duration operator/(std::int64_t rhs) const;
246 
247  Duration& operator/=(double rhs);
248 
249  Duration& operator/=(int rhs);
250 
251  Duration& operator/=(std::int64_t rhs);
252 
253  bool operator<(const Duration& rhs) const;
254 
255  bool operator<=(const Duration& rhs) const;
256 
257  bool operator==(const Duration& rhs) const;
258 
259  bool operator!=(const Duration& rhs) const;
260 
261  bool operator>=(const Duration& rhs) const;
262 
263  bool operator>(const Duration& rhs) const;
264 
265  protected:
266  Duration(std::int64_t microSeconds);
267 
268  /**
269  * @brief Current duration in microseconds.
270  */
271  std::int64_t _microSeconds;
272  };
273 
274 
275  std::ostream& operator<<(std::ostream& out, const Duration& rhs);
276 
277 } // namespace armarx::core::time
278 
279 
280 namespace armarx
281 {
282  using core::time::Duration;
283 } // namespace armarx
armarx::core::time::Duration::toMinutes
std::int64_t toMinutes() const
Returns the amount of minutes.
Definition: Duration.cpp:125
armarx::core::time::Duration::operator-=
Duration & operator-=(const Duration &rhs)
Definition: Duration.cpp:304
armarx::core::time::Duration::operator*=
Duration & operator*=(double rhs)
Definition: Duration.cpp:335
armarx::core::time::Duration::toDays
std::int64_t toDays() const
Returns the amount of days.
Definition: Duration.cpp:181
armarx::core::time::Duration::toSecondsDouble
double toSecondsDouble() const
Returns the amount of seconds.
Definition: Duration.cpp:104
armarx::core::time::Duration::operator+=
Duration & operator+=(const Duration &rhs)
Definition: Duration.cpp:282
armarx::core::time::Duration::operator==
bool operator==(const Duration &rhs) const
Definition: Duration.cpp:423
armarx::core::time
Definition: Clock.cpp:13
armarx::core::time::Duration::toMilliSecondsDouble
double toMilliSecondsDouble() const
Returns the amount of milliseconds.
Definition: Duration.cpp:76
armarx::core::time::Duration::toHoursDouble
double toHoursDouble() const
Returns the amount of hours.
Definition: Duration.cpp:160
armarx::core::time::operator<<
std::ostream & operator<<(std::ostream &out, const DateTime &rhs)
Definition: DateTime.cpp:226
armarx::core::time::Duration::isPositive
bool isPositive() const
Tests whether the duration is positive (value in µs > 0).
Definition: Duration.cpp:195
armarx::core::time::Duration::MicroSecondsDouble
static Duration MicroSecondsDouble(double microSeconds)
Constructs a duration in microseconds.
Definition: Duration.cpp:34
armarx::core::time::Duration::operator+
Duration operator+(const Duration &rhs) const
Definition: Duration.cpp:275
armarx::core::time::Duration::Hours
static Duration Hours(std::int64_t hours)
Constructs a duration in hours.
Definition: Duration.cpp:139
armarx::core::time::Duration::DaysDouble
static Duration DaysDouble(double days)
Constructs a duration in days.
Definition: Duration.cpp:174
armarx::core::time::Duration::Seconds
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:83
armarx::core::time::Duration::operator>=
bool operator>=(const Duration &rhs) const
Definition: Duration.cpp:437
armarx::core::time::Duration::HoursDouble
static Duration HoursDouble(double hours)
Constructs a duration in hours.
Definition: Duration.cpp:146
armarx::core::time::Duration::operator<=
bool operator<=(const Duration &rhs) const
Definition: Duration.cpp:416
armarx::core::time::Duration::SecondsDouble
static Duration SecondsDouble(double seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:90
armarx::core::time::Duration::MilliSecondsDouble
static Duration MilliSecondsDouble(double milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:62
armarx::core::time::Duration::Minutes
static Duration Minutes(std::int64_t minutes)
Constructs a duration in minutes.
Definition: Duration.cpp:111
armarx::core::time::Duration::toMinutesDouble
double toMinutesDouble() const
Returns the amount of minutes.
Definition: Duration.cpp:132
armarx::core::time::Duration::toHours
std::int64_t toHours() const
Returns the amount of hours.
Definition: Duration.cpp:153
armarx::core::time::Duration::operator<
bool operator<(const Duration &rhs) const
Definition: Duration.cpp:409
armarx::core::time::Duration::operator/
double operator/(const Duration &rhs) const
Definition: Duration.cpp:357
armarx::core::time::Duration::_microSeconds
std::int64_t _microSeconds
Current duration in microseconds.
Definition: Duration.h:271
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:17
armarx::core::time::Duration::toDurationString
std::string toDurationString() const
String representation of the current duration in minimal/default format.
Definition: Duration.cpp:209
armarx::core::time::Duration::MinutesDouble
static Duration MinutesDouble(double minutes)
Constructs a duration in minutes.
Definition: Duration.cpp:118
armarx::core::time::Duration::toMilliSeconds
std::int64_t toMilliSeconds() const
Returns the amount of milliseconds.
Definition: Duration.cpp:69
armarx::core::time::Duration::toMicroSeconds
std::int64_t toMicroSeconds() const
Returns the amount of microseconds.
Definition: Duration.cpp:41
armarx::core::time::Duration::operator!=
bool operator!=(const Duration &rhs) const
Definition: Duration.cpp:430
armarx::core::time::Duration::operator/=
Duration & operator/=(double rhs)
Definition: Duration.cpp:387
armarx::core::time::Duration::Days
static Duration Days(std::int64_t days)
Constructs a duration in days.
Definition: Duration.cpp:167
armarx::core::time::Duration::MicroSeconds
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition: Duration.cpp:27
armarx::core::time::Duration::toDaysDouble
double toDaysDouble() const
Returns the amount of days.
Definition: Duration.cpp:188
armarx::core::time::Duration::operator>
bool operator>(const Duration &rhs) const
Definition: Duration.cpp:444
armarx::core::time::Duration::operator*
Duration operator*(double rhs) const
Definition: Duration.cpp:312
armarx::core::time::Duration::toSeconds
std::int64_t toSeconds() const
Returns the amount of seconds.
Definition: Duration.cpp:97
armarx::armem::Duration
armarx::core::time::Duration Duration
Definition: forward_declarations.h:14
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:55
armarx::core::time::Duration::operator-
Duration operator-() const
Definition: Duration.cpp:290
armarx::core::time::Duration::isZero
bool isZero() const
Definition: Duration.cpp:202
armarx::core::time::Duration::toMicroSecondsDouble
double toMicroSecondsDouble() const
Returns the amount of microseconds.
Definition: Duration.cpp:48
armarx::core::time::Duration::Duration
Duration()
Constructs a zero-duration.
Definition: Duration.cpp:14