Skip to content

Commit

Permalink
Fix boot_time on windows
Browse files Browse the repository at this point in the history
Fixes #345
  • Loading branch information
Orycterope committed Sep 27, 2021
1 parent b292f15 commit 0d28d37
Showing 1 changed file with 21 additions and 1 deletion.
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 boot_timestamp = smol::block_on(boot_time()).unwrap().value;
std::thread::sleep(Duration::from_secs(1));
let current_timestamp = std::time::SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs_f64();
assert!(boot_timestamp < current_timestamp, "Boot time is greater than current time. Boot timestamp: {}, current timestamp: {}", boot_timestamp, current_timestamp);
}
}

0 comments on commit 0d28d37

Please sign in to comment.