Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get Date & Time from NTP server and update my system clock.... using QT Widget application
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sekhar
    wrote on last edited by
    #1

    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
    jsulmJ 1 Reply Last reply
    0
    • S Sekhar

      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
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @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?

          S Offline
          S Offline
          Sekhar
          wrote on last edited by Sekhar
          #4

          @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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved