绝代码农 发表于 2021-7-16 20:20:04

i.mx287学习笔记5-Time

1.网络时间接口下面介绍几个常用的时间同步服务器的域名及IP地址:
域名    IP地址time-a.nist.gov129.6.15.28time-b.nist.gov129.6.15.29time-a.timefreq.bldrdoc.gov 132.163.4.101time-b.timefreq.bldrdoc132.163.4.102time-c.timefreq.bldrdoc.gov132.163.4.103utcnist.colorado.edu128.138.140.44time.nist.gov192.43.244.18time-nw.nist.gov 131.107.1.10nist1.datum.com66.243.43.21nist1-dc.glassey.com 216.200.93.8nist1-ny.glassey.com208.184.49.9nist1-sj.glassey.com207.126.98.204nist1.aol-ca.truetime.com207.200.81.113nist1.aol-va.truetime.com205.188.185.33国家授时210.72.145.44
 
2.QT电脑端测试
源码
QStringList net_time;
    QTcpSocket *socket = new QTcpSocket();
    socket->connectToHost("time.nist.gov", 13);

    if (socket->waitForConnected())
    {
      if (socket->waitForReadyRead())
      {
            QString str(socket->readAll());
            str = str.trimmed();
            str = str.section(" ", 1, 2);
            qDebug() <<"Time:"<< str;
      }
    }
    else
    {
      qDebug("Connected:err");
    }
    socket->close();
    delete socket;
3.显示系统时间初始化
QTimer *timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(timerUpdate()));
    timer->start(1000);显示:
void MainWindow::timerUpdate(void)
{
    QDateTime time = QDateTime::currentDateTime();
    QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
    ui->label->setText(str);
}
效果:

4.开启定时器初始化
qsrand(time(0));
    startTimer(1000);源码
void MainWindow::timerEvent(QTimerEvent *t)
{
    *t =*t;
    QString ab = QString::number(count);

    ui->label_2->setText(ab);
    count++;
}
效果,1秒自加一次

 

文档来源:51CTO技术博客https://blog.51cto.com/u_15294654/3112500
页: [1]
查看完整版本: i.mx287学习笔记5-Time