summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--include/anope.h4
-rw-r--r--include/sysconf.h.cmake3
-rw-r--r--src/init.cpp1
-rw-r--r--src/logger.cpp11
-rw-r--r--src/main.cpp5
-rw-r--r--src/misc.cpp23
-rw-r--r--src/socketengines/epoll.cpp2
-rw-r--r--src/socketengines/kqueue.cpp2
-rw-r--r--src/socketengines/poll.cpp2
-rw-r--r--src/socketengines/select.cpp2
-rw-r--r--src/win32/anope_windows.h1
-rw-r--r--src/win32/windows.cpp16
13 files changed, 42 insertions, 31 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index cac30aedd..52f983079 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -224,6 +224,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "DEBUG" OR CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINF
endif()
# Check for the existence of the following functions
+check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
check_function_exists(umask HAVE_UMASK)
check_function_exists(epoll_wait HAVE_EPOLL)
check_function_exists(poll HAVE_POLL)
diff --git a/include/anope.h b/include/anope.h
index 819de40ee..91d073133 100644
--- a/include/anope.h
+++ b/include/anope.h
@@ -367,6 +367,7 @@ namespace Anope
* Use this unless you need very specific time checks
*/
extern CoreExport time_t CurTime;
+ extern CoreExport long long CurTimeNs;
/** The debug level we are running at.
*/
@@ -575,6 +576,9 @@ namespace Anope
* @param s2 The second string.
*/
extern CoreExport size_t Distance(const Anope::string &s1, const Anope::string &s2);
+
+ /** Update the current time. */
+ extern CoreExport void UpdateTime();
}
/** sepstream allows for splitting token separated lists.
diff --git a/include/sysconf.h.cmake b/include/sysconf.h.cmake
index 8c1a35e72..927c75910 100644
--- a/include/sysconf.h.cmake
+++ b/include/sysconf.h.cmake
@@ -20,6 +20,9 @@
// Whether Anope was built in debug mode.
#cmakedefine01 DEBUG_BUILD
+// Whether the clock_gettime() function is available.
+#cmakedefine01 HAVE_CLOCK_GETTIME
+
// Whether Anope was built with localization support.
#cmakedefine01 HAVE_LOCALIZATION
diff --git a/src/init.cpp b/src/init.cpp
index 8b9703493..3dcf7460c 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -290,6 +290,7 @@ bool Anope::Init(int ac, char **av)
umask(DEFUMASK);
#endif
+ Anope::UpdateTime();
Serialize::RegisterTypes();
/* Parse command line arguments */
diff --git a/src/logger.cpp b/src/logger.cpp
index 1b8fe04f4..9813ae150 100644
--- a/src/logger.cpp
+++ b/src/logger.cpp
@@ -29,20 +29,15 @@
static Anope::string GetTimeStamp()
{
char tbuf[256];
- time_t t;
- if (time(&t) < 0)
- t = Anope::CurTime;
-
- tm tm = *localtime(&t);
+ Anope::UpdateTime();
+ auto tm = *localtime(&Anope::CurTime);
if (Anope::Debug)
{
char *s;
- struct timeval tv;
- gettimeofday(&tv, NULL);
strftime(tbuf, sizeof(tbuf) - 1, "[%b %d %H:%M:%S", &tm);
s = tbuf + strlen(tbuf);
- s += snprintf(s, sizeof(tbuf) - (s - tbuf), ".%06d", static_cast<int>(tv.tv_usec));
+ s += snprintf(s, sizeof(tbuf) - (s - tbuf), ".%06lld", static_cast<long long>(Anope::CurTimeNs / 1000));
strftime(s, sizeof(tbuf) - (s - tbuf) - 1, " %Y]", &tm);
}
else
diff --git a/src/main.cpp b/src/main.cpp
index 97099b234..7106579c0 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -36,8 +36,9 @@ Anope::string Anope::QuitReason;
static Anope::string BinaryDir; /* Full path to services bin directory */
-time_t Anope::StartTime = time(NULL);
-time_t Anope::CurTime = time(NULL);
+time_t Anope::StartTime = 0;
+time_t Anope::CurTime = 0;
+long long Anope::CurTimeNs = 0;
size_t Anope::CurrentUplink = -1;
diff --git a/src/misc.cpp b/src/misc.cpp
index d2c05da12..1984477f7 100644
--- a/src/misc.cpp
+++ b/src/misc.cpp
@@ -785,3 +785,26 @@ size_t Anope::Distance(const Anope::string &s1, const Anope::string &s2)
}
return costs[s2.length()];
}
+
+void Anope::UpdateTime()
+{
+#ifdef _WIN32
+ SYSTEMTIME st;
+ GetSystemTime(&st);
+
+ CurTime = time(nullptr);
+ CurTimeNs = st.wMilliseconds;
+#elif HAVE_CLOCK_GETTIME
+ struct timespec ts;
+ clock_gettime(CLOCK_REALTIME, &ts);
+
+ CurTime = ts.tv_sec;
+ CurTimeNs = ts.tv_nsec;
+#else
+ struct timeval tv;
+ gettimeofday(&tv, nullptr);
+
+ CurTime = tv.tv_sec;
+ CurTimeNs = tv.tv_usec * 1000;
+#endif
+}
diff --git a/src/socketengines/epoll.cpp b/src/socketengines/epoll.cpp
index eb70de630..470db7ce8 100644
--- a/src/socketengines/epoll.cpp
+++ b/src/socketengines/epoll.cpp
@@ -76,7 +76,7 @@ void SocketEngine::Process()
events.resize(events.size() * 2);
int total = epoll_wait(EngineHandle, &events.front(), events.size(), Config->ReadTimeout * 1000);
- Anope::CurTime = time(NULL);
+ Anope::UpdateTime();
/* EINTR can be given if the read timeout expires */
if (total == -1)
diff --git a/src/socketengines/kqueue.cpp b/src/socketengines/kqueue.cpp
index c57ab1b7d..d0fee2e1d 100644
--- a/src/socketengines/kqueue.cpp
+++ b/src/socketengines/kqueue.cpp
@@ -77,7 +77,7 @@ void SocketEngine::Process()
static timespec kq_timespec = { Config->ReadTimeout, 0 };
int total = kevent(kq_fd, &change_events.front(), change_count, &event_events.front(), event_events.size(), &kq_timespec);
change_count = 0;
- Anope::CurTime = time(NULL);
+ Anope::UpdateTime();
/* EINTR can be given if the read timeout expires */
if (total == -1)
diff --git a/src/socketengines/poll.cpp b/src/socketengines/poll.cpp
index feeac5967..409879f87 100644
--- a/src/socketengines/poll.cpp
+++ b/src/socketengines/poll.cpp
@@ -100,7 +100,7 @@ void SocketEngine::Change(Socket *s, bool set, SocketFlag flag)
void SocketEngine::Process()
{
int total = poll(&events.front(), events.size(), Config->ReadTimeout * 1000);
- Anope::CurTime = time(NULL);
+ Anope::UpdateTime();
/* EINTR can be given if the read timeout expires */
if (total < 0)
diff --git a/src/socketengines/select.cpp b/src/socketengines/select.cpp
index 61ff54560..6e02062db 100644
--- a/src/socketengines/select.cpp
+++ b/src/socketengines/select.cpp
@@ -100,7 +100,7 @@ void SocketEngine::Process()
#endif
int sresult = select(MaxFD + 1, &rfdset, &wfdset, &efdset, &tval);
- Anope::CurTime = time(NULL);
+ Anope::UpdateTime();
if (sresult == -1)
{
diff --git a/src/win32/anope_windows.h b/src/win32/anope_windows.h
index fe75668bc..87eaff2a9 100644
--- a/src/win32/anope_windows.h
+++ b/src/win32/anope_windows.h
@@ -65,7 +65,6 @@ namespace Anope
extern CoreExport void OnStartup();
extern CoreExport void OnShutdown();
extern CoreExport USHORT WindowsGetLanguage(const Anope::string &lang);
-extern CoreExport int gettimeofday(timeval *tv, void *);
extern int setenv(const char *name, const char *value, int overwrite);
extern int unsetenv(const char *name);
extern int mkstemp(char *input);
diff --git a/src/win32/windows.cpp b/src/win32/windows.cpp
index dcdf9d1a9..f2a1da443 100644
--- a/src/win32/windows.cpp
+++ b/src/win32/windows.cpp
@@ -64,22 +64,6 @@ USHORT WindowsGetLanguage(const Anope::string &lang)
return LANG_NEUTRAL;
}
-/** Like gettimeofday(), but it works on Windows.
- * @param tv A timeval struct
- * @param tz Should be NULL, it is not used
- * @return 0 on success
- */
-int gettimeofday(timeval *tv, void *)
-{
- SYSTEMTIME st;
- GetSystemTime(&st);
-
- tv->tv_sec = Anope::CurTime;
- tv->tv_usec = st.wMilliseconds;
-
- return 0;
-}
-
int setenv(const char *name, const char *value, int overwrite)
{
return SetEnvironmentVariable(name, value);