From 6ff23247ec35956f7fff6360d8905eab08479feb Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Fri, 9 Jun 2023 16:47:06 +0300 Subject: [PATCH] - minor refactoring - renamed TimeHelper to chronox --- src/engine/helper/HL_Sync.cpp | 36 ++++++++++++++++++++++++++--------- src/engine/helper/HL_Sync.h | 8 +++++++- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/engine/helper/HL_Sync.cpp b/src/engine/helper/HL_Sync.cpp index 83768277..9344d1d3 100644 --- a/src/engine/helper/HL_Sync.cpp +++ b/src/engine/helper/HL_Sync.cpp @@ -77,19 +77,21 @@ uint64_t ThreadHelper::getCurrentId() // ------------------- TimeHelper --------------- using namespace std::chrono; +// Milliseconds starting from the epoch static uint64_t TimestampStartPoint = duration_cast(steady_clock::now().time_since_epoch()).count(); + +// Seconds starting from the epoch static time_t TimestampBase = time(nullptr); -uint64_t TimeHelper::getTimestamp() +// Returns number of milliseconds starting from 01 Jan 1970 GMT +uint64_t chronox::getTimestamp() { time_point t = steady_clock::now(); - uint64_t ms = duration_cast< milliseconds >(t.time_since_epoch()).count(); - return ms - TimestampStartPoint + TimestampBase * 1000; } -uint64_t TimeHelper::getUptime() +uint64_t chronox::getUptime() { time_point t = steady_clock::now(); @@ -98,7 +100,7 @@ uint64_t TimeHelper::getUptime() return ms - TimestampStartPoint; } -uint32_t TimeHelper::getDelta(uint32_t later, uint32_t earlier) +uint32_t chronox::getDelta(uint32_t later, uint32_t earlier) { if (later > earlier) return later - earlier; @@ -109,14 +111,30 @@ uint32_t TimeHelper::getDelta(uint32_t later, uint32_t earlier) return 0; } -TimeHelper::ExecutionTime::ExecutionTime() +timespec chronox::toTimespec(uint64_t milliseconds) { - mStart = TimeHelper::getTimestamp(); + timespec r; + r.tv_sec = milliseconds / 1000; + r.tv_nsec = milliseconds % 1000; + r.tv_nsec *= 1000 * 1000; + return r; } -uint64_t TimeHelper::ExecutionTime::getSpentTime() const +int64_t chronox::getDelta(const timespec& a, const timespec& b) { - return TimeHelper::getTimestamp() - mStart; + uint64_t ms_a = a.tv_sec * 1000 + a.tv_nsec / 10000000; + uint64_t ms_b = b.tv_sec * 1000 + a.tv_nsec / 10000000; + return ms_a - ms_b; +} + +chronox::ExecutionTime::ExecutionTime() +{ + mStart = chronox::getTimestamp(); +} + +uint64_t chronox::ExecutionTime::getSpentTime() const +{ + return chronox::getTimestamp() - mStart; } // --------------- BufferQueue ----------------- diff --git a/src/engine/helper/HL_Sync.h b/src/engine/helper/HL_Sync.h index c739916d..fbebf184 100644 --- a/src/engine/helper/HL_Sync.h +++ b/src/engine/helper/HL_Sync.h @@ -46,7 +46,7 @@ public: static uint64_t getCurrentId(); }; -class TimeHelper +class chronox { public: // Returns current timestamp in milliseconds @@ -59,6 +59,12 @@ public: // Handles cases when clock is wrapped. static uint32_t getDelta(uint32_t later, uint32_t earlier); + // Converts number of milliseconds starting from Epoch begin to timespec. + static timespec toTimespec(uint64_t milliseconds); + + // Returns difference between timestamps in milliseconds + static int64_t getDelta(const timespec& a, const timespec& b); + class ExecutionTime { public: