C++20 introduces std::chrono::from_stream() to parse date/time from a string and store it in a std::chrono::time_point. For example:
std::stringstream ss("2018-12-09T00:00:00+0130");
std::chrono::time_point<std::chrono::utc_clock> tp;
if (std::chrono::from_stream(ss, "%FT%T%z", tp))
{
std::cout << "Time point: " << tp << '\n';
}
else
std::cout << "Not a valid timestamp\n";
However, GCC prior to version 14, as well as clang up to at least version 20.1, have not implemented this. Attempts to use from_stream result in a compiler error:
error: ‘from_stream’ is not a member of ‘std::chrono’
What workarounds are available in these versions? I can easily parse year/month/day/hour/minute/second/UTC offset (hours/minutes) into individual variables or a std::tm, but there doesn’t seem to be a straightforward way to construct a time_point from that.
system_clock has from_time_t() (utc_clock doesn’t), but that requires me to use legacy C code, which has its own flaws such as not being thread-safe.
How can I construct a std::chrono::time_point from pre-parsed field values, in a way that’s portable and doesn’t require new dependencies?
date.hinstead.YYYY-MM-dd HH:mm:ss +/-HH:mm. No non-Gregorian calendars, no localized month or weekday names, no pretty timezone names and whatever else a full-blown parser would need to support.<chrono>library. His implementation is the reference forstd. You won't get any better advice than the<date.h>.date.h. When you later upgrade to a compiler withstd::chrono::from_streamyou can simply replacedate::withstd::chrono::.