Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get Date & Time from NTP server and update my system clock.... using QT Widget application



  • Can any one tell me how to get Date and Time from NTP server and synchronize with system clock? using QT widget application?
    i got a code from internet but it does not working.....

    mainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    unsigned char buf[10];
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        udpSocket->close();
    }
    
    void MainWindow::changeEvent(QEvent *e)
    {
        QMainWindow::changeEvent(e);
        switch (e->type()) {
        case QEvent::LanguageChange:
            ui->retranslateUi(this);
            break;
        default:
            break;
        }
    }
    
    void MainWindow::on_Update_clicked()
    {
        QHostInfo info = QHostInfo::fromName("time.nist.gov");//("0.pool.ntp.org");
        QString ipAddress = info.addresses().first().toString();
        ui->ipAddress->setText(ipAddress);
        udpSocket = new QUdpSocket(this);
    
        connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
        connect(udpSocket, SIGNAL(connected()), this, SLOT(connectSuccsess()));
        udpSocket->abort();
    //    udpSocket->connectToHost("time.nist.gov",123);//("0.pool.ntp.org", 123);
    
    }
    void MainWindow::readPendingDatagrams()
    {
       // ui->statusLabel->setText("Reading...");
        QByteArray newTime;
        QDateTime Epoch(QDate(1900, 1, 1));
        QDateTime unixStart(QDate(1970, 1, 1));
        do {
            newTime.resize(udpSocket->pendingDatagramSize());
            udpSocket->read(newTime.data(), newTime.size());
        } while(udpSocket->hasPendingDatagrams());
    
        QByteArray TransmitTimeStamp ;
        TransmitTimeStamp = newTime.right(8);
    
        quint64 seconds = 0;
        for (int i=0; i<= 3; ++i)
        {
             seconds = (seconds << 8) | TransmitTimeStamp[i];
        }
        ui->plainTextEdit->appendPlainText(QString::number(seconds, 10));
        QDateTime newestTime;
        newestTime.setTime_t(seconds - Epoch.secsTo(unixStart));
         ui->plainTextEdit->appendPlainText(newestTime.toString());
    }
    void MainWindow::connectSuccsess()
    {
       // ui->time->setText("Connected");
        ui->time->setText("Connected-jkjlk");
        QByteArray timeRequest(48, 0);
        timeRequest[0] = '\x23';
        udpSocket->flush();
        udpSocket->write(timeRequest);
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    MainWindow.h 
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QAbstractSocket>
    #include <QUdpSocket>
    #include <QHostInfo>
    #include <QHostAddress>
    #include <QDateTime>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    protected:
        void changeEvent(QEvent *e);
    
    private slots:
        void on_Update_clicked();
        void readPendingDatagrams();
        void connectSuccsess();
    private:
        Ui::MainWindow *ui;
        QUdpSocket *udpSocket;
    };
    
    #endif // MAINWINDOW_H

  • Qt Champions 2019

    @Sekhar said in How to get Date & Time from NTP server and update my system clock.... using QT Widget application:

    i got a code from internet but it does not working

    Please tell us what exactly does not work, or do you expect us to guess?


  • Lifetime Qt Champion

    Hi,

    And to add to @jsulm, this is usually a task managed by a system service. You will also have to request higher privilege to modify your system's clock.



  • @jsulm
    thanks for your response. i would like to get the date and time from NTP server not form System(Date & Time) this is my primary task. I don't know how to get the data from NTP server.
    and in the above code i was tried but got the wrong timestamp(date and time) like 16th jan 2036.


Log in to reply