1use crate::arith::derive_arith;
19use std::ops::Neg;
20
21#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
69#[repr(C)]
70pub struct IntervalMonthDayNano {
71    pub months: i32,
73    pub days: i32,
75    pub nanoseconds: i64,
77}
78
79impl IntervalMonthDayNano {
80    pub const ZERO: Self = Self::new(0, 0, 0);
82
83    pub const ONE: Self = Self::new(1, 1, 1);
85
86    pub const MINUS_ONE: Self = Self::new(-1, -1, -1);
88
89    pub const MAX: Self = Self::new(i32::MAX, i32::MAX, i64::MAX);
91
92    pub const MIN: Self = Self::new(i32::MIN, i32::MIN, i64::MIN);
94
95    #[inline]
97    pub const fn new(months: i32, days: i32, nanoseconds: i64) -> Self {
98        Self {
99            months,
100            days,
101            nanoseconds,
102        }
103    }
104
105    #[inline]
107    pub fn wrapping_abs(self) -> Self {
108        Self {
109            months: self.months.wrapping_abs(),
110            days: self.days.wrapping_abs(),
111            nanoseconds: self.nanoseconds.wrapping_abs(),
112        }
113    }
114
115    #[inline]
117    pub fn checked_abs(self) -> Option<Self> {
118        Some(Self {
119            months: self.months.checked_abs()?,
120            days: self.days.checked_abs()?,
121            nanoseconds: self.nanoseconds.checked_abs()?,
122        })
123    }
124
125    #[inline]
127    pub fn wrapping_neg(self) -> Self {
128        Self {
129            months: self.months.wrapping_neg(),
130            days: self.days.wrapping_neg(),
131            nanoseconds: self.nanoseconds.wrapping_neg(),
132        }
133    }
134
135    #[inline]
137    pub fn checked_neg(self) -> Option<Self> {
138        Some(Self {
139            months: self.months.checked_neg()?,
140            days: self.days.checked_neg()?,
141            nanoseconds: self.nanoseconds.checked_neg()?,
142        })
143    }
144
145    #[inline]
147    pub fn wrapping_add(self, other: Self) -> Self {
148        Self {
149            months: self.months.wrapping_add(other.months),
150            days: self.days.wrapping_add(other.days),
151            nanoseconds: self.nanoseconds.wrapping_add(other.nanoseconds),
152        }
153    }
154
155    #[inline]
157    pub fn checked_add(self, other: Self) -> Option<Self> {
158        Some(Self {
159            months: self.months.checked_add(other.months)?,
160            days: self.days.checked_add(other.days)?,
161            nanoseconds: self.nanoseconds.checked_add(other.nanoseconds)?,
162        })
163    }
164
165    #[inline]
167    pub fn wrapping_sub(self, other: Self) -> Self {
168        Self {
169            months: self.months.wrapping_sub(other.months),
170            days: self.days.wrapping_sub(other.days),
171            nanoseconds: self.nanoseconds.wrapping_sub(other.nanoseconds),
172        }
173    }
174
175    #[inline]
177    pub fn checked_sub(self, other: Self) -> Option<Self> {
178        Some(Self {
179            months: self.months.checked_sub(other.months)?,
180            days: self.days.checked_sub(other.days)?,
181            nanoseconds: self.nanoseconds.checked_sub(other.nanoseconds)?,
182        })
183    }
184
185    #[inline]
187    pub fn wrapping_mul(self, other: Self) -> Self {
188        Self {
189            months: self.months.wrapping_mul(other.months),
190            days: self.days.wrapping_mul(other.days),
191            nanoseconds: self.nanoseconds.wrapping_mul(other.nanoseconds),
192        }
193    }
194
195    pub fn checked_mul(self, other: Self) -> Option<Self> {
197        Some(Self {
198            months: self.months.checked_mul(other.months)?,
199            days: self.days.checked_mul(other.days)?,
200            nanoseconds: self.nanoseconds.checked_mul(other.nanoseconds)?,
201        })
202    }
203
204    #[inline]
206    pub fn wrapping_div(self, other: Self) -> Self {
207        Self {
208            months: self.months.wrapping_div(other.months),
209            days: self.days.wrapping_div(other.days),
210            nanoseconds: self.nanoseconds.wrapping_div(other.nanoseconds),
211        }
212    }
213
214    pub fn checked_div(self, other: Self) -> Option<Self> {
216        Some(Self {
217            months: self.months.checked_div(other.months)?,
218            days: self.days.checked_div(other.days)?,
219            nanoseconds: self.nanoseconds.checked_div(other.nanoseconds)?,
220        })
221    }
222
223    #[inline]
225    pub fn wrapping_rem(self, other: Self) -> Self {
226        Self {
227            months: self.months.wrapping_rem(other.months),
228            days: self.days.wrapping_rem(other.days),
229            nanoseconds: self.nanoseconds.wrapping_rem(other.nanoseconds),
230        }
231    }
232
233    pub fn checked_rem(self, other: Self) -> Option<Self> {
235        Some(Self {
236            months: self.months.checked_rem(other.months)?,
237            days: self.days.checked_rem(other.days)?,
238            nanoseconds: self.nanoseconds.checked_rem(other.nanoseconds)?,
239        })
240    }
241
242    #[inline]
244    pub fn wrapping_pow(self, exp: u32) -> Self {
245        Self {
246            months: self.months.wrapping_pow(exp),
247            days: self.days.wrapping_pow(exp),
248            nanoseconds: self.nanoseconds.wrapping_pow(exp),
249        }
250    }
251
252    #[inline]
254    pub fn checked_pow(self, exp: u32) -> Option<Self> {
255        Some(Self {
256            months: self.months.checked_pow(exp)?,
257            days: self.days.checked_pow(exp)?,
258            nanoseconds: self.nanoseconds.checked_pow(exp)?,
259        })
260    }
261}
262
263impl Neg for IntervalMonthDayNano {
264    type Output = Self;
265
266    #[cfg(debug_assertions)]
267    fn neg(self) -> Self::Output {
268        self.checked_neg().expect("IntervalMonthDayNano overflow")
269    }
270
271    #[cfg(not(debug_assertions))]
272    fn neg(self) -> Self::Output {
273        self.wrapping_neg()
274    }
275}
276
277derive_arith!(
278    IntervalMonthDayNano,
279    Add,
280    AddAssign,
281    add,
282    add_assign,
283    wrapping_add,
284    checked_add
285);
286derive_arith!(
287    IntervalMonthDayNano,
288    Sub,
289    SubAssign,
290    sub,
291    sub_assign,
292    wrapping_sub,
293    checked_sub
294);
295derive_arith!(
296    IntervalMonthDayNano,
297    Mul,
298    MulAssign,
299    mul,
300    mul_assign,
301    wrapping_mul,
302    checked_mul
303);
304derive_arith!(
305    IntervalMonthDayNano,
306    Div,
307    DivAssign,
308    div,
309    div_assign,
310    wrapping_div,
311    checked_div
312);
313derive_arith!(
314    IntervalMonthDayNano,
315    Rem,
316    RemAssign,
317    rem,
318    rem_assign,
319    wrapping_rem,
320    checked_rem
321);
322
323#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
349#[repr(C)]
350pub struct IntervalDayTime {
351    pub days: i32,
353    pub milliseconds: i32,
355}
356
357impl IntervalDayTime {
358    pub const ZERO: Self = Self::new(0, 0);
360
361    pub const ONE: Self = Self::new(1, 1);
363
364    pub const MINUS_ONE: Self = Self::new(-1, -1);
366
367    pub const MAX: Self = Self::new(i32::MAX, i32::MAX);
369
370    pub const MIN: Self = Self::new(i32::MIN, i32::MIN);
372
373    #[inline]
375    pub const fn new(days: i32, milliseconds: i32) -> Self {
376        Self { days, milliseconds }
377    }
378
379    #[inline]
381    pub fn wrapping_abs(self) -> Self {
382        Self {
383            days: self.days.wrapping_abs(),
384            milliseconds: self.milliseconds.wrapping_abs(),
385        }
386    }
387
388    #[inline]
390    pub fn checked_abs(self) -> Option<Self> {
391        Some(Self {
392            days: self.days.checked_abs()?,
393            milliseconds: self.milliseconds.checked_abs()?,
394        })
395    }
396
397    #[inline]
399    pub fn wrapping_neg(self) -> Self {
400        Self {
401            days: self.days.wrapping_neg(),
402            milliseconds: self.milliseconds.wrapping_neg(),
403        }
404    }
405
406    #[inline]
408    pub fn checked_neg(self) -> Option<Self> {
409        Some(Self {
410            days: self.days.checked_neg()?,
411            milliseconds: self.milliseconds.checked_neg()?,
412        })
413    }
414
415    #[inline]
417    pub fn wrapping_add(self, other: Self) -> Self {
418        Self {
419            days: self.days.wrapping_add(other.days),
420            milliseconds: self.milliseconds.wrapping_add(other.milliseconds),
421        }
422    }
423
424    #[inline]
426    pub fn checked_add(self, other: Self) -> Option<Self> {
427        Some(Self {
428            days: self.days.checked_add(other.days)?,
429            milliseconds: self.milliseconds.checked_add(other.milliseconds)?,
430        })
431    }
432
433    #[inline]
435    pub fn wrapping_sub(self, other: Self) -> Self {
436        Self {
437            days: self.days.wrapping_sub(other.days),
438            milliseconds: self.milliseconds.wrapping_sub(other.milliseconds),
439        }
440    }
441
442    #[inline]
444    pub fn checked_sub(self, other: Self) -> Option<Self> {
445        Some(Self {
446            days: self.days.checked_sub(other.days)?,
447            milliseconds: self.milliseconds.checked_sub(other.milliseconds)?,
448        })
449    }
450
451    #[inline]
453    pub fn wrapping_mul(self, other: Self) -> Self {
454        Self {
455            days: self.days.wrapping_mul(other.days),
456            milliseconds: self.milliseconds.wrapping_mul(other.milliseconds),
457        }
458    }
459
460    pub fn checked_mul(self, other: Self) -> Option<Self> {
462        Some(Self {
463            days: self.days.checked_mul(other.days)?,
464            milliseconds: self.milliseconds.checked_mul(other.milliseconds)?,
465        })
466    }
467
468    #[inline]
470    pub fn wrapping_div(self, other: Self) -> Self {
471        Self {
472            days: self.days.wrapping_div(other.days),
473            milliseconds: self.milliseconds.wrapping_div(other.milliseconds),
474        }
475    }
476
477    pub fn checked_div(self, other: Self) -> Option<Self> {
479        Some(Self {
480            days: self.days.checked_div(other.days)?,
481            milliseconds: self.milliseconds.checked_div(other.milliseconds)?,
482        })
483    }
484
485    #[inline]
487    pub fn wrapping_rem(self, other: Self) -> Self {
488        Self {
489            days: self.days.wrapping_rem(other.days),
490            milliseconds: self.milliseconds.wrapping_rem(other.milliseconds),
491        }
492    }
493
494    pub fn checked_rem(self, other: Self) -> Option<Self> {
496        Some(Self {
497            days: self.days.checked_rem(other.days)?,
498            milliseconds: self.milliseconds.checked_rem(other.milliseconds)?,
499        })
500    }
501
502    #[inline]
504    pub fn wrapping_pow(self, exp: u32) -> Self {
505        Self {
506            days: self.days.wrapping_pow(exp),
507            milliseconds: self.milliseconds.wrapping_pow(exp),
508        }
509    }
510
511    #[inline]
513    pub fn checked_pow(self, exp: u32) -> Option<Self> {
514        Some(Self {
515            days: self.days.checked_pow(exp)?,
516            milliseconds: self.milliseconds.checked_pow(exp)?,
517        })
518    }
519}
520
521impl Neg for IntervalDayTime {
522    type Output = Self;
523
524    #[cfg(debug_assertions)]
525    fn neg(self) -> Self::Output {
526        self.checked_neg().expect("IntervalDayMillisecond overflow")
527    }
528
529    #[cfg(not(debug_assertions))]
530    fn neg(self) -> Self::Output {
531        self.wrapping_neg()
532    }
533}
534
535derive_arith!(
536    IntervalDayTime,
537    Add,
538    AddAssign,
539    add,
540    add_assign,
541    wrapping_add,
542    checked_add
543);
544derive_arith!(
545    IntervalDayTime,
546    Sub,
547    SubAssign,
548    sub,
549    sub_assign,
550    wrapping_sub,
551    checked_sub
552);
553derive_arith!(
554    IntervalDayTime,
555    Mul,
556    MulAssign,
557    mul,
558    mul_assign,
559    wrapping_mul,
560    checked_mul
561);
562derive_arith!(
563    IntervalDayTime,
564    Div,
565    DivAssign,
566    div,
567    div_assign,
568    wrapping_div,
569    checked_div
570);
571derive_arith!(
572    IntervalDayTime,
573    Rem,
574    RemAssign,
575    rem,
576    rem_assign,
577    wrapping_rem,
578    checked_rem
579);