Debian Codegraph @

debian/rust-vendor/icu_datetime/src/range/difference.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 #![cfg_attr(
6 not(test),
7 allow(
8 unused,
9 reason = "TODO(#5448): difference resolution is not yet used in non-test code"
10 )
11 )]
12
13 use crate::format::DateTimeInputUnchecked;
14 use crate::provider::names::DayPeriodNames;
15 use icu_calendar::types::YearInfo;
16 use icu_time::Hour;
17
18 /// The greatest difference between two datetimes.
19 ///
20 /// Ordered from smallest to largest difference.
21 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
22 #[non_exhaustive]
23 pub enum Difference {
24 /// No difference (inputs are identical).
25 None,
26 /// Difference in second or subsecond (leads to fallback).
27 Second,
28 /// Difference in minute.
29 Minute,
30 /// Difference in hour.
31 Hour,
32 /// Difference in flexible day period.
33 DayPeriodB,
34 /// Difference in standard day period (AM/PM).
35 DayPeriodA,
36 /// Difference in day.
37 Day,
38 /// Difference in month.
39 Month,
40 /// Difference in year.
41 Year,
42 /// Difference in era.
43 Era,
44 /// Incomparable difference (e.g., timezone difference, different calendars).
45 Incomparable,
46 }
47
48 impl Difference {
49 /// Returns whether the difference is in a time field (minute, hour, or day period).
50 pub(crate) fn is_time_diff(self) -> bool {
51 matches!(
52 self,
53 Self::Minute | Self::Hour | Self::DayPeriodB | Self::DayPeriodA
54 )
55 }
56 /// Returns whether the difference is in a date field (day, month, year, era).
57 pub(crate) fn is_date_diff(self) -> bool {
58 matches!(self, Self::Day | Self::Month | Self::Year | Self::Era)
59 }
60 }
61 /// Resolves the greatest difference between two datetimes.
62 ///
63 /// If `dayperiod_names` is provided, it will be used to resolve flexible day periods (`B`).
64 /// If it is `None`, flexible day periods will be assumed to be the same, and only
65 /// standard AM/PM (`a`) will be compared.
66 pub(crate) fn resolve_difference(
67 input1: &DateTimeInputUnchecked,
68 input2: &DateTimeInputUnchecked,
69 dayperiod_names: Option<&DayPeriodNames<'_>>,
70 ) -> Difference {
71 if !input1.has_same_zone(input2) {
72 return Difference::Incomparable;
73 }
74
75 // Compare Date fields
76 match (input1.year, input2.year) {
77 (Some(YearInfo::Era(e1)), Some(YearInfo::Era(e2))) => {
78 if e1.era != e2.era {
79 return Difference::Era;
80 }
81 if e1.year != e2.year {
82 return Difference::Year;
83 }
84 }
85 (Some(YearInfo::Cyclic(c1)), Some(YearInfo::Cyclic(c2))) => {
86 if c1.related_iso != c2.related_iso {
87 return Difference::Year;
88 }
89 }
90 (None, None) => {}
91 _ => {
92 // One input has a year and the other does not (Some/None mismatch),
93 // or they have different year types (e.g., Era vs Cyclic).
94 // This is a major structural difference, so we fall back to Era
95 // (the largest date-specific difference).
96 return Difference::Era;
97 }
98 }
99
100 let month1 = input1.month.map(|m| m.to_input());
101 let month2 = input2.month.map(|m| m.to_input());
102 if month1 != month2 {
103 // This also catches the Some != None case in case
104 // one input chooses to not use month codes. This is
105 // expected: date time formatting typically needs month codes
106 // to work and "unspecified" should count as a difference.
107 return Difference::Month;
108 }
109
110 if input1.day_of_month != input2.day_of_month {
111 return Difference::Day;
112 }
113
114 // Compare Time fields
115 match (input1.hour, input2.hour) {
116 (Some(h1), Some(h2)) => {
117 if h1 != h2 {
118 // Check standard AM/PM (DayPeriodA)
119 let ampm1 = h1.number() < 12;
120 let ampm2 = h2.number() < 12;
121 if ampm1 != ampm2 {
122 return Difference::DayPeriodA;
123 }
124
125 // Check flexible day period (DayPeriodB)
126 if dayperiod_names.is_some_and(|dp| has_flexible_day_period_difference(dp, h1, h2))
127 {
128 return Difference::DayPeriodB;
129 }
130 return Difference::Hour;
131 }
132 }
133 (None, None) => {}
134 _ => {
135 // One input has an hour and the other does not (Some/None mismatch).
136 return Difference::Hour;
137 }
138 }
139
140 if input1.minute != input2.minute {
141 return Difference::Minute;
142 }
143
144 if input1.second != input2.second || input1.subsecond != input2.subsecond {
145 return Difference::Second;
146 }
147
148 Difference::None
149 }
150
151 fn has_flexible_day_period_difference(
152 dayperiod_names: &DayPeriodNames<'_>,
153 hour1: Hour,
154 hour2: Hour,
155 ) -> bool {
156 if let Some(rules) = dayperiod_names.day_period_rules() {
157 return rules.name_offset(hour1) != rules.name_offset(hour2);
158 }
159 false
160 }
161
162 #[cfg(test)]
163 mod tests {
164 use super::*;
165 use icu_calendar::Date;
166 use icu_time::Time;
167
168 fn create_input(y: i32, m: u8, d: u8, hr: u8, min: u8, sec: u8) -> DateTimeInputUnchecked {
169 let mut input = DateTimeInputUnchecked::default();
170 let date = Date::try_new_iso(y, m, d).unwrap();
171 input.set_date_fields_unchecked(date);
172 let time = Time::try_new(hr, min, sec, 0).unwrap();
173 input.set_time_fields(time);
174 input
175 }
176
177 #[test]
178 fn test_identical() {
179 let input1 = create_input(2024, 1, 15, 10, 30, 0);
180 let input2 = create_input(2024, 1, 15, 10, 30, 0);
181 assert_eq!(resolve_difference(&input1, &input2, None), Difference::None);
182 }
183
184 #[test]
185 fn test_year_diff() {
186 let input1 = create_input(2024, 1, 15, 10, 30, 0);
187 let input2 = create_input(2025, 1, 15, 10, 30, 0);
188 assert_eq!(resolve_difference(&input1, &input2, None), Difference::Year);
189 }
190
191 #[test]
192 fn test_month_diff() {
193 let input1 = create_input(2024, 1, 15, 10, 30, 0);
194 let input2 = create_input(2024, 2, 15, 10, 30, 0);
195 assert_eq!(
196 resolve_difference(&input1, &input2, None),
197 Difference::Month
198 );
199 }
200
201 #[test]
202 fn test_day_diff() {
203 let input1 = create_input(2024, 1, 15, 10, 30, 0);
204 let input2 = create_input(2024, 1, 16, 10, 30, 0);
205 assert_eq!(resolve_difference(&input1, &input2, None), Difference::Day);
206 }
207
208 #[test]
209 fn test_hour_diff_same_ampm() {
210 let input1 = create_input(2024, 1, 15, 10, 30, 0);
211 let input2 = create_input(2024, 1, 15, 11, 30, 0);
212 assert_eq!(resolve_difference(&input1, &input2, None), Difference::Hour);
213 }
214
215 #[test]
216 fn test_hour_diff_different_ampm() {
217 let input1 = create_input(2024, 1, 15, 10, 30, 0);
218 let input2 = create_input(2024, 1, 15, 22, 30, 0); // 10 PM
219 assert_eq!(
220 resolve_difference(&input1, &input2, None),
221 Difference::DayPeriodA
222 );
223 }
224
225 #[test]
226 fn test_minute_diff() {
227 let input1 = create_input(2024, 1, 15, 10, 30, 0);
228 let input2 = create_input(2024, 1, 15, 10, 31, 0);
229 assert_eq!(
230 resolve_difference(&input1, &input2, None),
231 Difference::Minute
232 );
233 }
234
235 #[test]
236 fn test_second_diff() {
237 let input1 = create_input(2024, 1, 15, 10, 30, 0);
238 let input2 = create_input(2024, 1, 15, 10, 30, 1);
239 assert_eq!(
240 resolve_difference(&input1, &input2, None),
241 Difference::Second
242 );
243 }
244
245 #[test]
246 fn test_timezone_diff() {
247 let mut input1 = create_input(2024, 1, 15, 10, 30, 0);
248 let mut input2 = create_input(2024, 1, 15, 10, 30, 0);
249 input1.zone_offset = Some(icu_time::zone::UtcOffset::zero());
250 input2.zone_offset = Some(icu_time::zone::UtcOffset::try_from_seconds(3600).unwrap());
251 assert_eq!(
252 resolve_difference(&input1, &input2, None),
253 Difference::Incomparable
254 );
255 }
256 }