From 0fb3661f139aadaf237fcbb4c5aae0f9cab5983e Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Tue, 14 Jan 2025 14:13:41 +0100 Subject: [PATCH] QEventDispatcherWin32: consistently use ms precision in calculations Windows does not support nanosecond-resolution timers, so we ceil nanosecond interval to milliseconds in registerTimers(). Later on, we also use millisecond-resolution to calculate the expected timeout. However, the remainingTime() method was using nanosecond resolution when doing the calculations. As a result, we could never get remainingTime() equal to the specified interval. This patch uses the same ceil-to-milliseconds logic in remainingTime() to make all time calculations consistent. It's not really possible to provide a reliable unit-test for this case, because we can only check that the remaining time does not exceed the specified interval. Amends 0d0b346322f6b078e6fe60cd3612e8d08a0d5f00. Task-number: QTBUG-129170 Task-number: QTBUG-132388 Change-Id: I48eaa15d55174e1925fc5eb3ca76b78d85e46f42 Reviewed-by: Thiago Macieira --- src/corelib/kernel/qeventdispatcher_win.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/corelib/kernel/qeventdispatcher_win.cpp b/src/corelib/kernel/qeventdispatcher_win.cpp index d5f54db68ed..ccbd3b8ce88 100644 --- a/src/corelib/kernel/qeventdispatcher_win.cpp +++ b/src/corelib/kernel/qeventdispatcher_win.cpp @@ -801,9 +801,9 @@ QEventDispatcherWin32::Duration QEventDispatcherWin32::remainingTime(Qt::TimerId // timer found, return time to wait using namespace std::chrono; using namespace std::chrono_literals; - const Duration currentTimeNs = steady_clock::now().time_since_epoch(); - const Duration timeoutNs = Duration{t->timeout * 1ms}; - return timeoutNs > currentTimeNs ? timeoutNs - currentTimeNs : 0ns; + const auto currentTimeMs = duration_cast(steady_clock::now().time_since_epoch()); + const auto timeoutMs = t->timeout * 1ms; + return timeoutMs > currentTimeMs ? milliseconds{timeoutMs - currentTimeMs} : 0ms; } #ifndef QT_NO_DEBUG