std::chrono::time_point(3) | C++ Standard Libary | std::chrono::time_point(3) |
NAME¶
std::chrono::time_point - std::chrono::time_point
Synopsis¶
Defined in header <chrono>
template<
class Clock, (since C++11)
class Duration = typename Clock::duration
> class time_point;
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 Clock
or be std::chrono::local_t (until C++23)
(since C++20).
Member types¶
Member type Definition
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)
operator+= modifies the time point by the given duration
operator-= (public member function)
operator++
operator++(int) increments or decrements the duration
operator-- (public member function)
operator--(int)
(C++20)
min returns the time point corresponding to the smallest duration
[static] (public static member function)
max returns the time point corresponding to the largest duration
[static] (public static member function)
Non-member functions¶
operator+ performs add and subtract operations involving a time
operator- point
(C++11) (function template)
operator==
operator!=
operator<
operator<=
operator>
operator>=
operator<=> compares two time points
(C++11) (function template)
(C++11)(removed in C++20)
(C++11)
(C++11)
(C++11)
(C++11)
(C++20)
time_point_cast converts a time point to another time point on the
(C++11) same clock, with a different duration
(function template)
floor(std::chrono::time_point) converts a time_point to another, rounding
down
(C++17) (function template)
ceil(std::chrono::time_point) converts a time_point to another, rounding up
(C++17) (function template)
round(std::chrono::time_point) converts a time_point to another, rounding to
(C++17) nearest, ties to even
(function template)
Helper classes¶
std::common_type<std::chrono::time_point> specializes the
std::common_type trait
(C++11) (class template specialization)
Example¶
// Run this code
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
void slow_motion()
{
static int a[] {1,2,3,4,5,6,7,8,9,10,11,12};
while (std::ranges::next_permutation(a).found)
{ } // generates 12! permutations
}
int main()
{
using namespace std::literals; // enables the usage of 24h, 1ms, 1s instead
of
// e.g. std::chrono::hours(24), accordingly
const std::chrono::time_point<std::chrono::system_clock> now =
std::chrono::system_clock::now();
const std::time_t t_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;
const std::chrono::time_point<std::chrono::steady_clock> start =
std::chrono::steady_clock::now();
slow_motion();
const auto end = std::chrono::steady_clock::now();
std::cout
<< "Slow calculations took "
<< std::chrono::duration_cast<std::chrono::microseconds>(end -
start).count() << "µs ≈ "
<< (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.
Slow calculations took 2090448µs ≈ 2090ms ≈ 2s.
See also¶
duration a time interval
(C++11) (class template)
2022.07.31 | http://cppreference.com |