diff --git a/include/znc/Utils.h b/include/znc/Utils.h index 5bcb6a4b..83127928 100644 --- a/include/znc/Utils.h +++ b/include/znc/Utils.h @@ -70,14 +70,8 @@ class CUtils { unsigned int uMin = 0, unsigned int uMax = ~0, unsigned int uDefault = ~0); - static unsigned long long GetMillTime() { - struct timeval tv; - unsigned long long iTime = 0; - gettimeofday(&tv, nullptr); - iTime = (unsigned long long)tv.tv_sec * 1000; - iTime += ((unsigned long long)tv.tv_usec / 1000); - return iTime; - } + static timeval GetTime(); + static unsigned long long GetMillTime(); #ifdef HAVE_LIBSSL static void GenerateCert(FILE* pOut, const CString& sHost = ""); #endif /* HAVE_LIBSSL */ diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 065c9e3c..a438fd18 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -37,12 +37,7 @@ CBufLine::CBufLine(const CString& sFormat, const CString& sText, CBufLine::~CBufLine() {} void CBufLine::UpdateTime() { - timeval tv; - if (0 != gettimeofday(&tv, nullptr)) { - tv.tv_sec = time(nullptr); - tv.tv_usec = 0; - } - m_Message.SetTime(tv); + m_Message.SetTime(CUtils::GetTime()); } CMessage CBufLine::ToMessage(const CClient& Client, diff --git a/src/Message.cpp b/src/Message.cpp index 426d009f..1eba534b 100644 --- a/src/Message.cpp +++ b/src/Message.cpp @@ -216,19 +216,7 @@ void CMessage::InitTime() { return; } -#ifdef HAVE_CLOCK_GETTIME - timespec ts; - if (clock_gettime(CLOCK_REALTIME, &ts) == 0) { - m_time.tv_sec = ts.tv_sec; - m_time.tv_usec = ts.tv_nsec / 1000; - return; - } -#endif - - if (gettimeofday(&m_time, nullptr)) { - m_time.tv_sec = time(nullptr); - m_time.tv_usec = 0; - } + m_time = CUtils::GetTime(); } void CMessage::InitType() { diff --git a/src/Utils.cpp b/src/Utils.cpp index 8ccd6baf..ab259664 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -418,6 +418,31 @@ inline CString FixGMT(CString sTZ) { } } // namespace +timeval CUtils::GetTime() { +#ifdef HAVE_CLOCK_GETTIME + timespec ts; + if (clock_gettime(CLOCK_REALTIME, &ts) == 0) { + return { ts.tv_sec, ts.tv_nsec / 1000 }; + } +#endif + + struct timeval tv; + if (gettimeofday(&tv, nullptr) == 0) { + return tv; + } + + // Last resort, no microseconds + return { time(nullptr), 0 }; +} + +unsigned long long CUtils::GetMillTime() { + struct timeval tv = GetTime(); + unsigned long long iTime = 0; + iTime = (unsigned long long)tv.tv_sec * 1000; + iTime += ((unsigned long long)tv.tv_usec / 1000); + return iTime; +} + CString CUtils::CTime(time_t t, const CString& sTimezone) { char s[30] = {}; // should have at least 26 bytes if (sTimezone.empty()) { diff --git a/src/ZNCDebug.cpp b/src/ZNCDebug.cpp index dfcd8651..08edd6bd 100644 --- a/src/ZNCDebug.cpp +++ b/src/ZNCDebug.cpp @@ -15,6 +15,7 @@ */ #include +#include #include #include #include @@ -29,8 +30,7 @@ bool CDebug::debug = #endif CDebugStream::~CDebugStream() { - timeval tTime; - gettimeofday(&tTime, nullptr); + timeval tTime = CUtils::GetTime(); time_t tSec = (time_t)tTime.tv_sec; // some systems (e.g. openbsd) define // tv_sec as long int instead of time_t tm tM; diff --git a/test/UtilsTest.cpp b/test/UtilsTest.cpp index cbfa49e9..bdd1fde6 100644 --- a/test/UtilsTest.cpp +++ b/test/UtilsTest.cpp @@ -95,11 +95,7 @@ TEST(UtilsTest, ServerTime) { CString str1 = CUtils::FormatServerTime(tv1); EXPECT_EQ("2011-10-19T16:40:51.620Z", str1); - timeval now; - if (gettimeofday(&now, nullptr)) { - now.tv_sec = time(nullptr); - now.tv_usec = 0; - } + timeval now = CUtils::GetTime(); // Strip microseconds, server time is ms only now.tv_usec = (now.tv_usec / 1000) * 1000;