Skip to content

Commit

Permalink
feat: get rid of std chrono system_clock
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk committed Sep 29, 2023
1 parent 30b5adf commit 6a5bb8d
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/native_ext/util/platform.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "platform.h"
#include <chrono>
#include <uv.h>

#ifdef __APPLE__
Expand All @@ -8,14 +7,6 @@

namespace Splunk {

namespace {
template <typename Duration>
int64_t SinceEpoch() {
return std::chrono::duration_cast<Duration>(std::chrono::system_clock::now().time_since_epoch())
.count();
}
} // namespace

#ifdef __APPLE__
int64_t HrTime() {
static mach_timebase_info_data_t timebase;
Expand All @@ -29,8 +20,30 @@ int64_t HrTime() {
int64_t HrTime() { return uv_hrtime(); }
#endif

int64_t MicroSecondsSinceEpoch() { return SinceEpoch<std::chrono::microseconds>(); }
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int64_t MicroSecondsSinceEpoch() {
FILETIME ft;
GetSystemTimePreciseAsFileTime(&ft);

int64_t t = (int64_t) ft.dwHighDateTime << 32 | ft.dwLowDateTime;
t -= 116444736000000000LL;
return t / 10LL;
}
#undef WIN32_LEAN_AND_MEAN
#else
#include <time.h>
int64_t MicroSecondsSinceEpoch() {
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) == 0) {
return ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL;
}

return 0;
}
#endif

int64_t MilliSecondsSinceEpoch() { return SinceEpoch<std::chrono::milliseconds>(); }
int64_t MilliSecondsSinceEpoch() { return MicroSecondsSinceEpoch() / 1000; }

} // namespace Splunk

0 comments on commit 6a5bb8d

Please sign in to comment.