Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert milliseconds to hours minutes seconds
QtWS25 Last Chance

Convert milliseconds to hours minutes seconds

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 5.3k Views
  • 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.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1

    I want to convert milliseconds to hours minutes seconds.
    There is also a way to do it manually, but I wonder if there is a library within qt.

    If you take 362439000 milliseconds as input and convert it to hours minutes and seconds,
    You should get a 99:99:99 result.

    fromTime_t, fromMSecsSinceStartOfDay
    I tried using the function, but it didn't give me the desired result.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #5

      Qt has nothing to do with recognising std::chrono. This between your compiler, which appears to be a Microsoft one, and you. The compiler is finding std::chrono just fine.

      duration_cast<std::chrono::seconds>(123123);  // error C2672
      

      The specific error message C2672: The compiler could not find an overloaded function that matches the specified function. No function was found that takes matching parameters, or no matching function has the required accessibility in context.

      This is unsurprising: the argument to the function is not a duration and there's no way to implicitly convert the int provided.
      (BTW: You are discarding the result, so this is not overly useful even if it compiled.)

      If you take 362439000 milliseconds as input and convert it to hours minutes and seconds,
      You should get a 99:99:99 result.

      Really? How many seconds are in a minute, or minutes in an hour?

      #include <chrono>
      #include <iostream>
      
      using namespace std::chrono;
      
      int main() {
        milliseconds ms(362439000);
        std::cout << duration_cast<hours>(ms).count() << ':'
                  << duration_cast<minutes>(ms).count() % 60 << ':'
                  << duration_cast<seconds>(ms).count() % 60;
      }
      

      Output:

      100:40:39
      
      I 1 Reply Last reply
      2
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #2

        std::chrono

        1 Reply Last reply
        0
        • I Offline
          I Offline
          IknowQT
          wrote on last edited by
          #3

          I looked at std:crono.
          qt doesn't recognize chrono, what should I do?

          
          #include <QObject>
          #include <chrono>
          
          using namespace std::chrono;
          
          class cDateTime
          {
          public:
          	cDateTime();
          	~cDateTime();
          };
          cDateTime::cDateTime()
          {
          	auto milliseconds = 362439000;	
          	duration_cast<std::chrono::seconds>(123123);  // error C2672
          }
          
          jsulmJ 1 Reply Last reply
          0
          • I IknowQT

            I looked at std:crono.
            qt doesn't recognize chrono, what should I do?

            
            #include <QObject>
            #include <chrono>
            
            using namespace std::chrono;
            
            class cDateTime
            {
            public:
            	cDateTime();
            	~cDateTime();
            };
            cDateTime::cDateTime()
            {
            	auto milliseconds = 362439000;	
            	duration_cast<std::chrono::seconds>(123123);  // error C2672
            }
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #4

            @IknowQT said in Convert milliseconds to hours minutes seconds:

            what should I do?

            Provide the error text instead of a number...
            Read C++ stdlib documentation, this has nothing to do with Qt (https://en.cppreference.com/w/cpp/chrono/duration, https://en.cppreference.com/w/cpp/chrono/duration/duration_cast).

            std::chrono::milliseconds milliseconds = 362439000;
            duration_cast<std::chrono::seconds>(milliseconds);
            

            Or

            duration_cast<std::chrono::seconds>(std::chrono::milliseconds(123123));
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            4
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #5

              Qt has nothing to do with recognising std::chrono. This between your compiler, which appears to be a Microsoft one, and you. The compiler is finding std::chrono just fine.

              duration_cast<std::chrono::seconds>(123123);  // error C2672
              

              The specific error message C2672: The compiler could not find an overloaded function that matches the specified function. No function was found that takes matching parameters, or no matching function has the required accessibility in context.

              This is unsurprising: the argument to the function is not a duration and there's no way to implicitly convert the int provided.
              (BTW: You are discarding the result, so this is not overly useful even if it compiled.)

              If you take 362439000 milliseconds as input and convert it to hours minutes and seconds,
              You should get a 99:99:99 result.

              Really? How many seconds are in a minute, or minutes in an hour?

              #include <chrono>
              #include <iostream>
              
              using namespace std::chrono;
              
              int main() {
                milliseconds ms(362439000);
                std::cout << duration_cast<hours>(ms).count() << ':'
                          << duration_cast<minutes>(ms).count() % 60 << ':'
                          << duration_cast<seconds>(ms).count() % 60;
              }
              

              Output:

              100:40:39
              
              I 1 Reply Last reply
              2
              • C ChrisW67

                Qt has nothing to do with recognising std::chrono. This between your compiler, which appears to be a Microsoft one, and you. The compiler is finding std::chrono just fine.

                duration_cast<std::chrono::seconds>(123123);  // error C2672
                

                The specific error message C2672: The compiler could not find an overloaded function that matches the specified function. No function was found that takes matching parameters, or no matching function has the required accessibility in context.

                This is unsurprising: the argument to the function is not a duration and there's no way to implicitly convert the int provided.
                (BTW: You are discarding the result, so this is not overly useful even if it compiled.)

                If you take 362439000 milliseconds as input and convert it to hours minutes and seconds,
                You should get a 99:99:99 result.

                Really? How many seconds are in a minute, or minutes in an hour?

                #include <chrono>
                #include <iostream>
                
                using namespace std::chrono;
                
                int main() {
                  milliseconds ms(362439000);
                  std::cout << duration_cast<hours>(ms).count() << ':'
                            << duration_cast<minutes>(ms).count() % 60 << ':'
                            << duration_cast<seconds>(ms).count() % 60;
                }
                

                Output:

                100:40:39
                
                I Offline
                I Offline
                IknowQT
                wrote on last edited by
                #6

                @ChrisW67

                I searched for it in various routes and the result is the same as the result you posted.

                99 hours 356400000
                99 Min 5940000
                99 sec 99000

                Add them all up and you get 362439000.
                I expected 99:99:99 to come out. It is disappointing that the expected results are different.

                J.HilkJ 1 Reply Last reply
                0
                • I IknowQT

                  @ChrisW67

                  I searched for it in various routes and the result is the same as the result you posted.

                  99 hours 356400000
                  99 Min 5940000
                  99 sec 99000

                  Add them all up and you get 362439000.
                  I expected 99:99:99 to come out. It is disappointing that the expected results are different.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #7

                  @IknowQT said in Convert milliseconds to hours minutes seconds:

                  99 hours 356400000
                  99 Min 5940000
                  99 sec 99000
                  Add them all up and you get 362439000.
                  I expected 99:99:99 to come out.

                  thats not how time works.

                  the highest factor, hours in your case, will always have to overflow numbers, where as minutes and seconds will be restrained between 0 and 59


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  4

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved