Skip to content

SystemTime::now should use GetSystemTimePreciseAsFileTime where available #67266

Closed
@dbregman

Description

@dbregman

The current implementation of std::time::SystemTime::now on Windows uses the GetSystemTimeAsFileTime function. This function has relatively poor resolution, between 1-16ms depending on the system, which is unacceptable for many real-world applications.

GetSystemTimePreciseAsFileTime on the other hand has a resolution between 1us and 100ns. This extra precision comes at a small performance cost[1], but it is still an extremely lightweight function call. I measured it at 20ns/call on my system. I believe GetSystemTimePreciseAsFileTime should be preferred whenever it is available (Windows Vista and higher)

There seems to be some FUD out there surrounding this function, however Microsoft recommends using this function in this fairly recent article [2] Quote:

When you need UTC-synchronized time stamps with a resolution of 1 microsecond or better, choose GetSystemTimePreciseAsFileTime

See also: Same issue was identified and fixed a couple years ago in the .net runtime: [3]

[1] https://devblogs.microsoft.com/oldnewthing/20170921-00/?p=97057
[2] https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps.
[3] dotnet/coreclr#9736

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.O-windowsOperating system: WindowsT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions