icu_datetime 2.4.0

Human-readable formatting of dates, times, and time zones in hundreds of locales
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use icu_calendar::Date;
use icu_datetime::fieldsets;
use icu_datetime::input::{DateTime, Time};
use icu_datetime::parts as datetime_parts;
use icu_datetime::range::{DateRangeFormatter, FixedCalendarDateRangeFormatter};
use icu_decimal::parts as decimal_parts;
use icu_locale_core::locale;
use writeable::{assert_writeable_eq, assert_writeable_parts_eq};

#[test]
fn test_date_range_gregorian() {
    let start = DateTime {
        date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
        time: Time::try_new(9, 0, 0, 0).unwrap(),
    };
    let end_same_day = DateTime {
        date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
    };
    let end_next_day = DateTime {
        date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
    };
    let end_next_month = DateTime {
        date: Date::try_new_gregorian(2024, 1, 5).unwrap(),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
    };

    // 1. Date-only range (YMD medium)
    {
        let fmt = FixedCalendarDateRangeFormatter::try_new(
            locale!("en").into(),
            fieldsets::YMD::medium(),
        )
        .unwrap();

        // Same day: should format as single date
        assert_writeable_eq!(fmt.format(&start, &end_same_day), "Dec 22, 2023");

        // Next day: day diff (uses thin spaces around en-dash)
        assert_writeable_eq!(
            fmt.format(&start, &end_next_day),
            "Dec 22\u{2009}–\u{2009}23, 2023"
        );

        // Next month: month/year diff (falls back to full range fallback since year differs)
        assert_writeable_eq!(
            fmt.format(&start, &end_next_month),
            "Dec 22, 2023\u{2009}–\u{2009}Jan 5, 2024"
        );
    }

    // 2. Time-only range (HM)
    {
        let fmt =
            FixedCalendarDateRangeFormatter::try_new(locale!("en").into(), fieldsets::T::hm())
                .unwrap();

        // Same day, different time (different day period AM/PM)
        assert_writeable_eq!(
            fmt.format(&start.time, &end_same_day.time),
            "9:00\u{202f}AM\u{2009}–\u{2009}5:00\u{202f}PM"
        );

        // Same day, different time (same day period AM)
        let end_same_am = Time::try_new(11, 0, 0, 0).unwrap();
        assert_writeable_eq!(
            fmt.format(&start.time, &end_same_am),
            "9:00\u{2009}–\u{2009}11:00\u{202f}AM"
        );

        // Different day: time-only formatter with date diff.
        // Should fall back to formatting both times fully.
        assert_writeable_eq!(
            fmt.format(&start, &end_next_day),
            "9:00\u{202f}AM\u{2009}–\u{2009}5:00\u{202f}PM"
        );
    }

    // 3. Date and Time range (YMDT medium)
    {
        let fmt = FixedCalendarDateRangeFormatter::try_new(
            locale!("en").into(),
            fieldsets::YMDT::medium(),
        )
        .unwrap();

        // Same day, different time: only time differs.
        // Case 3: Mixed range, only time differs. Date formatted once, glued to time range.
        assert_writeable_eq!(
            fmt.format(&start, &end_same_day),
            "Dec 22, 2023, 9:00\u{202f}AM\u{2009}–\u{2009}5:00\u{202f}PM"
        );

        // Different day, different time: date differs.
        // Case 4: Mixed range, date differs. Falls back to full range fallback.
        assert_writeable_eq!(
            fmt.format(&start, &end_next_day),
            "Dec 22, 2023, 9:00:00\u{202f}AM\u{2009}–\u{2009}Dec 23, 2023, 5:00:00\u{202f}PM"
        );
    }

    // 4. Edge Case: Date-only formatter with time difference
    {
        let fmt = FixedCalendarDateRangeFormatter::try_new(
            locale!("en").into(),
            fieldsets::YMD::medium(),
        )
        .unwrap();

        // Date-only formatter, but input has time diff (date is same).
        // Should format as a single date (no range).
        assert_writeable_eq!(fmt.format(&start, &end_same_day), "Dec 22, 2023");
    }
}

#[test]
fn test_date_range_any_calendar_dynamic_conversion() {
    let start = DateTime {
        date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
        time: Time::try_new(9, 0, 0, 0).unwrap(),
    };
    let end_next_day = DateTime {
        date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
    };

    // Explicitly request Buddhist calendar in the locale
    let fmt =
        DateRangeFormatter::try_new(locale!("th-u-ca-buddhist").into(), fieldsets::YMD::medium())
            .unwrap();

    // Gregorian input should be dynamically converted to Buddhist (2023 -> 2566)
    // Thai day range pattern has no spaces around en-dash: "22–23 ธ.ค. 2566"
    assert_writeable_eq!(fmt.format(&start, &end_next_day), "22–23 ธ.ค. 2566");
}

#[test]
fn test_date_range_timezone() {
    use icu_calendar::Date;
    use icu_datetime::fieldsets;
    use icu_datetime::input::Time;
    use icu_datetime::range::FixedCalendarDateRangeFormatter;
    use icu_locale_core::locale;
    use icu_time::zone::UtcOffset;
    use icu_time::{TimeZone, ZonedDateTime};
    use writeable::assert_writeable_eq;

    let zone1 = TimeZone::from_iana_id("Europe/Paris")
        .with_offset(Some(UtcOffset::try_from_seconds(3600).unwrap())); // UTC+1
    let zone2 = TimeZone::from_iana_id("Europe/Athens")
        .with_offset(Some(UtcOffset::try_from_seconds(7200).unwrap())); // UTC+2

    let start = ZonedDateTime {
        date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
        time: Time::try_new(9, 0, 0, 0).unwrap(),
        zone: zone1,
    };
    let end = ZonedDateTime {
        date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
        zone: zone2,
    };

    // YMDT with zone
    let fmt = FixedCalendarDateRangeFormatter::try_new(
        locale!("en").into(),
        fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort),
    )
    .unwrap();

    // Since timezones differ, it must fall back to formatting both sides fully and gluing them.
    assert_writeable_eq!(
        fmt.format(&start, &end),
        "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"
    );
}

#[test]
fn test_date_range_hebrew_leap() {
    use icu_calendar::Date;
    use icu_calendar::cal::Hebrew;
    use icu_datetime::fieldsets;
    use icu_datetime::input::{DateTime, Time};
    use icu_datetime::range::FixedCalendarDateRangeFormatter;
    use icu_locale_core::locale;
    use writeable::assert_writeable_eq;

    // 2011 was a Hebrew leap year (5771).
    // 2011-03-04 is 28 Adar I 5771.
    // 2011-04-03 is 28 Adar II 5771.
    let start_greg = Date::try_new_gregorian(2011, 3, 4).unwrap();
    let end_greg = Date::try_new_gregorian(2011, 4, 3).unwrap();

    let start = DateTime {
        date: start_greg.to_calendar(Hebrew),
        time: Time::try_new(9, 0, 0, 0).unwrap(),
    };
    let end = DateTime {
        date: end_greg.to_calendar(Hebrew),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
    };

    let fmt =
        FixedCalendarDateRangeFormatter::try_new(locale!("en").into(), fieldsets::YMD::medium())
            .unwrap();

    // Should format as a range spanning Adar I and Adar II.
    assert_writeable_eq!(
        fmt.format(&start, &end),
        "28 Adar I\u{2009}–\u{2009}28 Adar II 5771"
    );
}

