//******************************************************** // The following code example is taken from the book // C++20 - The Complete Guide // by Nicolai M. Josuttis (www.josuttis.com) // http://www.cppstd20.com // // The code is licensed under a // Creative Commons Attribution 4.0 International License // http://creativecommons.org/licenses/by/4.0/ //******************************************************** #include #include #include #include // parse year-month-day with optional hour:minute and optional :sec // - returns a time_point<> of the passed clock (default: system_clock) // in seconds template auto parseDateTime(const std::string& s) { // return value: std::chrono::time_point tp; // string stream to read from: std::istringstream sstrm{s}; // no string_view support auto posColon = s.find(":"); if (posColon != std::string::npos) { if (posColon != s.rfind(":")) { // multiple colons: std::chrono::from_stream(sstrm, "%F %T", tp); } else { // one colon: std::chrono::from_stream(sstrm, "%F %R", tp); } } else { // no colon: std::chrono::from_stream(sstrm, "%F", tp); } // handle invalid formats: assert((!sstrm.fail())); return tp; }