Search code examples
c++qtqt4qdatetime

Qt4 problem with formatting hours in QDateTime


I've a problem with following code:

QDateTime test2;
test2.setTime_t(25);
qDebug() << test2.toString("hh:mm:ss");

this prints "01:00:25" to output instead of 00:00:25. Why is the first hour set to 01 instead of 00 ?

I thought that maybe am/pm notation is used so i tried this

QDateTime test2;
test2.setTime_t(3600*22+25);
qDebug() << test2.toString("hh:mm:ss");

And still i received on output

"23:00:25"

Help :)


Solution

  • It's because you didn't set the QDateTime to UTC. So, 00:00:25 on Jan 1st 1970 in UTC time was probably 01:00:25 in your local timezone? And your code says "10:00:25" for me, at UTC+10 :)

    Try this:

    QDateTime test2;
    test2.setTimeSpec(Qt::UTC);    
    test2.setTime_t(25);
    qDebug() << test2.toString("hh:mm:ss");