[SOLVED] Time Conversion
-
Hi there
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.
Thx
-
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"?
-
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)
{
QString formattedTime;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;
}@
-
-
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(...).
--
-
-
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 codeFinally 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!
-
QTime n(0,0,0,0);
QTime t=n.addMSecs(value);
QString result=t.toString("hh:mm:ss.zzz"); -
QTime n(0,0,0,0);
QTime t=n.addMSecs(value);
QString result=t.toString("hh:mm:ss.zzz");@NIvil-Wilson said in [SOLVED] Time Conversion:
QTime n(0,0,0,0);
QTime t=n.addMSecs(value);
QString result=t.toString("hh:mm:ss.zzz");This is the best way to do it in modern Qt versions.
Unfortunately, you cannot construct aQTime
object directly from a millisecond value and we have to rely on theaddMSecs
method.Heres my slightly shorter take:
QString result = QTime(0,0).addMSecs(value).toString("hh:mm:ss.zzz");
-
@NIvil-Wilson said in [SOLVED] Time Conversion:
QTime n(0,0,0,0);
QTime t=n.addMSecs(value);
QString result=t.toString("hh:mm:ss.zzz");This is the best way to do it in modern Qt versions.
Unfortunately, you cannot construct aQTime
object directly from a millisecond value and we have to rely on theaddMSecs
method.Heres my slightly shorter take:
QString result = QTime(0,0).addMSecs(value).toString("hh:mm:ss.zzz");
@__mk__ said in [SOLVED] Time Conversion:
Unfortunately, you cannot construct a QTime object directly from a millisecond value
What about (untested) QTime QTime::fromMSecsSinceStartOfDay(int msecs)
Returns a new QTime instance with the time set to the number of msecs since the start of the day, i.e. since 00:00:00.
?