Duration.cpp
Go to the documentation of this file.
1 #include "Duration.h"
2 
3 #include <iomanip>
4 #include <sstream>
5 
6 #include <SimoxUtility/algorithm/string/string_tools.h>
7 
9 
10 
11 namespace armarx::core::time
12 {
13 
14  Duration::Duration() : _microSeconds{0}
15  {
16  ;
17  }
18 
19 
20  Duration::Duration(std::int64_t microSeconds) : _microSeconds{microSeconds}
21  {
22  ;
23  }
24 
25 
26  Duration
27  Duration::MicroSeconds(const std::int64_t microSeconds)
28  {
29  return Duration(microSeconds);
30  }
31 
32 
33  Duration
34  Duration::MicroSecondsDouble(const double microSeconds)
35  {
36  return Duration(microSeconds);
37  }
38 
39 
40  std::int64_t
42  {
43  return _microSeconds;
44  }
45 
46 
47  double
49  {
50  return static_cast<double>(_microSeconds);
51  }
52 
53 
54  Duration
55  Duration::MilliSeconds(const std::int64_t milliSeconds)
56  {
57  return Duration::MicroSeconds(1'000 * milliSeconds);
58  }
59 
60 
61  Duration
62  Duration::MilliSecondsDouble(const double milliSeconds)
63  {
64  return Duration::MicroSeconds(1'000 * milliSeconds);
65  }
66 
67 
68  std::int64_t
70  {
71  return static_cast<std::int64_t>(toMilliSecondsDouble() + 0.5);
72  }
73 
74 
75  double
77  {
78  return static_cast<double>(_microSeconds) / 1'000;
79  }
80 
81 
82  Duration
83  Duration::Seconds(const std::int64_t seconds)
84  {
85  return Duration::MilliSeconds(1'000 * seconds);
86  }
87 
88 
89  Duration
90  Duration::SecondsDouble(const double seconds)
91  {
92  return Duration::MilliSeconds(1'000 * seconds);
93  }
94 
95 
96  std::int64_t
97  Duration::toSeconds() const
98  {
99  return static_cast<std::int64_t>(toSecondsDouble() + 0.5);
100  }
101 
102 
103  double
104  Duration::toSecondsDouble() const
105  {
106  return toMilliSecondsDouble() / 1'000;
107  }
108 
109 
110  Duration
111  Duration::Minutes(const std::int64_t minutes)
112  {
113  return Duration::Seconds(60 * minutes);
114  }
115 
116 
117  Duration
118  Duration::MinutesDouble(const double minutes)
119  {
120  return Duration::Seconds(60 * minutes);
121  }
122 
123 
124  std::int64_t
126  {
127  return static_cast<std::int64_t>(toMinutesDouble() + 0.5);
128  }
129 
130 
131  double
133  {
134  return toSecondsDouble() / 60;
135  }
136 
137 
138  Duration
139  Duration::Hours(const std::int64_t hours)
140  {
141  return Duration::Minutes(60 * hours);
142  }
143 
144 
145  Duration
146  Duration::HoursDouble(const double hours)
147  {
148  return Duration::Minutes(60 * hours);
149  }
150 
151 
152  std::int64_t
154  {
155  return static_cast<std::int64_t>(toHoursDouble() + 0.5);
156  }
157 
158 
159  double
161  {
162  return toMinutesDouble() / 60;
163  }
164 
165 
166  Duration
167  Duration::Days(const std::int64_t days)
168  {
169  return Duration::Hours(24 * days);
170  }
171 
172 
173  Duration
174  Duration::DaysDouble(const double days)
175  {
176  return Duration::Hours(24 * days);
177  }
178 
179 
180  std::int64_t
182  {
183  return static_cast<std::int64_t>(toDaysDouble() + 0.5);
184  }
185 
186 
187  double
189  {
190  return toHoursDouble() / 24;
191  }
192 
193 
194  bool
196  {
197  return _microSeconds > 0;
198  }
199 
200 
201  bool
203  {
204  return _microSeconds == 0;
205  }
206 
207 
208  std::string
210  {
211  double time_count = toMicroSecondsDouble();
212  std::string unit = "µs";
213 
214  if (time_count >= 1000)
215  {
216  time_count /= 1000;
217  unit = "ms";
218 
219  if (time_count >= 1000)
220  {
221  time_count /= 1000;
222  unit = "s";
223 
224  if (time_count >= 60)
225  {
226  time_count /= 60;
227  unit = "min";
228 
229  if (time_count >= 60)
230  {
231  time_count /= 60;
232  unit = "h";
233 
234  if (time_count >= 24)
235  {
236  time_count /= 24;
237  unit = "d";
238  }
239  }
240  }
241  }
242  }
243 
244  std::stringstream ss;
245  ss << std::setprecision(3) << time_count;
246  return ss.str() + unit;
247  }
248 
249 
250  std::string
251  Duration::toDurationString(const std::string& format) const
252  {
253  const std::int64_t usec = _microSeconds % 1'000'000;
254  const std::int64_t msec =
255  (static_cast<double>(usec) / 1'000) + 0.5; // Rounding towards next integer.
256  const time_t time = static_cast<time_t>(static_cast<double>(_microSeconds) / 1'000'000);
257 
258  struct tm tr;
259  localtime_r(&time, &tr);
260 
261  char buf[32];
262  if (strftime(buf, sizeof(buf), format.c_str(), &tr) == 0)
263  {
264  return std::string();
265  }
266 
267  std::string postformat(buf);
268  postformat = simox::alg::replace_all(postformat, "%msec", std::to_string(msec));
269  postformat = simox::alg::replace_all(postformat, "%usec", std::to_string(usec));
270  return postformat;
271  }
272 
273 
274  Duration
275  Duration::operator+(const Duration& rhs) const
276  {
277  return Duration(_microSeconds + rhs._microSeconds);
278  }
279 
280 
281  Duration&
282  Duration::operator+=(const Duration& rhs)
283  {
284  _microSeconds += rhs._microSeconds;
285  return *this;
286  }
287 
288 
289  Duration
290  Duration::operator-() const
291  {
292  return Duration(-_microSeconds);
293  }
294 
295 
296  Duration
297  Duration::operator-(const Duration& rhs) const
298  {
299  return Duration(_microSeconds - rhs._microSeconds);
300  }
301 
302 
303  Duration&
304  Duration::operator-=(const Duration& rhs)
305  {
306  _microSeconds -= rhs._microSeconds;
307  return *this;
308  }
309 
310 
311  Duration
312  Duration::operator*(const double rhs) const
313  {
314  Duration t;
315  t._microSeconds = static_cast<std::int64_t>(static_cast<double>(_microSeconds) * rhs + 0.5);
316  return t;
317  }
318 
319 
320  Duration
321  Duration::operator*(const int rhs) const
322  {
323  return *this * static_cast<double>(rhs);
324  }
325 
326 
327  Duration
328  Duration::operator*(const std::int64_t rhs) const
329  {
330  return *this * static_cast<double>(rhs);
331  }
332 
333 
334  Duration&
335  Duration::operator*=(const double rhs)
336  {
337  _microSeconds = static_cast<std::int64_t>(static_cast<double>(_microSeconds) * rhs);
338  return *this;
339  }
340 
341 
342  Duration&
343  Duration::operator*=(const int rhs)
344  {
345  return *this *= static_cast<double>(rhs);
346  }
347 
348 
349  Duration&
350  Duration::operator*=(const std::int64_t rhs)
351  {
352  return *this *= static_cast<double>(rhs);
353  }
354 
355 
356  double
357  Duration::operator/(const Duration& rhs) const
358  {
359  return static_cast<double>(_microSeconds) / static_cast<double>(rhs._microSeconds);
360  }
361 
362 
363  Duration
364  Duration::operator/(const double rhs) const
365  {
366  Duration t;
367  t._microSeconds = static_cast<std::int64_t>(static_cast<double>(_microSeconds) / rhs);
368  return t;
369  }
370 
371 
372  Duration
373  Duration::operator/(const int rhs) const
374  {
375  return *this / static_cast<double>(rhs);
376  }
377 
378 
379  Duration
380  Duration::operator/(const std::int64_t rhs) const
381  {
382  return *this / static_cast<double>(rhs);
383  }
384 
385 
386  Duration&
387  Duration::operator/=(const double rhs)
388  {
389  _microSeconds = static_cast<std::int64_t>(static_cast<double>(_microSeconds) / rhs);
390  return *this;
391  }
392 
393 
394  Duration&
395  Duration::operator/=(const int rhs)
396  {
397  return *this /= static_cast<double>(rhs);
398  }
399 
400 
401  Duration&
402  Duration::operator/=(const std::int64_t rhs)
403  {
404  return *this /= static_cast<double>(rhs);
405  }
406 
407 
408  bool
409  Duration::operator<(const Duration& rhs) const
410  {
411  return _microSeconds < rhs._microSeconds;
412  }
413 
414 
415  bool
416  Duration::operator<=(const Duration& rhs) const
417  {
418  return _microSeconds <= rhs._microSeconds;
419  }
420 
421 
422  bool
423  Duration::operator==(const Duration& rhs) const
424  {
425  return _microSeconds == rhs._microSeconds;
426  }
427 
428 
429  bool
430  Duration::operator!=(const Duration& rhs) const
431  {
432  return _microSeconds != rhs._microSeconds;
433  }
434 
435 
436  bool
437  Duration::operator>=(const Duration& rhs) const
438  {
439  return _microSeconds >= rhs._microSeconds;
440  }
441 
442 
443  bool
444  Duration::operator>(const Duration& rhs) const
445  {
446  return _microSeconds > rhs._microSeconds;
447  }
448 
449 
450  std::ostream&
451  operator<<(std::ostream& out, const Duration& rhs)
452  {
453  out << rhs.toDurationString();
454  return out;
455  }
456 
457 } // namespace armarx::core::time
armarx::core::time::Duration::toMinutes
std::int64_t toMinutes() const
Returns the amount of minutes.
Definition: Duration.cpp:125
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
Duration.h
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::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
Clock.h
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::HoursDouble
static Duration HoursDouble(double hours)
Constructs a duration in hours.
Definition: Duration.cpp:146
armarx::core::time::Duration::SecondsDouble
static Duration SecondsDouble(double seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:90
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::_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::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::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:55
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