Go to the documentation of this file.
6 #include <SimoxUtility/algorithm/string/string_tools.h>
62 Duration::MilliSecondsDouble(const double milliSeconds)
64 return Duration::MicroSeconds(1'000 * milliSeconds);
83 Duration::Seconds(const std::int64_t seconds)
85 return Duration::MilliSeconds(1'000 * seconds);
97 Duration::toSeconds() const
99 return static_cast<std::int64_t>(toSecondsDouble() + 0.5);
104 Duration::toSecondsDouble() const
106 return toMilliSecondsDouble() / 1'000;
212 std::string unit =
"µs";
214 if (time_count >= 1000)
219 if (time_count >= 1000)
224 if (time_count >= 60)
229 if (time_count >= 60)
234 if (time_count >= 24)
244 std::stringstream ss;
245 ss << std::setprecision(3) << time_count;
246 return ss.str() + unit;
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);
259 localtime_r(&time, &tr);
262 if (strftime(buf, sizeof(buf), format.c_str(), &tr) == 0)
264 return std::string();
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));
275 Duration::operator+(const Duration& rhs) const
277 return Duration(_microSeconds + rhs._microSeconds);
282 Duration::operator+=(const Duration& rhs)
284 _microSeconds += rhs._microSeconds;
290 Duration::operator-() const
292 return Duration(-_microSeconds);
297 Duration::operator-(const Duration& rhs) const
299 return Duration(_microSeconds - rhs._microSeconds);
304 Duration::operator-=(const Duration& rhs)
306 _microSeconds -= rhs._microSeconds;
312 Duration::operator*(const double rhs) const
315 t._microSeconds = static_cast<std::int64_t>(static_cast<double>(_microSeconds) * rhs + 0.5);
321 Duration::operator*(const int rhs) const
323 return *this * static_cast<double>(rhs);
328 Duration::operator*(const std::int64_t rhs) const
330 return *this * static_cast<double>(rhs);
335 Duration::operator*=(const double rhs)
337 _microSeconds = static_cast<std::int64_t>(static_cast<double>(_microSeconds) * rhs);
343 Duration::operator*=(const int rhs)
345 return *this *= static_cast<double>(rhs);
350 Duration::operator*=(const std::int64_t rhs)
352 return *this *= static_cast<double>(rhs);
357 Duration::operator/(const Duration& rhs) const
359 return static_cast<double>(_microSeconds) / static_cast<double>(rhs._microSeconds);
364 Duration::operator/(const double rhs) const
367 t._microSeconds = static_cast<std::int64_t>(static_cast<double>(_microSeconds) / rhs);
373 Duration::operator/(const int rhs) const
375 return *this / static_cast<double>(rhs);
380 Duration::operator/(const std::int64_t rhs) const
382 return *this / static_cast<double>(rhs);
387 Duration::operator/=(const double rhs)
389 _microSeconds = static_cast<std::int64_t>(static_cast<double>(_microSeconds) / rhs);
395 Duration::operator/=(const int rhs)
397 return *this /= static_cast<double>(rhs);
402 Duration::operator/=(const std::int64_t rhs)
404 return *this /= static_cast<double>(rhs);
409 Duration::operator<(const Duration& rhs) const
411 return _microSeconds < rhs._microSeconds;
416 Duration::operator<=(const Duration& rhs) const
418 return _microSeconds <= rhs._microSeconds;
423 Duration::operator==(const Duration& rhs) const
425 return _microSeconds == rhs._microSeconds;
430 Duration::operator!=(const Duration& rhs) const
432 return _microSeconds != rhs._microSeconds;
437 Duration::operator>=(const Duration& rhs) const
439 return _microSeconds >= rhs._microSeconds;
444 Duration::operator>(const Duration& rhs) const
446 return _microSeconds > rhs._microSeconds;
451 operator<<(std::ostream& out, const Duration& rhs)
453 out << rhs.toDurationString();
457 } // namespace armarx::core::time
std::int64_t toMinutes() const
Returns the amount of minutes.
std::int64_t toDays() const
Returns the amount of days.
double toSecondsDouble() const
Returns the amount of seconds.
double toMilliSecondsDouble() const
Returns the amount of milliseconds.
double toHoursDouble() const
Returns the amount of hours.
bool isPositive() const
Tests whether the duration is positive (value in µs > 0).
static Duration MicroSecondsDouble(double microSeconds)
Constructs a duration in microseconds.
static Duration Hours(std::int64_t hours)
Constructs a duration in hours.
static Duration DaysDouble(double days)
Constructs a duration in days.
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
static Duration HoursDouble(double hours)
Constructs a duration in hours.
static Duration SecondsDouble(double seconds)
Constructs a duration in seconds.
static Duration Minutes(std::int64_t minutes)
Constructs a duration in minutes.
double toMinutesDouble() const
Returns the amount of minutes.
std::int64_t toHours() const
Returns the amount of hours.
std::int64_t _microSeconds
Current duration in microseconds.
std::string toDurationString() const
String representation of the current duration in minimal/default format.
static Duration MinutesDouble(double minutes)
Constructs a duration in minutes.
std::int64_t toMilliSeconds() const
Returns the amount of milliseconds.
std::int64_t toMicroSeconds() const
Returns the amount of microseconds.
static Duration Days(std::int64_t days)
Constructs a duration in days.
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
double toDaysDouble() const
Returns the amount of days.
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
double toMicroSecondsDouble() const
Returns the amount of microseconds.
Duration()
Constructs a zero-duration.