Convert milliseconds to hours minutes seconds
-
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. -
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
-
std::chrono
-
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 }
-
@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));
-
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 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 99000Add them all up and you get 362439000.
I expected 99:99:99 to come out. It is disappointing that the expected results are different. -
@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