Time in C++20: Basic chrono terminology with time duration and point in time
The time zone functionality (C++20) is essentially based on the calendar functionality (C++20), which in turn is based on the chrono functionality (C++11). After the overview of basic chrono terminology in the last post, this time I introduce new data types in C++20.
Advertisement
Duration
C++14 introduced data types such as std::chrono::seconds for time durations and corresponding time literals such as 5s. C++20 added new data types. The table below shows each for completeness.
The time duration std::chrono::days and the calendar date std::chrono::day are often confused. The same applies to the time period std::chrono::years and the calendar date std::chrono::year.
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++.
Differences between the time durations std::chrono::days, std::chrono::years and the calendar types std::chrono::day, std::chrono::year
In C++20, two new literals were added for the new calendar types std::chrono::day and std::chrono::year. The literals d and y refer to a std::chrono::day and std::chrono::year.
The day literal represents a day of the month and is unspecified if it is out of range [0, 255] is.The year literal represents a year in the Gregorian calendar and is unspecified if it is outside the range [-32767, 32767] lies.
The following program illustrates the difference between std::chrono::days and std::chrono::day and accordingly between std::chrono::years and std::chrono::year.
// dayDays.cpp
#include
#include
int main() {
std::cout If you subtract two objects of type std::chrono::day (1), you get an object of type std::chrono::days. The same applies to the std::chrono::year (2) and std::chrono::years. Thanks to the using declaration with namespace std::chrono_literals, I can directly specify the time literals for std::chrono::day and std::chrono::year (3 and 4).
Include literals
There are different ways to include the literals.
using namespace std::literals: includes all C++ literals,using namespace std::chrono: includes the entire namespace std::chrono,using namespace std::chrono_literals: includes all time literals andusing namespace std::literals::chrono_literals: includes all time literals.
The literals.cpp program shows the use of the various using declarations.
// literals.cpp
#include
#include
int main() {
{
using namespace std::literals;
std::string cppString = “C++ string literal”s; // (1)
auto aMinute = 60s; // (2)
// duration aHour = 0.25h + 15min + 1800s;
}
{
using namespace std::chrono;
// std::string cppString = “C++ string literal”s;
auto aMinute = 60s;
duration aHour = 0.25h + 15min + 1800s; // (3)
}
{
using namespace std::chrono_literals;
// std::string cppString = “C++ String literal”s;
auto aMinute = 60s;
// duration aHour = 0.25h + 15min + 1800s;
}
}
The declarations in the std::literals namespace make it possible to use all built-in literals such as the string literal (“C++ string literal “s in 1) or the time literal (60s in line 2). std::chrono::duration cannot be used unqualified. On the contrary, the using declaration using namespace std::chrono allows the time literals and the data type std::chrono::duration (3) to be used unqualified: duration aHour = 0.25h + 15min + 1800s. All time literals are available thanks to the using declaration using namespace::std::chrono::literals.
time
In addition to clock and time duration, the third basic type in C++11 was std::chrono::time_point.
template
class time_point;
A std::chrono::time_point depends on the clock and the time duration. C++20 provides aliases for additional points in time.
With the exception of std::chrono::steady_clock, you can define a point in time with the specified time duration. For all clocks except std::chrono::file_clock it is possible to specify them for seconds. You can also set them for days with std::chrono::local_t and std::chrono::system_clock.
What’s next?
std::chrono::hh_mm_ss is the amount of time since midnight, divided into hours, minutes, seconds and fractions of seconds. This new data type in C++20 represents the time of day. (rme)
To home page