Skip to content

Commit

Permalink
Centralize logic to get current server time
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
torarnv committed Jul 5, 2016
1 parent 852c983 commit 02bfb9e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 34 deletions.
10 changes: 2 additions & 8 deletions include/znc/Utils.h
Expand Up @@ -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 */
Expand Down
7 changes: 1 addition & 6 deletions src/Buffer.cpp
Expand Up @@ -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,
Expand Down
14 changes: 1 addition & 13 deletions src/Message.cpp
Expand Up @@ -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() {
Expand Down
25 changes: 25 additions & 0 deletions src/Utils.cpp
Expand Up @@ -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()) {
Expand Down
4 changes: 2 additions & 2 deletions src/ZNCDebug.cpp
Expand Up @@ -15,6 +15,7 @@
*/

#include <znc/ZNCDebug.h>
#include <znc/Utils.h>
#include <iostream>
#include <sys/time.h>
#include <stdio.h>
Expand All @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions test/UtilsTest.cpp
Expand Up @@ -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;
Expand Down

0 comments on commit 02bfb9e

Please sign in to comment.