Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. parsing time (only) string with std::chrono

parsing time (only) string with std::chrono

Scheduled Pinned Locked Moved C / C++ / MFC
c++comdevopsjsontutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    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

    Richard Andrew x64R Mircea NeacsuM A 3 Replies Last reply
    0
    • M Maximilien

      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

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      You failed to say what the problem or question is. :)

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • M Maximilien

        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

        Mircea NeacsuM Offline
        Mircea NeacsuM Offline
        Mircea Neacsu
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • M Maximilien

          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

          A Offline
          A Offline
          Andre Oosthuizen
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            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

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            thanks.

            CI/CD = Continuous Impediment/Continuous Despair

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups