nfx-datetime 0.1.1
Cross-platform C++ DateTime library with 100-nanosecond precision and ISO 8601 support
Loading...
Searching...
No Matches
nfx::time::TimeSpan Class Referencefinal

Represents a time interval in 100-nanosecond ticks. More...

#include <nfx/datetime/TimeSpan.h>

Public Member Functions

constexpr TimeSpan (std::int64_t ticks=0) noexcept
 Construct from ticks (100-nanosecond units).
constexpr TimeSpan (std::chrono::system_clock::duration duration) noexcept
 Construct from std::chrono duration.
 TimeSpan (std::string_view iso8601String)
 Parse from ISO 8601 string.
 TimeSpan (const TimeSpan &)=default
 Copy constructor.
 TimeSpan (TimeSpan &&) noexcept=default
 Move constructor.
 ~TimeSpan ()=default
 Destructor.
TimeSpanoperator= (const TimeSpan &)=default
 Copy assignment operator.
TimeSpanoperator= (TimeSpan &&) noexcept=default
 Move assignment operator.
constexpr std::strong_ordering operator<=> (const TimeSpan &other) const noexcept
 Three-way comparison operator.
constexpr bool operator== (const TimeSpan &other) const noexcept
 Equality comparison.
constexpr TimeSpan operator+ (const TimeSpan &other) const noexcept
 Add time durations.
constexpr TimeSpan operator- (const TimeSpan &other) const noexcept
 Subtract time durations.
constexpr TimeSpan operator- () const noexcept
 Negate time duration.
constexpr TimeSpanoperator+= (const TimeSpan &other) noexcept
 Add time duration (in-place).
constexpr TimeSpanoperator-= (const TimeSpan &other) noexcept
 Subtract time duration (in-place).
TimeSpan operator* (double multiplier) const noexcept
 Multiply time duration by a scalar value.
TimeSpan operator/ (double divisor) const noexcept
 Divide time duration by a scalar value.
double operator/ (const TimeSpan &other) const noexcept
 Divide time duration by another TimeSpan.
double days () const noexcept
 Get total days.
double hours () const noexcept
 Get total hours.
double minutes () const noexcept
 Get total minutes.
double seconds () const noexcept
 Get total seconds.
double milliseconds () const noexcept
 Get total milliseconds.
double microseconds () const noexcept
 Get total microseconds.
double nanoseconds () const noexcept
 Get total nanoseconds.
constexpr std::int64_t ticks () const noexcept
 Get tick count.
std::string toString () const
 Convert to ISO 8601 duration string.
std::chrono::system_clock::duration toChrono () const noexcept
 Convert to std::chrono::system_clock::duration.

Static Public Member Functions

static bool fromString (std::string_view iso8601DurationString, TimeSpan &result) noexcept
 Parse a TimeSpan from a string safely without throwing exceptions.
static std::optional< TimeSpanfromString (std::string_view iso8601DurationString) noexcept
 Parse ISO 8601 duration string and return optional TimeSpan.
static TimeSpan fromChrono (const std::chrono::system_clock::duration &duration) noexcept
 Create TimeSpan from std::chrono::system_clock::duration.
static constexpr TimeSpan fromDays (double days) noexcept
 Create a TimeSpan from a number of days.
static constexpr TimeSpan fromHours (double hours) noexcept
 Create a TimeSpan from a number of hours.
static constexpr TimeSpan fromMinutes (double minutes) noexcept
 Create a TimeSpan from a number of minutes.
static constexpr TimeSpan fromSeconds (double seconds) noexcept
 Create a TimeSpan from a number of seconds.
static constexpr TimeSpan fromMilliseconds (double milliseconds) noexcept
 Create a TimeSpan from a number of milliseconds.
static constexpr TimeSpan fromMicroseconds (double microseconds) noexcept
 Create a TimeSpan from a number of microseconds.
static constexpr TimeSpan fromTicks (double ticks) noexcept
 Create a TimeSpan from a number of ticks (100-nanosecond units).

Friends

TimeSpan operator* (double multiplier, const TimeSpan &ts) noexcept
 Multiply time duration by a scalar value (reverse order).

Detailed Description

