The absl::Duration class represents a signed, fixed-length amount of time. A Duration is generated using a unit-specific factory function, or is the result of subtracting one absl::Time from another. Durations behave like unit-safe integers and they support all the natural integer-like arithmetic operations. Arithmetic overflows and saturates at +/- infinity. Duration is trivially destructible and should be passed by value rather than const reference.
Declared in <absl/time/time.h>
class Duration;
Factory functions Nanoseconds(), Microseconds(), Milliseconds(), Seconds(), Minutes(), Hours() and InfiniteDuration() allow for creation of constexpr Duration values
Examples:
constexpr absl::Duration ten_ns = absl::Nanoseconds(10); constexpr absl::Duration min = absl::Minutes(1); constexpr absl::Duration hour = absl::Hours(1); absl::Duration dur = 60 * min; // dur == hour absl::Duration half_sec = absl::Milliseconds(500); absl::Duration quarter_sec = 0.25 * absl::Seconds(1);
Duration values can be easily converted to an integral number of units using the division operator.
Example:
constexpr absl::Duration dur = absl::Milliseconds(1500); int64_t ns = dur / absl::Nanoseconds(1); // ns == 1500000000 int64_t ms = dur / absl::Milliseconds(1); // ms == 1500 int64_t sec = dur / absl::Seconds(1); // sec == 1 (subseconds truncated) int64_t min = dur / absl::Minutes(1); // min == 0
See the IDivDuration() and FDivDuration() functions below for details on how to access the fractional parts of the quotient.
Alternatively, conversions can be performed using helpers such as ToInt64Microseconds() and ToDoubleSeconds().
| Name | Description |
|---|---|
Duration [constructor] | Constructors |
operator= | Assigns a copy of another duration. |
operator%= | Sets this duration to its remainder modulo another duration. |
operator*= | Multiplication assignment operators |
operator+= | Adds a duration to this one. |
operator-= | Subtracts a duration from this one. |
operator/= | Division assignment operators |
| Name | Description |
|---|---|
absl::AbslHashValue | Combines this duration into a hash state. |
| Name | Description |
|---|---|
AbsDuration | Returns the absolute value of a duration. |
AbslUnparseFlag | Unparses a Duration value into a command-line string representation using the format specified by absl::ParseDuration(). |
Ceil | Returns the ceiling of a duration using the passed duration unit to its smallest value not less than the duration. |
DurationFromTimespec | Converts a timespec to an absl::Duration. |
DurationFromTimeval | Converts a timeval to an absl::Duration. |
FDivDuration | Divides a Duration numerator into a fractional number of units of a Duration denominator. |
Floor | Floors a duration using the passed duration unit to its largest value not greater than the duration. |
FormatDuration | Returns a string representing the duration in the form "72h3m0.5s". Returns "inf" or "-inf" for +/- InfiniteDuration(). |
FromChrono | Converts a std::chrono::microseconds value to an absl::Duration. |
FromChrono | Converts a std::chrono::hours value to an absl::Duration. |
FromChrono | Converts a std::chrono::nanoseconds value to an absl::Duration. |
FromChrono | Converts a std::chrono::milliseconds value to an absl::Duration. |
FromChrono | Converts a std::chrono::seconds value to an absl::Duration. |
FromChrono | Converts a std::chrono::minutes value to an absl::Duration. |
Hours | Constructs a Duration from a floating-point number of hours. |
Hours | Constructs a Duration from an integral number of hours. |
IDivDuration | Divides a numerator Duration by a denominator Duration, returning the quotient and remainder. The remainder always has the same sign as the numerator. The returned quotient and remainder respect the identity: |
InfiniteDuration | Returns an infinite Duration. To get a Duration representing negative infinity, use -InfiniteDuration(). |
Microseconds | Constructs a Duration from a floating-point number of microseconds. |
Microseconds | Constructs a Duration from an integral number of microseconds. |
Milliseconds | Constructs a Duration from a floating-point number of milliseconds. |
Milliseconds | Constructs a Duration from an integral number of milliseconds. |
Minutes | Constructs a Duration from a floating-point number of minutes. |
Minutes | Constructs a Duration from an integral number of minutes. |
Nanoseconds | Constructs a Duration from a floating-point number of nanoseconds. |
Nanoseconds | Constructs a Duration from an integral number of nanoseconds. |
Seconds | Constructs a Duration from a floating-point number of seconds. |
Seconds | Constructs a Duration from an integral number of seconds. |
SleepFor | Sleeps for the specified duration, expressed as an absl::Duration. |
ToChronoHours | Converts an absl::Duration to a std::chrono::hours value. |
ToChronoMicroseconds | Converts an absl::Duration to a std::chrono::microseconds value. |
ToChronoMilliseconds | Converts an absl::Duration to a std::chrono::milliseconds value. |
ToChronoMinutes | Converts an absl::Duration to a std::chrono::minutes value. |
ToChronoNanoseconds | Converts an absl::Duration to a std::chrono::nanoseconds value. |
ToChronoSeconds | Converts an absl::Duration to a std::chrono::seconds value. |
ToDoubleHours | Converts a Duration to a floating-point count of hours. |
ToDoubleMicroseconds | Converts a Duration to a floating-point count of microseconds. |
ToDoubleMilliseconds | Converts a Duration to a floating-point count of milliseconds. |
ToDoubleMinutes | Converts a Duration to a floating-point count of minutes. |
ToDoubleNanoseconds | Converts a Duration to a floating-point count of nanoseconds. |
ToDoubleSeconds | Converts a Duration to a floating-point count of seconds. |
ToInt64Hours | Converts a Duration to an integral count of hours. |
ToInt64Microseconds | Converts a Duration to an integral count of microseconds. |
ToInt64Milliseconds | Converts a Duration to an integral count of milliseconds. |
ToInt64Minutes | Converts a Duration to an integral count of minutes. |
ToInt64Nanoseconds | Converts a Duration to an integral count of nanoseconds. |
ToInt64Seconds | Converts a Duration to an integral count of seconds. |
ToTimespec | Converts an absl::Duration to a timespec. |
ToTimeval | Converts an absl::Duration to a timeval. |
Trunc | Truncates a duration (toward zero) to a multiple of a non-zero unit. |
UnparseFlag | Deprecated. Use AbslUnparseFlag() instead. |
ZeroDuration | Returns a zero-length duration. This function behaves just like the default constructor, but the name helps make the semantics clear at call sites. |
operator!= | Returns whether two durations are unequal. |
operator% | Returns the remainder of one duration divided by another. |
operator* | Returns the product of a scalar and a duration. |
operator* | Returns the product of a duration and a scalar. |
operator+ | Returns the time obtained by advancing a time by a duration. |
operator+ | Returns the sum of two durations. |
operator- | Returns the difference of two durations. |
operator- | Returns the negation of a duration. |
operator- | Returns the duration between two times. |
operator/ | Returns the integer quotient of one duration divided by another. |
operator/ | Returns the quotient of a duration divided by a scalar. |
operator< | Returns whether one duration is less than another. |
operator<= | Returns whether one duration is less than or equal to another. |
operator<=> | Three-way-compares two durations. |
operator== | Returns whether two durations are equal. |
operator> | Returns whether one duration is greater than another. |
operator>= | Returns whether one duration is greater than or equal to another. |