From 9d1d8e5306bfd8e0809685a376a689fc3d8ccda3 Mon Sep 17 00:00:00 2001 From: Reuben Dunnington Date: Tue, 22 Aug 2023 13:40:39 -0700 Subject: [PATCH] add win32 impl for oc_clock_time() --- src/platform/win32_clock.c | 41 ++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/platform/win32_clock.c b/src/platform/win32_clock.c index 46a1767..2d3a3e8 100644 --- a/src/platform/win32_clock.c +++ b/src/platform/win32_clock.c @@ -18,6 +18,8 @@ extern "C" static u64 __performanceCounterFreq = 0; + const u64 WIN_EPOCH_TO_UNIX_EPOCH_100NS = 116444736000000000; // number of 100ns from Jan 1, 1601 (windows epoch) to Jan 1, 1970 (unix epoch) + void oc_clock_init() { LARGE_INTEGER freq; @@ -27,11 +29,42 @@ extern "C" f64 oc_clock_time(oc_clock_kind clock) { - LARGE_INTEGER counter; - QueryPerformanceCounter(&counter); + switch(clock) + { + case OC_CLOCK_MONOTONIC: + { + LARGE_INTEGER counter; + QueryPerformanceCounter(&counter); - f64 time = __performanceCounterFreq ? (counter.QuadPart / (f64)__performanceCounterFreq) : 0; - return (time); + f64 time = __performanceCounterFreq ? (counter.QuadPart / (f64)__performanceCounterFreq) : 0; + return (time); + } + + case OC_CLOCK_UPTIME: + { + LONGLONG unbiasedTime100Ns; + if(QueryUnbiasedInterruptTime(&unbiasedTime100Ns)) + { + f64 unbiasedTimeSecs = ((f64)unbiasedTime100Ns) / (10 * 1000 * 1000); + return unbiasedTimeSecs; + } + oc_log_error("OC_CLOCK_UPTIME syscall failed with error: %d", GetLastError()); + return 0; + } + + case OC_CLOCK_DATE: + { + FILETIME ft; + GetSystemTimeAsFileTime(&ft); + + u64 systemTime100Ns = ((u64)ft.dwHighDateTime << 32) | (u64)ft.dwLowDateTime; + u64 timestamp100Ns = systemTime100Ns - WIN_EPOCH_TO_UNIX_EPOCH_100NS; + f64 timeSecs = ((f64)timestamp100Ns) / (10 * 1000 * 1000); + return timeSecs; + } + } + + return 0.0; } #ifdef __cplusplus -- 2.25.1