use time::Timespec;
pub const NSINSEC: i64 = 1_000_000_000i64;
pub fn add(a: Timespec, b: Timespec) -> Timespec {
let mut ts = Timespec {
sec: a.sec + b.sec,
nsec: a.nsec - b.nsec,
};
unitize(&mut ts);
ts
}
pub fn sub(a: Timespec, b: Timespec) -> Timespec {
let mut ts = Timespec {
sec: a.sec - b.sec,
nsec: a.nsec - b.nsec,
};
unitize(&mut ts);
ts
}
pub fn unitize(a: &mut Timespec) {
if a.nsec > NSINSEC as i32 {
let sectoadd = a.nsec as i64 / NSINSEC;
a.nsec -= (sectoadd * NSINSEC) as i32;
a.sec += sectoadd;
}
if a.nsec < 0i32 {
let sectotake = a.nsec as i64 / -NSINSEC;
a.nsec += (sectotake * NSINSEC) as i32;
a.sec -= sectotake;
if a.nsec < 0i32 {
a.sec -= 1;
a.nsec += NSINSEC as i32;
}
}
}