Represents a time interval in 100-nanosecond ticks.

Provides high-precision time duration representation and arithmetic operations. Compatible with ISO 8601 duration format for parsing and formatting operations.

Key features:

  • 100-nanosecond tick precision for maximum accuracy
  • Range: ±10,675,199 days (approximately ±29,247 years)
  • Arithmetic operations (addition, subtraction, comparison)
  • ISO 8601 duration parsing and formatting (P[n]Y[n]M[n]DT[n]H[n]M[n]S)
  • Factory methods for common time units (days, hours, minutes, seconds)
  • Conversion methods to various time units with fractional support

This class represents a duration or elapsed time interval, not a specific point in time. For absolute time values, use DateTime or DateTimeOffset.

Note
TimeSpan is designed for duration calculations and should not be used to represent calendar dates or clock times. Use DateTime or DateTimeOffset instead.

Definition at line 135 of file TimeSpan.h.

Constructor & Destructor Documentation

◆ TimeSpan() [1/3]

nfx::time::TimeSpan::TimeSpan ( std::int64_t ticks = 0)
inlineexplicitconstexprnoexcept

Construct from ticks (100-nanosecond units).

Parameters
ticksNumber of 100-nanosecond intervals for this TimeSpan (defaults to 0)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TimeSpan() [2/3]

nfx::time::TimeSpan::TimeSpan ( std::chrono::system_clock::duration duration)
inlineexplicitconstexprnoexcept

Construct from std::chrono duration.

Parameters
durationSystem clock duration to convert from
Here is the call graph for this function:

◆ TimeSpan() [3/3]

nfx::time::TimeSpan::TimeSpan ( std::string_view iso8601String)
explicit

Parse from ISO 8601 string.

Parameters
iso8601StringISO 8601 formatted string to parse
Here is the call graph for this function:

Member Function Documentation

◆ days()

double nfx::time::TimeSpan::days ( ) const
inlinenodiscardnoexcept

Get total days.

Returns
The total number of days represented by this TimeSpan (fractional)
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromChrono()

TimeSpan nfx::time::TimeSpan::fromChrono ( const std::chrono::system_clock::duration & duration)
staticnodiscardnoexcept

Create TimeSpan from std::chrono::system_clock::duration.

Converts a std::chrono duration to a TimeSpan. Values outside the representable range of TimeSpan will be clamped to the nearest valid TimeSpan value.

Parameters
durationThe system_clock duration to convert
Returns
A TimeSpan representing the same time duration
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromDays()

constexpr TimeSpan nfx::time::TimeSpan::fromDays ( double days)
inlinestaticnodiscardconstexprnoexcept

Create a TimeSpan from a number of days.

Parameters
daysThe number of days for the TimeSpan duration
Returns
TimeSpan representing the specified number of days
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromHours()

constexpr TimeSpan nfx::time::TimeSpan::fromHours ( double hours)
inlinestaticnodiscardconstexprnoexcept

Create a TimeSpan from a number of hours.

Parameters
hoursThe number of hours for the TimeSpan duration
Returns
TimeSpan representing the specified number of hours
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromMicroseconds()

constexpr TimeSpan nfx::time::TimeSpan::fromMicroseconds ( double microseconds)
inlinestaticnodiscardconstexprnoexcept

Create a TimeSpan from a number of microseconds.

Parameters
microsecondsThe number of microseconds for the TimeSpan duration
Returns
TimeSpan representing the specified number of microseconds
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromMilliseconds()

constexpr TimeSpan nfx::time::TimeSpan::fromMilliseconds ( double milliseconds)
inlinestaticnodiscardconstexprnoexcept

Create a TimeSpan from a number of milliseconds.

Parameters
millisecondsThe number of milliseconds for the TimeSpan duration
Returns
TimeSpan representing the specified number of milliseconds
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromMinutes()

constexpr TimeSpan nfx::time::TimeSpan::fromMinutes ( double minutes)
inlinestaticnodiscardconstexprnoexcept

Create a TimeSpan from a number of minutes.

Parameters
minutesThe number of minutes for the TimeSpan duration
Returns
TimeSpan representing the specified number of minutes
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromSeconds()

