< cpp‎ | chrono‎ | duration
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
Three-way comparison
General utilities
Date and time
Function objects
Formatting library (C++20)
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Elementary string conversions

Defined in header <chrono>
template< class Rep, class Period >
constexpr std::chrono::duration<Rep, Period> abs( std::chrono::duration<Rep, Period> d );
(since C++17)

Returns the absolute value of the duration d. Specifically, if d >=, return d, otherwise return -d.

The function does not participate in the overload resolution unless std::numeric_limits<Rep>::is_signed is true.


[edit] Parameters

d - duration

[edit] Return value

Absolute value of d.

[edit] Possible implementation

template<class Rep, class Period,
    class = std::enable_if_t<std::numeric_limits<Rep>::is_signed>>
constexpr std::chrono::duration<Rep, Period> abs(std::chrono::duration<Rep, Period> d)
    return d >= ? +d : -d;

[edit] Example

#include <chrono>
#include <iostream>
int main()
    using namespace std::chrono;
    static_assert(abs(-42s) == std::chrono::abs(42s));
    std::cout << "abs(+3min) = " << abs(3min).count() << '\n'
              << "abs(-3min) = " << abs(-3min).count() << '\n';


abs(+3min) = 3
abs(-3min) = 3

[edit] See also

implements unary + and unary -
(public member function) [edit]
computes absolute value of an integral value (|x|)
(function) [edit]