Skip to main content

date_differencer/diff/
time_support.rs

1use time::{OffsetDateTime, PrimitiveDateTime, UtcDateTime};
2
3use super::DateTimeParts;
4
5impl DateTimeParts for PrimitiveDateTime {
6    #[inline]
7    fn year(&self) -> i32 {
8        let (year, ..) = self.to_calendar_date();
9
10        year
11    }
12
13    #[inline]
14    fn month(&self) -> u8 {
15        let (_, month, _) = self.to_calendar_date();
16
17        u8::from(month)
18    }
19
20    #[inline]
21    fn day(&self) -> u8 {
22        let (_, _, day) = self.to_calendar_date();
23
24        day
25    }
26
27    #[inline]
28    fn hour(&self) -> u8 {
29        let (hour, ..) = self.as_hms_nano();
30
31        hour
32    }
33
34    #[inline]
35    fn minute(&self) -> u8 {
36        let (_, minute, ..) = self.as_hms_nano();
37
38        minute
39    }
40
41    #[inline]
42    fn second(&self) -> u8 {
43        let (_, _, second, _) = self.as_hms_nano();
44
45        second
46    }
47
48    #[inline]
49    fn nanosecond(&self) -> u32 {
50        let (_, _, _, nanosecond) = self.as_hms_nano();
51
52        nanosecond
53    }
54}
55
56impl DateTimeParts for OffsetDateTime {
57    #[inline]
58    fn year(&self) -> i32 {
59        let (year, ..) = self.to_calendar_date();
60
61        year
62    }
63
64    #[inline]
65    fn month(&self) -> u8 {
66        let (_, month, _) = self.to_calendar_date();
67
68        u8::from(month)
69    }
70
71    #[inline]
72    fn day(&self) -> u8 {
73        let (_, _, day) = self.to_calendar_date();
74
75        day
76    }
77
78    #[inline]
79    fn hour(&self) -> u8 {
80        let (hour, ..) = self.to_hms_nano();
81
82        hour
83    }
84
85    #[inline]
86    fn minute(&self) -> u8 {
87        let (_, minute, ..) = self.to_hms_nano();
88
89        minute
90    }
91
92    #[inline]
93    fn second(&self) -> u8 {
94        let (_, _, second, _) = self.to_hms_nano();
95
96        second
97    }
98
99    #[inline]
100    fn nanosecond(&self) -> u32 {
101        let (_, _, _, nanosecond) = self.to_hms_nano();
102
103        nanosecond
104    }
105
106    #[inline]
107    fn to_same_time_zone(&self, other: Self) -> Self {
108        other.checked_to_offset(self.offset()).unwrap_or(other)
109    }
110}
111
112impl DateTimeParts for UtcDateTime {
113    #[inline]
114    fn year(&self) -> i32 {
115        let (year, ..) = self.date().to_calendar_date();
116
117        year
118    }
119
120    #[inline]
121    fn month(&self) -> u8 {
122        let (_, month, _) = self.date().to_calendar_date();
123
124        u8::from(month)
125    }
126
127    #[inline]
128    fn day(&self) -> u8 {
129        let (_, _, day) = self.date().to_calendar_date();
130
131        day
132    }
133
134    #[inline]
135    fn hour(&self) -> u8 {
136        let (hour, ..) = self.as_hms_nano();
137
138        hour
139    }
140
141    #[inline]
142    fn minute(&self) -> u8 {
143        let (_, minute, ..) = self.as_hms_nano();
144
145        minute
146    }
147
148    #[inline]
149    fn second(&self) -> u8 {
150        let (_, _, second, _) = self.as_hms_nano();
151
152        second
153    }
154
155    #[inline]
156    fn nanosecond(&self) -> u32 {
157        let (_, _, _, nanosecond) = self.as_hms_nano();
158
159        nanosecond
160    }
161}