Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How C++ uses the chrono library to handle dates and times

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how C++ uses the chrono library to deal with the date and time. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Date and time related library chrono is provided in Clippers 11. Date and time can be easily handled through the chrono library, which facilitates the development of the program. The chrono library mainly contains three types of classes: interval duration, clock clocks, and point-in-time time point.

1. Time interval duration

1.1 Common class members

Duration represents a time interval, which is used to record the length of time. It can represent an interval of seconds, minutes, or hours. The prototype of duration is as follows:

/ / defined in the header file template

< class Rep, class Period = std::ratio>

Class duration

The ratio class represents the number of seconds per clock cycle, where the first template parameter Num represents the numerator, Denom represents the denominator, and the denominator defaults to 1, so ratio represents a numerator divided by the denominator, for example: ratio represents a clock cycle of 2 seconds, ratio represents a minute, ratio represents an hour, and ratio represents a day. Ratio stands for 1000 seconds, or 1 millisecond, ratio for one microsecond, and ratio for one nanosecond.

For ease of use, some common time intervals are defined in the standard library, such as hours, minutes, seconds, milliseconds, microseconds, nanoseconds, all under the chrono namespace, as follows:

Type definition nanoseconds: std::chrono::nanosecondsduration microseconds: std::chrono::microsecondsduration milliseconds: std::chrono:: millisecondsdurationseconds: std::chrono::secondsduration minutes: std::chrono::minutesduration hours: std::chrono::hoursduration

Note: each predefined duration type up to hours covers a range of at least ±292 years.

The constructor prototype of the duration class is as follows:

/ / 1. Copy the constructor duration (const duration&) = default;// 2. Construct the object template by specifying the type of clock cycle

< class Rep2 >

Constexpr explicit duration (const Rep2& r) / / 3. Construct the object template by specifying the clock cycle type and the clock cycle length

< class Rep2, class Period2 >

Constexpr duration (const duration& d)

To make it easier to operate between duration objects, operator overloading is performed within the class:

Operator overload describes operator= assignment content (public member function) operator+ operator- assignment content (public member function) operator++ operator++ (int) operator- operator- (int) increasing or decreasing period count (public member function) operator+= operator-= operator*= operator/= operator%= implements compound assignment between two periods (public member function)

The duration class also provides a method count () to get the number of clock cycles of the time interval. The function prototype is as follows:

The use of the constexpr rep count () const;1.2 class

The example code for constructing an event interval object through a constructor is as follows:

# include # include using namespace std;int main () {chrono::hours h (1); / / one hour chrono::milliseconds ms {3}; / 3 millisecond chrono::duration ks (3); / / 3000 seconds / / chrono::duration d3 (3.5); / / error chrono::duration dd (6.6) / / 6.6s / / the number of clock cycles represented by decimals chrono::duration hz (3.5);}

H (1) the clock cycle is 1 hour, with a total of 1 clock cycle, so the time interval represented by h is 1 hour.

The ms (3) clock cycle is 1 millisecond, with a total of 3 clock cycles, so the time interval represented by ms is 3 milliseconds

The ks (3) clock cycle is 1000 seconds, with a total of three clock cycles, so the time interval represented by ks is 3000 seconds.

The clock cycle of D3,3.5 is 1000 seconds, and the number of clock cycles can only be expressed by shaping, but floating-point numbers are specified here, so the syntax is incorrect.

Dd (6.6) clock cycle is the default of 1 second, with a total of 6.6 clock cycles, so the time interval represented by dd is 6.6 seconds.

The clock cycle of hz (3. 5) is 1 stroke 30 seconds, with a total of 3. 5 clock cycles, so the time interval represented by hz is 1 stroke 30 seconds.

The chrono library encapsulates clock cycles of different lengths according to the duration class (which can also be customized). Based on this clock cycle, the total time interval can be obtained by setting the number of cycles (clock cycle * number of cycles = total time interval).

The sample code is as follows:

# include # include int main () {std::chrono::milliseconds ms {3}; / 3 milliseconds std::chrono::microseconds us = 2ms; / / 6000 microseconds / / interval period 1pm 30 seconds std::chrono::duration hz (3. 5); std::cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report