From ec2bf7a57df392039d536fe4d13a90f0fa065603 Mon Sep 17 00:00:00 2001 From: f4exb Date: Tue, 13 Nov 2018 13:19:54 +0100 Subject: [PATCH] New time utility class using std::chrono --- sdrbase/CMakeLists.txt | 2 ++ sdrbase/commands/command.cpp | 17 ++++------------- sdrbase/commands/command.h | 2 -- sdrbase/sdrbase.pro | 2 ++ sdrbase/util/timeutil.cpp | 27 +++++++++++++++++++++++++++ sdrbase/util/timeutil.h | 26 ++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 sdrbase/util/timeutil.cpp create mode 100644 sdrbase/util/timeutil.h diff --git a/sdrbase/CMakeLists.txt b/sdrbase/CMakeLists.txt index 9613b434a..1040d5948 100644 --- a/sdrbase/CMakeLists.txt +++ b/sdrbase/CMakeLists.txt @@ -81,6 +81,7 @@ set(sdrbase_SOURCES util/simpleserializer.cpp #util/spinlock.cpp util/uid.cpp + util/timeutil.cpp plugin/plugininterface.cpp plugin/pluginapi.cpp @@ -204,6 +205,7 @@ set(sdrbase_HEADERS util/simpleserializer.h #util/spinlock.h util/uid.h + util/timeutil.h webapi/webapiadapterinterface.h webapi/webapirequestmapper.h diff --git a/sdrbase/commands/command.cpp b/sdrbase/commands/command.cpp index d1d52c2d9..e9199b4b9 100644 --- a/sdrbase/commands/command.cpp +++ b/sdrbase/commands/command.cpp @@ -21,6 +21,7 @@ #include "command.h" #include "util/simpleserializer.h" +#include "util/timeutil.h" Command::Command() : m_currentProcess(nullptr), @@ -200,7 +201,7 @@ void Command::run(const QString& apiAddress, int apiPort, int deviceSetIndex) connect(m_currentProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStateChanged(QProcess::ProcessState))); m_currentProcess->setProcessChannelMode(QProcess::MergedChannels); - m_currentProcessStartTimeStampms = nowms(); + m_currentProcessStartTimeStampms = TimeUtil::nowms(); m_currentProcess->start(m_currentProcessCommandLine); } @@ -256,7 +257,7 @@ void Command::processStateChanged(QProcess::ProcessState newState) void Command::processError(QProcess::ProcessError error) { //qDebug("Command::processError: %d state: %d", error, m_currentProcessState); - m_currentProcessFinishTimeStampms = nowms(); + m_currentProcessFinishTimeStampms = TimeUtil::nowms(); m_currentProcessError = error; m_isInError = true; @@ -280,7 +281,7 @@ void Command::processError(QProcess::ProcessError error) void Command::processFinished(int exitCode, QProcess::ExitStatus exitStatus) { //qDebug("Command::processFinished: (%d) %d", exitCode, exitStatus); - m_currentProcessFinishTimeStampms = nowms(); + m_currentProcessFinishTimeStampms = TimeUtil::nowms(); m_currentProcessExitCode = exitCode; m_currentProcessExitStatus = exitStatus; m_hasExited = true; @@ -297,13 +298,3 @@ void Command::processFinished(int exitCode, QProcess::ExitStatus exitStatus) m_currentProcess->deleteLater(); // make sure other threads can still access it until all events have been processed m_currentProcess = nullptr; // for this thread it can assume it was deleted } - -uint64_t Command::nowms() -{ - auto now = std::chrono::system_clock::now(); - auto now_ms = std::chrono::time_point_cast(now); - auto epoch = now_ms.time_since_epoch(); - auto value = std::chrono::duration_cast(epoch); - - return value.count(); -} \ No newline at end of file diff --git a/sdrbase/commands/command.h b/sdrbase/commands/command.h index 92e628ead..baae07f71 100644 --- a/sdrbase/commands/command.h +++ b/sdrbase/commands/command.h @@ -91,8 +91,6 @@ public: } private: - uint64_t nowms(); //!< Get now time in milliseconds - QString m_group; QString m_description; QString m_command; diff --git a/sdrbase/sdrbase.pro b/sdrbase/sdrbase.pro index 2cb0d17e4..eb6eed3f8 100644 --- a/sdrbase/sdrbase.pro +++ b/sdrbase/sdrbase.pro @@ -122,6 +122,7 @@ SOURCES += audio/audiodevicemanager.cpp\ util/samplesourceserializer.cpp\ util/simpleserializer.cpp\ util/uid.cpp\ + util/timeutil.cpp\ plugin/plugininterface.cpp\ plugin/pluginapi.cpp\ plugin/pluginmanager.cpp\ @@ -220,6 +221,7 @@ HEADERS += audio/audiodevicemanager.h\ util/samplesourceserializer.h\ util/simpleserializer.h\ util/uid.h\ + util/timeutil.h\ webapi/webapiadapterinterface.h\ webapi/webapirequestmapper.h\ webapi/webapiserver.h\ diff --git a/sdrbase/util/timeutil.cpp b/sdrbase/util/timeutil.cpp new file mode 100644 index 000000000..ce405bcc8 --- /dev/null +++ b/sdrbase/util/timeutil.cpp @@ -0,0 +1,27 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Edouard Griffiths, F4EXB // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include "timeutil.h" + +uint64_t TimeUtil::nowms() +{ + auto now = std::chrono::system_clock::now(); + auto now_ms = std::chrono::time_point_cast(now); + auto epoch = now_ms.time_since_epoch(); + auto value = std::chrono::duration_cast(epoch); + + return value.count(); +} diff --git a/sdrbase/util/timeutil.h b/sdrbase/util/timeutil.h new file mode 100644 index 000000000..cd983a8d7 --- /dev/null +++ b/sdrbase/util/timeutil.h @@ -0,0 +1,26 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Edouard Griffiths, F4EXB // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include + +#include "export.h" + +class SDRBASE_API TimeUtil +{ +public: + static uint64_t nowms(); //!< returns the current epoch in milliseconds +};