constexpr TimeSpan nfx::time::TimeSpan::fromSeconds ( double seconds)
inlinestaticnodiscardconstexprnoexcept

Create a TimeSpan from a number of seconds.

Parameters
secondsThe number of seconds for the TimeSpan duration
Returns
TimeSpan representing the specified number of seconds
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromString() [1/2]

std::optional< TimeSpan > nfx::time::TimeSpan::fromString ( std::string_view iso8601DurationString)
staticnodiscardnoexcept

Parse ISO 8601 duration string and return optional TimeSpan.

Parameters
iso8601DurationStringThe ISO 8601 duration string to parse (e.g., "PT1H30M" or "123.456")
Returns
std::optional<TimeSpan> containing the parsed value if successful, std::nullopt otherwise
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:

◆ fromString() [2/2]

bool nfx::time::TimeSpan::fromString ( std::string_view iso8601DurationString,
TimeSpan & result )
staticnodiscardnoexcept

Parse a TimeSpan from a string safely without throwing exceptions.

Supports the following formats:

  • ISO 8601 duration (standard): "PT1H30M45S", "P1DT2H", "-PT45S", "PT0.5S"
  • Numeric seconds (convenience): "123.45", "-60" (interpreted as seconds)
    Parameters
    iso8601DurationStringThe string to parse
    resultReference to store the parsed TimeSpan if successful
    Returns
    true if parsing succeeded, false otherwise
    Note
    This function is marked [[nodiscard]] - the return value should not be ignored
    Format Requirements
    ISO 8601 format must start with 'P' and contain at least one component (H, M, S, or D). Numeric format accepts only digits, decimal point, and optional leading minus sign. Empty strings and invalid formats return false.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromTicks()

constexpr TimeSpan nfx::time::TimeSpan::fromTicks ( double ticks)
inlinestaticnodiscardconstexprnoexcept

Create a TimeSpan from a number of ticks (100-nanosecond units).

Parameters
ticksThe number of 100-nanosecond ticks for the TimeSpan duration
Returns
TimeSpan representing the specified number of ticks
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hours()

double nfx::time::TimeSpan::hours ( ) const
inlinenodiscardnoexcept

Get total hours.

Returns
The total number of hours represented by this TimeSpan (fractional)
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ microseconds()

double nfx::time::TimeSpan::microseconds ( ) const
inlinenodiscardnoexcept

Get total microseconds.

Returns
The total number of microseconds represented by this TimeSpan (fractional)
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ milliseconds()

double nfx::time::TimeSpan::milliseconds ( ) const
inlinenodiscardnoexcept

Get total milliseconds.

Returns
The total number of milliseconds represented by this TimeSpan (fractional)
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ minutes()

double nfx::time::TimeSpan::minutes ( ) const
inlinenodiscardnoexcept

Get total minutes.

Returns
The total number of minutes represented by this TimeSpan (fractional)
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nanoseconds()

double nfx::time::TimeSpan::nanoseconds ( ) const
inlinenodiscardnoexcept

Get total nanoseconds.

Returns
The total number of nanoseconds represented by this TimeSpan (fractional)
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator*()

TimeSpan nfx::time::TimeSpan::operator* ( double multiplier) const
inlinenoexcept

Multiply time duration by a scalar value.

Parameters
multiplierThe value to multiply by (integer or floating-point)
Returns
New TimeSpan representing the scaled duration
Note
Integer values are automatically converted to double
Here is the call graph for this function:

◆ operator+()

TimeSpan nfx::time::TimeSpan::operator+ ( const TimeSpan & other) const
inlineconstexprnoexcept

Add time durations.

Parameters
otherThe TimeSpan to add to this TimeSpan
Returns
New TimeSpan representing the sum of both durations
Here is the call graph for this function:

◆ operator+=()

TimeSpan & nfx::time::TimeSpan::operator+= ( const TimeSpan & other)
inlineconstexprnoexcept

Add time duration (in-place).

Parameters
otherThe TimeSpan to add to this TimeSpan
Returns
Reference to this TimeSpan after adding the duration
Here is the call graph for this function:

◆ operator-() [1/2]

