drasi_core/evaluation/variable_value/
duration.rs1use chrono::Duration as ChronoDuration;
16use core::fmt::{self, Display};
17use std::hash::Hash;
18use std::ops;
19
20#[derive(Clone, Eq, Hash, PartialEq)]
21pub struct Duration {
22 duration: ChronoDuration,
23 year: i64,
24 month: i64,
25 }
31
32impl ops::Add<Duration> for Duration {
33 type Output = Duration;
34 #[allow(clippy::unwrap_used)]
35 fn add(self, duration2: Duration) -> Duration {
36 let new_duration = self.duration().checked_add(duration2.duration()).unwrap();
37 let mut year = self.year() + duration2.year();
38 let mut month = self.month() + duration2.month();
39 if month > 12 {
40 year += 1;
41 month -= 12;
42 }
43 Duration::new(new_duration, year, month)
44 }
45}
46
47impl ops::Sub<Duration> for Duration {
48 type Output = Duration;
49 #[allow(clippy::unwrap_used)]
50 fn sub(self, duration2: Duration) -> Duration {
51 let new_duration = self.duration().checked_sub(duration2.duration()).unwrap();
52 let mut year = self.year() - duration2.year();
53 let mut month = self.month() - duration2.month();
54 if month < 1 {
55 year -= 1;
56 month += 12;
57 }
58 Duration::new(new_duration, year, month)
59 }
60}
61
62impl Duration {
63 pub fn new(duration: ChronoDuration, year: i64, month: i64) -> Self {
64 Duration {
65 duration,
66 year,
67 month,
68 }
69 }
70
71 pub fn duration(&self) -> &ChronoDuration {
72 &self.duration
73 }
74
75 pub fn year(&self) -> &i64 {
76 &self.year
77 }
78
79 pub fn month(&self) -> &i64 {
80 &self.month
81 }
82}
83
84impl Display for Duration {
85 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
86 self.duration.fmt(formatter)
87 }
88}