Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[SOLVED] Time Conversion
just this short question. How is it possible to convert an amount of milliseconds into a formatted QString like hours:minutes:secons:milliseconds. Important is that it should have nothing to do with a normal day-stamp. The amount of hours should be able to go into infinite and not just to 24.
DenisKormalev last edited by
Right way I think will be to add big number methematics here and get hours, minutes, seconds and msecs by simple division.
You could use a class that I wrote (together with ZapB) and that we hope will be in Qt 4.8: QTimeSpan. You can find the sources on "Gitorious":https://qt.gitorious.org/+qt-interest/qt/qt-interest-qt/commit/c0b04af873df049407ae740ef396c8e252c942bc. It provides all you need, and supports everything from milliseconds to years.
Ok, therefore I have an other question. How can I convert a integer to a QString with fixed length where the missing numbers are replaced with zeros? E.g. the int value 6 converts into a String "06"?
giesbert last edited by
You can use QString::arg like here:
QString text = QString("%1").arg(intVal, // value
6, // field width
10, // base for conversion
QLatin1Char('0')); // fill char
Thx a lot. I tried to implement the QTimeSpan class into my creator but it isn't recognized by it. Anyway, if it's within QT 4.8, I can wait, until then I solved it like this:
@QString timeConversion::timeConversion(int msecs)
int hours = msecs/(1000*60*60); int minutes = (msecs-(hours*1000*60*60))/(1000*60); int seconds = (msecs-(minutes*1000*60)-(hours*1000*60*60))/1000; int milliseconds = msecs-(seconds*1000)-(minutes*1000*60)-(hours*1000*60*60); formattedTime.append(QString("%1").arg(hours, 2, 10, QLatin1Char('0')) + ":" + QString( "%1" ).arg(minutes, 2, 10, QLatin1Char('0')) + ":" + QString( "%1" ).arg(seconds, 2, 10, QLatin1Char('0')) + ":" + QString( "%1" ).arg(milliseconds, 3, 10, QLatin1Char('0'))); return formattedTime;
No, QTimeSpan is not in Qt yet, but you can simply copy/paste the code out. I do that too in my current project. I can make the code available for easy inclusion in current projects, if you like.
Guest last edited by
If the idea is to get time and date like "Thu May 12 11:50:21 CEST 2011", I'd use time(...) to get the time, then ctime(...) to get it as a char*, then QString(...) to bring it back to Qt.
If some other format is needed, I'd use time(...), then gmtime(...) or localtime(...), then sprintf(...), then QString(...).
No, the idea was to get a good format for a period of time, not for a point in time. Hours might be more than 24, for instance.
Well, I don't know much "around" QT and the Creator. I've tried to copy the code where your link leads to in my QT Folder.
src/corelib/global/qnamespace.h <- added the necessary code
src/corelib/tools/qtimespan.cpp <- downloaded and copied into "tools"
src/corelib/tools/qtimespan.h <- downloaded and copied into "tools"
src/corelib/tools/tools.pri <- added necessary code
Finally tried to #include "qtimespan.h" in my project but it didn't recognized anything.
OK, I have created an "easily downloadable link":http://dl.dropbox.com/u/16442531/qtimespan.zip for a package you can just use until the time it is in part of Qt proper.
You can just put the .h and the .cpp files in your sources directory, and add them to your .pro file. Then, where you want to use QTimeSpan, just #include "qtimespan.h".
Note that doing this, will generate a compilation warning about a redefinition of Q_EXPORT. It is save to ignore that in this case. \
Instead of including the files directly in your .pro file, you can also use the include() directive to include the .pri file in your project.
There is just to add, thx!