Closed wAuner closed 1 year ago
There's no parse
flags to help with this one.
If you know the units you are parsing, it isn't difficult to parse the pieces with something like:
istringstream in{"20ms"};
string units;
int mi;
in >> mi >> units;
milliseconds d{mi};
assert(units == "ms");
If you don't know the units ahead of time, you could parse into nanoseconds
and switch on the units among a small set of assumed units:
istringstream in{"20ms"};
string units;
int i;
in >> i >> units;
nanoseconds d;
if (units == "ms")
d = milliseconds{i};
else if (units == "h")
d = hours{i};
else
cerr << "parse error\n";
Thank you Howard. Now that I see the examples, it really is simple. Thanks for your good work.
Is it possible to parse strings like "20ms", "5ms", "1h" etc. to
chrono::duration
s? I was looking atfrom_stream
andparse
, but I wasn't able to find a promising format specifier. Thanks.