Browse Source

update get timestamp (ns)

xuqiang 4 months ago
parent
commit
d675d0094e
2 changed files with 10 additions and 0 deletions
  1. 2 0
      src/mainwindow.cpp
  2. 8 0
      src/utils.cpp

+ 2 - 0
src/mainwindow.cpp

@@ -9,6 +9,7 @@
 #include "abstractwidget.h"
 #include <QSettings>
 #include <QDateTime>
+#include <time.h>
 
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)
@@ -220,6 +221,7 @@ void MainWindow::onUpdateScreen(const QJsonObject &obj)
 
     qint64 startTime = obj["time_arrived"].toString().toLongLong();
     qint64 endTime = Utils::currentNanoTimestamp();
+    LOG_INFO("startTime: {}, endTime: {}", startTime, endTime);
     LOG_INFO("update screen duration: {}ns", endTime - startTime);
 }
 

+ 8 - 0
src/utils.cpp

@@ -1,6 +1,7 @@
 #include "utils.h"
 #include <QDateTime>
 #include <QElapsedTimer>
+#include <time.h>
 
 Utils::Utils(QObject *parent)
     : QObject{parent}
@@ -15,6 +16,7 @@ QString Utils::timestamp()
 
 qint64 Utils::currentNanoTimestamp()
 {
+#if 0
     static QElapsedTimer timer;
     static qint64 baseNano = 0;
     static bool initialized = false;
@@ -30,4 +32,10 @@ qint64 Utils::currentNanoTimestamp()
 
     // 当前 epoch 纳秒时间戳 = 基准时间 + 已经过的纳秒
     return baseNano + timer.nsecsElapsed();
+#endif
+
+    struct timespec ts;
+    clock_gettime(CLOCK_REALTIME, &ts);
+    qint64 timestamp = ts.tv_sec * 1000000000LL + ts.tv_nsec;
+    return timestamp;
 }