Home » Time in C++20: New data types for time of day and calendar date

Time in C++20: New data types for time of day and calendar date

by admin
Time in C++20: New data types for time of day and calendar date

Time in C++20: New data types for time of day and calendar date

Advertisement

After the articles on the basic data types for the chrono functionality, this time I’m looking at a new data type in C++20.

Rainer Grimm has been working as a software architect, team and training manager for many years. He enjoys writing articles on the programming languages ​​C++, Python and Haskell, but also enjoys speaking frequently at specialist conferences. On his blog Modern C++ he deals intensively with his passion C++.

std::chrono::hh_mm_ss is the duration since midnight, divided into hours, minutes, seconds and fractions of seconds. This data type is typically used as a formatting tool. The following table initially gives a brief overview of the std::chrono::hh_mm_ss instance tOfDay.

Depending on the time duration used, the static member returns the corresponding tOfDay.fractional_width. If no such value for fractional_width in range [0, 18] exists, fractional_width is equal to 6. See for example std::chrono::duration> in the following table.

Using the std::chrono::hh_mm_ss data type is quite simple.

// timeOfDay.cpp

#include
#include

int main() {

using namespace std::chrono_literals;

std::cout In (1), I create a new instance of std::chrono::hh_mm_ss: timeOfDay. Thanks to the Chrono literals from C++14, I can add a few time durations to initialize a Time of Day object. With C++20 you can output timeOfDay directly (2). The rest should be easy to read. (4) returns the components of the time since midnight in hours, minutes, seconds and fractions of a second. (5) returns the time since midnight in seconds. (6) is more interesting: the seconds given correspond to the time shown in (2). (7) returns whether the specified hour is in the morning. (8) finally returns the 12-hour equivalent of the specified hour.

See also  Time in C++20: Creating calendar events

Here is the output of the program:

A new data type of the Chrono extension in C++20 is a calendar date. C++20 supports various ways to create and interact with a calendar date. First of all, what does a calendar date look like?

A Calendar date consists of a year, a month and a day. That’s why C++20 has a special data type std::chrono::year_month_day. C++20 has much more to offer. The following table is intended to provide an overview of the calendar types before I show different use cases.

thanks to the Cute Syntax one can use std::chrono::operator / to create Gregorian calendar dates.

Calendar data types support various operations. The following table provides an overview. For readability reasons, I partially ignore the std::chrono namespace.

The increment and decrement operations ++/– are supported in the prefix and postfix versions. Adding or subtracting +/- requires objects of data type std::chrono::duration. This means that if you make the difference between two objects of the data type std::chrono::day, you get an object of the type std::chrono::days. is the new three-way comparison operator.

The following program uses the operations for the calendar types:

// calendarOperations.cpp

#include
#include

int main() {

std::cout 2021y/July: ”
2021y/July) The program performs operations on std::chrono::month (1), std::chrono::weekday (2), std::chrono::year_month (3) and std::chrono::year_month_weekday_last (4).

When adding or subtracting the time period std::chrono::months, modulo operations are automatically applied (5 and 6). Subtracting two std::chrono::month objects equals 1 month. There are 2629746 seconds in a month (7). Accordingly, one can add a time period std::chrono::days to or subtract from the calendar dates std::chrono::day (8 and 9). Subtracting two std::chrono::day objects produces a std::chrono::days object. std::chrono::year_month allows subtraction (10), difference (11) and comparison of times (12). Objects of type std::chrono::weekday_last allow the addition/subtraction of the std::chrono::months and std::chrono::years time periods. Additionally, these std::chrono::weekday_last objects can be compared with each other.

See also  New in .NET 8.0 [6]: ref readonly in C# 12.0

C++20 supports constants and literals to make the use of data types more convenient. (rme)

To home page

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy