steady_clock: Introduce a real time clock
This commit is contained in:
parent
dcd13a7566
commit
6f9918552c
2 changed files with 36 additions and 0 deletions
|
@ -23,6 +23,19 @@ static s64 WindowsQueryPerformanceCounter() {
|
||||||
QueryPerformanceCounter(&counter);
|
QueryPerformanceCounter(&counter);
|
||||||
return counter.QuadPart;
|
return counter.QuadPart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static s64 GetSystemTimeNS() {
|
||||||
|
// GetSystemTimePreciseAsFileTime returns the file time in 100ns units.
|
||||||
|
static constexpr s64 Multiplier = 100;
|
||||||
|
// Convert Windows epoch to Unix epoch.
|
||||||
|
static constexpr s64 WindowsEpochToUnixEpochNS = 0x19DB1DED53E8000LL;
|
||||||
|
|
||||||
|
FILETIME filetime;
|
||||||
|
GetSystemTimePreciseAsFileTime(&filetime);
|
||||||
|
return Multiplier * ((static_cast<s64>(filetime.dwHighDateTime) << 32) +
|
||||||
|
static_cast<s64>(filetime.dwLowDateTime)) -
|
||||||
|
WindowsEpochToUnixEpochNS;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
SteadyClock::time_point SteadyClock::Now() noexcept {
|
SteadyClock::time_point SteadyClock::Now() noexcept {
|
||||||
|
@ -53,4 +66,16 @@ SteadyClock::time_point SteadyClock::Now() noexcept {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RealTimeClock::time_point RealTimeClock::Now() noexcept {
|
||||||
|
#if defined(_WIN32)
|
||||||
|
return time_point{duration{GetSystemTimeNS()}};
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
return time_point{duration{clock_gettime_nsec_np(CLOCK_REALTIME)}};
|
||||||
|
#else
|
||||||
|
timespec ts;
|
||||||
|
clock_gettime(CLOCK_REALTIME, &ts);
|
||||||
|
return time_point{std::chrono::seconds{ts.tv_sec} + std::chrono::nanoseconds{ts.tv_nsec}};
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
}; // namespace Common
|
}; // namespace Common
|
||||||
|
|
|
@ -20,4 +20,15 @@ struct SteadyClock {
|
||||||
[[nodiscard]] static time_point Now() noexcept;
|
[[nodiscard]] static time_point Now() noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct RealTimeClock {
|
||||||
|
using rep = s64;
|
||||||
|
using period = std::nano;
|
||||||
|
using duration = std::chrono::nanoseconds;
|
||||||
|
using time_point = std::chrono::time_point<RealTimeClock>;
|
||||||
|
|
||||||
|
static constexpr bool is_steady = false;
|
||||||
|
|
||||||
|
[[nodiscard]] static time_point Now() noexcept;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
||||||
|
|
Loading…
Reference in a new issue