mirror of
https://github.com/znc/znc.git
synced 2026-05-03 03:52:33 +02: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
@@ -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()) {
|
||||
|
||||
Reference in New Issue
Block a user