C++ Utilities: CppUtilities::TimeSpan Class Reference

C++ Utilities 5.33.0 Useful C++ classes and routines such as argument parser, IO and conversion utilities

Loading...
Searching...
No Matches
Public Types | 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 Types
using | TickType = std::int64_t

## Public Member Functions
constexpr | TimeSpan ()
| Constructs a new instance of the TimeSpan class with zero ticks.
constexpr | TimeSpan (TickType ticks)
| Constructs a new instance of the TimeSpan class with the specified number of ticks.
TickType & | ticks ()
| Returns a mutable reference to the total ticks.
constexpr TickType | totalTicks () const
| Returns the number of ticks that represent the value of the current TimeSpan class.
constexpr double | totalMicroseconds () const
| Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
constexpr double | totalMilliseconds () const
| Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
constexpr double | totalSeconds () const
| Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
constexpr double | totalMinutes () const
| Returns the value of the current TimeSpan class expressed in whole and fractional minutes.
constexpr double | totalHours () const
| Returns the value of the current TimeSpan class expressed in whole and fractional hours.
constexpr double | totalDays () const
| Returns the value of the current TimeSpan class expressed in whole and fractional days.
constexpr int | nanoseconds () const
| Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
constexpr int | microseconds () const
| Returns the microseconds component of the time interval represented by the current TimeSpan class.
constexpr int | milliseconds () const
| Returns the milliseconds component of the time interval represented by the current TimeSpan class.
constexpr int | seconds () const
| Returns the seconds component of the time interval represented by the current TimeSpan class.
constexpr int | minutes () const
| Returns the minutes component of the time interval represented by the current TimeSpan class.
constexpr int | hours () const
| Returns the hours component of the time interval represented by the current TimeSpan class.
constexpr int | days () const
| Returns the days component of the time interval represented by the current TimeSpan class.
constexpr bool | operator== (const TimeSpan &other) const
| Indicates whether two TimeSpan instances are equal.
constexpr bool | operator!= (const TimeSpan &other) const
| Indicates whether two TimeSpan instances are not equal.
constexpr bool | operator< (const TimeSpan &other) const
| Indicates whether a specified TimeSpan is less than another specified TimeSpan.
constexpr bool | operator> (const TimeSpan &other) const
| Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
constexpr bool | operator<= (const TimeSpan &other) const
| Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
constexpr bool | operator>= (const TimeSpan &other) const
| Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan.
constexpr TimeSpan | operator+ (const TimeSpan &other) const
| Adds two TimeSpan instances.
constexpr TimeSpan | operator- (const TimeSpan &other) const
| Subtracts one TimeSpan instance from another.
constexpr TimeSpan | operator* (double factor) const
| Multiplies a TimeSpan by the specified factor.
constexpr TimeSpan | operator/ (double factor) const
| Divides a TimeSpan by the specified factor.
constexpr double | operator/ (TimeSpan other) const
| Computes the ratio between two TimeSpan instances.
TimeSpan & | operator+= (const TimeSpan &other)
| Adds another TimeSpan to the current instance.
TimeSpan & | operator-= (const TimeSpan &other)
| Subtracts another TimeSpan from the current instance.
TimeSpan & | operator*= (double factor)
| Multiplies the current instance by the specified factor.
TimeSpan & | operator/= (double factor)
| Divides the current instance by the specified factor.
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.
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.
constexpr bool | isNull () const
| Returns true if the time interval represented by the current TimeSpan class is null.
constexpr bool | isNegative () const
| Returns true if the time interval represented by the current TimeSpan class is negative.
constexpr bool | isNegativeInfinity () const
| Returns whether the time interval represented by the current instance is the smallest representable TimeSpan.
constexpr bool | isInfinity () const
| Returns whether the time interval represented by the current instance is the longest representable TimeSpan.

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

