Does anybody know of a fast, portable way for parsing date/time strings which contain fractional seconds?
For example:
2017-10-23T07:23:08.78
Thus far I can parse everything else with:
std::tm tm = {};
std::stringstream datestr("2017-10-23T07:23:08.78");
datestr >> std::get_time(&tm, "%Y-%m-dT%H:%M:%S.??");
The above snippet was modified from this post.
This other post shows the clever trick of parsing the year twice. Unfortunately, since the fractional second is the last field in my representation, it will overwrite whatever valid field I actually want to use.
The reference documentation (here) does not list any conversion specifier for fractional seconds nor does it list any specifier which can be used to match and discard arbitrary digits (same as %t
or %n
for whitespace.
To be clear, I am perfectly OK with discarding this portion of the precision. I would prefer not to manipulate it with raw string functions but that seems the only option I can find.
Finally, this string is coming from a remote server so the format is out of my control. My environment is currently Posix though I would prefer as much portability as reasonably possible.
Edit: This question has been flagged as a duplicate of this. I do not believe this to be so as my question is specifically about parsing / discarding tokens which do not have a character specifier. This is unique from the other post which uses, for example, %z
to parse timezone (of which I am also not interested in). My question is specifically targeted at parsing, not interpretation of the resultant value.