TimeSpan nfx::time::TimeSpan::operator- ( ) const
inlineconstexprnoexcept

Negate time duration.

Returns
New TimeSpan representing the negated duration
Here is the call graph for this function:

◆ operator-() [2/2]

TimeSpan nfx::time::TimeSpan::operator- ( const TimeSpan & other) const
inlineconstexprnoexcept

Subtract time durations.

Parameters
otherThe TimeSpan to subtract from this TimeSpan
Returns
New TimeSpan representing the difference between durations
Here is the call graph for this function:

◆ operator-=()

TimeSpan & nfx::time::TimeSpan::operator-= ( const TimeSpan & other)
inlineconstexprnoexcept

Subtract time duration (in-place).

Parameters
otherThe TimeSpan to subtract from this TimeSpan
Returns
Reference to this TimeSpan after subtracting the duration
Here is the call graph for this function:

◆ operator/() [1/2]

double nfx::time::TimeSpan::operator/ ( const TimeSpan & other) const
inlinenoexcept

Divide time duration by another TimeSpan.

Parameters
otherThe TimeSpan to divide by
Returns
The ratio of this TimeSpan to the other (how many times the other fits in this)
Here is the call graph for this function:

◆ operator/() [2/2]

TimeSpan nfx::time::TimeSpan::operator/ ( double divisor) const
inlinenoexcept

Divide time duration by a scalar value.

Parameters
divisorThe value to divide by (integer or floating-point)
Returns
New TimeSpan representing the scaled duration
Note
Integer values are automatically converted to double
Here is the call graph for this function:

◆ operator<=>()

std::strong_ordering nfx::time::TimeSpan::operator<=> ( const TimeSpan & other) const
inlineconstexprnoexcept

Three-way comparison operator.

Parameters
otherThe TimeSpan to compare with
Returns
std::strong_ordering indicating the relative order of the two TimeSpans
Here is the call graph for this function:

◆ operator=() [1/2]

TimeSpan & nfx::time::TimeSpan::operator= ( const TimeSpan & )
default

Copy assignment operator.

Returns
Reference to this TimeSpan after assignment
Here is the call graph for this function:

◆ operator=() [2/2]

TimeSpan & nfx::time::TimeSpan::operator= ( TimeSpan && )
defaultnoexcept

Move assignment operator.

Returns
Reference to this TimeSpan after assignment
Here is the call graph for this function:

◆ operator==()

bool nfx::time::TimeSpan::operator== ( const TimeSpan & other) const
inlineconstexprnoexcept

Equality comparison.

Parameters
otherThe TimeSpan to compare with
Returns
true if both TimeSpans represent the same duration, false otherwise
Here is the call graph for this function:

◆ seconds()

double nfx::time::TimeSpan::seconds ( ) const
inlinenodiscardnoexcept

Get total seconds.

Returns
The total number of seconds represented by this TimeSpan (fractional)
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ticks()

std::int64_t nfx::time::TimeSpan::ticks ( ) const
inlinenodiscardconstexprnoexcept

Get tick count.

Returns
The number of 100-nanosecond intervals in this TimeSpan
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ toChrono()

std::chrono::system_clock::duration nfx::time::TimeSpan::toChrono ( ) const
nodiscardnoexcept

Convert to std::chrono::system_clock::duration.

Converts this TimeSpan to a std::chrono duration type. Values outside the representable range of std::chrono::system_clock::duration will be clamped to the nearest representable value.

Returns
A system_clock::duration representing this TimeSpan
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ toString()

std::string nfx::time::TimeSpan::toString ( ) const
nodiscard

Convert to ISO 8601 duration string.

Returns
String representation in ISO 8601 duration format (e.g., "PT1H30M45S")
Note
This function is marked [[nodiscard]] - the return value should not be ignored
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator*

TimeSpan operator* ( double multiplier,
const TimeSpan & ts )
friend

Multiply time duration by a scalar value (reverse order).

Parameters
multiplierThe value to multiply by (integer or floating-point)
tsThe TimeSpan to multiply
Returns
New TimeSpan representing the scaled duration
Note
Integer values are automatically converted to double

The documentation for this class was generated from the following file: