#![no_std]
#[cfg(feature = "std")]
extern crate alloc;
use core::ops::{Add, Sub, Neg, AddAssign, SubAssign, Mul, MulAssign};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct Time(i128);
impl Time {
pub const fn from_unix_nanos(time: i128) -> Self {
Time(time)
}
pub const fn from_unix_secs(time: i64) -> Self {
Time(time as i128 * 1_000_000_000)
}
pub const fn unix_nanos(&self) -> i128 {
self.0
}
pub const fn add(self, duration: Duration) -> Self {
Time(self.0 + duration.0)
}
pub const fn sub(self, duration: Duration) -> Self {
Time(self.0 - duration.0)
}
pub const fn since(self, time: Time) -> Duration {
Duration(self.0 - time.0)
}
pub const fn until(self, time: Time) -> Duration {
Duration(time.0 - self.0)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct Duration(i128);
impl Duration {
pub const fn from_nanos(duration: i128) -> Self {
Duration(duration)
}
pub const fn from_secs(duration: i64) -> Self {
Duration(duration as i128 * 1_000_000_000)
}
pub const fn nanos(&self) -> i128 {
self.0
}
pub const fn times(self, rhs: i128) -> Self {
Duration(self.0 * rhs)
}
pub const SECOND: Duration = Duration(1_000_000_000);
pub const MINUTE: Duration = Duration::SECOND.times(60);
pub const HOUR: Duration = Duration::MINUTE.times(60);
pub const DAY: Duration = Duration::HOUR.times(24);
pub const WEEK: Duration = Duration::DAY.times(7);
}
impl Add<Duration> for Time {
type Output = Time;
fn add(self, rhs: Duration) -> Self::Output {
Time(self.0 + rhs.0)
}
}
impl AddAssign<Duration> for Time {
fn add_assign(&mut self, rhs: Duration) {
self.0 += rhs.0;
}
}
impl Sub<Duration> for Time {
type Output = Time;
fn sub(self, rhs: Duration) -> Self::Output {
Time(self.0 - rhs.0)
}
}
impl SubAssign<Duration> for Time {
fn sub_assign(&mut self, rhs: Duration) {
self.0 -= rhs.0;
}
}
impl Sub<Time> for Time {
type Output = Duration;
fn sub(self, rhs: Time) -> Self::Output {
Duration(self.0 - rhs.0)
}
}
impl Neg for Duration {
type Output = Duration;
fn neg(self) -> Self::Output {
Duration(-self.0)
}
}
impl Mul<i128> for Duration {
type Output = Duration;
fn mul(self, rhs: i128) -> Self::Output {
Duration(self.0 * rhs)
}
}
impl MulAssign<i128> for Duration {
fn mul_assign(&mut self, rhs: i128) {
self.0 *= rhs;
}
}