Debian Codegraph @

debian/rust-vendor/icu_datetime/tests/range.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 use icu_calendar::Date;
6 use icu_datetime::fieldsets;
7 use icu_datetime::input::{DateTime, Time};
8 use icu_datetime::range::{DateRangeFormatter, FixedCalendarDateRangeFormatter};
9 use icu_locale_core::locale;
10 use writeable::assert_writeable_eq;
11
12 #[test]
13 fn test_date_range_gregorian() {
14 let start = DateTime {
15 date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
16 time: Time::try_new(9, 0, 0, 0).unwrap(),
17 };
18 let end_same_day = DateTime {
19 date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
20 time: Time::try_new(17, 0, 0, 0).unwrap(),
21 };
22 let end_next_day = DateTime {
23 date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
24 time: Time::try_new(17, 0, 0, 0).unwrap(),
25 };
26 let end_next_month = DateTime {
27 date: Date::try_new_gregorian(2024, 1, 5).unwrap(),
28 time: Time::try_new(17, 0, 0, 0).unwrap(),
29 };
30
31 // 1. Date-only range (YMD medium)
32 {
33 let fmt = FixedCalendarDateRangeFormatter::try_new(
34 locale!("en").into(),
35 fieldsets::YMD::medium(),
36 )
37 .unwrap();
38
39 // Same day: should format as single date
40 assert_writeable_eq!(fmt.format(&start, &end_same_day), "Dec 22, 2023");
41
42 // Next day: day diff (uses thin spaces around en-dash)
43 assert_writeable_eq!(
44 fmt.format(&start, &end_next_day),
45 "Dec 22\u{2009}–\u{2009}23, 2023"
46 );
47
48 // Next month: month/year diff (falls back to full range fallback since year differs)
49 assert_writeable_eq!(
50 fmt.format(&start, &end_next_month),
51 "Dec 22, 2023\u{2009}–\u{2009}Jan 5, 2024"
52 );
53 }
54
55 // 2. Time-only range (HM)
56 {
57 let fmt =
58 FixedCalendarDateRangeFormatter::try_new(locale!("en").into(), fieldsets::T::hm())
59 .unwrap();
60
61 // Same day, different time (different day period AM/PM)
62 assert_writeable_eq!(
63 fmt.format(&start.time, &end_same_day.time),
64 "9:00\u{202f}AM\u{2009}–\u{2009}5:00\u{202f}PM"
65 );
66
67 // Same day, different time (same day period AM)
68 let end_same_am = Time::try_new(11, 0, 0, 0).unwrap();
69 assert_writeable_eq!(
70 fmt.format(&start.time, &end_same_am),
71 "9:00\u{2009}–\u{2009}11:00\u{202f}AM"
72 );
73
74 // Different day: time-only formatter with date diff.
75 // Should fall back to formatting both times fully.
76 assert_writeable_eq!(
77 fmt.format(&start, &end_next_day),
78 "9:00\u{202f}AM\u{2009}–\u{2009}5:00\u{202f}PM"
79 );
80 }
81
82 // 3. Date and Time range (YMDT medium)
83 {
84 let fmt = FixedCalendarDateRangeFormatter::try_new(
85 locale!("en").into(),
86 fieldsets::YMDT::medium(),
87 )
88 .unwrap();
89
90 // Same day, different time: only time differs.
91 // Case 3: Mixed range, only time differs. Date formatted once, glued to time range.
92 assert_writeable_eq!(
93 fmt.format(&start, &end_same_day),
94 "Dec 22, 2023, 9:00\u{202f}AM\u{2009}–\u{2009}5:00\u{202f}PM"
95 );
96
97 // Different day, different time: date differs.
98 // Case 4: Mixed range, date differs. Falls back to full range fallback.
99 assert_writeable_eq!(
100 fmt.format(&start, &end_next_day),
101 "Dec 22, 2023, 9:00:00\u{202f}AM\u{2009}–\u{2009}Dec 23, 2023, 5:00:00\u{202f}PM"
102 );
103 }
104
105 // 4. Edge Case: Date-only formatter with time difference
106 {
107 let fmt = FixedCalendarDateRangeFormatter::try_new(
108 locale!("en").into(),
109 fieldsets::YMD::medium(),
110 )
111 .unwrap();
112
113 // Date-only formatter, but input has time diff (date is same).
114 // Should format as a single date (no range).
115 assert_writeable_eq!(fmt.format(&start, &end_same_day), "Dec 22, 2023");
116 }
117 }
118
119 #[test]
120 fn test_date_range_any_calendar_dynamic_conversion() {
121 let start = DateTime {
122 date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
123 time: Time::try_new(9, 0, 0, 0).unwrap(),
124 };
125 let end_next_day = DateTime {
126 date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
127 time: Time::try_new(17, 0, 0, 0).unwrap(),
128 };
129
130 // Explicitly request Buddhist calendar in the locale
131 let fmt =
132 DateRangeFormatter::try_new(locale!("th-u-ca-buddhist").into(), fieldsets::YMD::medium())
133 .unwrap();
134
135 // Gregorian input should be dynamically converted to Buddhist (2023 -> 2566)
136 // Thai day range pattern has no spaces around en-dash: "22–23 ธ.ค. 2566"
137 assert_writeable_eq!(fmt.format(&start, &end_next_day), "22–23 ธ.ค. 2566");
138 }
139
140 #[test]
141 fn test_date_range_timezone() {
142 use icu_calendar::Date;
143 use icu_datetime::fieldsets;
144 use icu_datetime::input::Time;
145 use icu_datetime::range::FixedCalendarDateRangeFormatter;
146 use icu_locale_core::locale;
147 use icu_time::zone::UtcOffset;
148 use icu_time::{TimeZone, ZonedDateTime};
149 use writeable::assert_writeable_eq;
150
151 let zone1 = TimeZone::from_iana_id("Europe/Paris")
152 .with_offset(Some(UtcOffset::try_from_seconds(3600).unwrap())); // UTC+1
153 let zone2 = TimeZone::from_iana_id("Europe/Athens")
154 .with_offset(Some(UtcOffset::try_from_seconds(7200).unwrap())); // UTC+2
155
156 let start = ZonedDateTime {
157 date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
158 time: Time::try_new(9, 0, 0, 0).unwrap(),
159 zone: zone1,
160 };
161 let end = ZonedDateTime {
162 date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
163 time: Time::try_new(17, 0, 0, 0).unwrap(),
164 zone: zone2,
165 };
166
167 // YMDT with zone
168 let fmt = FixedCalendarDateRangeFormatter::try_new(
169 locale!("en").into(),
170 fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort),
171 )
172 .unwrap();
173
174 // Since timezones differ, it must fall back to formatting both sides fully and gluing them.
175 assert_writeable_eq!(
176 fmt.format(&start, &end),
177 "Dec 22, 2023, 9:00:00\u{202f}AM GMT+1\u{2009}–\u{2009}Dec 22, 2023, 5:00:00\u{202f}PM GMT+2"
178 );
179 }
180
181 #[test]
182 fn test_date_range_hebrew_leap() {
183 use icu_calendar::Date;
184 use icu_calendar::cal::Hebrew;
185 use icu_datetime::fieldsets;
186 use icu_datetime::input::{DateTime, Time};
187 use icu_datetime::range::FixedCalendarDateRangeFormatter;
188 use icu_locale_core::locale;
189 use writeable::assert_writeable_eq;
190
191 // 2011 was a Hebrew leap year (5771).
192 // 2011-03-04 is 28 Adar I 5771.
193 // 2011-04-03 is 28 Adar II 5771.
194 let start_greg = Date::try_new_gregorian(2011, 3, 4).unwrap();
195 let end_greg = Date::try_new_gregorian(2011, 4, 3).unwrap();
196
197 let start = DateTime {
198 date: start_greg.to_calendar(Hebrew),
199 time: Time::try_new(9, 0, 0, 0).unwrap(),
200 };
201 let end = DateTime {
202 date: end_greg.to_calendar(Hebrew),
203 time: Time::try_new(17, 0, 0, 0).unwrap(),
204 };
205
206 let fmt =
207 FixedCalendarDateRangeFormatter::try_new(locale!("en").into(), fieldsets::YMD::medium())
208 .unwrap();
209
210 // Should format as a range spanning Adar I and Adar II.
211 assert_writeable_eq!(
212 fmt.format(&start, &end),
213 "28 Adar I\u{2009}–\u{2009}28 Adar II 5771"
214 );
215 }
216
217 /// Range version of `test_minute_optional_hour_cycle` from `simple_test` (PR #8237)
218 #[test]
219 fn test_minute_optional_hour_cycle() {
220 use icu_datetime::options::TimePrecision;
221 use icu_datetime::range::NoCalendarRangeFormatter;
222
223 let time = Time::try_new(7, 0, 0, 0).unwrap();
224 let time_hour_diff = Time::try_new(8, 0, 0, 0).unwrap();
225 let time_minute_diff = Time::try_new(7, 15, 0, 0).unwrap();
226
227 let fs = fieldsets::T::short().with_time_precision(TimePrecision::MinuteOptional);
228
229 // en-US (default h12): zero minutes omitted
230 let fmt_en = NoCalendarRangeFormatter::try_new(locale!("en-US").into(), fs).unwrap();
231 assert_writeable_eq!(
232 fmt_en.format(&time, &time_hour_diff),
233 "7\u{2009}–\u{2009}8\u{202f}AM"
234 );
235 assert_writeable_eq!(
236 fmt_en.format(&time, &time_minute_diff),
237 // The range pattern takes precedence here
238 "7:00\u{2009}–\u{2009}7:15\u{202f}AM"
239 );
240
241 // fr (default h23): zero minutes retained
242 let fmt_fr = NoCalendarRangeFormatter::try_new(locale!("fr").into(), fs).unwrap();
243 assert_writeable_eq!(
244 fmt_fr.format(&time, &time_hour_diff),
245 "07:00\u{2009}–\u{2009}08:00"
246 );
247 assert_writeable_eq!(
248 fmt_fr.format(&time, &time_minute_diff),
249 "07:00\u{2009}–\u{2009}07:15"
250 );
251
252 // en-US with -u-hc-h23 override: zero minutes retained
253 let fmt_en_h23 =
254 NoCalendarRangeFormatter::try_new(locale!("en-US-u-hc-h23").into(), fs).unwrap();
255 assert_writeable_eq!(
256 fmt_en_h23.format(&time, &time_hour_diff),
257 "07:00\u{2009}–\u{2009}08:00"
258 );
259 assert_writeable_eq!(
260 fmt_en_h23.format(&time, &time_minute_diff),
261 "07:00\u{2009}–\u{2009}07:15"
262 );
263
264 // fr with -u-hc-h12 override: zero minutes omitted
265 let fmt_fr_12 = NoCalendarRangeFormatter::try_new(locale!("fr-u-hc-h12").into(), fs).unwrap();
266 assert_writeable_eq!(
267 fmt_fr_12.format(&time, &time_hour_diff),
268 "7\u{2009}–\u{2009}8\u{202f}AM"
269 );
270 assert_writeable_eq!(
271 fmt_fr_12.format(&time, &time_minute_diff),
272 // The range pattern takes precedence here
273 "7:00\u{2009}–\u{2009}7:15\u{202f}AM"
274 );
275
276 // fr with -u-hc-h11 override: zero minutes omitted
277 let fmt_fr_h11 = NoCalendarRangeFormatter::try_new(locale!("fr-u-hc-h11").into(), fs).unwrap();
278 assert_writeable_eq!(
279 fmt_fr_h11.format(&time, &time_hour_diff),
280 "7\u{2009}–\u{2009}8\u{202f}AM"
281 );
282 assert_writeable_eq!(
283 fmt_fr_h11.format(&time, &time_minute_diff),
284 // The range pattern takes precedence here
285 "7:00\u{2009}–\u{2009}7:15\u{202f}AM"
286 );
287
288 // en-US with -u-hc-c24 override: zero minutes retained
289 let fmt_en_c24 =
290 NoCalendarRangeFormatter::try_new(locale!("en-US-u-hc-c24").into(), fs).unwrap();
291 assert_writeable_eq!(
292 fmt_en_c24.format(&time, &time_hour_diff),
293 "07:00\u{2009}–\u{2009}08:00"
294 );
295 assert_writeable_eq!(
296 fmt_en_c24.format(&time, &time_minute_diff),
297 "07:00\u{2009}–\u{2009}07:15"
298 );
299
300 // fr with -u-hc-c12 override: zero minutes omitted
301 let fmt_fr_c12 = NoCalendarRangeFormatter::try_new(locale!("fr-u-hc-c12").into(), fs).unwrap();
302 assert_writeable_eq!(
303 fmt_fr_c12.format(&time, &time_hour_diff),
304 "7\u{2009}–\u{2009}8\u{202f}AM"
305 );
306 assert_writeable_eq!(
307 fmt_fr_c12.format(&time, &time_minute_diff),
308 // The range pattern takes precedence here
309 "7:00\u{2009}–\u{2009}7:15\u{202f}AM"
310 );
311 }
312
313 #[test]
314 fn test_date_range_ej() {
315 use icu_calendar::Date;
316 use icu_datetime::fieldsets;
317 use icu_datetime::input::{DateTime, Time};
318 use icu_datetime::range::{DateRangeFormatter, FixedCalendarDateRangeFormatter};
319 use icu_locale_core::locale;
320 use writeable::assert_writeable_eq;
321
322 let start_greg = Date::try_new_gregorian(2024, 8, 9).unwrap(); // Friday
323 let end_greg = Date::try_new_gregorian(2024, 8, 10).unwrap(); // Saturday
324
325 let start = DateTime {
326 date: start_greg,
327 time: Time::try_new(20, 40, 0, 0).unwrap(),
328 };
329 let end_same_day = DateTime {
330 date: start_greg,
331 time: Time::try_new(21, 50, 0, 0).unwrap(),
332 };
333 let end_next_day = DateTime {
334 date: end_greg,
335 time: Time::try_new(21, 50, 0, 0).unwrap(),
336 };
337
338 let fs = fieldsets::E::short().with_time_hm();
339
340 let fmt = FixedCalendarDateRangeFormatter::try_new(locale!("en-US").into(), fs).unwrap();
341 let any_cal_fmt = DateRangeFormatter::try_new(locale!("en-US").into(), fs).unwrap();
342
343 // Overlap patterns like `ej` (weekday + time) operate as unified date-time patterns
344 // without independent date and glue separation in `DateTimeZonePatternSelectionData`.
345 // When range formatting is invoked, both formatters cleanly produce the expected
346 // fallback range output without field conflicts.
347 //
348 // Note: This behavior relies on the assumption that any locale in CLDR supporting `ej` in
349 // date/time patterns also defines corresponding interval range pattern data. This invariant
350 // is verified across all locales and calendars in datagen tests:
351 // `icu_provider_source::datetime::range_patterns::tests`.
352 assert_writeable_eq!(
353 fmt.format(&start, &end_same_day),
354 "Fri 8:40\u{202f}PM\u{2009}–\u{2009}Fri 9:50\u{202f}PM"
355 );
356 assert_writeable_eq!(
357 fmt.format(&start, &end_next_day),
358 "Fri 8:40\u{202f}PM\u{2009}–\u{2009}Sat 9:50\u{202f}PM"
359 );
360
361 assert_writeable_eq!(
362 any_cal_fmt.format(&start, &end_same_day),
363 "Fri 8:40\u{202f}PM\u{2009}–\u{2009}Fri 9:50\u{202f}PM"
364 );
365 assert_writeable_eq!(
366 any_cal_fmt.format(&start, &end_next_day),
367 "Fri 8:40\u{202f}PM\u{2009}–\u{2009}Sat 9:50\u{202f}PM"
368 );
369 }
370
371 // Tests documenting issues when date range formatting falls back to `root` (`und`).
372 //
373 // TODO(#8359): Implement a fix and enable these tests.
374 //
375 // When a locale or calendar lacks custom interval patterns in CLDR, locale fallback walks up
376 // the hierarchy to `root` (`und`). CLDR's `root` defines interval patterns using English/ISO conventions
377 // (e.g. `y MMM d–d`, `G y-MM-dd–y-MM-dd`). This causes two major categories of problems:
378 //
379 // 1. Missing Names: The `root` range pattern often contains
380 // symbols/lengths (such as `MMM` or `G`) that were not loaded by the single `DateTimeFormatter`
381 // (e.g., `zh` whose single date pattern uses numeric month `M`, or `YMD::long()` which loads
382 // `MMMM` but not `MMM`). This can lead to violations of assumptions.
383 //
384 // 2. Incorrect patterns: `root` patterns impose YMD order on
385 // locales that use Day-Month-Year (such as German, French, Spanish). The results
386 // will be incorrect, and, importantly, not consistent with the results of regular date
387 // formatting, which can be confusing.
388
389 /// Chinese (zh) with `YMD::medium()`:
390 /// Single date pattern is "y年M月d日" (numeric month 'M').
391 /// Single `DateTimeFormatter` only loads numeric month data, not abbreviated month names ("MMM").
392 /// But range pattern falls back to `root` (`und`), which specifies "y MMM d–d".
393 /// When executing the `root` pattern, `FormattedSingleSide` fails with `NamesNotLoaded`.
394 #[cfg(debug_assertions)]
395 #[test]
396 #[should_panic(expected = "unexpected error in FormattedSingleSide: NamesNotLoaded")]
397 fn test_root_fallback_names_not_loaded_zh() {
398 use icu_calendar::Date;
399 use icu_datetime::fieldsets;
400 use icu_datetime::input::{DateTime, Time};
401 use icu_datetime::range::DateRangeFormatter;
402 use icu_locale_core::locale;
403
404 let start = DateTime {
405 date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
406 time: Time::try_new(9, 0, 0, 0).unwrap(),
407 };
408 let end_day = DateTime {
409 date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
410 time: Time::try_new(17, 0, 0, 0).unwrap(),
411 };
412
413 let fmt_zh =
414 DateRangeFormatter::try_new(locale!("zh").into(), fieldsets::YMD::medium()).unwrap();
415 let _ = fmt_zh.format(&start, &end_day).to_string();
416 }
417
418 /// Spanish with Hebrew calendar (es-u-ca-hebrew) and `YMD::long()`:
419 /// Single `DateTimeFormatter` for `YMD::long()` loads full month names ("MMMM"), but range pattern
420 /// falls back to `root` (`und`), which uses abbreviated month names ("MMM").
421 #[cfg(debug_assertions)]
422 #[test]
423 #[should_panic(expected = "unexpected error in FormattedSingleSide: NamesNotLoaded")]
424 fn test_root_fallback_names_not_loaded_es_hebrew() {
425 use icu_calendar::Date;
426 use icu_datetime::fieldsets;
427 use icu_datetime::input::{DateTime, Time};
428 use icu_datetime::range::DateRangeFormatter;
429 use icu_locale_core::locale;
430
431 let start = DateTime {
432 date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
433 time: Time::try_new(9, 0, 0, 0).unwrap(),
434 };
435 let end_day = DateTime {
436 date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
437 time: Time::try_new(17, 0, 0, 0).unwrap(),
438 };
439
440 let fmt_es_hebrew =
441 DateRangeFormatter::try_new(locale!("es-u-ca-hebrew").into(), fieldsets::YMD::long())
442 .unwrap();
443 let _ = fmt_es_hebrew.format(&start, &end_day).to_string();
444 }
445
446 /// German with Buddhist calendar (de-u-ca-buddhist) and `YMD::long()`:
447 /// Single `DateTimeFormatter` for `YMD::long()` loads full month names ("MMMM"), but range pattern
448 /// falls back to `root` (`und`), which uses abbreviated month names ("MMM").
449 #[cfg(debug_assertions)]
450 #[test]
451 #[should_panic(expected = "unexpected error in FormattedSingleSide: NamesNotLoaded")]
452 fn test_root_fallback_names_not_loaded_de_buddhist() {
453 use icu_calendar::Date;
454 use icu_datetime::fieldsets;
455 use icu_datetime::input::{DateTime, Time};
456 use icu_datetime::range::DateRangeFormatter;
457 use icu_locale_core::locale;
458
459 let start = DateTime {
460 date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
461 time: Time::try_new(9, 0, 0, 0).unwrap(),
462 };
463 let end_day = DateTime {
464 date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
465 time: Time::try_new(17, 0, 0, 0).unwrap(),
466 };
467
468 let fmt_de_buddhist =
469 DateRangeFormatter::try_new(locale!("de-u-ca-buddhist").into(), fieldsets::YMD::long())
470 .unwrap();
471 let _ = fmt_de_buddhist.format(&start, &end_day).to_string();
472 }
473
474 #[test]
475 fn test_root_fallback_issues_field_order() {
476 use icu_calendar::Date;
477 use icu_datetime::fieldsets;
478 use icu_datetime::input::{DateTime, Time};
479 use icu_datetime::range::DateRangeFormatter;
480 use icu_locale_core::locale;
481 use writeable::assert_writeable_eq;
482
483 let start = DateTime {
484 date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
485 time: Time::try_new(9, 0, 0, 0).unwrap(),
486 };
487 let end_day = DateTime {
488 date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
489 time: Time::try_new(17, 0, 0, 0).unwrap(),
490 };
491
492 // German (de) uses Day.Month.Year order (e.g. "22.12.2023").
493 // For Buddhist calendar in YMD::medium(), `de` has no custom range pattern in CLDR.
494 // Falling back to `root` uses the root pattern "G y-MM-dd – y-MM-dd" (ISO order with era first).
495 let fmt_de_buddhist =
496 DateRangeFormatter::try_new(locale!("de-u-ca-buddhist").into(), fieldsets::YMD::medium())
497 .unwrap();
498 // This produces "BE 2566-12-22 – 2566-12-23" instead of German Day-first order:
499 assert_writeable_eq!(
500 fmt_de_buddhist.format(&start, &end_day),
501 "BE 2566-12-22\u{2009}–\u{2009}2566-12-23"
502 );
503
504 // Hebrew calendar in German:
505 let fmt_de_hebrew =
506 DateRangeFormatter::try_new(locale!("de-u-ca-hebrew").into(), fieldsets::YMD::medium())
507 .unwrap();
508 // This produces "AM 5784-04-10 – 5784-04-11" instead of German Day-first order:
509 assert_writeable_eq!(
510 fmt_de_hebrew.format(&start, &end_day),
511 "AM 5784-04-10\u{2009}–\u{2009}5784-04-11"
512 );
513 }