/// Range version of `test_minute_optional_hour_cycle` from `simple_test` (PR #8237)
#[test]
fn test_minute_optional_hour_cycle() {
    use icu_datetime::options::TimePrecision;
    use icu_datetime::range::NoCalendarRangeFormatter;

    let time = Time::try_new(7, 0, 0, 0).unwrap();
    let time_hour_diff = Time::try_new(8, 0, 0, 0).unwrap();
    let time_minute_diff = Time::try_new(7, 15, 0, 0).unwrap();

    let fs = fieldsets::T::short().with_time_precision(TimePrecision::MinuteOptional);

    // en-US (default h12): zero minutes omitted
    let fmt_en = NoCalendarRangeFormatter::try_new(locale!("en-US").into(), fs).unwrap();
    assert_writeable_eq!(
        fmt_en.format(&time, &time_hour_diff),
        "7\u{2009}–\u{2009}8\u{202f}AM"
    );
    assert_writeable_eq!(
        fmt_en.format(&time, &time_minute_diff),
        // The range pattern takes precedence here
        "7:00\u{2009}–\u{2009}7:15\u{202f}AM"
    );

    // fr (default h23): zero minutes retained
    let fmt_fr = NoCalendarRangeFormatter::try_new(locale!("fr").into(), fs).unwrap();
    assert_writeable_eq!(
        fmt_fr.format(&time, &time_hour_diff),
        "07:00\u{2009}–\u{2009}08:00"
    );
    assert_writeable_eq!(
        fmt_fr.format(&time, &time_minute_diff),
        "07:00\u{2009}–\u{2009}07:15"
    );

    // en-US with -u-hc-h23 override: zero minutes retained
    let fmt_en_h23 =
        NoCalendarRangeFormatter::try_new(locale!("en-US-u-hc-h23").into(), fs).unwrap();
    assert_writeable_eq!(
        fmt_en_h23.format(&time, &time_hour_diff),
        "07:00\u{2009}–\u{2009}08:00"
    );
    assert_writeable_eq!(
        fmt_en_h23.format(&time, &time_minute_diff),
        "07:00\u{2009}–\u{2009}07:15"
    );

    // fr with -u-hc-h12 override: zero minutes omitted
    let fmt_fr_12 = NoCalendarRangeFormatter::try_new(locale!("fr-u-hc-h12").into(), fs).unwrap();
    assert_writeable_eq!(
        fmt_fr_12.format(&time, &time_hour_diff),
        "7\u{2009}–\u{2009}8\u{202f}AM"
    );
    assert_writeable_eq!(
        fmt_fr_12.format(&time, &time_minute_diff),
        // The range pattern takes precedence here
        "7:00\u{2009}–\u{2009}7:15\u{202f}AM"
    );

    // fr with -u-hc-h11 override: zero minutes omitted
    let fmt_fr_h11 = NoCalendarRangeFormatter::try_new(locale!("fr-u-hc-h11").into(), fs).unwrap();
    assert_writeable_eq!(
        fmt_fr_h11.format(&time, &time_hour_diff),
        "7\u{2009}–\u{2009}8\u{202f}AM"
    );
    assert_writeable_eq!(
        fmt_fr_h11.format(&time, &time_minute_diff),
        // The range pattern takes precedence here
        "7:00\u{2009}–\u{2009}7:15\u{202f}AM"
    );

    // en-US with -u-hc-c24 override: zero minutes retained
    let fmt_en_c24 =
        NoCalendarRangeFormatter::try_new(locale!("en-US-u-hc-c24").into(), fs).unwrap();
    assert_writeable_eq!(
        fmt_en_c24.format(&time, &time_hour_diff),
        "07:00\u{2009}–\u{2009}08:00"
    );
    assert_writeable_eq!(
        fmt_en_c24.format(&time, &time_minute_diff),
        "07:00\u{2009}–\u{2009}07:15"
    );

    // fr with -u-hc-c12 override: zero minutes omitted
    let fmt_fr_c12 = NoCalendarRangeFormatter::try_new(locale!("fr-u-hc-c12").into(), fs).unwrap();
    assert_writeable_eq!(
        fmt_fr_c12.format(&time, &time_hour_diff),
        "7\u{2009}–\u{2009}8\u{202f}AM"
    );
    assert_writeable_eq!(
        fmt_fr_c12.format(&time, &time_minute_diff),
        // The range pattern takes precedence here
        "7:00\u{2009}–\u{2009}7:15\u{202f}AM"
    );
}

