-
-
Notifications
You must be signed in to change notification settings - Fork 313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(win): inconsistent boot_time #1372
Conversation
@@ -63,7 +63,14 @@ unsafe impl<T> Sync for Wrap<T> {} | |||
|
|||
unsafe fn boot_time() -> u64 { | |||
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) { | |||
Ok(n) => n.as_secs().saturating_sub(GetTickCount64() / 1_000), | |||
Ok(n) => { | |||
let system_time_ns = n.as_nanos(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add your explanation here as a comment as future me will likely wonder what the hell is going on in here. 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We were effectively rounding twice: Duration::as_secs
discards nanosecond precision, and GetTickCount64() / 1_000
truncates due to integer division. This leads to inconsistent seconds depending on how each value is rounded.
By using nanoseconds for the subtraction and converting to seconds only once at the end, we achieve consistent rounding and stable results.
Hi future Guillaume! 👋
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I meant as a code comment in the code. 😛
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must admit I was slightly confused 🤣 I'll add a comment!
Apart from the code comment, looks good to me, thanks! |
Awesome, thanks! Once CI run is done, I'll merge. |
I noticed inconsistent boot times when using the
System::boot_time()
function on Windows. This PR aims to fix this.Refines the
boot_time
function on Windows to improve consistency by enhancing precision in the uptime calculation. The updated version uses nanoseconds throughout to avoid rounding errors, and it caps the result safely within u64 limits.