Skip to content

iox::units::Duration🔗

More...

#include <duration.hpp>

Protected Types🔗

Name
using uint64_t Seconds_t
using uint32_t Nanoseconds_t

Public Functions🔗

Name
template \<typename T >
constexpr Duration
fromNanoseconds(const T value)
Constructs a new Duration object from nanoseconds.
template \<typename T >
constexpr Duration
fromMicroseconds(const T value)
Constructs a new Duration object from microseconds.
template \<typename T >
constexpr Duration
fromMilliseconds(const T value)
Constructs a new Duration object from milliseconds.
template \<typename T >
constexpr Duration
fromSeconds(const T value)
Constructs a new Duration object from seconds.
template \<typename T >
constexpr Duration
fromMinutes(const T value)
Constructs a new Duration object from minutes.
template \<typename T >
constexpr Duration
fromHours(const T value)
Constructs a new Duration object from hours.
template \<typename T >
constexpr Duration
fromDays(const T value)
Constructs a new Duration object from days.
constexpr Duration(const struct timeval & value)
Construct a Duration object from timeval.
constexpr Duration(const struct timespec & value)
Construct a Duration object from timespec.
constexpr Duration(const struct itimerspec & value)
Construct a Duration object from itimerspec.
constexpr Duration(const std::chrono::milliseconds & value)
Construct a Duration object from std::chrono::milliseconds.
constexpr Duration(const std::chrono::nanoseconds & value)
Construct a Duration object from std::chrono::nanoseconds.
Duration & operator=(const std::chrono::milliseconds & rhs)
Assigns a std::chrono::milliseconds to an duration object.
constexpr bool operator==(const Duration & rhs) const
Equal to operator.
constexpr bool operator!=(const Duration & rhs) const
Not equal to operator.
constexpr bool operator<(const Duration & rhs) const
Less than operator.
constexpr bool operator<=(const Duration & rhs) const
Less than or equal to operator.
constexpr bool operator>(const Duration & rhs) const
Greater than operator.
constexpr bool operator>=(const Duration & rhs) const
Greater than or equal to operator.
constexpr Duration operator+(const Duration & rhs) const
Creates Duration object by addition.
constexpr Duration operator-(const Duration & rhs) const
Creates Duration object by subtraction.
template \<typename T >
constexpr Duration
operator*(const T & rhs) const
Creates Duration object by multiplication.
constexpr uint64_t toNanoseconds() const
returns the duration in nanoseconds
constexpr uint64_t toMicroseconds() const
returns the duration in microseconds
constexpr uint64_t toMilliseconds() const
returns the duration in milliseconds
constexpr uint64_t toSeconds() const
returns the duration in seconds
constexpr uint64_t toMinutes() const
returns the duration in minutes
constexpr uint64_t toHours() const
returns the duration in hours
constexpr uint64_t toDays() const
returns the duration in days
struct timespec timespec(const TimeSpecReference & reference =TimeSpecReference::None) const
converts duration in a timespec c struct
constexpr operator struct timeval() const
converts duration in a timeval c struct timeval::tv_sec = seconds since the Epoch (01.01.1970) timeval::tv_usec = microseconds

Protected Functions🔗

Name
constexpr Duration(const Seconds_t seconds, const Nanoseconds_t nanoseconds)
Constructs a Duration from seconds and nanoseconds.
constexpr Duration createDuration(const Seconds_t seconds, const Nanoseconds_t nanoseconds)
constexpr Duration max()
constexpr Duration zero()

Public Attributes🔗

Name
constexpr uint32_t SECS_PER_MINUTE
constexpr uint32_t SECS_PER_HOUR
constexpr uint32_t HOURS_PER_DAY
constexpr uint32_t MILLISECS_PER_SEC
constexpr uint32_t MICROSECS_PER_SEC
constexpr uint32_t NANOSECS_PER_MICROSEC
constexpr uint32_t NANOSECS_PER_MILLISEC
constexpr uint32_t NANOSECS_PER_SEC

Friends🔗

