template<classClock,classDuration=typenameClock::duration>classtime_point;(since C++11)Class template std::chrono::time_point represents a point in time. It is implemented as if it stores a value of type Duration indicating the time interval from the start of the Clock's epoch.
Clock must meet the requirements for
or be std::chrono::local_t(since C++20).
(until C++23)Member types
Type Description Clockclock
the clock on which this time point is measured
(typedef)Durationduration
a
type used to measure the time since epoch
(typedef)duration::reprep
an arithmetic type representing the number of ticks of the duration
(typedef)duration::periodperiod
a
type representing the tick period of the duration
(typedef)Member functions
constructs a new time point
(public member function)
returns the time point as duration since the start of its clock
(public member function)
modifies the time point by the given duration
(public member function)
operator++operator++(int)operator--operator--(int)
(C++20)
increments or decrements the duration
(public member function)
[static]
returns the time point corresponding to the smallest duration
(public static member function)
[static]
returns the time point corresponding to the largest duration
(public static member function)
Non-member functions
(C++11)
performs add and subtract operations involving a time point
(function template)
operator==operator!=operator<operator<=operator>operator>=operator<=>
(C++11)(C++11)(removed in C++20)(C++11)(C++11)(C++11)(C++11)(C++20)
compares two time points
(function template)
(C++11)
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 time_point to another, rounding down
(function template)
(C++17)
converts a time_point to another, rounding up
(function template)
round(std::chrono::time_point)
(C++17)
converts a time_point to another, rounding to nearest, ties to even
(function template)
Helper classes
Example
Run this code
#include<algorithm>#include<chrono>#include<ctime>#include<iomanip>#include<iostream>voidslow_motion(){staticinta[]{1,2,3,4,5,6,7,8,9,10,11,12};// Generate Γ(13) == 12! permutations:while(std::ranges::next_permutation(a).found){}}intmain(){usingnamespacestd::literals;// enables literal suffixes, e.g. 24h, 1ms, 1s.conststd::chrono::time_point<std::chrono::system_clock>now=std::chrono::system_clock::now();conststd::time_tt_c=std::chrono::system_clock::to_time_t(now-24h);std::cout<<"24 hours ago, the time was "<<std::put_time(std::localtime(&t_c),"%F %T.\n")<<std::flush;conststd::chrono::time_point<std::chrono::steady_clock>start=std::chrono::steady_clock::now();std::cout<<"Different clocks are not comparable: \n"" System time: "<<now.time_since_epoch()<<"\n"" Steady time: "<<start.time_since_epoch()<<'\n';slow_motion();constautoend=std::chrono::steady_clock::now();std::cout<<"Slow calculations took "<<std::chrono::duration_cast<std::chrono::microseconds>(end-start)<<" ≈ "<<(end-start)/1ms<<"ms ≈ "// almost equivalent form of the above, but<<(end-start)/1s<<"s.\n";// using milliseconds and seconds accordingly}Possible output:
24 hours ago, the time was 2021-02-15 18:28:52. Different clocks are not comparable: System time: 1666497022681282572ns Steady time: 413668317434475ns Slow calculations took 2090448µs ≈ 2090ms ≈ 2s. See also