## Static Public Attributes
static constexpr TickType | nanosecondsPerTick = 100L
static constexpr TickType | ticksPerMicrosecond = 10L
static constexpr TickType | ticksPerMillisecond = 10000L
static constexpr TickType | ticksPerSecond = 10000000L
static constexpr TickType | ticksPerMinute = 600000000L
static constexpr TickType | ticksPerHour = 36000000000L
static constexpr TickType | ticksPerDay = 864000000000L

## 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.

Definition at line 25 of file timespan.h.

## Member Typedef Documentation

## ◆ TickType

using CppUtilities::TimeSpan::TickType = std::int64_t

Definition at line 29 of file timespan.h.

## Constructor & Destructor Documentation

## ◆ TimeSpan() [1/2]

CppUtilities::TimeSpan::TimeSpan | ( |  | ) |
| inlineexplicitconstexpr

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

Definition at line 114 of file timespan.h.

## ◆ TimeSpan() [2/2]

CppUtilities::TimeSpan::TimeSpan | ( | TickType | ticks | ) |
| inlineexplicitconstexpr

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

Definition at line 122 of file timespan.h.

## Member Function Documentation

## ◆ days()

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

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

Definition at line 355 of file timespan.h.

## ◆ fromDays()

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

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

Definition at line 162 of file timespan.h.

## ◆ fromHours()

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

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

Definition at line 154 of file timespan.h.

## ◆ fromMilliseconds()

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

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

Definition at line 130 of file timespan.h.

## ◆ fromMinutes()

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

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

Definition at line 146 of file timespan.h.

## ◆ fromSeconds()

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

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

Definition at line 138 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

  Throws | a ConversionException if the specified str does not match the expected format.

The expected format is "days:hours:minutes:seconds", e.g. "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. White-spaces before and after parts are ignored.

It is also possible to specify one or more values with a unit, e.g. "2w 1d 5h 1m 0.5s". The units "w" (weeks), "d" (days), "h" (hours), "m" (minutes) and "s" (seconds) are supported.

Definition at line 73 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

  Throws | a 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 217 of file timespan.h.

## ◆ hours()

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

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

Definition at line 347 of file timespan.h.

## ◆ infinity()

TimeSpan CppUtilities::TimeSpan::infinity | ( |  | ) |
| inlinestaticconstexpr

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

Definition at line 233 of file timespan.h.

## ◆ isInfinity()

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

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

Definition at line 562 of file timespan.h.

## ◆ isNegative()

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

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

Definition at line 546 of file timespan.h.

## ◆ isNegativeInfinity()

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

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

Definition at line 554 of file timespan.h.

## ◆ isNull()

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

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

Definition at line 538 of file timespan.h.

## ◆ microseconds()

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

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

Definition at line 315 of file timespan.h.

## ◆ milliseconds()

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

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

Definition at line 323 of file timespan.h.

## ◆ minutes()

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

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

Definition at line 339 of file timespan.h.

## ◆ nanoseconds()

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 307 of file timespan.h.

## ◆ negativeInfinity()

TimeSpan CppUtilities::TimeSpan::negativeInfinity | ( |  | ) |
| inlinestaticconstexpr

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

Definition at line 225 of file timespan.h.

## ◆ operator!=()

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

Indicates whether two TimeSpan instances are not equal.

Definition at line 371 of file timespan.h.

## ◆ operator*()

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

Multiplies a TimeSpan by the specified factor.

Definition at line 427 of file timespan.h.

## ◆ operator*=()

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

Multiplies the current instance by the specified factor.

Definition at line 487 of file timespan.h.

## ◆ operator+()

TimeSpan CppUtilities::TimeSpan::operator+ | ( | const TimeSpan & | other | ) | const
| inlineconstexpr

Adds two TimeSpan instances.

Definition at line 411 of file timespan.h.

## ◆ operator+=()

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

Adds another TimeSpan to the current instance.

Definition at line 469 of file timespan.h.

## ◆ operator-()

TimeSpan CppUtilities::TimeSpan::operator- | ( | const TimeSpan & | other | ) | const
| inlineconstexpr