Name
constexpr friend Duration duration_literals::operator""_ns(unsigned long long int )
constexpr friend Duration duration_literals::operator""_us(unsigned long long int )
constexpr friend Duration duration_literals::operator""_ms(unsigned long long int )
constexpr friend Duration duration_literals::operator""_s(unsigned long long int )
constexpr friend Duration duration_literals::operator""_m(unsigned long long int )
constexpr friend Duration duration_literals::operator""_h(unsigned long long int )
constexpr friend Duration duration_literals::operator""_d(unsigned long long int )
constexpr friend Duration operator*(const T & lhs, const Duration & rhs)
creates Duration object by multiplying object T with a duration
std::ostream & operator<<(std::ostream & stream, const Duration & t)
stream operator for the Duration class

Detailed Description🔗

class iox::units::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;

Protected Types Documentation🔗

using Seconds_t🔗

using iox::units::Duration::Seconds_t =  uint64_t;

using Nanoseconds_t🔗

using iox::units::Duration::Nanoseconds_t =  uint32_t;

Public Functions Documentation🔗

function fromNanoseconds🔗

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

Constructs a new Duration object from nanoseconds.

Parameters:

  • value as nanoseconds

Template Parameters:

  • T is an integer type for the value

Return: a new Duration object

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

function fromMicroseconds🔗

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

Constructs a new Duration object from microseconds.

Parameters:

  • value as microseconds

Template Parameters:

  • T is an integer type for the value

Return: a new Duration object

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

function fromMilliseconds🔗

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

Constructs a new Duration object from milliseconds.

Parameters:

  • value as milliseconds

Template Parameters:

  • T is an integer type for the value

Return: a new Duration object

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

function fromSeconds🔗

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

Constructs a new Duration object from seconds.

Parameters:

  • value as seconds

Template Parameters:

  • T is an integer type for the value

Return: a new Duration object

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

function fromMinutes🔗

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

Constructs a new Duration object from minutes.

Parameters:

  • value as minutes

Template Parameters:

  • T is an integer type for the value

Return: a new Duration object

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

function fromHours🔗

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

Constructs a new Duration object from hours.

Parameters:

  • value as hours

Template Parameters:

  • T is an integer type for the value

Return: a new Duration object

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

function fromDays🔗

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

Constructs a new Duration object from days.

Parameters:

  • value as days

Template Parameters:

  • T is an integer type for the value

Return: a new Duration object

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

function Duration🔗

inline explicit constexpr Duration(
    const struct timeval & value
)

Construct a Duration object from timeval.

Parameters:

  • value as timeval

function Duration🔗

inline explicit constexpr Duration(
    const struct timespec & value
)

Construct a Duration object from timespec.

Parameters:

  • value as timespec

function Duration🔗

inline explicit constexpr Duration(
    const struct itimerspec & value
)

Construct a Duration object from itimerspec.

Parameters:

  • value as itimerspec

Note: only it_interval from the itimerspec is used

function Duration🔗

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

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

Parameters:

  • value as milliseconds

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

function Duration🔗

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

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

Parameters:

  • value as nanoseconds

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

function operator=🔗

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

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

Parameters:

  • rhs is the right hand side of the assignment

Return: a reference to the Duration object with the assigned millisecond value

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

function operator==🔗

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

Equal to operator.

Parameters:

  • rhs is the right hand side of the comparison

Return: true if duration equal to rhs

function operator!=🔗

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

Not equal to operator.

Parameters:

  • rhs is the right hand side of the comparison

Return: true if duration not equal to rhs

function operator<🔗

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

Less than operator.

Parameters:

  • rhs is the right hand side of the comparison

Return: true if duration is less than rhs

function operator<=🔗

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

Less than or equal to operator.

Parameters:

  • rhs is the right hand side of the comparison

Return: true if duration is less than or equal to rhs

function operator>🔗

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

Greater than operator.

Parameters:

  • rhs is the right hand side of the comparison

Return: true if duration is greater than rhs

function operator>=🔗

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

Greater than or equal to operator.

Parameters:

  • rhs is the right hand side of the comparison

Return: true if duration is greater than or equal to rhs

function operator+🔗

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

Creates Duration object by addition.

Parameters:

  • rhs is the second summand

Return: a new Duration object

Todo#607 issue warning or fail

function operator-🔗

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

Creates Duration object by subtraction.

Parameters:

  • rhs is the subtrahend

Return: a new Duration object

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

Todo#607 issue warning or fail

function operator*🔗

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

Creates Duration object by multiplication.

Parameters:

  • rhs is the multiplicator

Template Parameters:

  • T is an arithmetic type for the multiplicator

