use core::fmt;
use core::ops::{Add, Div, Mul, Neg, Sub};
use crate::math;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Duration {
sec: i64,
nanos: i32,
}
impl Duration {
pub const ZERO: Self = Self { sec: 0, nanos: 0 };
pub const NANOSECOND: Self = Self { sec: 0, nanos: 1 };
pub const SECOND: Self = Self { sec: 1, nanos: 0 };
pub fn from_parts(sec: i64, nanos: i64) -> Self {
Self::checked_from_parts(sec, nanos).expect("duration overflow")
}
pub fn checked_from_parts(sec: i64, nanos: i64) -> Option<Self> {
let total = (sec as i128)
.checked_mul(1_000_000_000)?
.checked_add(nanos as i128)?;
Self::from_nanoseconds(total)
}
pub fn from_nanoseconds(ns: i128) -> Option<Self> {
let sec = ns.div_euclid(1_000_000_000);
let nanos = ns.rem_euclid(1_000_000_000);
if sec < i64::MIN as i128 || sec > i64::MAX as i128 {
return None;
}
Some(Self {
sec: sec as i64,
nanos: nanos as i32,
})
}
pub fn from_seconds_f64(seconds: f64) -> Self {
Self::checked_from_seconds_f64(seconds).expect("invalid or out-of-range duration")
}
pub fn checked_from_seconds_f64(seconds: f64) -> Option<Self> {
if !seconds.is_finite() {
return None;
}
let whole = math::floor(seconds);
if whole < i64::MIN as f64 || whole >= i64::MAX as f64 {
return None;
}
let frac = seconds - whole;
let nanos = math::round(frac * 1_000_000_000.0) as i64;
Self::checked_from_parts(whole as i64, nanos)
}
pub fn sec(&self) -> i64 {
self.sec
}
pub fn nanos(&self) -> i32 {
self.nanos
}
pub fn total_nanoseconds(&self) -> i128 {
self.sec as i128 * 1_000_000_000 + self.nanos as i128
}
pub fn seconds_f64(&self) -> f64 {
self.sec as f64 + self.nanos as f64 * 1e-9
}
pub fn is_zero(&self) -> bool {
self.sec == 0 && self.nanos == 0
}
pub fn signum(&self) -> i8 {
if self.sec < 0 || (self.sec == 0 && self.nanos < 0) {
-1
} else if self.sec > 0 || self.nanos > 0 {
1
} else {
0
}
}
pub fn abs(&self) -> Self {
if *self < Self::ZERO {
self.checked_neg().expect("duration overflow")
} else {
*self
}
}
pub fn checked_neg(&self) -> Option<Self> {
Self::from_nanoseconds(self.total_nanoseconds().checked_neg()?)
}
pub fn checked_add(&self, other: Self) -> Option<Self> {
Self::from_nanoseconds(
self.total_nanoseconds()
.checked_add(other.total_nanoseconds())?,
)
}
pub fn checked_sub(&self, other: Self) -> Option<Self> {
Self::from_nanoseconds(
self.total_nanoseconds()
.checked_sub(other.total_nanoseconds())?,
)
}
pub fn checked_mul_i64(&self, rhs: i64) -> Option<Self> {
let total = self.total_nanoseconds().checked_mul(rhs as i128)?;
Self::from_nanoseconds(total)
}
pub fn floor(&self, step: Self) -> Self {
let step_ns = positive_step(step);
let total = self.total_nanoseconds();
Self::from_nanoseconds(total.div_euclid(step_ns) * step_ns).expect("duration overflow")
}
pub fn ceil(&self, step: Self) -> Self {
let step_ns = positive_step(step);
let total = self.total_nanoseconds();
let quotient = total.div_euclid(step_ns);
let remainder = total.rem_euclid(step_ns);
let result = if remainder == 0 {
quotient * step_ns
} else {
(quotient + 1) * step_ns
};
Self::from_nanoseconds(result).expect("duration overflow")
}
pub fn round(&self, step: Self) -> Self {
let step_ns = positive_step(step);
let total = self.total_nanoseconds();
let quotient = total.div_euclid(step_ns);
let remainder = total.rem_euclid(step_ns);
let result = if remainder * 2 >= step_ns {
(quotient + 1) * step_ns
} else {
quotient * step_ns
};
Self::from_nanoseconds(result).expect("duration overflow")
}
}
fn positive_step(step: Duration) -> i128 {
let step_ns = step.total_nanoseconds();
assert!(step_ns > 0, "step must be positive");
step_ns
}
impl Add for Duration {
type Output = Duration;
fn add(self, rhs: Self) -> Self::Output {
self.checked_add(rhs).expect("duration overflow")
}
}
impl Sub for Duration {
type Output = Duration;
fn sub(self, rhs: Self) -> Self::Output {
self.checked_sub(rhs).expect("duration overflow")
}
}
impl Neg for Duration {
type Output = Duration;
fn neg(self) -> Self::Output {
self.checked_neg().expect("duration overflow")
}
}
impl Mul<i64> for Duration {
type Output = Duration;
fn mul(self, rhs: i64) -> Self::Output {
self.checked_mul_i64(rhs).expect("duration overflow")
}
}
impl Div<i64> for Duration {
type Output = Duration;
fn div(self, rhs: i64) -> Self::Output {
assert!(rhs != 0, "division by zero");
let total = self.total_nanoseconds() / rhs as i128;
Self::from_nanoseconds(total).expect("duration overflow")
}
}
impl fmt::Display for Duration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let total = self.total_nanoseconds();
let sign = if total < 0 { "-" } else { "" };
let absolute = total.abs();
let sec = absolute / 1_000_000_000;
let nanos = absolute % 1_000_000_000;
if nanos == 0 {
write!(f, "{sign}{sec} s")
} else {
write!(f, "{sign}{sec}.{nanos:09} s")
}
}
}