Subtracts one TimeSpan instance from another.

Definition at line 419 of file timespan.h.

## ◆ operator-=()

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

Subtracts another TimeSpan from the current instance.

Definition at line 478 of file timespan.h.

## ◆ operator/() [1/2]

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

Divides a TimeSpan by the specified factor.

Definition at line 435 of file timespan.h.

## ◆ operator/() [2/2]

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

Computes the ratio between two TimeSpan instances.

Definition at line 461 of file timespan.h.

## ◆ operator/=()

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

Divides the current instance by the specified factor.

Definition at line 496 of file timespan.h.

## ◆ operator<()

bool CppUtilities::TimeSpan::operator< | ( | const TimeSpan & | other | ) | const
| inlineconstexpr

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

Definition at line 379 of file timespan.h.

## ◆ operator<=()

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 395 of file timespan.h.

## ◆ operator==()

bool CppUtilities::TimeSpan::operator== | ( | const TimeSpan & | other | ) | const
| inlineconstexpr

Indicates whether two TimeSpan instances are equal.

Definition at line 363 of file timespan.h.

## ◆ operator>()

bool CppUtilities::TimeSpan::operator> | ( | const TimeSpan & | other | ) | const
| inlineconstexpr

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

Definition at line 387 of file timespan.h.

## ◆ operator>=()

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 403 of file timespan.h.

## ◆ seconds()

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

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

Definition at line 331 of file timespan.h.

## ◆ ticks()

TimeSpan::TickType & CppUtilities::TimeSpan::ticks | ( |  | ) |
| inline

Returns a mutable reference to the total ticks.

Definition at line 241 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 184 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 528 of file timespan.h.

## ◆ totalDays()

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

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

Definition at line 297 of file timespan.h.

## ◆ totalHours()

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

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

Definition at line 289 of file timespan.h.

## ◆ totalMicroseconds()

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

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

Definition at line 257 of file timespan.h.

## ◆ totalMilliseconds()

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

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

Definition at line 265 of file timespan.h.

## ◆ totalMinutes()

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

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

Definition at line 281 of file timespan.h.

## ◆ totalSeconds()

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

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

Definition at line 273 of file timespan.h.

## ◆ totalTicks()

TimeSpan::TickType CppUtilities::TimeSpan::totalTicks | ( |  | ) | const
| inlineconstexpr

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

Definition at line 249 of file timespan.h.

## Friends And Related Symbol Documentation

## ◆ DateTime

friend class DateTime
| friend

Definition at line 26 of file timespan.h.

## Member Data Documentation

## ◆ nanosecondsPerTick

TickType CppUtilities::TimeSpan::nanosecondsPerTick = 100L
| staticconstexpr

Definition at line 99 of file timespan.h.

## ◆ ticksPerDay

TickType CppUtilities::TimeSpan::ticksPerDay = 864000000000L
| staticconstexpr

Definition at line 105 of file timespan.h.

## ◆ ticksPerHour

TickType CppUtilities::TimeSpan::ticksPerHour = 36000000000L
| staticconstexpr

Definition at line 104 of file timespan.h.

## ◆ ticksPerMicrosecond

TickType CppUtilities::TimeSpan::ticksPerMicrosecond = 10L
| staticconstexpr

Definition at line 100 of file timespan.h.

## ◆ ticksPerMillisecond

TickType CppUtilities::TimeSpan::ticksPerMillisecond = 10000L
| staticconstexpr

Definition at line 101 of file timespan.h.

## ◆ ticksPerMinute

TickType CppUtilities::TimeSpan::ticksPerMinute = 600000000L
| staticconstexpr

Definition at line 103 of file timespan.h.

## ◆ ticksPerSecond

TickType CppUtilities::TimeSpan::ticksPerSecond = 10000000L
| staticconstexpr

Definition at line 102 of file timespan.h.

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

- chrono/timespan.h
- chrono/timespan.cpp