Return: a new Duration object

Note:

  • A duration of 0 will always result in 0, no matter if multiplied with NaN or +Inf
  • There is no explicit division operator! This can be achieved by multiplication with the inverse of the divisor.
  • Multiplication of a non-zero duration with NaN and +Inf results in a saturated max duration

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

Todo#607 issue warning or fail

function toNanoseconds🔗

inline constexpr uint64_t toNanoseconds() const

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.

Todo#607 issue warning or fail

function toMicroseconds🔗

inline constexpr uint64_t toMicroseconds() const

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.
  • The remaining nanoseconds are truncated, similar to the casting behavior of a float to an int.

Todo#607 issue warning or fail

function toMilliseconds🔗

inline constexpr uint64_t toMilliseconds() const

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.
  • The remaining microseconds are truncated, similar to the casting behavior of a float to an int.

Todo#607 issue warning or fail

function toSeconds🔗

inline constexpr uint64_t toSeconds() const

returns the duration in seconds

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

function toMinutes🔗

inline constexpr uint64_t toMinutes() const

returns the duration in minutes

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

function toHours🔗

inline constexpr uint64_t toHours() const

returns the duration in hours

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

function toDays🔗

inline constexpr uint64_t toDays() const

returns the duration in days

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

function timespec🔗

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

converts duration in a timespec c struct

function operator struct timeval🔗

constexpr operator struct timeval() const

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

Protected Functions Documentation🔗

function Duration🔗

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

Constructs a Duration from seconds and nanoseconds.

Parameters:

  • seconds portion of the duration
  • nanoseconds portion of the duration

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

Todo#607 issue warning or fail

function createDuration🔗

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

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

function max🔗

static inline constexpr Duration max()

function zero🔗

static inline constexpr Duration zero()

Public Attributes Documentation🔗

variable SECS_PER_MINUTE🔗

static constexpr uint32_t SECS_PER_MINUTE {60U};

variable SECS_PER_HOUR🔗

static constexpr uint32_t SECS_PER_HOUR {3600U};

variable HOURS_PER_DAY🔗

static constexpr uint32_t HOURS_PER_DAY {24U};

variable MILLISECS_PER_SEC🔗

static constexpr uint32_t MILLISECS_PER_SEC {1000U};

variable MICROSECS_PER_SEC🔗

static constexpr uint32_t MICROSECS_PER_SEC {MILLISECS_PER_SEC * 1000U};

variable NANOSECS_PER_MICROSEC🔗

static constexpr uint32_t NANOSECS_PER_MICROSEC {1000U};

variable NANOSECS_PER_MILLISEC🔗

static constexpr uint32_t NANOSECS_PER_MILLISEC {NANOSECS_PER_MICROSEC * 1000U};

variable NANOSECS_PER_SEC🔗

static constexpr uint32_t NANOSECS_PER_SEC {NANOSECS_PER_MILLISEC * 1000U};

Friends🔗

friend duration_literals::operator""_ns🔗

friend constexpr friend Duration duration_literals::operator""_ns(
    unsigned long long int 
);

friend duration_literals::operator""_us🔗

friend constexpr friend Duration duration_literals::operator""_us(
    unsigned long long int 
);

friend duration_literals::operator""_ms🔗

friend constexpr friend Duration duration_literals::operator""_ms(
    unsigned long long int 
);

friend duration_literals::operator""_s🔗

friend constexpr friend Duration duration_literals::operator""_s(
    unsigned long long int 
);

friend duration_literals::operator""_m🔗

friend constexpr friend Duration duration_literals::operator""_m(
    unsigned long long int 
);

friend duration_literals::operator""_h🔗

friend constexpr friend Duration duration_literals::operator""_h(
    unsigned long long int 
);

friend duration_literals::operator""_d🔗

friend constexpr friend Duration duration_literals::operator""_d(
    unsigned long long int 
);

friend operator*🔗

friend constexpr friend Duration operator*(
    const T & lhs,
    const Duration & rhs
);

creates Duration object by multiplying object T with a duration

Parameters:

  • lhs is the multiplicator
  • rhs is the multiplicant

Template Parameters:

  • T is an arithmetic type for the multiplicator

Return: a new Duration object

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

friend operator<<🔗

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

stream operator for the Duration class


Updated on 17 June 2021 at 11:15:26 CEST