Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the cleanest way to translate 42010958 Milliseconds to Hours:Minutes:Seconds in Qt?

The QTime Class offers me a bit of a pickle insofar as it does not allow me to set milliseconds above certain values, thus making an entry like this invalid.:

    QTime time;
    time.setHMS(0,0,0,42010958); // I normally use a variable

Considering that the range of milliseconds I'm dealing with is between about 1000 and 1000000000, I'm not terribly keen on writing a tremendous amount of of integer conversion code to sanitise each entry, but I will do what I have to do.

What is the cleanest way to convert 42010958 Milliseconds into Hours:Minutes:Seconds in Qt?

like image 702
Anon Avatar asked Dec 11 '22 14:12

Anon


2 Answers

"Cleanest" is a matter of taste, but here's how I would do it:

int milliseconds = 42010958;
int seconds      = milliseconds / 1000;
milliseconds     = milliseconds % 1000;
int minutes      = seconds / 60; 
seconds          = seconds % 60;
int hours        = minutes / 60;
minutes          = minutes % 60;

QTime time;
time.setHMS(hours, minutes, seconds, milliseconds);
like image 109
Jeremy Friesner Avatar answered Dec 14 '22 23:12

Jeremy Friesner


You can use QTime::fromMSecsSinceStartOfDay.

#include <QtCore>

int main(int argc, char *argv[])
{
    QTime time = QTime::fromMSecsSinceStartOfDay(42010958);
    qDebug() << time.toString("hh:mm:ss:zzz");
    return EXIT_SUCCESS;
}
like image 36
thuga Avatar answered Dec 15 '22 00:12

thuga