|
KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
|
|
#include <timespan.h>
Public Types | |
| enum | Format { Auto , Milliseconds , MicroSeconds , Abbreviated , NoMilliseconds } |
| Output format for toString(). More... | |
Public Member Functions | |
| TimeSpan () | |
| Default constructor — creates a zero-length time span. | |
| TimeSpan (double days, double hours, double minutes, double seconds, double milliseconds=0, double microseconds=0, double nanoseconds=0) | |
| Construct a TimeSpan from explicit time components. | |
| TimeSpan (const TimeSpan &other) | |
| Copy constructor. | |
| TimeSpan (const timespec &other) | |
| Construct a TimeSpan from a POSIX timespec value. | |
| TimeSpan & | operator= (const TimeSpan &other) |
| Copy-assign from another TimeSpan. | |
| TimeSpan & | operator= (const timespec &other) |
| Assign from a POSIX timespec. | |
| TimeSpan | operator+ (const TimeSpan &other) const |
| Add two time spans. | |
| TimeSpan | operator- (const TimeSpan &other) const |
| Subtract a time span. | |
| TimeSpan | operator* (const TimeSpan &other) const |
| Multiply two time spans component-wise. | |
| TimeSpan | operator* (double value) const |
| Scale by a scalar factor. | |
| TimeSpan | operator/ (const TimeSpan &other) const |
| Divide two time spans component-wise. | |
| TimeSpan | operator/ (double value) const |
| Divide by a scalar factor. | |
| void | operator+= (const TimeSpan &other) |
| Add-assign a TimeSpan. | |
| void | operator-= (const TimeSpan &other) |
| Subtract-assign a TimeSpan. | |
| void | operator*= (const TimeSpan &other) |
| Multiply-assign by a TimeSpan. | |
| void | operator/= (const TimeSpan &other) |
| Divide-assign by a TimeSpan. | |
| void | operator*= (double value) |
| Multiply-assign by a scalar. | |
| void | operator/= (double value) |
| Divide-assign by a scalar. | |
| bool | operator== (const TimeSpan &other) const |
| Equality comparison. | |
| bool | operator!= (const TimeSpan &other) const |
| Inequality comparison. | |
| bool | operator> (const TimeSpan &other) const |
| Greater-than comparison. | |
| bool | operator< (const TimeSpan &other) const |
| Less-than comparison. | |
| bool | operator>= (const TimeSpan &other) const |
| Greater-than-or-equal comparison. | |
| bool | operator<= (const TimeSpan &other) const |
| Less-than-or-equal comparison. | |
| qint64 | days () const |
| Return the number of whole days in the days field of this TimeSpan when printed. | |
| qint64 | hours () const |
| hours | |
| qint64 | minutes () const |
| minutes | |
| qint64 | seconds () const |
| seconds | |
| qint64 | milliseconds () const |
| milliseconds | |
| qint64 | microseconds () const |
| microseconds | |
| qint64 | nanoseconds () const |
| nanoseconds | |
| double | totalSeconds () const |
| totalSeconds | |
| double | totalMinutes () const |
| totalMinutes | |
| double | totalHours () const |
| totalHours | |
| double | totalDays () const |
| totalDays | |
| double | totalMilliseconds () const |
| totalMilliseconds | |
| double | totalMicroseconds () const |
| totalMicroseconds | |
| double | totalNanoseconds () const |
| totalNanoseconds | |
| bool | isZero () const |
| Is this timespan zero total nanoseconds. | |
| bool | isNegative () const |
| Return true if this time span represents a negative duration. | |
| bool | isPositive () const |
| Return true if this time span represents a positive (non-zero) duration. | |
| TimeSpan | absoluteValue () const |
| Return the absolute value of this time span. | |
| bool | isValid () const |
| Return true if this time span was successfully constructed or parsed. | |
| void | toTimeSpec (struct timespec ×pec) const |
| Populate a POSIX timespec structure from this TimeSpan. | |
| QString | toString (Format format, bool microseconds=false) const |
| Format this TimeSpan as a string in the specified format. | |
| QString | toString (bool microseconds=false) const |
| Format this TimeSpan as a string using the Auto format. | |
| QString | toAbbreviatedFormat (bool showMilliseconds=false) const |
| Format this TimeSpan using the abbreviated display format (e.g. | |
| QString | toDumpString () const |
| Format this TimeSpan as a verbose diagnostic string showing all internal fields. | |
| TimeSpan | addNanoseconds (double nanoseconds) const |
| Return a new TimeSpan with nanoseconds added. | |
| TimeSpan | addMicroseconds (double microseconds) const |
| Return a new TimeSpan with microseconds added. | |
| TimeSpan | addMilliseconds (double milliseconds) const |
| Return a new TimeSpan with milliseconds added. | |
| TimeSpan | addSeconds (double seconds) const |
| Return a new TimeSpan with seconds added. | |
| TimeSpan | addMinutes (double minutes) const |
| Return a new TimeSpan with minutes added. | |
| TimeSpan | addHours (double hours) const |
| Return a new TimeSpan with hours added. | |
| TimeSpan | addDays (double days) const |
| Return a new TimeSpan with days added. | |
| QVariant | toVariant () const |
| Wrap this TimeSpan in a QVariant for storage or signal passing. | |
Static Public Member Functions | |
| static TimeSpan | fromVariant (const QVariant &value) |
| Construct a TimeSpan from a QVariant holding a TimeSpan value. | |
| static TimeSpan | fromNanoseconds (double nanoseconds) |
| Construct a TimeSpan from a nanosecond count. | |
| static TimeSpan | fromMicroseconds (double microseconds) |
| Construct a TimeSpan from a microsecond count. | |
| static TimeSpan | fromMilliseconds (double milliseconds) |
| Construct a TimeSpan from a millisecond count. | |
| static TimeSpan | fromSeconds (double seconds) |
| Construct a TimeSpan from a second count. | |
| static TimeSpan | fromMinutes (double minutes) |
| Construct a TimeSpan from a minute count. | |
| static TimeSpan | fromHours (double hours) |
| Construct a TimeSpan from an hour count. | |
| static TimeSpan | fromDays (double days) |
| Construct a TimeSpan from a day count. | |
| static TimeSpan | fromString (const QString &timeString, bool *parsed=nullptr) |
| Parse a TimeSpan from a formatted duration string. | |
| static TimeSpan | zero () |
| Return a zero-length TimeSpan. | |
| static TimeSpan | invalid () |
| Return an invalid (sentinel) TimeSpan whose isValid() returns false. | |
| static TimeSpan | diff (const QDateTime &now, const QDateTime &then) |
| Compute the signed difference between two QDateTime values (now − then). | |
| static TimeSpan | diff (std::chrono::system_clock::time_point now, std::chrono::system_clock::time_point then) |
| Compute the signed difference between two chrono time points (now − then). | |
| static TimeSpan | absDiff (const QDateTime &t1, const QDateTime &t2) |
| Compute the absolute (non-negative) difference between two QDateTime values. | |
| static TimeSpan | absDiff (std::chrono::system_clock::time_point t1, std::chrono::system_clock::time_point t2) |
| Compute the absolute difference between two chrono time points. | |
| static TimeSpan | max (const TimeSpan &t1, const TimeSpan &t2) |
| Return the larger of two time spans. | |
| static TimeSpan | min (const TimeSpan &t1, const TimeSpan &t2) |
| Return the smaller of two time spans. | |
This class represents a span of time with nanosecond resolution, and provides an interface that is very similar to the .NET TimeSpan class.
Operators are supplied for comparison and assignment.
Conversions are supplied to convert between timespec and TimeSpan.
Use the absDiff() static method to obtain the difference between two QDateTime objects.
NOTE: The underlying representation is a signed 64-bit integer value of nanoseconds. Therefore, the largest possible value is 9,223,372,036,854,775,807 giving a maximum range of approximately 292 years.
This was re-written December 2021 to have nanosecond resolution. The interface is compatible with the previous TimeSpan object.
Stephen Punak, December 27 2021
A signed 64-bit nanosecond-resolution duration value, similar to the .NET TimeSpan class.
Provides arithmetic, comparison, and conversion operations across all common time units. The underlying storage is a signed 64-bit nanosecond count, giving a maximum range of approximately 292 years. Use the static factory methods (fromSeconds(), fromMilliseconds(), etc.) or absDiff() to construct instances conveniently.
Definition at line 41 of file timespan.h.
| enum TimeSpan::Format |
Output format for toString().
Definition at line 192 of file timespan.h.
|
inline |
Default constructor — creates a zero-length time span.
Definition at line 45 of file timespan.h.
| TimeSpan::TimeSpan | ( | double | days, |
| double | hours, | ||
| double | minutes, | ||
| double | seconds, | ||
| double | milliseconds = 0, |
||
| double | microseconds = 0, |
||
| double | nanoseconds = 0 |
||
| ) |
Construct a TimeSpan from explicit time components.
| days | Number of days |
| hours | Number of hours |
| minutes | Number of minutes |
| seconds | Number of seconds |
| milliseconds | Number of milliseconds (default 0) |
| microseconds | Number of microseconds (default 0) |
| nanoseconds | Number of nanoseconds (default 0) |
| TimeSpan::TimeSpan | ( | const timespec & | other | ) |
Construct a TimeSpan from a POSIX timespec value.
| other | timespec containing tv_sec and tv_nsec fields |
|
static |
Compute the absolute (non-negative) difference between two QDateTime values.
| t1 | First QDateTime |
| t2 | Second QDateTime |
Referenced by DateRange::duration().
|
static |
Compute the absolute difference between two chrono time points.
| t1 | First time point |
| t2 | Second time point |
| TimeSpan TimeSpan::absoluteValue | ( | ) | const |
Return the absolute value of this time span.
| TimeSpan TimeSpan::addDays | ( | double | days | ) | const |
| TimeSpan TimeSpan::addHours | ( | double | hours | ) | const |
| TimeSpan TimeSpan::addMicroseconds | ( | double | microseconds | ) | const |
| TimeSpan TimeSpan::addMilliseconds | ( | double | milliseconds | ) | const |
| TimeSpan TimeSpan::addMinutes | ( | double | minutes | ) | const |
| TimeSpan TimeSpan::addNanoseconds | ( | double | nanoseconds | ) | const |
| TimeSpan TimeSpan::addSeconds | ( | double | seconds | ) | const |
| qint64 TimeSpan::days | ( | ) | const |
Return the number of whole days in the days field of this TimeSpan when printed.
|
static |
Compute the signed difference between two QDateTime values (now − then).
| now | Minuend QDateTime |
| then | Subtrahend QDateTime |
|
static |
Compute the signed difference between two chrono time points (now − then).
| now | Minuend time point |
| then | Subtrahend time point |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
inlinestatic |
| qint64 TimeSpan::hours | ( | ) | const |
hours
|
static |
|
inline |
Return true if this time span represents a negative duration.
Definition at line 293 of file timespan.h.
|
inline |
Return true if this time span represents a positive (non-zero) duration.
Definition at line 298 of file timespan.h.
|
inline |
Return true if this time span was successfully constructed or parsed.
Definition at line 309 of file timespan.h.
|
inline |
Is this timespan zero total nanoseconds.
Definition at line 288 of file timespan.h.
Return the larger of two time spans.
Definition at line 514 of file timespan.h.
| qint64 TimeSpan::microseconds | ( | ) | const |
microseconds
| qint64 TimeSpan::milliseconds | ( | ) | const |
milliseconds
Return the smaller of two time spans.
Definition at line 522 of file timespan.h.
| qint64 TimeSpan::minutes | ( | ) | const |
minutes
| qint64 TimeSpan::nanoseconds | ( | ) | const |
nanoseconds
| bool TimeSpan::operator!= | ( | const TimeSpan & | other | ) | const |
Inequality comparison.
| other | TimeSpan to compare with |
| TimeSpan TimeSpan::operator* | ( | double | value | ) | const |
Scale by a scalar factor.
| value | Scalar multiplier |
| void TimeSpan::operator*= | ( | const TimeSpan & | other | ) |
| void TimeSpan::operator*= | ( | double | value | ) |
Multiply-assign by a scalar.
| value | Scalar multiplier |
| void TimeSpan::operator+= | ( | const TimeSpan & | other | ) |
| void TimeSpan::operator-= | ( | const TimeSpan & | other | ) |
| TimeSpan TimeSpan::operator/ | ( | double | value | ) | const |
Divide by a scalar factor.
| value | Scalar divisor |
| void TimeSpan::operator/= | ( | const TimeSpan & | other | ) |
| void TimeSpan::operator/= | ( | double | value | ) |
Divide-assign by a scalar.
| value | Scalar divisor |
| bool TimeSpan::operator< | ( | const TimeSpan & | other | ) | const |
Less-than comparison.
| other | TimeSpan to compare with |
| bool TimeSpan::operator<= | ( | const TimeSpan & | other | ) | const |
Less-than-or-equal comparison.
| other | TimeSpan to compare with |
| TimeSpan & TimeSpan::operator= | ( | const timespec & | other | ) |
Assign from a POSIX timespec.
| other | timespec to assign from |
| bool TimeSpan::operator== | ( | const TimeSpan & | other | ) | const |
Equality comparison.
| other | TimeSpan to compare with |
| bool TimeSpan::operator> | ( | const TimeSpan & | other | ) | const |
Greater-than comparison.
| other | TimeSpan to compare with |
| bool TimeSpan::operator>= | ( | const TimeSpan & | other | ) | const |
Greater-than-or-equal comparison.
| other | TimeSpan to compare with |
| qint64 TimeSpan::seconds | ( | ) | const |
seconds
| QString TimeSpan::toAbbreviatedFormat | ( | bool | showMilliseconds = false | ) | const |
Format this TimeSpan using the abbreviated display format (e.g.
"1h 2m 3s").
| showMilliseconds | If true, append milliseconds to the output |
| QString TimeSpan::toDumpString | ( | ) | const |
Format this TimeSpan as a verbose diagnostic string showing all internal fields.
| QString TimeSpan::toString | ( | bool | microseconds = false | ) | const |
Format this TimeSpan as a string using the Auto format.
| microseconds | If true, include microsecond resolution |
| QString TimeSpan::toString | ( | Format | format, |
| bool | microseconds = false |
||
| ) | const |
Format this TimeSpan as a string in the specified format.
| format | Display format |
| microseconds | If true, include microsecond resolution |
| double TimeSpan::totalDays | ( | ) | const |
totalDays
| double TimeSpan::totalHours | ( | ) | const |
totalHours
| double TimeSpan::totalMicroseconds | ( | ) | const |
totalMicroseconds
| double TimeSpan::totalMilliseconds | ( | ) | const |
totalMilliseconds
Referenced by MutexEvent::wait().
| double TimeSpan::totalMinutes | ( | ) | const |
totalMinutes
| double TimeSpan::totalNanoseconds | ( | ) | const |
totalNanoseconds
| double TimeSpan::totalSeconds | ( | ) | const |
totalSeconds
| void TimeSpan::toTimeSpec | ( | struct timespec & | timespec | ) | const |
Populate a POSIX timespec structure from this TimeSpan.
| timespec | Output timespec to fill with tv_sec and tv_nsec |
|
inline |
Wrap this TimeSpan in a QVariant for storage or signal passing.
Definition at line 405 of file timespan.h.
|
inlinestatic |
Return a zero-length TimeSpan.
Definition at line 468 of file timespan.h.
References fromSeconds().