#[test]
fn test_date_range_ej() {
    use icu_calendar::Date;
    use icu_datetime::fieldsets;
    use icu_datetime::input::{DateTime, Time};
    use icu_datetime::range::{DateRangeFormatter, FixedCalendarDateRangeFormatter};
    use icu_locale_core::locale;
    use writeable::assert_writeable_eq;

    let start_greg = Date::try_new_gregorian(2024, 8, 9).unwrap(); // Friday
    let end_greg = Date::try_new_gregorian(2024, 8, 10).unwrap(); // Saturday

    let start = DateTime {
        date: start_greg,
        time: Time::try_new(20, 40, 0, 0).unwrap(),
    };
    let end_same_day = DateTime {
        date: start_greg,
        time: Time::try_new(21, 50, 0, 0).unwrap(),
    };
    let end_next_day = DateTime {
        date: end_greg,
        time: Time::try_new(21, 50, 0, 0).unwrap(),
    };

    let fs = fieldsets::E::short().with_time_hm();

    let fmt = FixedCalendarDateRangeFormatter::try_new(locale!("en-US").into(), fs).unwrap();
    let any_cal_fmt = DateRangeFormatter::try_new(locale!("en-US").into(), fs).unwrap();

    // Overlap patterns like `ej` (weekday + time) operate as unified date-time patterns
    // without independent date and glue separation in `DateTimeZonePatternSelectionData`.
    // When range formatting is invoked, both formatters cleanly produce the expected
    // fallback range output without field conflicts.
    //
    // Note: This behavior relies on the assumption that any locale in CLDR supporting `ej` in
    // date/time patterns also defines corresponding interval range pattern data. This invariant
    // is verified across all locales and calendars in datagen tests:
    // `icu_provider_source::datetime::range_patterns::tests`.
    assert_writeable_eq!(
        fmt.format(&start, &end_same_day),
        "Fri 8:40\u{202f}PM\u{2009}–\u{2009}Fri 9:50\u{202f}PM"
    );
    assert_writeable_eq!(
        fmt.format(&start, &end_next_day),
        "Fri 8:40\u{202f}PM\u{2009}–\u{2009}Sat 9:50\u{202f}PM"
    );

    assert_writeable_eq!(
        any_cal_fmt.format(&start, &end_same_day),
        "Fri 8:40\u{202f}PM\u{2009}–\u{2009}Fri 9:50\u{202f}PM"
    );
    assert_writeable_eq!(
        any_cal_fmt.format(&start, &end_next_day),
        "Fri 8:40\u{202f}PM\u{2009}–\u{2009}Sat 9:50\u{202f}PM"
    );
}

// Tests documenting issues when date range formatting falls back to `root` (`und`).
//
// TODO(#8359): Implement a fix and enable these tests.
//
// When a locale or calendar lacks custom interval patterns in CLDR, locale fallback walks up
// the hierarchy to `root` (`und`). CLDR's `root` defines interval patterns using English/ISO conventions
// (e.g. `y MMM d–d`, `G y-MM-dd–y-MM-dd`). This causes two major categories of problems:
//
// 1. Missing Names: The `root` range pattern often contains
//    symbols/lengths (such as `MMM` or `G`) that were not loaded by the single `DateTimeFormatter`
//    (e.g., `zh` whose single date pattern uses numeric month `M`, or `YMD::long()` which loads
//    `MMMM` but not `MMM`). This can lead to violations of assumptions.
//
// 2. Incorrect patterns: `root` patterns impose YMD order on
//    locales that use Day-Month-Year (such as German, French, Spanish). The results
//    will be incorrect, and, importantly, not consistent with the results of regular date
//    formatting, which can be confusing.

