HowardHinnant / date

A date and time library based on the C++11/14/17 <chrono> header
Other
3.08k stars 670 forks source link

`s >> date::parse()` does not ignore space as `s >> integer` do #715

Open Charles-lgc opened 2 years ago

Charles-lgc commented 2 years ago

(v3.0.1)

std::istringstream is("123 2020.12.11T09:11:12.123");
is >> integer_a >> date::parse("%Y.%m.%dT%H:%M:%S", timepoint);

This will fail to extract time from the stream. Maybe it is more consistent with the current behavior of operator>>(istream, v) to make date::parse skip "space" chars automatically?

HowardHinnant commented 2 years ago

You can modify the format string to match a wide variety of white space characters:

These can be combined to match even more patterns. For example "%n " matches one or more white space charcters, and "%n%t%t" matches one to three white space characters.

Charles-lgc commented 2 years ago

Thank you Howard, but I'm talking about the convention:

For standard streams, the skipws flag is set on initialization.

So the behavior of is>>date(parse) might be not consistent with this default standard assumption (i.e. skipws is set by default).

HowardHinnant commented 2 years ago

parse is a manipulator for calling from_stream. from_stream is documented to be consistent with other unformatted input functions (e.g. getline). The rationale for choosing this behavior is because the formatting is supplied by the fmt string argument which provides better whitespace control than do the formatted input functions (which have no way to control whitespace in the input other than skipws).

This behavior is now part of C++20, and efforts to change it have to be directed to the C++ Standards Committee (WG21). The "date" library now serves as a preview of this part of C++20 for those clients who can not yet migrate to C++20. Therefore to the extent practical, the date lib will follow the C++20 specification (though there are deviations made for practical reasons).