Skip to content

Commit bffbc91

Browse files
alichraghialexrp
authored andcommitted
std.time: more precise nanoTimestamp in windows
1 parent d12123a commit bffbc91

File tree

4 files changed

+7
-28
lines changed

4 files changed

+7
-28
lines changed

lib/std/os/windows/kernel32.zig

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -656,9 +656,4 @@ pub extern "kernel32" fn SetLastError(
656656

657657
// Everything Else
658658

659-
// TODO:
660-
// Wrapper around KUSER_SHARED_DATA.SystemTime.
661-
// Much better to use NtQuerySystemTime or NtQuerySystemTimePrecise for guaranteed 0.1ns precision.
662-
pub extern "kernel32" fn GetSystemTimeAsFileTime(lpSystemTimeAsFileTime: *FILETIME) callconv(.winapi) void;
663-
664659
pub extern "kernel32" fn GetSystemInfo(lpSystemInfo: *SYSTEM_INFO) callconv(.winapi) void;

lib/std/os/windows/ntdll.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub extern "ntdll" fn RtlVirtualUnwind(
9292
EstablisherFrame: *DWORD64,
9393
ContextPointers: ?*KNONVOLATILE_CONTEXT_POINTERS,
9494
) callconv(.winapi) *EXCEPTION_ROUTINE;
95+
pub extern "ntdll" fn RtlGetSystemTimePrecise() callconv(.winapi) LARGE_INTEGER;
9596
pub extern "ntdll" fn NtQueryInformationFile(
9697
FileHandle: HANDLE,
9798
IoStatusBlock: *IO_STATUS_BLOCK,

lib/std/posix.zig

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5693,7 +5693,10 @@ pub const ClockGetTimeError = error{UnsupportedClock} || UnexpectedError;
56935693

56945694
pub fn clock_gettime(clock_id: clockid_t) ClockGetTimeError!timespec {
56955695
var tp: timespec = undefined;
5696-
if (native_os == .wasi and !builtin.link_libc) {
5696+
5697+
if (native_os == .windows) {
5698+
@compileError("Windows does not support POSIX; use Windows-specific API or cross-platform std.time API");
5699+
} else if (native_os == .wasi and !builtin.link_libc) {
56975700
var ts: timestamp_t = undefined;
56985701
switch (system.clock_time_get(clock_id, 1, &ts)) {
56995702
.SUCCESS => {
@@ -5707,23 +5710,6 @@ pub fn clock_gettime(clock_id: clockid_t) ClockGetTimeError!timespec {
57075710
}
57085711
return tp;
57095712
}
5710-
if (native_os == .windows) {
5711-
if (clock_id == .REALTIME) {
5712-
var ft: windows.FILETIME = undefined;
5713-
windows.kernel32.GetSystemTimeAsFileTime(&ft);
5714-
// FileTime has a granularity of 100 nanoseconds and uses the NTFS/Windows epoch.
5715-
const ft64 = (@as(u64, ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
5716-
const ft_per_s = std.time.ns_per_s / 100;
5717-
tp = .{
5718-
.sec = @as(i64, @intCast(ft64 / ft_per_s)) + std.time.epoch.windows,
5719-
.nsec = @as(c_long, @intCast(ft64 % ft_per_s)) * 100,
5720-
};
5721-
return tp;
5722-
} else {
5723-
// TODO POSIX implementation of CLOCK.MONOTONIC on Windows.
5724-
return error.UnsupportedClock;
5725-
}
5726-
}
57275713

57285714
switch (errno(system.clock_gettime(clock_id, &tp))) {
57295715
.SUCCESS => return tp,

lib/std/time.zig

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,10 @@ pub fn microTimestamp() i64 {
4747
pub fn nanoTimestamp() i128 {
4848
switch (builtin.os.tag) {
4949
.windows => {
50-
// FileTime has a granularity of 100 nanoseconds and uses the NTFS/Windows epoch,
50+
// RtlGetSystemTimePrecise() has a granularity of 100 nanoseconds and uses the NTFS/Windows epoch,
5151
// which is 1601-01-01.
5252
const epoch_adj = epoch.windows * (ns_per_s / 100);
53-
var ft: windows.FILETIME = undefined;
54-
windows.kernel32.GetSystemTimeAsFileTime(&ft);
55-
const ft64 = (@as(u64, ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
56-
return @as(i128, @as(i64, @bitCast(ft64)) + epoch_adj) * 100;
53+
return @as(i128, windows.ntdll.RtlGetSystemTimePrecise() + epoch_adj) * 100;
5754
},
5855
.wasi => {
5956
var ns: std.os.wasi.timestamp_t = undefined;

0 commit comments

Comments
 (0)