//! System time management

use core::mem::MaybeUninit;

use abi::error::Error;
pub use abi::time::{ClockType, SystemTime};

/// Returns the [SystemTime] structure representing time as described by a given [ClockType].
#[inline]
pub fn get_clock(clock_type: ClockType) -> Result<SystemTime, Error> {
    let mut time = MaybeUninit::uninit();
    unsafe { crate::sys::get_clock(clock_type, &mut time) }?;
    Ok(unsafe { time.assume_init() })
}

/// Updates the system clock value
#[inline]
pub fn set_clock(clock_type: ClockType, time: SystemTime) -> Result<(), Error> {
    unsafe { crate::sys::set_clock(clock_type, &time) }
}

/// Returns the "real world" time.
#[inline]
pub fn get_real_time() -> Result<SystemTime, Error> {
    get_clock(ClockType::RealTime)
}

/// Returns the monotonic time since boot.
#[inline]
pub fn get_monotonic_time() -> Result<SystemTime, Error> {
    get_clock(ClockType::Monotonic)
}
