Skip to content

Commit

Permalink
std.time: more precise nanoTimestamp in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
alichraghi committed Feb 12, 2025
1 parent 58f9288 commit 63c6208
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 7 deletions.
6 changes: 1 addition & 5 deletions lib/std/os/windows/kernel32.zig
Original file line number Diff line number Diff line change
Expand Up @@ -656,9 +656,5 @@ pub extern "kernel32" fn SetLastError(

// Everything Else

// TODO:
// Wrapper around KUSER_SHARED_DATA.SystemTime.
// Much better to use NtQuerySystemTime or NtQuerySystemTimePrecise for guaranteed 0.1ns precision.
pub extern "kernel32" fn GetSystemTimeAsFileTime(lpSystemTimeAsFileTime: *FILETIME) callconv(.winapi) void;

pub extern "kernel32" fn GetSystemTimePreciseAsFileTime(lpSystemTimeAsFileTime: *FILETIME) callconv(.winapi) void;
pub extern "kernel32" fn GetSystemInfo(lpSystemInfo: *SYSTEM_INFO) callconv(.winapi) void;
2 changes: 1 addition & 1 deletion lib/std/posix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5710,7 +5710,7 @@ pub fn clock_gettime(clock_id: clockid_t) ClockGetTimeError!timespec {
if (native_os == .windows) {
if (clock_id == .REALTIME) {
var ft: windows.FILETIME = undefined;
windows.kernel32.GetSystemTimeAsFileTime(&ft);
windows.kernel32.GetSystemTimePreciseAsFileTime(&ft);
// FileTime has a granularity of 100 nanoseconds and uses the NTFS/Windows epoch.
const ft64 = (@as(u64, ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
const ft_per_s = std.time.ns_per_s / 100;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/time.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn nanoTimestamp() i128 {
// which is 1601-01-01.
const epoch_adj = epoch.windows * (ns_per_s / 100);
var ft: windows.FILETIME = undefined;
windows.kernel32.GetSystemTimeAsFileTime(&ft);
windows.kernel32.GetSystemTimePreciseAsFileTime(&ft);
const ft64 = (@as(u64, ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
return @as(i128, @as(i64, @bitCast(ft64)) + epoch_adj) * 100;
},
Expand Down

0 comments on commit 63c6208

Please sign in to comment.