std::chrono::duration (3) - Linux Manuals
std::chrono::duration: std::chrono::duration
NAME
std::chrono::duration - std::chrono::duration
Synopsis
Defined in header <chrono>
template<
class Rep, (since C++11)
class Period = std::ratio<1>
> class duration;
Class template std::chrono::duration represents a time interval.
It consists of a count of ticks of type Rep and a tick period, where the tick period is a compile-time rational constant representing the number of seconds from one tick to the next.
The only data stored in a duration is a tick count of type Rep. If Rep is floating point, then the duration can represent fractions of ticks. Period is included as part of the duration's type, and is only used when converting between different durations.
Member types
Member type Definition
rep Rep, an arithmetic type representing the number of ticks
period (until C++17)
Member functions
constructor (public member function)
operator= (public member function)
count (public member function)
zero returns the special duration value zero
[static]
min returns the special duration value min
[static]
max returns the special duration value max
[static]
operator+ (public member function)
operator-
operator++ increments or decrements the tick count
operator++(int) (public member function)
operator--
operator--(int)
operator+=
operator-= implements compound assignment between two durations
operator*= (public member function)
operator/=
operator%=
Non-member functions
std::common_type<std::chrono::duration> (class template specialization)
operator+
operator- implements arithmetic operations with durations as arguments
operator* (function template)
operator/
operator%
operator==
operator!= compares two durations
operator< (function template)
operator<=
operator>
operator>=
duration_cast (function template)
floor(std::chrono::duration) converts a duration to another, rounding down
(C++17)
ceil(std::chrono::duration) converts a duration to another, rounding up
(C++17)
round(std::chrono::duration) converts a duration to another, rounding to nearest, ties to even
(C++17)
abs(std::chrono::duration) obtains the absolute value of the duration
(C++17)
operator<< performs stream output on a duration
(C++20)
to_stream outputs a duration into a stream according to the provided format
(C++20)
from_stream parses a duration from a stream according to the provided format
(C++20)
Helper types
Type Definition
std::chrono::nanoseconds duration</*signed integer type of at least 64 bits*/, std::nano>
std::chrono::microseconds duration</*signed integer type of at least 55 bits*/, std::micro>
std::chrono::milliseconds duration</*signed integer type of at least 45 bits*/, std::milli>
std::chrono::seconds duration</*signed integer type of at least 35 bits*/>
std::chrono::minutes duration</*signed integer type of at least 29 bits*/, std::ratio<60>>
std::chrono::hours duration</*signed integer type of at least 23 bits*/, std::ratio<3600>>
std::chrono::days (since C++20) duration</*signed integer type of at least 25 bits*/, std::ratio<86400>>
std::chrono::weeks (since C++20) duration</*signed integer type of at least 22 bits*/, std::ratio<604800>>
std::chrono::months (since C++20) duration</*signed integer type of at least 20 bits*/, std::ratio<2629746>>
std::chrono::years (since C++20) duration</*signed integer type of at least 17 bits*/, std::ratio<31556952>>
Note: each of the predefined duration types up to hours covers a range of at least ±292 years.
Each of the predefined duration types days, weeks, months and years covers a range of at least ±40000 years. years is equal to 365.2425 days (the average length of a Gregorian year). months is equal to 30.436875 days (exactly 1/12 of years).(since C++20)
Helper classes
treat_as_floating_point (class template)
duration_values (class template)
Literals
Defined in inline namespace std::literals::chrono_literals
operator""h A std::chrono::duration literal representing hours
(C++14)
operator""min A std::chrono::duration literal representing minutes
(C++14)
operator""s A std::chrono::duration literal representing seconds
(C++14)
operator""ms A std::chrono::duration literal representing milliseconds
(C++14)
operator""us A std::chrono::duration literal representing microseconds
(C++14)
operator""ns A std::chrono::duration literal representing nanoseconds
(C++14)
Note: the literal suffixes d and y do not refer to days and years but to day and year, respectively. (since C++20)
Example
This example shows how to define several custom duration types and convert between types:
// Run this code
Output: