Skip to content
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 boot_time on windows #346

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion heim-host/src/sys/windows/boot_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,31 @@ pub async fn boot_time() -> Result<Time> {
filetime.assume_init().into_time()
};

let elapsed_since_boot = Time::new::<time::millisecond>(unsafe {
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-gettickcount64
// function returns an ULONGLONG and can't fail, apparently
sysinfoapi::GetTickCount64() as f64
});

// Seconds amount between the "Windows epoch" (January 1, 1601)
// and the Unix epoch (January 1, 1970).
// TODO: It would be nice to make it const,
// as soon as `uom` will mark `Time::new` as a `const fn`
let unix_epoch_delta = Time::new::<time::second>(11_644_473_600.0);

Ok(time - unix_epoch_delta)
Ok(time - elapsed_since_boot - unix_epoch_delta)
}

#[cfg(test)]
mod test {
use crate::boot_time;
use std::time::{SystemTime, Duration};

#[test]
fn check_boot_time() {
let current_timestamp = std::time::SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs_f64();
std::thread::sleep(Duration::from_secs(1));
let boot_timestamp = smol::block_on(boot_time()).unwrap().value;
assert!(boot_timestamp < current_timestamp, "Boot time is greater than current time. Boot timestamp: {}, current timestamp: {}", boot_timestamp, current_timestamp);
}
}