1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use crate::Interval;
use std::ops;

impl Interval {
    /// Checked interval subtraction. Computes `Interval - Interval` and `None` if there
    /// was an underflow.
    pub fn checked_sub(self, other_interval: Interval) -> Option<Interval> {
        Some(Interval {
            months: self.months.checked_sub(other_interval.months)?,
            days: self.days.checked_sub(other_interval.days)?,
            microseconds: self.microseconds.checked_sub(other_interval.microseconds)?,
        })
    }

    /// Shortcut method to subtract day time part to the interval. Any units smaller than
    /// a microsecond will be truncated.
    pub fn sub_day_time(self, days: i32, hours: i64, minutes: i64, seconds: f64) -> Interval {
        let hours_as_micro: i64 = hours * 3_600_000_000;
        let minutes_as_micro: i64 = minutes * 60_000_000;
        let seconds_as_micro: i64 = (seconds * 1_000_000.0).floor() as i64;
        let additional_micro: i64 = hours_as_micro + minutes_as_micro + seconds_as_micro;
        Interval {
            months: self.months,
            days: self.days - days,
            microseconds: self.microseconds - additional_micro,
        }
    }

    /// Checked day time subtraction. Computes the interval and will return `None` if a
    /// overflow/underflow has occured. Any units smaller than a microsecond will be truncated.
    pub fn checked_sub_day_time(
        self,
        days: i32,
        hours: i64,
        minutes: i64,
        seconds: f64,
    ) -> Option<Interval> {
        let hours_as_micro: i64 = hours.checked_mul(3_600_000_000)?;
        let minutes_as_micro: i64 = minutes.checked_mul(60_000_000)?;
        let seconds_as_micro: i64 = (seconds * 1_000_000.0).floor() as i64;
        let subtracted_micro: i64 = hours_as_micro
            .checked_add(minutes_as_micro)?
            .checked_add(seconds_as_micro)?;
        Some(Interval {
            months: self.months,
            days: self.days.checked_sub(days)?,
            microseconds: self.microseconds.checked_sub(subtracted_micro)?,
        })
    }

    /// Subtracts a year month interval.
    pub fn sub_year_month(self, year: i32, months: i32) -> Interval {
        let years_as_months = year * 12;
        Interval {
            months: self.months - years_as_months - months,
            days: self.days,
            microseconds: self.microseconds,
        }
    }

    /// Checked year month subtraction. Computes the interval and will return `None` if a
    /// overflow has occured.
    pub fn checked_sub_year_month(self, year: i32, months: i32) -> Option<Interval> {
        let years_as_months = year.checked_mul(12)?;
        Some(Interval {
            months: self
                .months
                .checked_sub(years_as_months)?
                .checked_sub(months)?,
            days: self.days,
            microseconds: self.microseconds,
        })
    }
}

impl ops::Sub for Interval {
    type Output = Interval;
    fn sub(self, other_interval: Interval) -> Interval {
        Interval {
            months: self.months - other_interval.months,
            days: self.days - other_interval.days,
            microseconds: self.microseconds - other_interval.microseconds,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sub_day_time() {
        let interval = Interval::new(13, 0, 0);
        let result = interval.sub_day_time(2, 0, 0, 2.12);
        assert_eq!(result, Interval::new(13, -2, -2120000));
    }

    #[test]
    fn test_checked_sub_day_time() {
        let interval = Interval::new(13, 0, 0);
        let result = interval.checked_sub_day_time(2, 0, 0, 2.12);
        assert_eq!(result, Some(Interval::new(13, -2, -2120000)));
    }

    #[test]
    fn test_checked_sub_day_time_2() {
        let interval = Interval::new(13, i32::min_value(), 0);
        println!("{:?}", interval.days);
        let result = interval.checked_sub_day_time(100, 0, 0, 2.12);
        println!("{:?}", result);
        assert_eq!(result, None);
    }

    #[test]
    fn test_sub_year_month() {
        let interval = Interval::new(13, 0, 0);
        let result = interval.sub_year_month(1, 1);
        assert_eq!(result, Interval::new(0, 0, 0));
    }

    #[test]
    fn test_checked_sub_year_month_1() {
        let interval = Interval::new(13, 0, 0);
        let result = interval.checked_sub_year_month(1, 1);
        assert_eq!(result, Some(Interval::new(0, 0, 0)));
    }

    #[test]
    fn test_checked_sub_year_month_2() {
        let interval = Interval::new(-20, 0, 0);
        let result = interval.checked_sub_year_month(i32::min_value(), 1);
        assert_eq!(result, None);
    }

    #[test]
    fn test_sub() {
        let interval = Interval::new(30, 0, 0);
        let other_interval = Interval::new(20, 0, 0);
        let result = interval - other_interval;
        assert_eq!(result, Interval::new(10, 0, 0));
    }

    #[test]
    fn test_checked_sub() {
        let interval = Interval::new(13, 0, 0);
        let interval_sub = Interval::new(2, 0, 0);
        let result = interval.checked_sub(interval_sub);
        assert_eq!(result, Some(Interval::new(11, 0, 0)));
    }

    #[test]
    fn test_checked_sub_2() {
        let interval = Interval::new(-10, 0, 0);
        let interval_sub = Interval::new(i32::max_value(), 0, 0);
        let result = interval.checked_sub(interval_sub);
        assert_eq!(result, None);
    }

}