|
| 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 | + /// Mixed difference (e.g., timezone difference, different calendars). |
| 45 | + Mixed, |
| 46 | +} |
| 47 | + |
| 48 | +/// Resolves the greatest difference between two datetimes. |
| 49 | +/// |
| 50 | +/// If `dayperiod_names` is provided, it will be used to resolve flexible day periods (`B`). |
| 51 | +/// If it is `None`, flexible day periods will be assumed to be the same, and only |
| 52 | +/// standard AM/PM (`a`) will be compared. |
| 53 | +pub(crate) fn resolve_difference( |
| 54 | + input1: &DateTimeInputUnchecked, |
| 55 | + input2: &DateTimeInputUnchecked, |
| 56 | + dayperiod_names: Option<&DayPeriodNames<'_>>, |
| 57 | +) -> Difference { |
| 58 | + if !input1.has_same_zone(input2) { |
| 59 | + return Difference::Mixed; |
| 60 | + } |
| 61 | + |
| 62 | + // Compare Date fields |
| 63 | + match (input1.year, input2.year) { |
| 64 | + (Some(YearInfo::Era(e1)), Some(YearInfo::Era(e2))) => { |
| 65 | + if e1.era != e2.era { |
| 66 | + return Difference::Era; |
| 67 | + } |
| 68 | + if e1.year != e2.year { |
| 69 | + return Difference::Year; |
| 70 | + } |
| 71 | + } |
| 72 | + (Some(YearInfo::Cyclic(c1)), Some(YearInfo::Cyclic(c2))) => { |
| 73 | + if c1.related_iso != c2.related_iso { |
| 74 | + return Difference::Year; |
| 75 | + } |
| 76 | + } |
| 77 | + (None, None) => {} |
| 78 | + _ => { |
| 79 | + // One input has a year and the other does not (Some/None mismatch), |
| 80 | + // or they have different year types (e.g., Era vs Cyclic). |
| 81 | + // This is a major structural difference, so we fall back to Era |
| 82 | + // (the largest date-specific difference). |
| 83 | + return Difference::Era; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + let month1 = input1.month.map(|m| m.to_input()); |
| 88 | + let month2 = input2.month.map(|m| m.to_input()); |
| 89 | + if month1 != month2 { |
| 90 | + // This also catches the Some != None case in case |
| 91 | + // one input chooses to not use month codes. This is |
| 92 | + // expected: date time formatting typically needs month codes |
| 93 | + // to work and "unspecified" should count as a difference. |
| 94 | + return Difference::Month; |
| 95 | + } |
| 96 | + |
| 97 | + if input1.day_of_month != input2.day_of_month { |
| 98 | + return Difference::Day; |
| 99 | + } |
| 100 | + |
| 101 | + // Compare Time fields |
| 102 | + match (input1.hour, input2.hour) { |
| 103 | + (Some(h1), Some(h2)) => { |
| 104 | + if h1 != h2 { |
| 105 | + // Check standard AM/PM (DayPeriodA) |
| 106 | + let ampm1 = h1.number() < 12; |
| 107 | + let ampm2 = h2.number() < 12; |
| 108 | + if ampm1 != ampm2 { |
| 109 | + return Difference::DayPeriodA; |
| 110 | + } |
| 111 | + |
| 112 | + // Check flexible day period (DayPeriodB) |
| 113 | + if dayperiod_names.is_some_and(|dp| has_flexible_day_period_difference(dp, h1, h2)) |
| 114 | + { |
| 115 | + return Difference::DayPeriodB; |
| 116 | + } |
| 117 | + return Difference::Hour; |
| 118 | + } |
| 119 | + } |
| 120 | + (None, None) => {} |
| 121 | + _ => { |
| 122 | + // One input has an hour and the other does not (Some/None mismatch). |
| 123 | + return Difference::Hour; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if input1.minute != input2.minute { |
| 128 | + return Difference::Minute; |
| 129 | + } |
| 130 | + |
| 131 | + if input1.second != input2.second || input1.subsecond != input2.subsecond { |
| 132 | + return Difference::Second; |
| 133 | + } |
| 134 | + |
| 135 | + Difference::None |
| 136 | +} |
| 137 | + |
| 138 | +fn has_flexible_day_period_difference( |
| 139 | + dayperiod_names: &DayPeriodNames<'_>, |
| 140 | + hour1: Hour, |
| 141 | + hour2: Hour, |
| 142 | +) -> bool { |
| 143 | + if let Some(rules) = dayperiod_names.day_period_rules() { |
| 144 | + return rules.name_offset(hour1) != rules.name_offset(hour2); |
| 145 | + } |
| 146 | + false |
| 147 | +} |
| 148 | + |
| 149 | +#[cfg(test)] |
| 150 | +mod tests { |
| 151 | + use super::*; |
| 152 | + use icu_calendar::Date; |
| 153 | + use icu_time::Time; |
| 154 | + |
| 155 | + fn create_input(y: i32, m: u8, d: u8, hr: u8, min: u8, sec: u8) -> DateTimeInputUnchecked { |
| 156 | + let mut input = DateTimeInputUnchecked::default(); |
| 157 | + let date = Date::try_new_iso(y, m, d).unwrap(); |
| 158 | + input.set_date_fields_unchecked(date); |
| 159 | + let time = Time::try_new(hr, min, sec, 0).unwrap(); |
| 160 | + input.set_time_fields(time); |
| 161 | + input |
| 162 | + } |
| 163 | + |
| 164 | + #[test] |
| 165 | + fn test_identical() { |
| 166 | + let input1 = create_input(2024, 1, 15, 10, 30, 0); |
| 167 | + let input2 = create_input(2024, 1, 15, 10, 30, 0); |
| 168 | + assert_eq!(resolve_difference(&input1, &input2, None), Difference::None); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn test_year_diff() { |
| 173 | + let input1 = create_input(2024, 1, 15, 10, 30, 0); |
| 174 | + let input2 = create_input(2025, 1, 15, 10, 30, 0); |
| 175 | + assert_eq!(resolve_difference(&input1, &input2, None), Difference::Year); |
| 176 | + } |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn test_month_diff() { |
| 180 | + let input1 = create_input(2024, 1, 15, 10, 30, 0); |
| 181 | + let input2 = create_input(2024, 2, 15, 10, 30, 0); |
| 182 | + assert_eq!( |
| 183 | + resolve_difference(&input1, &input2, None), |
| 184 | + Difference::Month |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + #[test] |
| 189 | + fn test_day_diff() { |
| 190 | + let input1 = create_input(2024, 1, 15, 10, 30, 0); |
| 191 | + let input2 = create_input(2024, 1, 16, 10, 30, 0); |
| 192 | + assert_eq!(resolve_difference(&input1, &input2, None), Difference::Day); |
| 193 | + } |
| 194 | + |
| 195 | + #[test] |
| 196 | + fn test_hour_diff_same_ampm() { |
| 197 | + let input1 = create_input(2024, 1, 15, 10, 30, 0); |
| 198 | + let input2 = create_input(2024, 1, 15, 11, 30, 0); |
| 199 | + assert_eq!(resolve_difference(&input1, &input2, None), Difference::Hour); |
| 200 | + } |
| 201 | + |
| 202 | + #[test] |
| 203 | + fn test_hour_diff_different_ampm() { |
| 204 | + let input1 = create_input(2024, 1, 15, 10, 30, 0); |
| 205 | + let input2 = create_input(2024, 1, 15, 22, 30, 0); // 10 PM |
| 206 | + assert_eq!( |
| 207 | + resolve_difference(&input1, &input2, None), |
| 208 | + Difference::DayPeriodA |
| 209 | + ); |
| 210 | + } |
| 211 | + |
| 212 | + #[test] |
| 213 | + fn test_minute_diff() { |
| 214 | + let input1 = create_input(2024, 1, 15, 10, 30, 0); |
| 215 | + let input2 = create_input(2024, 1, 15, 10, 31, 0); |
| 216 | + assert_eq!( |
| 217 | + resolve_difference(&input1, &input2, None), |
| 218 | + Difference::Minute |
| 219 | + ); |
| 220 | + } |
| 221 | + |
| 222 | + #[test] |
| 223 | + fn test_second_diff() { |
| 224 | + let input1 = create_input(2024, 1, 15, 10, 30, 0); |
| 225 | + let input2 = create_input(2024, 1, 15, 10, 30, 1); |
| 226 | + assert_eq!( |
| 227 | + resolve_difference(&input1, &input2, None), |
| 228 | + Difference::Second |
| 229 | + ); |
| 230 | + } |
| 231 | + |
| 232 | + #[test] |
| 233 | + fn test_timezone_diff() { |
| 234 | + let mut input1 = create_input(2024, 1, 15, 10, 30, 0); |
| 235 | + let mut input2 = create_input(2024, 1, 15, 10, 30, 0); |
| 236 | + input1.zone_offset = Some(icu_time::zone::UtcOffset::zero()); |
| 237 | + input2.zone_offset = Some(icu_time::zone::UtcOffset::try_from_seconds(3600).unwrap()); |
| 238 | + assert_eq!( |
| 239 | + resolve_difference(&input1, &input2, None), |
| 240 | + Difference::Mixed |
| 241 | + ); |
| 242 | + } |
| 243 | +} |
0 commit comments