I can not find anywhere else how to make it so that you can enter the time through ":". For example, 09:45. So that you can then compare the different values ​​of time. For example, so that you can distinguish between 09:20 and 09:30.

  • one
    So record time or enter time? And what is the problem of counting the hours first, then skipping the separator and counting the minutes, saving the result in standard units from <chrono> ? - VTT
  • Is there any way to make this time be in one variable? - Roma Matveyev
  • I have an example from pascal. I would like the same on c ++. Here is the time1 code: = 60 * ((ord (c) -ord ('0')) * 10+ ord (c1) -ord ('0')); - Roma Matveyev
  • This is a very strange Pascale example. .. - Kromster

1 answer 1

You probably need this:

 #include <chrono> void foo(){ using namespace std::chrono_literals; constexpr auto t1 = 2h + 30min + 15s; constexpr auto t2 = t1 - 15s; static_assert(t1 > t2); static_assert(t2 == 2h + 30min); constexpr auto h1 = std::chrono::duration_cast<std::chrono::hours>(t1); // Округляем до часа constexpr auto m1 = std::chrono::duration_cast<std::chrono::minutes>(t1 - h1); // Остаток округряем до минут constexpr auto s1 = std::chrono::duration_cast<std::chrono::seconds>(t1 - (h1 + m1));// Остаток округряем до секунд constexpr int simple_seconds = s1.count(); static_assert(simple_seconds == 15); }