一、前言
很多軟件都有時間同步的功能,尤其是Qt在嵌入式設備上的,有時候還有很多是沒有UI界面的程序,而硬件上有個時鍾,時間久了難免沒有電,需要從服務器來同步時間來保證本地的時間是正確的,不然本地記錄的一些日志的時間都是不正確的,很多還可能是1970年的。
NTP同步時間是個標准的協議,使用的端口是123端口,這個端口很牛逼,居然霸占了123這個端口,碉堡!使用NTP服務同步時間,需要設置個時間服務器IP地址,這個地址可以網上找到很多的,微軟自帶的那個有時候行有時候不行,因為默認用的UDP協議,所以是不可靠的,有丟包的可能,建議選擇一些國內的時間服務器,比如一些大學的時間服務器,還是比較准確可靠的。
二、代碼思路
NtpClient::NtpClient(QObject *parent) : QObject(parent)
{
ntpIP = "202.120.2.101";
udpSocket = new QUdpSocket(this);
connect(udpSocket, SIGNAL(connected()), this, SLOT(sendData()));
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
}
void NtpClient::sendData()
{
qint8 LI = 0;
qint8 VN = 3;
qint8 MODE = 3;
qint8 STRATUM = 0;
qint8 POLL = 4;
qint8 PREC = -6;
QDateTime epoch(QDate(1900, 1, 1));
qint32 second = quint32(epoch.secsTo(QDateTime::currentDateTime()));
qint32 temp = 0;
QByteArray timeRequest(48, 0);
timeRequest[0] = (LI << 6) | (VN << 3) | (MODE);
timeRequest[1] = STRATUM;
timeRequest[2] = POLL;
timeRequest[3] = PREC & 0xff;
timeRequest[5] = 1;
timeRequest[9] = 1;
timeRequest[40] = (temp = (second & 0xff000000) >> 24);
temp = 0;
timeRequest[41] = (temp = (second & 0x00ff0000) >> 16);
temp = 0;
timeRequest[42] = (temp = (second & 0x0000ff00) >> 8);
temp = 0;
timeRequest[43] = ((second & 0x000000ff));
udpSocket->write(timeRequest);
}
void NtpClient::readData()
{
QByteArray newTime;
QDateTime epoch(QDate(1900, 1, 1));
QDateTime unixStart(QDate(1970, 1, 1));
while (udpSocket->hasPendingDatagrams()) {
newTime.resize(udpSocket->pendingDatagramSize());
udpSocket->read(newTime.data(), newTime.size());
};
QByteArray transmitTimeStamp ;
transmitTimeStamp = newTime.right(8);
quint32 seconds = transmitTimeStamp.at(0);
quint8 temp = 0;
for (int i = 1; i <= 3; i++) {
seconds = (seconds << 8);
temp = transmitTimeStamp.at(i);
seconds = seconds + temp;
}
QDateTime dateTime;
dateTime.setTime_t(seconds - epoch.secsTo(unixStart));
#ifdef __arm__
#ifdef arma9
dateTime = dateTime.addSecs(60 * 60 * 8);
#endif
#endif
udpSocket->disconnectFromHost();
//有些時候返回的數據可能有誤或者解析不正確,導致填充的時間不正確
if (dateTime.isValid()) {
emit receiveTime(dateTime);
}
}
void NtpClient::setNtpIP(const QString &ntpIP)
{
if (this->ntpIP != ntpIP) {
this->ntpIP = ntpIP;
}
}
void NtpClient::getDateTime()
{
udpSocket->abort();
udpSocket->connectToHost(ntpIP, 123);
}
三、效果圖
四、開源主頁
以上作品完整源碼下載都在開源主頁,會持續不斷更新作品數量和質量,歡迎各位關注。