Class Duration

Class Documentation

class Duration
#include <iostream>
// ...
using namespace units;
using namespace units::duration_literals;
auto someDays = 2 * 7_d + 5_ns;
auto someSeconds = 42_s + 500_ms;
std::cout << someDays << std::endl;
std::cout << someDays.nanoSeconds<uint64_t>() << " ns" << std::endl;
std::cout << someSeconds.milliSeconds<int64_t>() << " ms" << std::endl;

Public Functions

explicit constexpr Duration(const struct timeval &value) noexcept

Construct a Duration object from timeval.

Parameters:

value[in] as timeval

explicit constexpr Duration(const struct timespec &value) noexcept

Construct a Duration object from timespec.

Parameters:

value[in] as timespec

explicit constexpr Duration(const struct itimerspec &value) noexcept

Construct a Duration object from itimerspec.

Note

only it_interval from the itimerspec is used

Parameters:

value[in] as itimerspec

explicit constexpr Duration(const std::chrono::milliseconds &value) noexcept

Construct a Duration object from std::chrono::milliseconds.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Parameters:

value[in] as milliseconds

explicit constexpr Duration(const std::chrono::nanoseconds &value) noexcept

Construct a Duration object from std::chrono::nanoseconds.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Parameters:

value[in] as nanoseconds

Duration &operator=(const std::chrono::milliseconds &rhs) noexcept

Assigns a std::chrono::milliseconds to an duration object.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Parameters:

rhs[in] is the right hand side of the assignment

Returns:

a reference to the Duration object with the assigned millisecond value

constexpr bool operator==(const Duration &rhs) const noexcept

Equal to operator.

Parameters:

rhs[in] is the right hand side of the comparison

Returns:

true if duration equal to rhs

constexpr bool operator!=(const Duration &rhs) const noexcept

Not equal to operator.

Parameters:

rhs[in] is the right hand side of the comparison

Returns:

true if duration not equal to rhs

constexpr bool operator<(const Duration &rhs) const noexcept

Less than operator.

Parameters:

rhs[in] is the right hand side of the comparison

Returns:

true if duration is less than rhs

constexpr bool operator<=(const Duration &rhs) const noexcept

Less than or equal to operator.

Parameters:

rhs[in] is the right hand side of the comparison

Returns:

true if duration is less than or equal to rhs

constexpr bool operator>(const Duration &rhs) const noexcept

Greater than operator.

Parameters:

rhs[in] is the right hand side of the comparison

Returns:

true if duration is greater than rhs

constexpr bool operator>=(const Duration &rhs) const noexcept

Greater than or equal to operator.

Parameters:

rhs[in] is the right hand side of the comparison

Returns:

true if duration is greater than or equal to rhs

constexpr Duration operator+(const Duration &rhs) const noexcept

Creates Duration object by addition.

Parameters:

rhs[in] is the second summand

Returns:

a new Duration object

constexpr Duration operator-(const Duration &rhs) const noexcept

Creates Duration object by subtraction.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Parameters:

rhs[in] is the subtrahend

Returns:

a new Duration object

template<typename T>
constexpr Duration operator*(const T &rhs) const noexcept

Creates Duration object by multiplication.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Note

A duration of 0 will always result in 0, no matter if multiplied with NaN or +Inf

Note

There is no explicit division operator! This can be achieved by multiplication with the inverse of the divisor.

Note

Multiplication of a non-zero duration with NaN and +Inf results in a saturated max duration

Template Parameters:

T – is an arithmetic type for the multiplicator

Parameters:

rhs[in] is the multiplicator

Returns:

a new Duration object

constexpr uint64_t toNanoseconds() const noexcept

returns the duration in nanoseconds

Note

If the duration in nanoseconds is larger than an uint64_t can represent, it will be clamped to the uint64_t max value.

constexpr uint64_t toMicroseconds() const noexcept

returns the duration in microseconds

Note

If the duration in microseconds is larger than an uint64_t can represent, it will be clamped to the uint64_t max value.

Note

The remaining nanoseconds are truncated, similar to the casting behavior of a float to an int.

constexpr uint64_t toMilliseconds() const noexcept

returns the duration in milliseconds

Note

If the duration in milliseconds is larger than an uint64_t can represent, it will be clamped to the uint64_t max value.

Note

The remaining microseconds are truncated, similar to the casting behavior of a float to an int.

constexpr uint64_t toSeconds() const noexcept

returns the duration in seconds

Note

The remaining milliseconds are truncated, similar to the casting behavior of a float to an int.

constexpr uint64_t toMinutes() const noexcept

returns the duration in minutes

Note

The remaining seconds are truncated, similar to the casting behavior of a float to an int.

constexpr uint64_t toHours() const noexcept

returns the duration in hours

Note

The remaining minutes are truncated, similar to the casting behavior of a float to an int.

constexpr uint64_t toDays() const noexcept

