sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use std::mem::zeroed;

use windows::Win32::Foundation::{FILETIME, HANDLE};
use windows::Win32::System::Threading::GetProcessTimes;

#[inline]
pub(super) fn check_sub(a: u64, b: u64) -> u64 {
    if a < b { a } else { a - b }
}

#[inline(always)]
pub(super) const fn filetime_to_u64(ft: FILETIME) -> u64 {
    ((ft.dwHighDateTime as u64) << 32) | (ft.dwLowDateTime as u64)
}

#[inline]
pub(super) unsafe fn get_process_times(handle: HANDLE) -> u64 {
    unsafe {
        let mut fstart: FILETIME = zeroed();
        let mut x = zeroed();

        let _err = GetProcessTimes(
            handle,
            &mut fstart as *mut FILETIME,
            &mut x as *mut FILETIME,
            &mut x as *mut FILETIME,
            &mut x as *mut FILETIME,
        );
        filetime_to_u64(fstart)
    }
}

#[inline]
fn compute_start(process_times: u64) -> u64 {
    // Seconds between the Windows epoch (1601-01-01) and the Linux epoch (1970-01-01).
    (process_times / 10_000_000).saturating_sub(11_644_473_600)
}

pub(super) fn get_start_and_run_time(handle: HANDLE, now: u64) -> (u64, u64) {
    unsafe {
        let process_times = get_process_times(handle);
        let start = compute_start(process_times);
        let run_time = check_sub(now, start);
        (start, run_time)
    }
}