use chrono::Duration as ChronoDuration;
use core::fmt::{self, Display};
use std::hash::Hash;
use std::ops;
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct Duration {
duration: ChronoDuration,
year: i64,
month: i64,
}
impl ops::Add<Duration> for Duration {
type Output = Duration;
#[allow(clippy::unwrap_used)]
fn add(self, duration2: Duration) -> Duration {
let new_duration = self.duration().checked_add(duration2.duration()).unwrap();
let mut year = self.year() + duration2.year();
let mut month = self.month() + duration2.month();
if month > 12 {
year += 1;
month -= 12;
}
Duration::new(new_duration, year, month)
}
}
impl ops::Sub<Duration> for Duration {
type Output = Duration;
#[allow(clippy::unwrap_used)]
fn sub(self, duration2: Duration) -> Duration {
let new_duration = self.duration().checked_sub(duration2.duration()).unwrap();
let mut year = self.year() - duration2.year();
let mut month = self.month() - duration2.month();
if month < 1 {
year -= 1;
month += 12;
}
Duration::new(new_duration, year, month)
}
}
impl Duration {
pub fn new(duration: ChronoDuration, year: i64, month: i64) -> Self {
Duration {
duration,
year,
month,
}
}
pub fn duration(&self) -> &ChronoDuration {
&self.duration
}
pub fn year(&self) -> &i64 {
&self.year
}
pub fn month(&self) -> &i64 {
&self.month
}
}
impl Display for Duration {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.duration.fmt(formatter)
}
}