Why: TimeSpec#

class TimeSpec : public timespec#

A wrapper around POSIX timespec that provides convenient arithmetic operations.

TimeSpec extends the standard timespec structure with type-safe construction and arithmetic operators for time manipulation.

Public Functions

inline TimeSpec()#

Default constructor - initializes to zero time.

inline explicit TimeSpec(uint64_t sec, uint64_t nsec)#

Construct a TimeSpec from seconds and nanoseconds.

Note

Asserts that nsec < 1 second

Parameters:
  • sec – Seconds component (must fit in time_t)

  • nsec – Nanoseconds component (must be < 1,000,000,000)

Throws:

std::overflow_error – if sec is too large for time_t

inline bool operator==(const TimeSpec &rhs) const#
inline bool operator!=(const TimeSpec &rhs) const#
inline bool operator<(const TimeSpec &rhs) const#
inline bool operator<=(const TimeSpec &rhs) const#
inline bool operator>(const TimeSpec &rhs) const#
inline bool operator>=(const TimeSpec &rhs) const#
inline TimeSpec operator+(const TimeSpec &rhs) const#

Add two TimeSpec values.

Note

Properly handles nanosecond overflow into seconds

Parameters:

rhs – The TimeSpec to add

Throws:

std::overflow_error – if the result exceeds time_t range

Returns:

A new TimeSpec representing the sum

inline TimeSpec &operator+=(const TimeSpec &rhs)#

Add another TimeSpec to this one.

Parameters:

rhs – The TimeSpec to add

Throws:

std::overflow_error – if the result exceeds time_t range

Returns:

Reference to this TimeSpec after addition

inline TimeSpec operator-(const TimeSpec &rhs) const#

Subtract one TimeSpec from another.

Note

Asserts that the result is non-negative (lhs >= rhs)

Note

Properly handles nanosecond underflow

Parameters:

rhs – The TimeSpec to subtract

Returns:

A new TimeSpec representing the difference

inline double to_seconds() const#

Convert TimeSpec to seconds as a floating-point value.

Returns:

The time value in seconds (with fractional nanoseconds)