tea_time/impls/
impl_timedelta.rs

1use std::cmp::Ordering;
2
3use chrono::Duration;
4
5use crate::TimeDelta;
6
7impl Default for TimeDelta {
8    #[inline(always)]
9    fn default() -> Self {
10        TimeDelta::nat()
11    }
12}
13
14impl From<Duration> for TimeDelta {
15    #[inline(always)]
16    fn from(duration: Duration) -> Self {
17        Self {
18            months: 0,
19            inner: duration,
20        }
21    }
22}
23
24impl From<Option<Duration>> for TimeDelta {
25    #[inline]
26    fn from(duration: Option<Duration>) -> Self {
27        if let Some(duration) = duration {
28            Self::from(duration)
29        } else {
30            Self::nat()
31        }
32    }
33}
34
35impl From<i64> for TimeDelta {
36    #[inline]
37    fn from(dt: i64) -> Self {
38        if dt == i64::MIN {
39            return TimeDelta::nat();
40        }
41        Duration::nanoseconds(dt).into()
42    }
43}
44
45impl From<Option<i64>> for TimeDelta {
46    #[inline]
47    fn from(dt: Option<i64>) -> Self {
48        if let Some(dt) = dt {
49            Self::from(dt)
50        } else {
51            Self::nat()
52        }
53    }
54}
55
56impl PartialOrd for TimeDelta {
57    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
58        if self.is_not_nat() {
59            // may not as expected
60            if self.months != other.months {
61                self.months.partial_cmp(&other.months)
62            } else {
63                self.inner.partial_cmp(&other.inner)
64            }
65        } else {
66            None
67        }
68    }
69}