mirror of
https://github.com/znc/znc.git
synced 2026-03-28 17:42:41 +01:00
Centralize logic to get current server time
A few different implementations of computing the current time were spread out through the code base, most of them using gettimeofday(). This centralizes the logic in CUtil::GetTime() for easier maintenance, and also allows all call sites to get the benefit of the clock_gettime() code path on systems that support it.
This commit is contained in:
committed by
Tor Arne Vestbø
parent
852c9832a0
commit
02bfb9eaf5
@@ -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 */
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
#include <znc/ZNCDebug.h>
|
||||
#include <znc/Utils.h>
|
||||
#include <iostream>
|
||||
#include <sys/time.h>
|
||||
#include <stdio.h>
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user