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