/// Chinese (zh) with `YMD::medium()`:
/// Single date pattern is "y年M月d日" (numeric month 'M').
/// Single `DateTimeFormatter` only loads numeric month data, not abbreviated month names ("MMM").
/// But range pattern falls back to `root` (`und`), which specifies "y MMM d–d".
/// When executing the `root` pattern, `FormattedSingleSide` fails with `NamesNotLoaded`.
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "unexpected error in FormattedSingleSide: NamesNotLoaded")]
fn test_root_fallback_names_not_loaded_zh() {
    use icu_calendar::Date;
    use icu_datetime::fieldsets;
    use icu_datetime::input::{DateTime, Time};
    use icu_datetime::range::DateRangeFormatter;
    use icu_locale_core::locale;

    let start = DateTime {
        date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
        time: Time::try_new(9, 0, 0, 0).unwrap(),
    };
    let end_day = DateTime {
        date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
    };

    let fmt_zh =
        DateRangeFormatter::try_new(locale!("zh").into(), fieldsets::YMD::medium()).unwrap();
    let _ = fmt_zh.format(&start, &end_day).to_string();
}

/// Spanish with Hebrew calendar (es-u-ca-hebrew) and `YMD::long()`:
/// Single `DateTimeFormatter` for `YMD::long()` loads full month names ("MMMM"), but range pattern
/// falls back to `root` (`und`), which uses abbreviated month names ("MMM").
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "unexpected error in FormattedSingleSide: NamesNotLoaded")]
fn test_root_fallback_names_not_loaded_es_hebrew() {
    use icu_calendar::Date;
    use icu_datetime::fieldsets;
    use icu_datetime::input::{DateTime, Time};
    use icu_datetime::range::DateRangeFormatter;
    use icu_locale_core::locale;

    let start = DateTime {
        date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
        time: Time::try_new(9, 0, 0, 0).unwrap(),
    };
    let end_day = DateTime {
        date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
    };

    let fmt_es_hebrew =
        DateRangeFormatter::try_new(locale!("es-u-ca-hebrew").into(), fieldsets::YMD::long())
            .unwrap();
    let _ = fmt_es_hebrew.format(&start, &end_day).to_string();
}

/// German with Buddhist calendar (de-u-ca-buddhist) and `YMD::long()`:
/// Single `DateTimeFormatter` for `YMD::long()` loads full month names ("MMMM"), but range pattern
/// falls back to `root` (`und`), which uses abbreviated month names ("MMM").
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "unexpected error in FormattedSingleSide: NamesNotLoaded")]
fn test_root_fallback_names_not_loaded_de_buddhist() {
    use icu_calendar::Date;
    use icu_datetime::fieldsets;
    use icu_datetime::input::{DateTime, Time};
    use icu_datetime::range::DateRangeFormatter;
    use icu_locale_core::locale;

    let start = DateTime {
        date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
        time: Time::try_new(9, 0, 0, 0).unwrap(),
    };
    let end_day = DateTime {
        date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
    };

    let fmt_de_buddhist =
        DateRangeFormatter::try_new(locale!("de-u-ca-buddhist").into(), fieldsets::YMD::long())
            .unwrap();
    let _ = fmt_de_buddhist.format(&start, &end_day).to_string();
}

