In this article, we will discuss the std::chrono::time_point in C++ with its example.
A class template called std::chrono::time_point is included in the <chrono> header of the C++ Standard Library. It is used to handle computations involving time and represents a particular point in time.
Template Specifications:
Clock:
This time point is measured using this clock function. Specific conditions must be met by the clock, otherwise (from C++20) it can be std::chrono::local_t .
Duration:
A std::chrono::duration type that calculates the amount of time since the clock's epoch began. It can be left out, and the clock's duration type will be used by default.
Types of Members
Clock:
For the clock linked to the time point, type an alias.
duration:
It is a type alias for the duration type, which is employed to calculate the epoch's duration.
It is an mathematical type that indicates how many ticks there are in the duration.
time frame:
It is a type of std::ratio that indicates the duration's tick period.
Member Functions
Constructor:
Construct a new time point based on the given clock and duration.
timesinceepoch:
It provides the time interval measured from the beginning of the clock point.
operator+= and operator-=:
It alters the time point by the specified amount of time.
operator++, operator++(int), operator--, operator--(int) (C++20):
It increases or decreases the time frame.
min:
The time point corresponding to the shortest possible duration is returned by this function.
max:
It provides the time point that corresponds to the longest period that is feasible.
Non-Member Activities
operator+ and operator-.:
It operates addition and subtraction on time points.
operator==, operator!=, operator<, operator<=, operator>, operator>=, operator<=> (C++20):
Two time points are compared.
timepointcast:
It converts a given time point to a different time point with a different duration on the same clock.
floor(std::chrono::time_point) (C++17):
Round down and convert one time point to another.
ceil(std::chrono::time_point) (C++17):
Rounding up, convert one time point to another.
round(C++17) std::chrono::time_point:
Round a time point to the nearest even number while converting it to another.
Support Classes
std::commontype<std::chrono::timepoint> (C++11):
It specializes the std::commontype trait for std::chrono::timepoint .
std::hash<std::chrono::time_point> (C++26):
It gives std::chrono::time_point hash support.
Example:1
Let's take an example to illustrate the std::chrono::time_point in C++:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
using std::chrono::system_clock;
int main()
{
system_clock::time_point now = system_clock::now();
std::time_t now_c = system_clock::to_time_t(
now - std::chrono::hours(24 * 7)); // 7 days ago
std::cout << "Current date and time: "
<< std::put_time(std::localtime(&now_c), "%F %T") << '\n';
now_c = system_clock::to_time_t(now);
std::cout << "One week ago, the time was "
<< std::put_time(std::localtime(&now_c), "%F %T") << '\n';
}
Output:
Current date and time: 2023-11-04 17:16:09
One week ago, the time was 2023-10-28 17:16:09
Example:2
This example uses <chrono> facilities to calculate and print the execution time in microseconds.
#include <iostream>
#include <chrono>
using std::chrono::duration_cast;
using std::chrono::microseconds;
using std::chrono::steady_clock;
int main()
{
steady_clock::time_point start = steady_clock::now();
// Replace this block with the code you want to measure.
for (int i = 0; i < 1000000; ++i) {
// Some computation or operation.
}
steady_clock::time_point end = steady_clock::now();
std::cout << "Execution time: "
<< duration_cast<microseconds>(end - start).count()
<< " microseconds.\n";
}
Output:
Execution time: 7 microseconds.