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 MembersClock: 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 FunctionsConstructor: 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 Activitiesoperator+ 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 Classesstd::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:1Let'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:2This example uses <chrono> facilities to calculate and print the execution time in microseconds. Output: Execution time: 7 microseconds. Next TopicStd::nullopt in C++ |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India