Files
c3c/lib/std/time/os/time_win32.c3
Christoffer Lerno 4c1edfb941 Dev (#777)
* The new @if directive.
2023-06-10 23:16:28 +02:00

29 lines
1.0 KiB
C

module std::time::os @if(WIN32_LIBC);
import std::os::win32;
extern fn void win32_GetSystemTimeAsFileTime(Win32_FILETIME* time) @extern("GetSystemTimeAsFileTime");
extern fn Win32_BOOL win32_QueryPerformanceFrequency(Win32_LARGE_INTEGER* lpFrequency) @extern("QueryPerformanceFrequency");
extern fn Win32_BOOL win32_QueryPerformanceCounter(Win32_LARGE_INTEGER* lpPerformanceCount) @extern("QueryPerformanceCounter");
const ulong WINDOWS_TICK_US @local = 10;
const ulong WIN_TO_UNIX_EPOCH_US @local = 116444736000000000u64 / WINDOWS_TICK_US;
fn Clock native_clock()
{
static Win32_LARGE_INTEGER freq;
if (!freq.quadPart)
{
if (!win32_QueryPerformanceFrequency(&freq)) return 0;
}
Win32_LARGE_INTEGER counter @noinit;
if (!win32_QueryPerformanceCounter(&counter)) return 0;
return (Clock)counter.quadPart;
}
fn Time native_timestamp()
{
Win32_FILETIME ft @noinit;
win32_GetSystemTimeAsFileTime(&ft);
ulong result = (ulong)ft.dwHighDateTime << 32 | ft.dwLowDateTime;
return (Time)(result / WINDOWS_TICK_US - WIN_TO_UNIX_EPOCH_US);
}