#[test]
fn test_root_fallback_issues_field_order() {
    use icu_calendar::Date;
    use icu_datetime::fieldsets;
    use icu_datetime::input::{DateTime, Time};
    use icu_datetime::range::DateRangeFormatter;
    use icu_locale_core::locale;
    use writeable::assert_writeable_eq;

    let start = DateTime {
        date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
        time: Time::try_new(9, 0, 0, 0).unwrap(),
    };
    let end_day = DateTime {
        date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
    };

    // German (de) uses Day.Month.Year order (e.g. "22.12.2023").
    // For Buddhist calendar in YMD::medium(), `de` has no custom range pattern in CLDR.
    // Falling back to `root` uses the root pattern "G y-MM-dd – y-MM-dd" (ISO order with era first).
    let fmt_de_buddhist =
        DateRangeFormatter::try_new(locale!("de-u-ca-buddhist").into(), fieldsets::YMD::medium())
            .unwrap();
    // This produces "BE 2566-12-22 – 2566-12-23" instead of German Day-first order:
    assert_writeable_eq!(
        fmt_de_buddhist.format(&start, &end_day),
        "BE 2566-12-22\u{2009}–\u{2009}2566-12-23"
    );

    // Hebrew calendar in German:
    let fmt_de_hebrew =
        DateRangeFormatter::try_new(locale!("de-u-ca-hebrew").into(), fieldsets::YMD::medium())
            .unwrap();
    // This produces "AM 5784-04-10 – 5784-04-11" instead of German Day-first order:
    assert_writeable_eq!(
        fmt_de_hebrew.format(&start, &end_day),
        "AM 5784-04-10\u{2009}–\u{2009}5784-04-11"
    );
}
#[test]
fn test_date_range_parts() {
    let start = DateTime {
        date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
        time: Time::try_new(9, 0, 0, 0).unwrap(),
    };
    let end_same_day = DateTime {
        date: Date::try_new_gregorian(2023, 12, 22).unwrap(),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
    };
    let end_next_day = DateTime {
        date: Date::try_new_gregorian(2023, 12, 23).unwrap(),
        time: Time::try_new(17, 0, 0, 0).unwrap(),
    };

    {
        let fmt = FixedCalendarDateRangeFormatter::try_new(
            locale!("en").into(),
            fieldsets::YMD::medium(),
        )
        .unwrap();

        // Same day: no parts annotations since everything is shared.
        assert_writeable_parts_eq!(
            fmt.format(&start, &end_same_day),
            "Dec 22, 2023",
            [
                (0, 3, datetime_parts::MONTH),
                (4, 6, decimal_parts::INTEGER),
                (4, 6, datetime_parts::DAY),
                (8, 12, decimal_parts::INTEGER),
                (8, 12, datetime_parts::YEAR),
            ]
        );

        // TODO: this should only mark "22" and "23" as not shared,
        // but might need to modify split_on_repeated_field to return
        // the specific fields that are duplicated.
        assert_writeable_parts_eq!(
            fmt.format(&start, &end_next_day),
            "Dec 22\u{2009}–\u{2009}23, 2023",
            [
                (0, 15, datetime_parts::RANGE_START),
                (0, 3, datetime_parts::MONTH),
                (4, 6, decimal_parts::INTEGER),
                (4, 6, datetime_parts::DAY),
                (15, 23, datetime_parts::RANGE_END),
                (15, 17, decimal_parts::INTEGER),
                (15, 17, datetime_parts::DAY),
                (19, 23, decimal_parts::INTEGER),
                (19, 23, datetime_parts::YEAR),
            ]
        );
    }

    {
        let fmt = FixedCalendarDateRangeFormatter::try_new(
            locale!("en").into(),
            fieldsets::YMDT::medium(),
        )
        .unwrap();

        // Same day, different time: only time differs.
        // Date part is shared, time part is not shared.
        assert_writeable_parts_eq!(
            fmt.format(&start, &end_same_day),
            "Dec 22, 2023, 9:00\u{202f}AM\u{2009}–\u{2009}5:00\u{202f}PM",
            [
                (0, 3, datetime_parts::MONTH),
                (4, 6, decimal_parts::INTEGER),
                (4, 6, datetime_parts::DAY),
                (8, 12, decimal_parts::INTEGER),
                (8, 12, datetime_parts::YEAR),
                (14, 23, datetime_parts::RANGE_START),
                (14, 15, decimal_parts::INTEGER),
                (14, 15, datetime_parts::HOUR),
                (16, 18, decimal_parts::INTEGER),
                (16, 18, datetime_parts::MINUTE),
                (21, 23, datetime_parts::DAY_PERIOD),
                (32, 41, datetime_parts::RANGE_END),
                (32, 33, decimal_parts::INTEGER),
                (32, 33, datetime_parts::HOUR),
                (34, 36, decimal_parts::INTEGER),
                (34, 36, datetime_parts::MINUTE),
                (39, 41, datetime_parts::DAY_PERIOD)
            ]
        );

        // Different day, different time: date differs.
        // Only the range separator is shared.
        assert_writeable_parts_eq!(
            fmt.format(&start, &end_next_day),
            "Dec 22, 2023, 9:00:00\u{202f}AM\u{2009}–\u{2009}Dec 23, 2023, 5:00:00\u{202f}PM",
            [
                (0, 26, datetime_parts::RANGE_START),
                (0, 3, datetime_parts::MONTH),
                (4, 6, decimal_parts::INTEGER),
                (4, 6, datetime_parts::DAY),
                (8, 12, decimal_parts::INTEGER),
                (8, 12, datetime_parts::YEAR),
                (14, 15, decimal_parts::INTEGER),
                (14, 15, datetime_parts::HOUR),
                (16, 18, decimal_parts::INTEGER),
                (16, 18, datetime_parts::MINUTE),
                (19, 21, decimal_parts::INTEGER),
                (19, 21, datetime_parts::SECOND),
                (24, 26, datetime_parts::DAY_PERIOD),
                (35, 61, datetime_parts::RANGE_END),
                (35, 38, datetime_parts::MONTH),
                (39, 41, decimal_parts::INTEGER),
                (39, 41, datetime_parts::DAY),
                (43, 47, decimal_parts::INTEGER),
                (43, 47, datetime_parts::YEAR),
                (49, 50, decimal_parts::INTEGER),
                (49, 50, datetime_parts::HOUR),
                (51, 53, decimal_parts::INTEGER),
                (51, 53, datetime_parts::MINUTE),
                (54, 56, decimal_parts::INTEGER),
                (54, 56, datetime_parts::SECOND),
                (59, 61, datetime_parts::DAY_PERIOD)
            ]
        );
    }
}

