void TimeSync::getNTPTime() { QUdpSocket sock; sock.connectToHost("0.pool.ntp.org",123); if(!sock.waitForConnected(1000))return; QByteArray data(48,char(0)); *(reinterpret_cast<qint32 *>(&data.data()[0]))=4194959577; if(sock.write(data)<0||!sock.waitForReadyRead(3000)||sock.bytesAvailable()!=48)return; data=sock.readAll(); quint32 seconds=qToBigEndian(*(reinterpret_cast<quint32 *>(&data.data()[40]))); quint32 fraction=qToBigEndian(*(reinterpret_cast<quint32 *>(&data.data()[44]))); quint32 newTime=QDateTime::fromMSecsSinceEpoch(seconds*1000ll+fraction*1000ll/0x100000000ll-2208988800000ll).toTime_t(); if(newTime<1451606400 || newTime>4000000000){ QThread::msleep(500); emit startSync(); return; } qint32 tempTimeShift=qint64(newTime)-qint64(QDateTime::currentDateTime().toTime_t()); if(timeShift!=0)tempTimeShift=qint32((qint64(timeShift)+qint64(tempTimeShift))/2); if(tempTimeShift>3600 || tempTimeShift<-3600){ static bool showMessage=true; if(showMessage)emit warningMessage(julyTr("TIME_SYNC_ERROR","Your clock is not set. Please close the Qt Bitcoin Trader and set the clock. Changing time at Qt Bitcoin Trader enabled can cause errors and damage the keys.")); showMessage=false; } else timeShift.fetchAndStoreOrdered(tempTimeShift); getNTPTimeRetryCount++; if(getNTPTimeRetryCount<3)emit startSync(); }