1

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

drahnr
  • 6,782
  • 5
  • 48
  • 75
  • 2
    Note that 1000ns is 1us (microsecond), and QDateTime methods uses miliseconds. So you should probably divide ns by 1000000 instead od 1000. – chalup Jul 01 '10 at 09:43
  • You're right, but this was fixed in my code, just forgot to change in that "hackaway" – drahnr Jul 01 '10 at 20:08

1 Answers1

4

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.

gnud
  • 77,584
  • 5
  • 64
  • 78