/// Tests for CLDR Step 5 behavior:
/// "If a match is found from previous steps, compute the calendar field with the greatest
/// difference between start and end datetime. If there is no difference among any of the
/// fields in the pattern, format as a single date using availableFormats, and return."
#[test]
fn test_step_5_single_date_fallback() {
    use icu_calendar::Date;
    use icu_datetime::fieldsets;
    use icu_datetime::input::{DateTime, Time};
    use icu_datetime::options::TimePrecision;
    use icu_datetime::range::{FixedCalendarDateRangeFormatter, NoCalendarRangeFormatter};
    use icu_locale_core::locale;
    use writeable::assert_writeable_eq;

    // 1. Time range with minutes precision (T::hm()):
    // Differences in seconds (smaller than pattern precision) should format as single time.
    let fmt_t_hm =
        NoCalendarRangeFormatter::try_new(locale!("en").into(), fieldsets::T::hm()).unwrap();
    let t_930_10 = Time::try_new(9, 30, 10, 0).unwrap();
    let t_930_40 = Time::try_new(9, 30, 40, 0).unwrap();
    assert_writeable_eq!(fmt_t_hm.format(&t_930_10, &t_930_40), "9:30\u{202f}AM");

    // When minutes differ, it formats as a range.
    let t_945 = Time::try_new(9, 45, 0, 0).unwrap();
    assert_writeable_eq!(
        fmt_t_hm.format(&t_930_10, &t_945),
        "9:30\u{2009}–\u{2009}9:45\u{202f}AM"
    );

    // In contrast, when seconds ARE in the pattern (T::hms()), seconds diff should fall back
    // to the range pattern (gluing formatted sides) since seconds differ on screen.
    let fmt_t_hms =
        NoCalendarRangeFormatter::try_new(locale!("en").into(), fieldsets::T::hms()).unwrap();
    assert_writeable_eq!(
        fmt_t_hms.format(&t_930_10, &t_930_40),
        "9:30:10\u{202f}AM\u{2009}–\u{2009}9:30:40\u{202f}AM"
    );

    // 2. Time range with hour precision (T hour-only):
    // Differences in minutes should format as single time.
    let fmt_t_h = NoCalendarRangeFormatter::try_new(
        locale!("en").into(),
        fieldsets::T::short().with_time_precision(TimePrecision::Hour),
    )
    .unwrap();
    let t_14_10 = Time::try_new(14, 10, 0, 0).unwrap();
    let t_14_40 = Time::try_new(14, 40, 0, 0).unwrap();
    assert_writeable_eq!(fmt_t_h.format(&t_14_10, &t_14_40), "2\u{202f}PM");

    // When hours differ, it formats as a range.
    let t_16_40 = Time::try_new(16, 40, 0, 0).unwrap();
    assert_writeable_eq!(
        fmt_t_h.format(&t_14_10, &t_16_40),
        "2\u{2009}–\u{2009}4\u{202f}PM"
    );

    // 3. Date range with Year and Month (YM):
    // Differences in day should format as a single month.
    let fmt_ym =
        FixedCalendarDateRangeFormatter::try_new(locale!("en").into(), fieldsets::YM::medium())
            .unwrap();
    let d_2023_12_01 = Date::try_new_gregorian(2023, 12, 1).unwrap();
    let d_2023_12_15 = Date::try_new_gregorian(2023, 12, 15).unwrap();
    assert_writeable_eq!(fmt_ym.format(&d_2023_12_01, &d_2023_12_15), "Dec 2023");

    // When months differ, it formats as a range.
    let d_2023_05_01 = Date::try_new_gregorian(2023, 5, 1).unwrap();
    assert_writeable_eq!(
        fmt_ym.format(&d_2023_05_01, &d_2023_12_01),
        "May\u{2009}–\u{2009}Dec 2023"
    );

    // 4. Date range with Year only (Y):
    // Differences in month and day should format as a single year.
    let fmt_y =
        FixedCalendarDateRangeFormatter::try_new(locale!("en").into(), fieldsets::Y::medium())
            .unwrap();
    assert_writeable_eq!(fmt_y.format(&d_2023_05_01, &d_2023_12_15), "2023");

    // When years differ, it formats as a range.
    let d_2025_05_01 = Date::try_new_gregorian(2025, 5, 1).unwrap();
    assert_writeable_eq!(
        fmt_y.format(&d_2023_05_01, &d_2025_05_01),
        "2023\u{2009}–\u{2009}2025"
    );

    // 5. Mixed date-time range (YMDT):
    // With minutes time precision, differences in seconds format as a single datetime.
    let fmt_ymdt_hm = FixedCalendarDateRangeFormatter::try_new(
        locale!("en").into(),
        fieldsets::YMD::medium().with_time_hm(),
    )
    .unwrap();
    let dt_1 = DateTime {
        date: d_2023_12_01,
        time: t_930_10,
    };
    let dt_2 = DateTime {
        date: d_2023_12_01,
        time: t_930_40,
    };
    assert_writeable_eq!(
        fmt_ymdt_hm.format(&dt_1, &dt_2),
        "Dec 1, 2023, 9:30\u{202f}AM"
    );

    // When hours differ on the same day, formats time range.
    let dt_3 = DateTime {
        date: d_2023_12_01,
        time: Time::try_new(17, 30, 0, 0).unwrap(),
    };
    assert_writeable_eq!(
        fmt_ymdt_hm.format(&dt_1, &dt_3),
        "Dec 1, 2023, 9:30\u{202f}AM\u{2009}–\u{2009}5:30\u{202f}PM"
    );
}