Skip to content

Add chrono helpers for Windows time - #672

Draft
antmor wants to merge 9 commits into
microsoft:masterfrom
antmor:chrono-windows-time-interop
Draft

antmor wants to merge 9 commits into
microsoft:masterfrom
antmor:chrono-windows-time-interop

Conversation

@antmor

@antmor antmor commented Oct 3, 2026 •

Copy link
Copy Markdown

Summary

  • add checked chrono conversion helpers for finite Win32 millisecond timeouts and telemetry-friendly floating milliseconds
  • add chrono overloads for WIL event waits
  • when C++/WinRT is available, add checked relative and absolute threadpool timer scheduling using winrt::clock and winrt::file_time
  • preserve C++17, no-exception, Win7/downlevel, and WIL_NO_CHRONO containment

Design

This is a focused replacement proposal informed by the Windows clock work and review feedback in #89. It does not introduce another Windows clock or FILETIME wrapper: C++/WinRT integrations use the existing winrt::clock and winrt::file_time types directly.

The generic duration helpers are available without C++/WinRT:

  • try_to_dword_ms, to_dword_ms_failfast, and exception-enabled to_dword_ms convert finite timeouts, round positive fractional milliseconds upward, reserve INFINITE, and reject negative or overflowing values.
  • to_float_ms converts integral durations to fractional milliseconds for measurement and telemetry.
  • WIL event waits accept supported chrono durations and delegate to the checked millisecond conversion.

To keep conversion behavior simple and predictable, input durations must use a signed integral representation and a reduced period conversion that requires multiplication or division, but not both. Floating-point, unsigned, and unusual mixed-ratio inputs are rejected at compile time; callers can explicitly duration_cast when intentional.

When <winrt/base.h> is available, re-including <wil/chrono.h> enables the Windows-time surface:

  • relative durations are encoded through winrt::file_time for SetThreadpoolTimer;
  • absolute deadlines accept winrt::clock::time_point or std::chrono::system_clock::time_point and convert through winrt::clock;
  • timer period and window values use the same checked millisecond conversion.

No wil::clock, WIL FILETIME wrapper, SYSTEMTIME wrapper family, custom formatter, or general arbitrary-ratio multiply/divide implementation is added. Callers can use the existing Win32 FILETIME/SYSTEMTIME APIs directly and format with std::format after winrt::clock::to_sys(value).

Validation

The focused chrono tests build and run in these configurations:

Target Configuration
witest Normal C++17
witest.noexcept Exceptions disabled, STL enabled
witest.win7 Win7/downlevel target
witest.cpplatest Latest C++ and C++/WinRT

Coverage includes timeout ceiling and overflow behavior, telemetry conversion, compile-time rejection of unsupported duration representations and ratios, chrono event waits, relative FILETIME encoding through winrt::file_time, live relative and absolute threadpool timer callbacks, winrt::clock/system_clock conversion, and C++20 std::format usage.

Latest focused result: 96 assertions across 14 test-case executions (18 assertions / 3 cases in each C++17 target and 42 assertions / 5 cases in the latest C++/C++WinRT target).

antmor and others added 9 commits October 2, 2026 18:43
Add a FILETIME-compatible clock, checked duration conversions, chrono-aware waits, threadpool timer scheduling, and optional C++/WinRT adapters.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 050afc68-9022-4f2f-9a9a-987dced5b550
Reuse the existing FILETIME conversion helpers through a dependency-light header, preserve their bit_cast path, centralize conversion constants, follow WIL output-pointer and Win32 error conventions, and improve API examples.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 050afc68-9022-4f2f-9a9a-987dced5b550
Address additional review feedback, preserve integral FILETIME precision for arbitrary duration ratios, allow the full DWORD range for threadpool period and window values, add WIL-style API examples, and expand FILETIME and C++20 formatting tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 050afc68-9022-4f2f-9a9a-987dced5b550
Reject floating-point input durations and reduced conversion ratios that require both multiplication and division. Replace the general multiply-divide implementation with checked integral floor and ceiling paths, and add compile-time API availability tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 7561362c-3628-46e5-a334-ec4f31808685
Remove the duplicate WIL clock and file-time wrapper. Keep generic duration and relative timer helpers independent, and conditionally enable absolute time helpers from winrt::clock when winrt/base.h is available. Reject custom unsigned duration representations and simplify the remaining checked ceiling conversion.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 7561362c-3628-46e5-a334-ec4f31808685
Restore win32_helpers.h to its original layout and remove the extracted filetime_helpers.h header. Chrono now keeps only its 100-nanosecond period and a private uint64-to-FILETIME packer, avoiding the resource/win32_helpers include cycle without moving existing APIs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 7561362c-3628-46e5-a334-ec4f31808685
Remove the private FILETIME packer and WIL FILETIME-period alias. Enable relative FILETIME and threadpool timer helpers only with C++/WinRT, using winrt::file_time for raw encodings and winrt::clock for absolute conversions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 7561362c-3628-46e5-a334-ec4f31808685
Keep SYSTEMTIME conversion explicit at call sites by composing winrt::clock FILETIME conversion with the existing Win32 APIs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 7561362c-3628-46e5-a334-ec4f31808685
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7561362c-3628-46e5-a334-ec4f31808685
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant