Qt QDateTime nanoseconds from 1/1/1970
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 (开发者_Python百科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.
精彩评论