[SOLVED] Time Conversion
-
wrote on 12 May 2011, 08:04 last edited by
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
-
wrote on 12 May 2011, 08:08 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.
-
wrote on 12 May 2011, 08:27 last edited by
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.
-
wrote on 12 May 2011, 08:50 last edited by
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"?
-
wrote on 12 May 2011, 08:56 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
@ -
wrote on 12 May 2011, 09:24 last edited by
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;
}@
-
wrote on 12 May 2011, 09:47 last edited by
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.
-
wrote on 12 May 2011, 09:50 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(...).
--
-
-
wrote on 12 May 2011, 09:52 last edited by
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.
-
wrote on 12 May 2011, 11:01 last edited by
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.
-
wrote on 12 May 2011, 11:16 last edited by
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.
-
wrote on 23 May 2011, 13:03 last edited by
There is just to add, thx!
-
wrote on 17 Jan 2023, 16:34 last edited by
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");wrote on 15 May 2024, 11:00 last edited by@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");
wrote on 15 May 2024, 11:54 last edited by@__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.
?