C++ Utilities  5.10.5
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
CppUtilities::TimeSpan Class Reference

Represents a time interval. More...

#include <timespan.h>

Public Member Functions

constexpr TimeSpan ()
 Constructs a new instance of the TimeSpan class with zero ticks. More...
 
constexpr TimeSpan (std::int64_t ticks)
 Constructs a new instance of the TimeSpan class with the specified number of ticks. More...
 
std::int64_t & ticks ()
 Returns a mutable reference to the total ticks. More...
 
constexpr std::int64_t totalTicks () const
 Returns the number of ticks that represent the value of the current TimeSpan class. More...
 
constexpr double totalMicroseconds () const
 Returns the value of the current TimeSpan class expressed in whole and fractional microseconds. More...
 
constexpr double totalMilliseconds () const
 Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds. More...
 
constexpr double totalSeconds () const
 Returns the value of the current TimeSpan class expressed in whole and fractional seconds. More...
 
constexpr double totalMinutes () const
 Returns the value of the current TimeSpan class expressed in whole and fractional minutes. More...
 
constexpr double totalHours () const
 Returns the value of the current TimeSpan class expressed in whole and fractional hours. More...
 
constexpr double totalDays () const
 Returns the value of the current TimeSpan class expressed in whole and fractional days. More...
 
constexpr int nanoseconds () const
 Returns the nanoseconds component of the time interval represented by the current TimeSpan class. More...
 
constexpr int microseconds () const
 Returns the microseconds component of the time interval represented by the current TimeSpan class. More...
 
constexpr int milliseconds () const
 Returns the milliseconds component of the time interval represented by the current TimeSpan class. More...
 
constexpr int seconds () const
 Returns the seconds component of the time interval represented by the current TimeSpan class. More...
 
constexpr int minutes () const
 Returns the minutes component of the time interval represented by the current TimeSpan class. More...
 
constexpr int hours () const
 Returns the hours component of the time interval represented by the current TimeSpan class. More...
 
constexpr int days () const
 Returns the days component of the time interval represented by the current TimeSpan class. More...
 
constexpr bool operator== (const TimeSpan &other) const
 Indicates whether two TimeSpan instances are equal. More...
 
constexpr bool operator!= (const TimeSpan &other) const
 Indicates whether two TimeSpan instances are not equal. More...
 
constexpr bool operator< (const TimeSpan &other) const
 Indicates whether a specified TimeSpan is less than another specified TimeSpan. More...
 
constexpr bool operator> (const TimeSpan &other) const
 Indicates whether a specified TimeSpan is greater than another specified TimeSpan. More...
 
constexpr bool operator<= (const TimeSpan &other) const
 Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan. More...
 
constexpr bool operator>= (const TimeSpan &other) const
 Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan. More...
 
constexpr TimeSpan operator+ (const TimeSpan &other) const
 Adds two TimeSpan instances. More...
 
constexpr TimeSpan operator- (const TimeSpan &other) const
 Subtracts one TimeSpan instance from another. More...
 
constexpr TimeSpan operator* (double factor) const
 Multiplies a TimeSpan by the specified factor. More...
 
constexpr TimeSpan operator/ (double factor) const
 Divides a TimeSpan by the specified factor. More...
 
constexpr double operator/ (TimeSpan other) const
 Computes the ratio between two TimeSpan instances. More...
 
TimeSpanoperator+= (const TimeSpan &other)
 Adds another TimeSpan to the current instance. More...
 
TimeSpanoperator-= (const TimeSpan &other)
 Subtracts another TimeSpan from the current instance. More...
 
TimeSpanoperator*= (double factor)
 Multiplies the current instance by the specified factor. More...
 
TimeSpanoperator/= (double factor)
 Divides the current instance by the specified factor. More...
 
std::string toString (TimeSpanOutputFormat format=TimeSpanOutputFormat::Normal, bool fullSeconds=false) const
 Converts the value of the current TimeSpan object to its equivalent std::string representation according the given format. More...
 
void toString (std::string &result, TimeSpanOutputFormat format=TimeSpanOutputFormat::Normal, bool fullSeconds=false) const
 Converts the value of the current TimeSpan object to its equivalent std::string representation according the given format. More...
 
