diff --git a/timer.cpp b/timer.cpp index bff7e9f..8c45338 100644 --- a/timer.cpp +++ b/timer.cpp @@ -24,16 +24,13 @@ License along with FlashMQ. If not, see . void CallbackEntry::updateExectedAt() { - this->lastExecuted = currentMSecsSinceEpoch(); + this->lastExecuted = std::chrono::steady_clock::now(); } uint64_t CallbackEntry::getNextCallMs() const { - int64_t elapsedSinceLastCall = currentMSecsSinceEpoch() - lastExecuted; - if (elapsedSinceLastCall < 0) // Correct for clock drift - elapsedSinceLastCall = 0; - - int64_t newDelay = this->interval - elapsedSinceLastCall; + const std::chrono::milliseconds elapsedSinceLastCall = std::chrono::duration_cast(std::chrono::steady_clock::now() - lastExecuted); + int64_t newDelay = this->interval - elapsedSinceLastCall.count(); if (newDelay < 0) newDelay = 0; return newDelay; diff --git a/timer.h b/timer.h index 67230f8..2fd1e9b 100644 --- a/timer.h +++ b/timer.h @@ -29,7 +29,7 @@ License along with FlashMQ. If not, see . struct CallbackEntry { - uint64_t lastExecuted = currentMSecsSinceEpoch(); // assume the first one executed to avoid instantly calling it. + std::chrono::time_point lastExecuted = std::chrono::steady_clock::now(); uint64_t interval = 0; std::function f = nullptr; std::string name; diff --git a/utils.cpp b/utils.cpp index 50a57b0..79f78fc 100644 --- a/utils.cpp +++ b/utils.cpp @@ -294,14 +294,6 @@ bool startsWith(const std::string &s, const std::string &needle) return s.find(needle) == 0; } -int64_t currentMSecsSinceEpoch() -{ - struct timeval te; - gettimeofday(&te, NULL); - int64_t milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; - return milliseconds; -} - std::string getSecureRandomString(const ssize_t len) { std::vector buf(len); diff --git a/utils.h b/utils.h index 0906ea7..0a83fa3 100644 --- a/utils.h +++ b/utils.h @@ -65,7 +65,6 @@ void rtrim(std::string &s); void trim(std::string &s); bool startsWith(const std::string &s, const std::string &needle); -int64_t currentMSecsSinceEpoch(); std::string getSecureRandomString(const ssize_t len); std::string str_tolower(std::string s); bool stringTruthiness(const std::string &val);