Scroll to navigation

std::chrono::duration(3) C++ Standard Libary std::chrono::duration(3)

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 fraction representing the time in 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)
period typename Period::type
(since C++17), a std::ratio representing the tick period (i.e. the
number of second's fractions per tick)

Member functions


constructor constructs new duration
(public member function)
operator= assigns the contents
(public member function)
count returns the count of ticks
(public member function)
zero returns the special duration value zero
[static] (public static member function)
min returns the special duration value min
[static] (public static member function)
max returns the special duration value max
[static] (public static member function)
operator+ implements unary + and unary -
operator- (public member function)
operator++
operator++(int) increments or decrements the tick count
operator-- (public member function)
operator--(int)
operator+=
operator-= implements compound assignment between two durations
operator*= (public member function)
operator/=
operator%=

Non-member functions


operator+
operator- implements arithmetic operations with durations as
operator* arguments
operator/ (function template)
operator%
(C++11)
operator==
operator!=
operator<
operator<=
operator>
operator>=
operator<=> compares two durations
(C++11) (function template)
(C++11)(removed in C++20)
(C++11)
(C++11)
(C++11)
(C++11)
(C++20)
duration_cast converts a duration to another, with a different tick
(C++11) interval
(function template)
floor(std::chrono::duration) converts a duration to another, rounding down
(C++17) (function template)
ceil(std::chrono::duration) converts a duration to another, rounding up
(C++17) (function template)
round(std::chrono::duration) converts a duration to another, rounding to nearest,
(C++17) ties to even
(function template)
abs(std::chrono::duration) obtains the absolute value of the duration
(C++17) (function template)
operator<< performs stream output on a duration
(C++20) (function template)
from_stream parses a duration from a stream according to the
(C++20) provided format
(function template)

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 (since C++20)
days (the average length of a Gregorian year). months is equal to
30.436875 days (exactly 1/12 of years).

Helper classes


std::common_type<std::chrono::duration> specializes the std::common_type trait
(C++11) (class template specialization)
treat_as_floating_point indicates that a duration is convertible to
(C++11) duration with different tick period
(class template)
duration_values constructs zero, min, and max values of a
(C++11) tick count of given type
(class template)
specialization of std::formatter that
std::formatter<std::chrono::duration> formats a duration according to the provided
(C++20) format
(class template specialization)

Literals


Defined in inline namespace std::literals::chrono_literals
operator""h A std::chrono::duration literal representing hours
(C++14) (function)
operator""min A std::chrono::duration literal representing minutes
(C++14) (function)
operator""s A std::chrono::duration literal representing seconds
(C++14) (function)
operator""ms A std::chrono::duration literal representing milliseconds
(C++14) (function)
operator""us A std::chrono::duration literal representing microseconds
(C++14) (function)
operator""ns A std::chrono::duration literal representing nanoseconds
(C++14) (function)


Note: the literal suffixes d and y do not refer to days and years but (since C++20)
to day and year, respectively.


Feature-test macro: __cpp_lib_chrono_udls (for user defined literals)

Notes


The actual time interval (in seconds) that is held by a duration object d is roughly
equal to d.count() * D::period::num / D::period::den, where D is of type
chrono::duration<> and d is an object of such type.

Example


This example shows how to define several custom duration types and convert between
types:

// Run this code


#include <iostream>
#include <chrono>
using namespace std::chrono_literals;


template<typename T1, typename T2>
using mul = std::ratio_multiply<T1, T2>;


int main()
{
using shakes = std::chrono::duration<int, mul<std::deca, std::nano>>;
using jiffies = std::chrono::duration<int, std::centi>;
using microfortnights = std::chrono::duration<float,
mul<mul<std::ratio<2>, std::chrono::weeks::period>, std::micro>>;
using nanocenturies = std::chrono::duration<float,
mul<mul<std::hecto, std::chrono::years::period>, std::nano>>;
using fps_24 = std::chrono::duration<double, std::ratio<1,24>>;


std::cout << "1 second is:\n";


// integer scale conversion with no precision loss: no cast
std::cout << std::chrono::microseconds(1s).count() << " microseconds\n"
<< shakes(1s).count() << " shakes\n"
<< jiffies(1s).count() << " jiffies\n";


// integer scale conversion with precision loss: requires a cast
std::cout << std::chrono::duration_cast<std::chrono::minutes>(1s).count()
<< " minutes\n";


// floating-point scale conversion: no cast
std::cout << microfortnights(1s).count() << " microfortnights\n"
<< nanocenturies(1s).count() << " nanocenturies\n"
<< fps_24(1s).count() << " frames at 24fps\n";
}

Output:


1 second is:
1000000 microseconds
100000000 shakes
100 jiffies
0 minutes
0.82672 microfortnights
0.316887 nanocenturies
24 frames at 24fps

2022.07.31 http://cppreference.com