constexpr bool isNull () const
 Returns true if the time interval represented by the current TimeSpan class is null. More...
 
constexpr bool isNegative () const
 Returns true if the time interval represented by the current TimeSpan class is negative. More...
 
constexpr bool isNegativeInfinity () const
 Returns whether the time interval represented by the current instance is the smallest representable TimeSpan. More...
 
constexpr bool isInfinity () const
 Returns whether the time interval represented by the current instance is the longest representable TimeSpan. More...
 

Static Public Member Functions

static constexpr TimeSpan fromMilliseconds (double milliseconds)
 Constructs a new instance of the TimeSpan class with the specified number of milliseconds. More...
 
static constexpr TimeSpan fromSeconds (double seconds)
 Constructs a new instance of the TimeSpan class with the specified number of seconds. More...
 
static constexpr TimeSpan fromMinutes (double minutes)
 Constructs a new instance of the TimeSpan class with the specified number of minutes. More...
 
static constexpr TimeSpan fromHours (double hours)
 Constructs a new instance of the TimeSpan class with the specified number of hours. More...
 
static constexpr TimeSpan fromDays (double days)
 Constructs a new instance of the TimeSpan class with the specified number of days. More...
 
static TimeSpan fromString (const std::string &str, char separator=':')
 Parses the given std::string as TimeSpan. More...
 
static TimeSpan fromString (const char *str, char separator)
 Parses the given C-style string as TimeSpan. More...
 
static constexpr TimeSpan negativeInfinity ()
 Constructs a new instance of the TimeSpan class with the minimal number of ticks. More...
 
static constexpr TimeSpan infinity ()
 Constructs a new instance of the TimeSpan class with the maximal number of ticks. More...
 

Static Public Attributes

static constexpr std::int64_t nanosecondsPerTick = 100uL
 
static constexpr std::int64_t ticksPerMicrosecond = 10uL
 
static constexpr std::int64_t ticksPerMillisecond = 10000uL
 
static constexpr std::int64_t ticksPerSecond = 10000000uL
 
static constexpr std::int64_t ticksPerMinute = 600000000uL
 
static constexpr std::int64_t ticksPerHour = 36000000000uL
 
static constexpr std::int64_t ticksPerDay = 864000000000uL
 

Friends

class DateTime
 

Detailed Description

Represents a time interval.

Note that the TimeSpan class is meant to express a time interval independently of the concrete starting DateTime and end DateTime and hence can not be expressed in years and month. For that use case, use the Period class instead.

Remarks
Time values are measured in 100-nanosecond units called ticks.
Todo:
  • Add method for parsing custom string formats.
  • Add method for printing to custom string formats.

Definition at line 25 of file timespan.h.

Constructor & Destructor Documentation

◆ TimeSpan() [1/2]

constexpr CppUtilities::TimeSpan::TimeSpan ( )
inlineexplicitconstexpr

Constructs a new instance of the TimeSpan class with zero ticks.

Definition at line 97 of file timespan.h.

◆ TimeSpan() [2/2]

constexpr CppUtilities::TimeSpan::TimeSpan ( std::int64_t  ticks)
inlineexplicitconstexpr

Constructs a new instance of the TimeSpan class with the specified number of ticks.

Definition at line 105 of file timespan.h.

Member Function Documentation

◆ days()

constexpr int CppUtilities::TimeSpan::days ( ) const
inlineconstexpr

Returns the days component of the time interval represented by the current TimeSpan class.

Definition at line 296 of file timespan.h.

◆ fromDays()

constexpr TimeSpan CppUtilities::TimeSpan::fromDays ( double  days)
inlinestaticconstexpr

Constructs a new instance of the TimeSpan class with the specified number of days.

Definition at line 145 of file timespan.h.

◆ fromHours()

constexpr TimeSpan CppUtilities::TimeSpan::fromHours ( double  hours)
inlinestaticconstexpr

Constructs a new instance of the TimeSpan class with the specified number of hours.

Definition at line 137 of file timespan.h.

◆ fromMilliseconds()

constexpr TimeSpan CppUtilities::TimeSpan::fromMilliseconds ( double  milliseconds)
inlinestaticconstexpr

Constructs a new instance of the TimeSpan class with the specified number of milliseconds.

Definition at line 113 of file timespan.h.

◆ fromMinutes()

constexpr TimeSpan CppUtilities::TimeSpan::fromMinutes ( double  minutes)
inlinestaticconstexpr

Constructs a new instance of the TimeSpan class with the specified number of minutes.

Definition at line 129 of file timespan.h.

◆ fromSeconds()

constexpr TimeSpan CppUtilities::TimeSpan::fromSeconds ( double  seconds)
inlinestaticconstexpr

Constructs a new instance of the TimeSpan class with the specified number of seconds.

Definition at line 121 of file timespan.h.

◆ fromString() [1/2]

TimeSpan CppUtilities::TimeSpan::fromString ( const char *  str,
char  separator 
)
static

Parses the given C-style string as TimeSpan.

Exceptions
Throwsa ConversionException if the specified str does not match the expected format.

The expected format is "days:hours:minutes:seconds", eg. "5:31:4.521" for 5 hours, 31 minutes and 4.521 seconds. So parts at the front can be omitted and the parts can be fractions. The colon can be changed by specifying another separator.

Definition at line 36 of file timespan.cpp.

◆ fromString() [2/2]

TimeSpan CppUtilities::TimeSpan::fromString ( const std::string &  str,
char  separator = ':' 
)
inlinestatic

Parses the given std::string as TimeSpan.

Exceptions
Throwsa ConversionException if the specified str does not match the expected format.

The expected format is "days:hours:minutes:seconds", eg. "5:31:4.521" for 5 hours, 31 minutes and 4.521 seconds. So parts at the front can be omitted and the parts can be fractions. The colon can be changed by specifying another separator.

Definition at line 158 of file timespan.h.

◆ hours()

constexpr int CppUtilities::TimeSpan::hours ( ) const
inlineconstexpr

Returns the hours component of the time interval represented by the current TimeSpan class.

Definition at line 288 of file timespan.h.

◆ infinity()

constexpr TimeSpan CppUtilities::TimeSpan::infinity ( )
inlinestaticconstexpr

Constructs a new instance of the TimeSpan class with the maximal number of ticks.

Definition at line 174 of file timespan.h.

◆ isInfinity()

constexpr bool CppUtilities::TimeSpan::isInfinity ( ) const
inlineconstexpr

Returns whether the time interval represented by the current instance is the longest representable TimeSpan.

Definition at line 465 of file timespan.h.

◆ isNegative()

constexpr bool CppUtilities::TimeSpan::isNegative ( ) const
inlineconstexpr

Returns true if the time interval represented by the current TimeSpan class is negative.

Definition at line 449 of file timespan.h.

◆ isNegativeInfinity()

constexpr bool CppUtilities::TimeSpan::isNegativeInfinity ( ) const
inlineconstexpr

Returns whether the time interval represented by the current instance is the smallest representable TimeSpan.

Definition at line 457 of file timespan.h.

◆ isNull()

constexpr bool CppUtilities::TimeSpan::isNull ( ) const
inlineconstexpr

Returns true if the time interval represented by the current TimeSpan class is null.

Definition at line 441 of file timespan.h.

◆ microseconds()

constexpr int CppUtilities::TimeSpan::microseconds ( ) const
constexpr

Returns the microseconds component of the time interval represented by the current TimeSpan class.

Definition at line 256 of file timespan.h.

◆ milliseconds()

constexpr int CppUtilities::TimeSpan::milliseconds ( ) const
inlineconstexpr

Returns the milliseconds component of the time interval represented by the current TimeSpan class.

Definition at line 264 of file timespan.h.

◆ minutes()

constexpr int CppUtilities::TimeSpan::minutes ( ) const
inlineconstexpr

Returns the minutes component of the time interval represented by the current TimeSpan class.

Definition at line 280 of file timespan.h.

◆ nanoseconds()

constexpr int CppUtilities::TimeSpan::nanoseconds ( ) const
constexpr

