Skip to content

Commit aef08cb

Browse files
authored
Implement difference resolution on DateTimeInput (#8147)
Part of #5448 This implements the routine that can compare two dates and figure out what the greatest different field is, including handling flexible day period differences. I figured this is a smaller piece that we can land early reducing the size of the final set of changes. I don't _love_ landing unused code, but "unused and tested" is fine and a good way to build things up piece by piece. This also creates the `range` module where all the rest of this is going to live. ## Changelog: N/A
1 parent ba900d3 commit aef08cb

5 files changed

Lines changed: 264 additions & 0 deletions

File tree

components/datetime/src/format/input.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,12 @@ impl DateTimeInputUnchecked {
154154
.into_option(),
155155
}
156156
}
157+
158+
/// Checks if all the time zone fields (currently: offset, ID, and name timestamp) are the same.
159+
#[allow(dead_code)] // TODO(#5448): range formatting is WIP
160+
pub(crate) fn has_same_zone(&self, other: &Self) -> bool {
161+
self.zone_offset == other.zone_offset
162+
&& self.zone_id == other.zone_id
163+
&& self.zone_name_timestamp == other.zone_name_timestamp
164+
}
157165
}

components/datetime/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ pub mod options;
135135
pub mod parts;
136136
pub mod pattern;
137137
pub mod provider;
138+
/// Date and time range formatting.
139+
#[cfg(feature = "unstable")]
140+
pub mod range;
138141
pub(crate) mod raw;
139142
pub mod scaffold;
140143
pub(crate) mod size_test_macro;

components/datetime/src/provider/names.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,11 @@ impl DayPeriodNames<'_> {
805805
self.names.get(4 + offset)
806806
}
807807
}
808+
/// Gets the day period rules, if present.
809+
pub fn day_period_rules(&self) -> Option<DayPeriodRules> {
810+
let (rules, _) = self.names.get(4)?.split_at_checked(4)?;
811+
DayPeriodRules::decode_from_str(rules)
812+
}
808813
}
809814

810815
/// Calendar-agnostic year name data marker
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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+
pub(crate) mod difference;

0 commit comments

Comments
 (0)