parsing time (only) string with std::chrono
-
Maybe there's a simpler way to do this. Or a good tutorial ? I have a time string formatted like this (with spaces): "07 h 08 min 51 s" According to the [documentation](https://en.cppreference.com/w/cpp/chrono/parse), should the format string should be "%H h %M min %S s" ? And if I understand how this should work, I should be able to do something like that, no? Is there something I am missing ? or just the documentation too obtuse ? const std::string time("07 h 08 min 51 s"); const std::string format("%H h %M min %S s"); std::chrono::time_point tp; std::stringstream iss(time); iss >> std::chrono::parse(format, tp);
CI/CD = Continuous Impediment/Continuous Despair
-
Maybe there's a simpler way to do this. Or a good tutorial ? I have a time string formatted like this (with spaces): "07 h 08 min 51 s" According to the [documentation](https://en.cppreference.com/w/cpp/chrono/parse), should the format string should be "%H h %M min %S s" ? And if I understand how this should work, I should be able to do something like that, no? Is there something I am missing ? or just the documentation too obtuse ? const std::string time("07 h 08 min 51 s"); const std::string format("%H h %M min %S s"); std::chrono::time_point tp; std::stringstream iss(time); iss >> std::chrono::parse(format, tp);
CI/CD = Continuous Impediment/Continuous Despair
You failed to say what the problem or question is. :)
The difficult we do right away... ...the impossible takes slightly longer.
-
Maybe there's a simpler way to do this. Or a good tutorial ? I have a time string formatted like this (with spaces): "07 h 08 min 51 s" According to the [documentation](https://en.cppreference.com/w/cpp/chrono/parse), should the format string should be "%H h %M min %S s" ? And if I understand how this should work, I should be able to do something like that, no? Is there something I am missing ? or just the documentation too obtuse ? const std::string time("07 h 08 min 51 s"); const std::string format("%H h %M min %S s"); std::chrono::time_point tp; std::stringstream iss(time); iss >> std::chrono::parse(format, tp);
CI/CD = Continuous Impediment/Continuous Despair
Your time string doesn't fully specify a time point. You can change it to:
std::chrono::duration d; iss >> std::chrono::parse (format, d);
and
d
is correctly calculated as 25731, which represents your time converted to seconds. In the docs the key point is:Quote:
If
from_stream
fails to parse everything specified by the format string, or if insufficient information is parsed to specify a complete result, or if parsing discloses contradictory information,is.setstate(std::ios_base::failbit)
is called.(highlight is mine).
Mircea
-
Maybe there's a simpler way to do this. Or a good tutorial ? I have a time string formatted like this (with spaces): "07 h 08 min 51 s" According to the [documentation](https://en.cppreference.com/w/cpp/chrono/parse), should the format string should be "%H h %M min %S s" ? And if I understand how this should work, I should be able to do something like that, no? Is there something I am missing ? or just the documentation too obtuse ? const std::string time("07 h 08 min 51 s"); const std::string format("%H h %M min %S s"); std::chrono::time_point tp; std::stringstream iss(time); iss >> std::chrono::parse(format, tp);
CI/CD = Continuous Impediment/Continuous Despair
I found the following article from MS at - 'MS Learn - functions[^], scroll down to the Time of day tab which might be what you are looking for.
-
Your time string doesn't fully specify a time point. You can change it to:
std::chrono::duration d; iss >> std::chrono::parse (format, d);
and
d
is correctly calculated as 25731, which represents your time converted to seconds. In the docs the key point is:Quote:
If
from_stream
fails to parse everything specified by the format string, or if insufficient information is parsed to specify a complete result, or if parsing discloses contradictory information,is.setstate(std::ios_base::failbit)
is called.(highlight is mine).
Mircea
thanks.
CI/CD = Continuous Impediment/Continuous Despair