Returns the nanoseconds component of the time interval represented by the current TimeSpan class.

Remarks
The accuracy of the TimeSpan class is 100-nanoseconds. Hence the returned value will always have two zeros at the end (in decimal representation).

Definition at line 248 of file timespan.h.

◆ negativeInfinity()

constexpr TimeSpan CppUtilities::TimeSpan::negativeInfinity ( )
inlinestaticconstexpr

Constructs a new instance of the TimeSpan class with the minimal number of ticks.

Definition at line 166 of file timespan.h.

◆ operator!=()

constexpr bool CppUtilities::TimeSpan::operator!= ( const TimeSpan other) const
inlineconstexpr

Indicates whether two TimeSpan instances are not equal.

Definition at line 312 of file timespan.h.

◆ operator*()

constexpr TimeSpan CppUtilities::TimeSpan::operator* ( double  factor) const
inlineconstexpr

Multiplies a TimeSpan by the specified factor.

Definition at line 368 of file timespan.h.

◆ operator*=()

TimeSpan & CppUtilities::TimeSpan::operator*= ( double  factor)
inline

Multiplies the current instance by the specified factor.

Definition at line 410 of file timespan.h.

◆ operator+()

constexpr TimeSpan CppUtilities::TimeSpan::operator+ ( const TimeSpan other) const
inlineconstexpr

Adds two TimeSpan instances.

Definition at line 352 of file timespan.h.

◆ operator+=()

TimeSpan & CppUtilities::TimeSpan::operator+= ( const TimeSpan other)
inline

Adds another TimeSpan to the current instance.

Definition at line 392 of file timespan.h.

◆ operator-()

constexpr TimeSpan CppUtilities::TimeSpan::operator- ( const TimeSpan other) const
inlineconstexpr

Subtracts one TimeSpan instance from another.

Definition at line 360 of file timespan.h.

◆ operator-=()

TimeSpan & CppUtilities::TimeSpan::operator-= ( const TimeSpan other)
inline

Subtracts another TimeSpan from the current instance.

Definition at line 401 of file timespan.h.

◆ operator/() [1/2]

constexpr TimeSpan CppUtilities::TimeSpan::operator/ ( double  factor) const
inlineconstexpr

Divides a TimeSpan by the specified factor.

Definition at line 376 of file timespan.h.

◆ operator/() [2/2]

constexpr double CppUtilities::TimeSpan::operator/ ( TimeSpan  other) const
inlineconstexpr

Computes the ratio between two TimeSpan instances.

Definition at line 384 of file timespan.h.

◆ operator/=()

TimeSpan & CppUtilities::TimeSpan::operator/= ( double  factor)
inline

Divides the current instance by the specified factor.

Definition at line 419 of file timespan.h.

◆ operator<()

constexpr bool CppUtilities::TimeSpan::operator< ( const TimeSpan other) const
inlineconstexpr

Indicates whether a specified TimeSpan is less than another specified TimeSpan.

Definition at line 320 of file timespan.h.

◆ operator<=()

constexpr bool CppUtilities::TimeSpan::operator<= ( const TimeSpan other) const
inlineconstexpr

Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.

Definition at line 336 of file timespan.h.

◆ operator==()

constexpr bool CppUtilities::TimeSpan::operator== ( const TimeSpan other) const
inlineconstexpr

Indicates whether two TimeSpan instances are equal.

Definition at line 304 of file timespan.h.

◆ operator>()

constexpr bool CppUtilities::TimeSpan::operator> ( const TimeSpan other) const
inlineconstexpr

Indicates whether a specified TimeSpan is greater than another specified TimeSpan.

Definition at line 328 of file timespan.h.

◆ operator>=()

constexpr bool CppUtilities::TimeSpan::operator>= ( const TimeSpan other) const
inlineconstexpr

Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan.

Definition at line 344 of file timespan.h.

◆ seconds()

constexpr int CppUtilities::TimeSpan::seconds ( ) const
inlineconstexpr

Returns the seconds component of the time interval represented by the current TimeSpan class.

Definition at line 272 of file timespan.h.

◆ ticks()

