[#absl-Duration] = xref:absl.adoc[absl]::Duration :relfileprefix: ../ :mrdocs: 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. == Synopsis Declared in `<absl/time/time.h>` [source,cpp,subs="verbatim,replacements,macros,-callouts"] ---- class Duration; ---- == Description 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()`. == Member Functions [cols="1,4"] |=== | Name| Description | xref:absl/Duration/2constructor-03.adoc[`Duration`] [.small]#[constructor]# | Constructors | xref:absl/Duration/operator_assign.adoc[`operator=`] | Assigns a copy of another duration. | xref:absl/Duration/operator_mod_eq.adoc[`operator%=`] | Sets this duration to its remainder modulo another duration. | xref:absl/Duration/operator_star_eq-0a9.adoc[`operator*=`] | Multiplication assignment operators | xref:absl/Duration/operator_plus_eq.adoc[`operator+=`] | Adds a duration to this one. | xref:absl/Duration/operator_minus_eq.adoc[`operator‐=`] | Subtracts a duration from this one. | xref:absl/Duration/operator_slash_eq-01.adoc[`operator/=`] | Division assignment operators |=== == Friends [cols="1,4"] |=== | Name| Description | `xref:absl/AbslHashValue-0e.adoc[absl::AbslHashValue]` | Combines this duration into a hash state. |=== == Non-Member Functions [cols="1,4"] |=== | Name| Description | xref:absl/AbsDuration.adoc[`AbsDuration`] | Returns the absolute value of a duration. | xref:absl/AbslUnparseFlag-08.adoc[`AbslUnparseFlag`] | Unparses a Duration value into a command‐line string representation using the format specified by `absl::ParseDuration()`. | xref:absl/Ceil.adoc[`Ceil`] | Returns the ceiling of a duration using the passed duration unit to its smallest value not less than the duration. | xref:absl/DurationFromTimespec.adoc[`DurationFromTimespec`] | Converts a `timespec` to an `absl::Duration`. | xref:absl/DurationFromTimeval.adoc[`DurationFromTimeval`] | Converts a `timeval` to an `absl::Duration`. | xref:absl/FDivDuration.adoc[`FDivDuration`] | Divides a `Duration` numerator into a fractional number of units of a `Duration` denominator. | xref:absl/Floor.adoc[`Floor`] | Floors a duration using the passed duration unit to its largest value not greater than the duration. | xref:absl/FormatDuration.adoc[`FormatDuration`] | Returns a string representing the duration in the form "72h3m0.5s". Returns "inf" or "‐inf" for +/‐ `InfiniteDuration()`. | xref:absl/FromChrono-00.adoc[`FromChrono`] | Converts a std::chrono::microseconds value to an absl::Duration. | xref:absl/FromChrono-03.adoc[`FromChrono`] | Converts a std::chrono::hours value to an absl::Duration. | xref:absl/FromChrono-05.adoc[`FromChrono`] | Converts a std::chrono::nanoseconds value to an absl::Duration. | xref:absl/FromChrono-0a.adoc[`FromChrono`] | Converts a std::chrono::milliseconds value to an absl::Duration. | xref:absl/FromChrono-0d3.adoc[`FromChrono`] | Converts a std::chrono::seconds value to an absl::Duration. | xref:absl/FromChrono-0f.adoc[`FromChrono`] | Converts a std::chrono::minutes value to an absl::Duration. | xref:absl/Hours-08.adoc[`Hours`] | Constructs a `Duration` from a floating‐point number of hours. | xref:absl/Hours-0b.adoc[`Hours`] | Constructs a `Duration` from an integral number of hours. | xref:absl/IDivDuration.adoc[`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: | xref:absl/InfiniteDuration.adoc[`InfiniteDuration`] | Returns an infinite `Duration`. To get a `Duration` representing negative infinity, use `‐InfiniteDuration()`. | xref:absl/Microseconds-05.adoc[`Microseconds`] | Constructs a `Duration` from a floating‐point number of microseconds. | xref:absl/Microseconds-0e.adoc[`Microseconds`] | Constructs a `Duration` from an integral number of microseconds. | xref:absl/Milliseconds-01f.adoc[`Milliseconds`] | Constructs a `Duration` from a floating‐point number of milliseconds. | xref:absl/Milliseconds-04.adoc[`Milliseconds`] | Constructs a `Duration` from an integral number of milliseconds. | xref:absl/Minutes-07.adoc[`Minutes`] | Constructs a `Duration` from a floating‐point number of minutes. | xref:absl/Minutes-08.adoc[`Minutes`] | Constructs a `Duration` from an integral number of minutes. | xref:absl/Nanoseconds-05.adoc[`Nanoseconds`] | Constructs a `Duration` from a floating‐point number of nanoseconds. | xref:absl/Nanoseconds-0a.adoc[`Nanoseconds`] | Constructs a `Duration` from an integral number of nanoseconds. | xref:absl/Seconds-02.adoc[`Seconds`] | Constructs a `Duration` from a floating‐point number of seconds. | xref:absl/Seconds-0b.adoc[`Seconds`] | Constructs a `Duration` from an integral number of seconds. | xref:absl/SleepFor.adoc[`SleepFor`] | Sleeps for the specified duration, expressed as an `absl::Duration`. | xref:absl/ToChronoHours.adoc[`ToChronoHours`] | Converts an absl::Duration to a std::chrono::hours value. | xref:absl/ToChronoMicroseconds.adoc[`ToChronoMicroseconds`] | Converts an absl::Duration to a std::chrono::microseconds value. | xref:absl/ToChronoMilliseconds.adoc[`ToChronoMilliseconds`] | Converts an absl::Duration to a std::chrono::milliseconds value. | xref:absl/ToChronoMinutes.adoc[`ToChronoMinutes`] | Converts an absl::Duration to a std::chrono::minutes value. | xref:absl/ToChronoNanoseconds.adoc[`ToChronoNanoseconds`] | Converts an absl::Duration to a std::chrono::nanoseconds value. | xref:absl/ToChronoSeconds.adoc[`ToChronoSeconds`] | Converts an absl::Duration to a std::chrono::seconds value. | xref:absl/ToDoubleHours.adoc[`ToDoubleHours`] | Converts a Duration to a floating‐point count of hours. | xref:absl/ToDoubleMicroseconds.adoc[`ToDoubleMicroseconds`] | Converts a Duration to a floating‐point count of microseconds. | xref:absl/ToDoubleMilliseconds.adoc[`ToDoubleMilliseconds`] | Converts a Duration to a floating‐point count of milliseconds. | xref:absl/ToDoubleMinutes.adoc[`ToDoubleMinutes`] | Converts a Duration to a floating‐point count of minutes. | xref:absl/ToDoubleNanoseconds.adoc[`ToDoubleNanoseconds`] | Converts a Duration to a floating‐point count of nanoseconds. | xref:absl/ToDoubleSeconds.adoc[`ToDoubleSeconds`] | Converts a Duration to a floating‐point count of seconds. | xref:absl/ToInt64Hours.adoc[`ToInt64Hours`] | Converts a Duration to an integral count of hours. | xref:absl/ToInt64Microseconds.adoc[`ToInt64Microseconds`] | Converts a Duration to an integral count of microseconds. | xref:absl/ToInt64Milliseconds.adoc[`ToInt64Milliseconds`] | Converts a Duration to an integral count of milliseconds. | xref:absl/ToInt64Minutes.adoc[`ToInt64Minutes`] | Converts a Duration to an integral count of minutes. | xref:absl/ToInt64Nanoseconds.adoc[`ToInt64Nanoseconds`] | Converts a Duration to an integral count of nanoseconds. | xref:absl/ToInt64Seconds.adoc[`ToInt64Seconds`] | Converts a Duration to an integral count of seconds. | xref:absl/ToTimespec-06.adoc[`ToTimespec`] | Converts an `absl::Duration` to a `timespec`. | xref:absl/ToTimeval-01.adoc[`ToTimeval`] | Converts an `absl::Duration` to a `timeval`. | xref:absl/Trunc.adoc[`Trunc`] | Truncates a duration (toward zero) to a multiple of a non‐zero unit. | xref:absl/UnparseFlag-06.adoc[`UnparseFlag`] | Deprecated. Use `AbslUnparseFlag()` instead. | xref:absl/ZeroDuration.adoc[`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. | xref:absl/operator_not_eq-0e7.adoc[`operator!=`] | Returns whether two durations are unequal. | xref:absl/operator_mod-02.adoc[`operator%`] | Returns the remainder of one duration divided by another. | xref:absl/operator_star-02.adoc[`operator*`] | Returns the product of a scalar and a duration. | xref:absl/operator_star-07.adoc[`operator*`] | Returns the product of a duration and a scalar. | xref:absl/operator_plus-04.adoc[`operator+`] | Returns the time obtained by advancing a time by a duration. | xref:absl/operator_plus-0c.adoc[`operator+`] | Returns the sum of two durations. | xref:absl/operator_minus-01a.adoc[`operator‐`] | Returns the difference of two durations. | xref:absl/operator_minus-09.adoc[`operator‐`] | Returns the negation of a duration. | xref:absl/operator_minus-0b.adoc[`operator‐`] | Returns the duration between two times. | xref:absl/operator_slash-05.adoc[`operator/`] | Returns the integer quotient of one duration divided by another. | xref:absl/operator_slash-07.adoc[`operator/`] | Returns the quotient of a duration divided by a scalar. | xref:absl/operator_lt-06f.adoc[`operator<`] | Returns whether one duration is less than another. | xref:absl/operator_le-075.adoc[`operator<=`] | Returns whether one duration is less than or equal to another. | xref:absl/operator_3way-03e.adoc[`operator<=>`] | Three‐way‐compares two durations. | xref:absl/operator_eq-023.adoc[`operator==`] | Returns whether two durations are equal. | xref:absl/operator_gt-06d.adoc[`operator>`] | Returns whether one duration is greater than another. | xref:absl/operator_ge-0b.adoc[`operator>=`] | Returns whether one duration is greater than or equal to another. |=== [.small]#Created with https://www.mrdocs.com[MrDocs]#