문제

I am about to read data From a File which has stored it's time in nanoseconds from 1/1/1970. My problem is I want to read it to a QDateTime object, but it simply does not work as I want it to and the Qt Documentation did not help me either.

Note: milliseconds raster is enough for my purposes Here my current approach:

void setDateTime(qint64 &ns)
{
    _datetime.setDate(QDate(1970,1,1));
    _datetime.setTime(QTime(0,0,0,0));
    ns /= 1000; //ns are now ms
    qDebug() << "| ms = " << ns;
    qDebug() << "| days = " << static_cast<int>(ns%(60*60*24*1E6));
    _datetime.addDays( static_cast<int>(ns%(60*60*24*1000)) );
    _datetime.addMSecs( ns - ((ns/(60*60*24*1000))*60*60*24*1E6) );
    qDebug() << "| dt = " << _datetime;
}

the result is always

 | dt =  QDateTime("Thu Jan 1 00:00:00 1970") 

which is surely wrong

Can anybody tell where my flaw is? Thanks for any tips and help.

Edit: setTime_t is obviously what I wanted (except for msec resolution), and that works as expected, but I am really curious why the above approach does not work.

Edit changed hack-away bug from 1E6 multiplicative to 1E6

도움이 되었습니까?

해결책

QDateTime::addDays() and QDateTime::addMSecs() are const functions returning a new QDateTime. You're simply throwning the return value away.

And yes, this is written in the documentation.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top