Time in C++: Parsing formatted input

0
30
Time in C++: Parsing formatted input




This article is the eleventh in my detailed journey through the chrono extensions in C++20.

Advertisement


  1. Time in C++20: An introduction to the chrono terminology
  2. Time in C++20: Introducing chronological terminology with time span and time point
  3. Time in C++20: new data types for time of day and calendar date
  4. Time in C++20: creating calendar events
  5. Time in C++20: display and check calendar appointments
  6. Time in C++20: query calendar dates and ordinal dates
  7. Time in C++20: details about working with time zones
  8. Time Zones in C++20: Online Classes
  9. Time in C++20: Chrono I/O
  10. 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.

Disagreements on water and about water: The Java community is leading the way in innovationDisagreements on water and about water: The Java community is leading the way in innovation

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 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 isa 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.



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';

}

Now my precise introduction to the Chrono library is complete. In my next article I will write about concurrency in C++20.


(RME)

Product Staff: Don’t be a lone warrior, reach out and get help!Product Staff: Don’t be a lone warrior, reach out and get help!

LEAVE A REPLY

Please enter your comment!
Please enter your name here