You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
chrono is the name of a header, but also of a sub-namespace: All
the elements in this header (except for the common_type specializations)
are not defined directly under the std namespace (like most of the standard
library) but under the std::chrono namespace.
The elements in this header deal with time. This is done mainly by means of
three concepts: durations, time points and clocks.
Durations are template classes that measure time spans by means of a
count and a period., like: one minute, two hours, or ten milliseconds.
For the std::chrono::duration class, only std::ratio< num1, num2 > can
be used.
The corresponding named types of std::ratio< num1, num2 > (e.g.,
std::chrono::seconds) cannot be used directly in
std::chrono::duration; instead, use std::ratio< num1, num2 > (e.g.,
std::ratio< 1, 1 >).
However, std::chrono::time_point, std::chrono::time_point_cast, and
std::chrono::duration_cast support these named types.
In some C++ STL types, std::ratio< num1, num2 >has alias names, such
as std::milli.
All of these are alias names are predefined std::chrono::duration
specializations.
std::chrono::hours: Signed integral type of at least 23 bits, period
std::ratio< 3600, 1 >.
std::chrono::minutes: Signed integral type of at least 29 bits, period
std::ratio< 60, 1 >.
std::chrono::seconds (Default period): Signed integral type of at least 35
bits, period std::ratio< 1, 1 >.
std::chrono::milliseconds: Signed integral type of at least 45 bits, period
std::ratio< 1, 1000 >.
std::chrono::microseconds: Signed integral type of at least 55 bits, period
std::ratio< 1, 1000000 >.
std::chrono::nanoseconds: Signed integral type of at least 64 bits, period
std::ratio< 1, 1000000000 >.
Member Types
rep: Rep, an arithmetic type, or a class emulating an arithmetic type,
representing the number of ticks.
period: Period (until C++17) 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).
(destructor): Destroy duration (public member function).
count: Returns the count of ticks (public member function).
zero [static]: Returns the special duration value zero (public static
member function).
min [static]: Returns the special duration value min (public static
member function).
max [static]: Returns the special duration value max (public static
member function).
duration_cast: Converts a duration to another, with a different tick
interval (function template).
floor( std::chrono::duration ) (C++17): Converts a duration to another,
rounding down (function template).
ceil( std::chrono::duration ) (C++17): Converts a duration to another,
rounding up (function template).
round( std::chrono::duration ) (C++17): Converts a duration to another,
rounding to nearest, ties to even (function template).
abs( std::chrono::duration ) (C++17): Obtains the absolute value of the
duration (function template).
from_stream (C++20): Parses a duration from a stream according to the
provided format (function template).
Helper Classes
std::common_type< std::chrono::duration >: Specializes the
std::common_type trait (class template specialization).
treat_as_floating_point: Indicates that a duration is convertible to
duration with different tick period (class template).
duration_values: Constructs zero, min, and max values of a tick count of
given type (class template).
std::formatter< std::chrono::duration > (C++20): Formatting support for
duration (class template specialization).
std::hash< std::chrono::duration > (C++26): Hash support for
std::chrono::duration (class template specialization).
Helper Specializations
template< class Rep, class Period > constexpr bool enable_nonlocking_formatter_optimization< chrono::duration< Rep, Period > > = enable_nonlocking_formatter_optimization< Rep >;
(since C++23): This specialization of
std::enable_nonlocking_formatter_optimization enables efficient
implementation of std::print and std::println for printing a
std::chrono::duration object when the template parameter Rep enables it.
Literals
Defined in inline namespace std::literals::chrono_literals.
operator""h (C++14): A std::chrono::duration literal representing hours
(function).
operator""min (C++14): A std::chrono::duration literal representing
minutes (function).
operator""s (C++14): A std::chrono::duration literal representing seconds
(function).
operator""ms (C++14): A std::chrono::duration literal representing
milliseconds (function).
operator""us (C++14): A std::chrono::duration literal representing
microseconds (function).
operator""ns (C++14): A std::chrono::duration literal representing
nanoseconds (function).
Note: the literal suffixes d and y do not refer to days and years but to
day and year, respectively. (since C++20).
Time Points
Explanation
Time points are template classes that express a point in time
relative to a clock's epoch.
Internally, their instance objects store an object of a
std::chrono::duration type, and uses the Clock type as a reference for
its epoch.
Clock: A clock class, such as std::chrono::system_clock,
std::chrono::steady_clock, std::chrono::high_resolution_clock or a custom
clock class.
Duration: A std::chrono::duration type. Programmers do not need to
specify it manually. It is automatically set to a std::chrono::duration
type corresponding to the Clock.
Clock::duration: The std::chrono::duration type defined inside the
Clock class. Each Clock class has its own default
std::chrono::duration type.
Member Types
clock: Clock, the clock on which this time point is measured.
duration: Duration, a std::chrono::duration type used to measure the
time since epoch.
rep: Rep, an arithmetic type representing the number of ticks of the
duration.
period: Period, a std::ratio type representing the tick period of the
duration.
Member Functions
(constructor): Constructs a new time point (public member function).
time_since_epoch: Returns the time point as duration since the start of its
clock (public member function).
operators: Operators for std::chrono::time_point (function template).
time_since_epoch: Time since epoch (public member function).
min [static]: Returns the time point corresponding to the smallest
duration (public static member function).
max [static]: Returns the time point corresponding to the largest
duration (public static member function).
Non-member Functions
operators: Operators for std::chrono::time_point (function template).
time_point_cast: Converts a time point to another time point on the same
clock, with a different duration (function template).
floor( std::chrono::time_point ) (C++17): Converts a
std::chrono::time_point to another, rounding down (function template).
ceil( std::chrono::time_point ) (C++17): Converts a
std::chrono::time_point to another, rounding up (function template).
round( std::chrono::time_point ) (C++17): Converts a
std::chrono::time_point to another, rounding to nearest, ties to even
(function template).
clock_cast (C++20): Convert time points of one clock to another (function
template).
Helper Classes
std::common_type< std::chrono::time_point >: Specializes the
std::common_type trait (class template specialization).
std::hash< std::chrono::time_point > (C++26): Hash support for
std::chrono::time_point (class template specialization).
Clocks
Explanation
Clocks are a framework that relates a time point to real physical
time.
The library provides at least three clocks that provide means to
express the current timeas a std::chrono::time_point:
std::chrono::system_clock, std::chrono::steady_clock and
std::chrono::high_resolution_clock. All of them are classes.
Usage:
std::chrono::system_clock: Timestamps, timers.
std::chrono::steady_clock: Performance measurement, timeouts and delays.
rep: An arithmetic type representing the number of ticks in the clock's
duration. For system clocks, it's a signed arithmetic type.
period: A std::ratio type representing the tick period of the clock, in
seconds.
duration: std::chrono::duration< rep, period >. For system clocks,
capable of representing negative durations.
time_point: std::chrono::time_point< Clock >.
Member Constants of Three Clock Classes
constexpr bool is_steady [static]: True if the time between ticks is
always constant, i.e. calls to now() return values that increase
monotonically even in case of some external clock adjustment, otherwise
false. However, for steady clock flag, always true (public static member
constant).
Member Functions of Three Clock Classes
now [static]: Returns a std::chrono::time_point representing the
current point in time. (public static member function).
Member Functions of std::chrono::system_clock
to_time_t [static]: Converts a system clock time point to std::time_t
(public static member function).
from_time_t [static]: Converts std::time_t to a system clock time point
(public static member function).