returns the duration in days

Note

The remaining hours are truncated, similar to the casting behavior of a float to an int.

struct timespec timespec(const TimeSpecReference &reference = TimeSpecReference::None) const noexcept

converts duration in a timespec c struct

constexpr operator struct timeval() const noexcept

converts duration in a timeval c struct timeval::tv_sec = seconds since the Epoch (01.01.1970) timeval::tv_usec = microseconds

Public Static Functions

template<typename T>
static constexpr Duration fromNanoseconds(const T value) noexcept

Constructs a new Duration object from nanoseconds.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Template Parameters:

T – is an integer type for the value

Parameters:

value[in] as nanoseconds

Returns:

a new Duration object

template<typename T>
static constexpr Duration fromMicroseconds(const T value) noexcept

Constructs a new Duration object from microseconds.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Template Parameters:

T – is an integer type for the value

Parameters:

value[in] as microseconds

Returns:

a new Duration object

template<typename T>
static constexpr Duration fromMilliseconds(const T value) noexcept

Constructs a new Duration object from milliseconds.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Template Parameters:

T – is an integer type for the value

Parameters:

value[in] as milliseconds

Returns:

a new Duration object

template<typename T>
static constexpr Duration fromSeconds(const T value) noexcept

Constructs a new Duration object from seconds.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Template Parameters:

T – is an integer type for the value

Parameters:

value[in] as seconds

Returns:

a new Duration object

template<typename T>
static constexpr Duration fromMinutes(const T value) noexcept

Constructs a new Duration object from minutes.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Template Parameters:

T – is an integer type for the value

Parameters:

value[in] as minutes

Returns:

a new Duration object

template<typename T>
static constexpr Duration fromHours(const T value) noexcept

Constructs a new Duration object from hours.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Template Parameters:

T – is an integer type for the value

Parameters:

value[in] as hours

Returns:

a new Duration object

template<typename T>
static constexpr Duration fromDays(const T value) noexcept

Constructs a new Duration object from days.

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Template Parameters:

T – is an integer type for the value

Parameters:

value[in] as days

Returns:

a new Duration object

static constexpr Duration max() noexcept

Constructs a new Duration object of maximum allowed length. Useful for functions which should have an “infinite” timeout.

static constexpr Duration zero() noexcept

Constructs a new Duration object with a duration of zero.

Public Static Attributes

static constexpr uint32_t SECS_PER_MINUTE = {60U}
static constexpr uint32_t SECS_PER_HOUR = {3600U}
static constexpr uint32_t HOURS_PER_DAY = {24U}
static constexpr uint32_t MILLISECS_PER_SEC = {1000U}
static constexpr uint32_t MICROSECS_PER_SEC = {MILLISECS_PER_SEC * 1000U}
static constexpr uint32_t NANOSECS_PER_MICROSEC = {1000U}
static constexpr uint32_t NANOSECS_PER_MILLISEC = {NANOSECS_PER_MICROSEC * 1000U}
static constexpr uint32_t NANOSECS_PER_SEC = {NANOSECS_PER_MILLISEC * 1000U}

Protected Types

using Seconds_t = uint64_t
using Nanoseconds_t = uint32_t

Protected Functions

constexpr Duration(const Seconds_t seconds, const Nanoseconds_t nanoseconds) noexcept

Constructs a Duration from seconds and nanoseconds.

Note

this is protected to be able to use it in unit tests

Parameters:
  • seconds[in] portion of the duration

  • nanoseconds[in] portion of the duration

Protected Static Functions

static constexpr Duration createDuration(const Seconds_t seconds, const Nanoseconds_t nanoseconds) noexcept

Note

this is factory method is necessary to build with msvc due to issues calling a protected constexpr ctor from public methods

Friends

friend constexpr friend Duration duration_literals::operator""_ns (unsigned long long int) noexcept
friend constexpr friend Duration duration_literals::operator""_us (unsigned long long int) noexcept
friend constexpr friend Duration duration_literals::operator""_ms (unsigned long long int) noexcept
friend constexpr friend Duration duration_literals::operator""_s (unsigned long long int) noexcept
friend constexpr friend Duration duration_literals::operator""_m (unsigned long long int) noexcept
friend constexpr friend Duration duration_literals::operator""_h (unsigned long long int) noexcept
friend constexpr friend Duration duration_literals::operator""_d (unsigned long long int) noexcept
template<typename T> friend constexpr friend Duration operator* (const T &lhs, const Duration &rhs) noexcept

creates Duration object by multiplying object T with a duration

Attention

Since negative durations are not allowed, the duration will be clamped to 0

Template Parameters:

T – is an arithmetic type for the multiplicator

Parameters:
  • lhs[in] is the multiplicator

  • rhs[in] is the multiplicant

Returns:

a new Duration object

friend std::ostream &operator<<(std::ostream &stream, const Duration &t) noexcept

stream operator for the Duration class