std::int64_t & CppUtilities::TimeSpan::ticks ( )
inline

Returns a mutable reference to the total ticks.

Definition at line 182 of file timespan.h.

◆ toString() [1/2]

void CppUtilities::TimeSpan::toString ( std::string &  result,
TimeSpanOutputFormat  format = TimeSpanOutputFormat::Normal,
bool  fullSeconds = false 
) const

Converts the value of the current TimeSpan object to its equivalent std::string representation according the given format.

If fullSeconds is true the time interval will be rounded to full seconds.

The string representation will be stored in result.

Definition at line 81 of file timespan.cpp.

◆ toString() [2/2]

std::string CppUtilities::TimeSpan::toString ( TimeSpanOutputFormat  format = TimeSpanOutputFormat::Normal,
bool  fullSeconds = false 
) const
inline

Converts the value of the current TimeSpan object to its equivalent std::string representation according the given format.

If fullSeconds is true the time interval will be rounded to full seconds.

Definition at line 431 of file timespan.h.

◆ totalDays()

constexpr double CppUtilities::TimeSpan::totalDays ( ) const
inlineconstexpr

Returns the value of the current TimeSpan class expressed in whole and fractional days.

Definition at line 238 of file timespan.h.

◆ totalHours()

constexpr double CppUtilities::TimeSpan::totalHours ( ) const
inlineconstexpr

Returns the value of the current TimeSpan class expressed in whole and fractional hours.

Definition at line 230 of file timespan.h.

◆ totalMicroseconds()

constexpr double CppUtilities::TimeSpan::totalMicroseconds ( ) const
constexpr

Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.

Definition at line 198 of file timespan.h.

◆ totalMilliseconds()

constexpr double CppUtilities::TimeSpan::totalMilliseconds ( ) const
inlineconstexpr

Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.

Definition at line 206 of file timespan.h.

◆ totalMinutes()

constexpr double CppUtilities::TimeSpan::totalMinutes ( ) const
inlineconstexpr

Returns the value of the current TimeSpan class expressed in whole and fractional minutes.

Definition at line 222 of file timespan.h.

◆ totalSeconds()

constexpr double CppUtilities::TimeSpan::totalSeconds ( ) const
inlineconstexpr

Returns the value of the current TimeSpan class expressed in whole and fractional seconds.

Definition at line 214 of file timespan.h.

◆ totalTicks()

constexpr std::int64_t CppUtilities::TimeSpan::totalTicks ( ) const
inlineconstexpr

Returns the number of ticks that represent the value of the current TimeSpan class.

Definition at line 190 of file timespan.h.

Friends And Related Function Documentation

◆ DateTime

friend class DateTime
friend

Definition at line 26 of file timespan.h.

Member Data Documentation

◆ nanosecondsPerTick

constexpr std::int64_t CppUtilities::TimeSpan::nanosecondsPerTick = 100uL
staticconstexpr

Definition at line 82 of file timespan.h.

◆ ticksPerDay

constexpr std::int64_t CppUtilities::TimeSpan::ticksPerDay = 864000000000uL
staticconstexpr

Definition at line 88 of file timespan.h.

◆ ticksPerHour

constexpr std::int64_t CppUtilities::TimeSpan::ticksPerHour = 36000000000uL
staticconstexpr

Definition at line 87 of file timespan.h.

◆ ticksPerMicrosecond

constexpr std::int64_t CppUtilities::TimeSpan::ticksPerMicrosecond = 10uL
staticconstexpr

Definition at line 83 of file timespan.h.

◆ ticksPerMillisecond

constexpr std::int64_t CppUtilities::TimeSpan::ticksPerMillisecond = 10000uL
staticconstexpr

Definition at line 84 of file timespan.h.

◆ ticksPerMinute

constexpr std::int64_t CppUtilities::TimeSpan::ticksPerMinute = 600000000uL
staticconstexpr

Definition at line 86 of file timespan.h.

◆ ticksPerSecond

constexpr std::int64_t CppUtilities::TimeSpan::ticksPerSecond = 10000000uL
staticconstexpr

Definition at line 85 of file timespan.h.


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