Javatpoint Logo
Javatpoint Logo

std::Chrono::Time_point in C++

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.

rep:

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.

time_since_epoch():

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.

time_point_cast:

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::common_type<std::chrono::time_point> (C++11):

It specializes the std::common_type trait for std::chrono::time_point.

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++:

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.

Output:

Execution time: 7 microseconds.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA