This article is the eleventh in my detailed journey through the chrono extensions in C++20.
Advertisement
- Time in C++20: An introduction to the chrono terminology
- Time in C++20: Introducing chronological terminology with time span and time point
- Time in C++20: new data types for time of day and calendar date
- Time in C++20: creating calendar events
- Time in C++20: display and check calendar appointments
- Time in C++20: query calendar dates and ordinal dates
- Time in C++20: details about working with time zones
- Time Zones in C++20: Online Classes
- Time in C++20: Chrono I/O
- C++20: Chrono I/O: unformatted and formatted
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 frequently speaking at specialist conferences. On his blog Modern C++ he discusses his passion C++ in depth.
The chrono library supports two way formatted input. You can use the function std::chrono::from_stream
Or std::chrono::parse
Usage. Both functions take an input stream and parse the input one at a time according to the format specification. All format specifications except %q
For units, periods added according to literal terms may be used.
std::chrono::from_stream
std::chrono::from_stream
There is a surcharge for different types of watches.
Clocks
std::chrono::system_time
std::chrono::utc_time
std::chrono::tai_time
std::chrono::gps_time
std::chrono::file_time
std::chrono::local_time
Calendar Dates
std::chrono::year_month_day
std::chrono::year_month
std::chrono::month_day
std::chrono::weekday
std::chrono::year
std::chrono::month
std::chrono::day
Various overloads require an input stream as initialization is
a format string fmt
and a time or calendar object chro – std::chrono::from_stream(is, fmt, chro)
.The Chrono object from the input stream is parsed according to the format string.
You can also use abbreviations abb
Specify a time zone and UTC time offset: std::chrono::from_stream(is, fmt, chro, abb, off).
offset has a data type std::chrono::minutes.
Program inputChrono.cpp
Uses formatted input to read a time and calendar date from an input stream.
// inputChrono.cpp
#include
#include
#include
#include
int main() {
std::cout << '\n';
std::chrono::sys_seconds timePoint;
std::istringstream iStream1{"2021-08-11 21:49:35"}; //(1)
std::chrono::from_stream(iStream1, "%F %T", timePoint); //(2)
if (iStream1) std::cout << "timePoint: "
<< timePoint << '\n';
else std::cerr << "timepoint: Reading failed\n";
std::chrono::year_month_day date1;
std::istringstream iStream2{"11/08/21"}; //(3)
std::chrono::from_stream(iStream2, "%x", date1); //(4)
if (iStream2) std::cout << "date1: " << date1 << '\n';
else std::cerr << "date1: Reading failed\n";
std::chrono::year_month_day date2;
std::istringstream iStream3{"11/15/21"};
std::chrono::from_stream(iStream3, "%x", date2); //(5)
if (iStream3) std::cout << "date2: " << date2 << '\n';
else std::cerr << "date2: Reading failed\n";
std::cout << '\n';
}
In (1 and 2), the input stream contains data (iStream1
) format string ("%F %T")
. The same applies to the input stream iStream2
(3) and the associated format string "%x"
(4). In contrast, there is no fifteenth month and the pars stage (5) fails. As a result, the failure of iStream3
decide. use of iStream3
in boolean expression false.
std::chrono::parse
according to std::chrono::from_stream
You can use the function std::chrono::parse
Use to parse the input. The following code snippet shows their equivalence.
std::chrono::from_stream(is, fmt, chro)
is >> std::chrono::parse(fmt, chro)
instead of std::chrono::from_stream
becomes std::chrono::parse
directly to the input stream is
called. std::chrono::parse
A format string is also required fmt
and a chrono object chro
,
So I can use the previous program. inputChrono.cpp
with std::chrono::from_stream
directly into the program inputChronoParse.cpp
with std::chrono::parse
rewrite
// inputChronoParse.cpp
#include
#include
#include
#include
int main() {
std::cout << '\n';
std::chrono::sys_seconds timePoint;
std::istringstream iStream1{"2021-08-11 21:49:35"};
iStream1 >> std::chrono::parse("%F %T", timePoint);
if (iStream1) std::cout << "timePoint: " << timePoint << '\n';
else std::cerr << "timepoint: Reading failed\n";
std::chrono::year_month_day date1;
std::istringstream iStream2{"11/08/21"};
iStream2 >> std::chrono::parse("%x", date1);
if (iStream2) std::cout << "date1: " << date1 << '\n';
else std::cerr << "date1: Reading failed\n";
std::chrono::year_month_day date2;
std::istringstream iStream3{"11/15/21"};
iStream3 >> std::chrono::parse("%x", date2);
if (iStream3) std::cout << "date2: " << date2 << '\n';
else std::cerr << "date2: Reading failed\n";
std::cout << '\n';
}
What will happen next?
Now my precise introduction to the Chrono library is complete. In my next article I will write about concurrency in C++20.
(RME)