use crate::sched::{Clockid, Timestamp};
use crate::{Error, Result};
use yanix::clock::{clock_getres, clock_gettime, ClockId};
pub(crate) fn res_get(clock_id: Clockid) -> Result<Timestamp> {
let clock_id: ClockId = clock_id.into();
let timespec = clock_getres(clock_id)?;
(timespec.tv_sec as Timestamp)
.checked_mul(1_000_000_000)
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as Timestamp))
.map_or(Err(Error::Overflow), |resolution| {
if resolution == 0 {
Err(Error::Inval)
} else {
Ok(resolution)
}
})
}
pub(crate) fn time_get(clock_id: Clockid) -> Result<Timestamp> {
let clock_id: ClockId = clock_id.into();
let timespec = clock_gettime(clock_id)?;
(timespec.tv_sec as Timestamp)
.checked_mul(1_000_000_000)
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as Timestamp))
.map_or(Err(Error::Overflow), Ok)
}