opportune 0.2.0

Calendar availability and scheduling calculations across constraints.
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
//! Validated wall-clock `window` specifications and the instants they allow.
//!
//! A [`WindowSpec`] is the parsed form of a window rule's `weekly` and `dates`
//! keys: a zone selection, hours per weekday and date overrides, all validated
//! once at construction, so evaluation never has to ask whether the
//! specification is well formed.
//!
//! Membership is decided from the instant's local date and time alone — an
//! instant satisfies the rule when its local reading does — so daylight saving
//! transitions need no special case: a local time inside a skipped hour simply
//! never occurs, and a repeated local time is judged twice, once per instant.
//! [`WindowSpec::intervals`] builds the same answer as half-open UTC minute
//! spans with the segment-start breakpoint algorithm.
//!
//! A specification names its zone rather than holding one, so a rule is a value
//! a caller can write down without a time zone database;
//! [`RuleZone::resolve`] turns the choice into the zone function the evaluation
//! reads, once per participant.
//!
//! ```
//! use opportune::intervals::Span;
//! use opportune::time::{
//!     Date, Instant, LocalResolution, RuleZone, TimeOfDay, WallEnd, WallRange, Weekday, Weekdays,
//!     WeeklyKey, WindowSpec, Zone, ZoneName, ZoneSchedule,
//! };
//!
//! let hours = WallRange::new(
//!     TimeOfDay::new(9, 0).unwrap(),
//!     WallEnd::Time(TimeOfDay::new(17, 0).unwrap()),
//! )
//! .unwrap();
//! let spec = WindowSpec::new(
//!     RuleZone::Named(ZoneName::new("UTC").unwrap()),
//!     vec![WeeklyKey::new(Weekdays::single(Weekday::Monday), vec![hours]).unwrap()],
//!     Vec::new(),
//! )
//! .unwrap();
//!
//! let at = |hour: u8| match Zone::utc()
//!     .resolve_local(Date::new(2027, 3, 8).unwrap(), TimeOfDay::new(hour, 0).unwrap())
//!     .unwrap()
//! {
//!     LocalResolution::Unique(instant) => instant,
//!     _ => panic!("UTC has no transitions"),
//! };
//! let schedule = ZoneSchedule::fixed(Zone::utc());
//! assert!(spec.contains(at(10), &schedule));
//! assert!(!spec.contains(at(8), &schedule));
//!
//! let day = spec
//!     .intervals(
//!         &schedule,
//!         at(0),
//!         Instant::from_minutes(at(0).minutes() + 1_440).unwrap(),
//!     )
//!     .unwrap();
//! assert_eq!(day.spans(), [Span::new(at(9).minutes(), at(17).minutes()).unwrap()]);
//! ```

use crate::intervals::IntervalSet;
use crate::time::breakpoints::{self, BreakpointSource};
use crate::time::error::{TimeError, WindowError};
use crate::time::{
    Date, Instant, TimeOfDay, TimeZoneDatabase, Weekday, Weekdays, Zone, ZoneName, ZoneSchedule,
};

/// The zone a rule's `zone` field names.
///
/// Each window and blackout is read in one of three zones: the participant's own
/// zone at the instant — which a trip moves — the event's zone, or a named IANA
/// zone. [`RuleZone::resolve`] turns the choice into the zone function the
/// evaluation reads.
///
/// ```
/// # #[cfg(feature = "bundled-tzdb")] {
/// use opportune::time::{RuleZone, TimeZoneDatabase, Zone, ZoneSchedule};
///
/// let db = TimeZoneDatabase::bundled().unwrap();
/// let participant = ZoneSchedule::fixed(Zone::utc());
/// let resolved = RuleZone::Participant
///     .resolve(&db, &participant, &Zone::utc())
///     .unwrap();
/// assert_eq!(resolved.home().name(), "UTC");
/// assert!(resolved.periods().is_empty());
/// # }
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RuleZone {
    /// The participant's zone at each instant, following their zone periods.
    Participant,
    /// The event's zone.
    Event,
    /// A named zone, whatever the event's or participant's zone is.
    Named(ZoneName),
}

impl RuleZone {
    /// The zone function this selection names, with every named zone resolved
    /// through `db`.
    ///
    /// `participant` supplies the zone periods for [`RuleZone::Participant`];
    /// `event` supplies the zone for [`RuleZone::Event`]. Each variant reads
    /// only one of the two, so a caller holding a single zone can pass it as
    /// `event` and [`ZoneSchedule::fixed`] over it as `participant`.
    ///
    /// # Errors
    /// Returns [`TimeError::ZoneUnknown`] when a named zone is not in `db`.
    pub fn resolve(
        &self,
        db: &TimeZoneDatabase,
        participant: &ZoneSchedule,
        event: &Zone,
    ) -> Result<ZoneSchedule, TimeError> {
        match self {
            Self::Participant => Ok(participant.clone()),
            Self::Event => Ok(ZoneSchedule::fixed(event.clone())),
            Self::Named(name) => Ok(ZoneSchedule::fixed(db.zone(name.as_str())?)),
        }
    }
}

/// The end of a wall-clock range, which may be `24:00`.
///
/// A [`TimeOfDay`] stops at `23:59`, so the end of the day needs its own value:
/// `24:00` is the instant the day ends, not the start of the next one.
///
/// ```
/// use opportune::time::{TimeOfDay, WallEnd};
///
/// let end = WallEnd::Time(TimeOfDay::new(17, 0).unwrap());
/// assert_eq!(end.minutes(), 1_020);
/// assert_eq!(WallEnd::Midnight.minutes(), 1_440);
/// assert_eq!(WallEnd::Midnight.to_string(), "24:00");
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum WallEnd {
    /// A time of day from `00:00` to `23:59`.
    Time(TimeOfDay),
    /// `24:00`, the end of the day.
    Midnight,
}

impl WallEnd {
    /// The end in minutes from the start of the day: `0` to `1440`.
    #[must_use]
    pub fn minutes(&self) -> u16 {
        match self {
            Self::Time(time) => time.minute_of_day(),
            Self::Midnight => TimeOfDay::MINUTES_PER_DAY,
        }
    }
}

impl core::fmt::Display for WallEnd {
    /// Formats the end as `HH:MM`, or `24:00` for the end of the day.
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Time(time) => time.fmt(f),
            Self::Midnight => f.write_str("24:00"),
        }
    }
}

impl From<TimeOfDay> for WallEnd {
    /// The end of a range at a time of day, so a range can be written
    /// `WallRange::new(from, until)` directly with a [`TimeOfDay`].
    fn from(time: TimeOfDay) -> Self {
        Self::Time(time)
    }
}

/// A half-open wall-clock range `[start, end)` within a day, on one day's key.
///
/// A range whose end is earlier than its start crosses midnight: `22:00-02:00`
/// runs from 22:00 on the key's day to 02:00 on the next one. `24:00` is a valid
/// end; a start equal to its end is rejected, because `00:00-00:00` is almost
/// certainly a mistyped `00:00-24:00`.
///
/// ```
/// use opportune::time::{TimeOfDay, WallEnd, WallRange};
///
/// let workday = WallRange::new(
///     TimeOfDay::new(9, 0).unwrap(),
///     WallEnd::Time(TimeOfDay::new(17, 0).unwrap()),
/// )
/// .unwrap();
/// assert_eq!(workday.start().to_string(), "09:00");
/// assert_eq!(workday.end().to_string(), "17:00");
///
/// let equal = WallRange::new(
///     TimeOfDay::MIDNIGHT,
///     WallEnd::Time(TimeOfDay::MIDNIGHT),
/// );
/// assert_eq!(
///     equal.unwrap_err().code(),
///     "window_range_empty"
/// );
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct WallRange {
    start: TimeOfDay,
    end: WallEnd,
}

impl WallRange {
    /// Creates the range `[start, end)`.
    ///
    /// # Errors
    /// Returns [`WindowError::RangeEmpty`] when `start` and `end` name the same
    /// minute, as `00:00-00:00` does.
    pub fn new(start: TimeOfDay, end: impl Into<WallEnd>) -> Result<Self, WindowError> {
        let end = end.into();
        if start.minute_of_day() == end.minutes() {
            return Err(WindowError::RangeEmpty { start });
        }
        Ok(Self { start, end })
    }

    /// The range's start.
    #[must_use]
    pub const fn start(&self) -> TimeOfDay {
        self.start
    }

    /// The range's end.
    #[must_use]
    pub const fn end(&self) -> WallEnd {
        self.end
    }

    /// The start in minutes from the start of the day.
    fn start_minutes(self) -> u16 {
        self.start.minute_of_day()
    }

    /// The end in minutes from the start of the day on which the range starts,
    /// so a crossing range's end is past `1440`.
    fn linear_end(self) -> u16 {
        if self.crosses_midnight() {
            self.end.minutes() + TimeOfDay::MINUTES_PER_DAY
        } else {
            self.end.minutes()
        }
    }

    /// Whether the range runs past the end of the day it starts on.
    fn crosses_midnight(self) -> bool {
        self.end.minutes() < self.start_minutes()
    }

    /// Whether the range covers `minute` of the day it starts on.
    fn covers(self, minute: u16) -> bool {
        if self.crosses_midnight() {
            minute >= self.start_minutes()
        } else {
            self.start_minutes() <= minute && minute < self.end.minutes()
        }
    }

    /// Whether a range that started on the previous date still covers `minute`
    /// of this one.
    fn spills_over(self, minute: u16) -> bool {
        self.crosses_midnight() && minute < self.end.minutes()
    }

    /// The local date and time at which this range stops, when it starts on
    /// `date`.
    fn end_boundary(self, date: Date) -> Result<(Date, TimeOfDay), TimeError> {
        match self.end {
            WallEnd::Time(time) if self.crosses_midnight() => Ok((date.plus_days(1)?, time)),
            WallEnd::Time(time) => Ok((date, time)),
            WallEnd::Midnight => Ok((date.plus_days(1)?, TimeOfDay::MIDNIGHT)),
        }
    }
}

/// One `weekly` key: the days it covers and the hours they share.
///
/// ```
/// use opportune::time::{TimeOfDay, WallEnd, WallRange, Weekday, Weekdays, WeeklyKey};
///
/// let hours = WallRange::new(
///     TimeOfDay::new(9, 0).unwrap(),
///     WallEnd::Time(TimeOfDay::new(17, 0).unwrap()),
/// )
/// .unwrap();
/// let key = WeeklyKey::new(
///     Weekdays::list([Weekday::Monday, Weekday::Wednesday]),
///     vec![hours],
/// )
/// .unwrap();
/// assert!(key.days().contains(Weekday::Monday));
/// assert_eq!(key.hours().len(), 1);
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WeeklyKey {
    days: Weekdays,
    hours: Vec<WallRange>,
}

impl WeeklyKey {
    /// Creates a key from the days it covers and the ranges they share.
    ///
    /// # Errors
    /// Returns [`WindowError::RangesOverlap`] when two ranges within the key
    /// overlap. A midnight-crossing range is measured past the end of the day it
    /// starts on, so `22:00-02:00` overlaps `23:00-01:00` but not `01:00-03:00`.
    pub fn new(days: Weekdays, hours: Vec<WallRange>) -> Result<Self, WindowError> {
        Ok(Self {
            days,
            hours: non_overlapping(hours)?,
        })
    }

    /// The days this key covers.
    #[must_use]
    pub fn days(&self) -> &Weekdays {
        &self.days
    }

    /// The hours every covered day shares, sorted by start.
    #[must_use]
    pub fn hours(&self) -> &[WallRange] {
        &self.hours
    }
}

/// An inclusive range of civil dates, the key side of a `dates` entry.
///
/// ```
/// use opportune::time::{Date, DateRange};
///
/// let holiday = DateRange::new(
///     Date::new(2026, 12, 24).unwrap(),
///     Date::new(2027, 1, 2).unwrap(),
/// )
/// .unwrap();
/// assert!(holiday.contains(Date::new(2026, 12, 25).unwrap()));
/// assert!(!holiday.contains(Date::new(2027, 1, 3).unwrap()));
/// assert!(DateRange::new(Date::new(2027, 1, 2).unwrap(), Date::new(2026, 12, 24).unwrap()).is_err());
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DateRange {
    start: Date,
    end: Date,
}

impl DateRange {
    /// Creates the inclusive range from `start` to `end`.
    ///
    /// # Errors
    /// Returns [`WindowError::DateRangeReversed`] when `end` falls before
    /// `start`.
    pub fn new(start: Date, end: Date) -> Result<Self, WindowError> {
        if end < start {
            return Err(WindowError::DateRangeReversed { start, end });
        }
        Ok(Self { start, end })
    }

    /// The range holding only `date`.
    #[must_use]
    pub fn single(date: Date) -> Self {
        Self {
            start: date,
            end: date,
        }
    }

    /// The first date in the range.
    #[must_use]
    pub const fn start(&self) -> Date {
        self.start
    }

    /// The last date in the range.
    #[must_use]
    pub const fn end(&self) -> Date {
        self.end
    }

    /// Whether the range covers `date`.
    #[must_use]
    pub fn contains(&self, date: Date) -> bool {
        self.start <= date && date <= self.end
    }
}

/// One `dates` entry: the dates it overrides and the hours they have.
///
/// An entry with no hours closes its dates, whatever the weekly key would have
/// allowed. An override also governs its own midnight-crossing spill: the spill
/// from an overridden date follows the override, and the spill *into* an
/// overridden date still comes from the previous date's key.
///
/// ```
/// use opportune::time::{Date, DateRange, DateKey};
///
/// // Christmas Day is closed.
/// let christmas = DateKey::new(
///     DateRange::single(Date::new(2026, 12, 25).unwrap()),
///     Vec::new(),
/// )
/// .unwrap();
/// assert!(christmas.hours().is_empty());
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DateKey {
    dates: DateRange,
    hours: Vec<WallRange>,
}

impl DateKey {
    /// Creates a key from the dates it overrides and the hours they have.
    ///
    /// # Errors
    /// Returns [`WindowError::RangesOverlap`] when two ranges within the key
    /// overlap.
    pub fn new(dates: DateRange, hours: Vec<WallRange>) -> Result<Self, WindowError> {
        Ok(Self {
            dates,
            hours: non_overlapping(hours)?,
        })
    }

    /// The dates this key overrides.
    #[must_use]
    pub fn dates(&self) -> &DateRange {
        &self.dates
    }

    /// The hours those dates have, sorted by start. Empty means closed.
    #[must_use]
    pub fn hours(&self) -> &[WallRange] {
        &self.hours
    }
}

/// A validated wall-clock `window` specification.
///
/// The specification is the parsed form of a window rule: the zone it is read
/// in, its `weekly` keys and its `dates` overrides. Construction rejects every
/// structurally ambiguous combination — a weekday or date claimed by two keys, a
/// reversed date range, overlapping ranges within a key, a range whose start
/// equals its end — so membership and interval building below can be total.
///
/// An instant is inside when its local time `m` on its local date `D` satisfies:
/// a range of `D`'s key with `a ≤ m < b`; a midnight-crossing range of `D`'s key
/// with `m ≥ a`; or a midnight-crossing range of `D - 1`'s key with `m < b`. A
/// date's key is its `dates` entry if it has one, otherwise its `weekly` day.
///
/// ```
/// use opportune::time::{
///     Date, Instant, LocalResolution, RuleZone, TimeOfDay, WallRange, WallEnd, Weekday, Weekdays,
///     WeeklyKey, WindowSpec, Zone, ZoneName, ZoneSchedule,
/// };
///
/// // Open 22:00 to 02:00 on Fridays, which runs into Saturday morning.
/// let night = WallRange::new(
///     TimeOfDay::new(22, 0).unwrap(),
///     WallEnd::Time(TimeOfDay::new(2, 0).unwrap()),
/// )
/// .unwrap();
/// let spec = WindowSpec::new(
///     RuleZone::Named(ZoneName::new("UTC").unwrap()),
///     vec![WeeklyKey::new(Weekdays::single(Weekday::Friday), vec![night]).unwrap()],
///     Vec::new(),
/// )
/// .unwrap();
/// let zones = ZoneSchedule::fixed(Zone::utc());
///
/// let at = |day: i8, hour: u8| match Zone::utc()
///     .resolve_local(Date::new(2027, 3, day).unwrap(), TimeOfDay::new(hour, 0).unwrap())
///     .unwrap()
/// {
///     LocalResolution::Unique(instant) => instant,
///     _ => panic!("UTC has no transitions"),
/// };
/// // Friday 2027-03-12 is open in the evening and the next morning.
/// assert!(spec.contains(at(12, 23), &zones));
/// assert!(spec.contains(at(13, 1), &zones));
/// assert!(!spec.contains(at(13, 2), &zones));
/// assert!(!spec.contains(at(12, 12), &zones));
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WindowSpec {
    zone: RuleZone,
    /// Indexed Monday to Sunday; an empty list means the day is closed.
    weekly: [Vec<WallRange>; 7],
    /// Sorted by start date and disjoint, so at most one key covers a date.
    dates: Vec<DateKey>,
}

impl WindowSpec {
    /// Creates a specification from its zone selection, `weekly` keys and
    /// `dates` overrides.
    ///
    /// # Errors
    /// Returns [`WindowError::WeekdayRepeated`] when two `weekly` keys claim the
    /// same day and [`WindowError::DateRepeated`] when two `dates` keys claim the
    /// same date. Range-level mistakes are rejected earlier, by [`WallRange`],
    /// [`WeeklyKey`], [`DateKey`] and [`DateRange`].
    pub fn new(
        zone: RuleZone,
        weekly: Vec<WeeklyKey>,
        dates: Vec<DateKey>,
    ) -> Result<Self, WindowError> {
        let mut days: [Vec<WallRange>; 7] = core::array::from_fn(|_| Vec::new());
        let mut claimed = 0u8;
        for key in weekly {
            for day in Weekday::ALL {
                if key.days().contains(day) {
                    if claimed & day.bit() != 0 {
                        return Err(WindowError::WeekdayRepeated { day });
                    }
                    claimed |= day.bit();
                    days[usize::from(day.index())] = key.hours().to_vec();
                }
            }
        }

        let mut dates = dates;
        dates.sort_by_key(|key| key.dates().start());
        for pair in dates.windows(2) {
            if pair[1].dates().start() <= pair[0].dates().end() {
                return Err(WindowError::DateRepeated {
                    date: pair[1].dates().start(),
                });
            }
        }

        Ok(Self {
            zone,
            weekly: days,
            dates,
        })
    }

    /// Whether `instant` is inside, read in `zones` — the zone function this
    /// specification's [`RuleZone`] resolved to.
    ///
    /// ```
    /// # use opportune::time::{Instant, RuleZone, WindowSpec, Zone, ZoneSchedule};
    /// // No keys at all: a window that is closed everywhere.
    /// let closed = WindowSpec::new(RuleZone::Event, Vec::new(), Vec::new()).unwrap();
    /// let zones = ZoneSchedule::fixed(Zone::utc());
    /// assert!(!closed.contains(Instant::MIN, &zones));
    /// ```
    #[must_use]
    pub fn contains(&self, instant: Instant, zones: &ZoneSchedule) -> bool {
        self.member(instant, zones.zone_at(instant))
    }

    /// The minutes of `from..until` that are inside, as half-open UTC spans.
    ///
    /// The result is built over exactly the range asked for — never clipped to
    /// midnight, a day, or any padded bound — and the range need not be
    /// midnight-aligned.
    ///
    /// # Errors
    /// Returns a [`TimeError`] only for a zone whose offsets are not whole
    /// minutes, which the database never produces inside the supported range.
    pub fn intervals(
        &self,
        zones: &ZoneSchedule,
        from: Instant,
        until: Instant,
    ) -> Result<IntervalSet, TimeError> {
        breakpoints::build(self, zones, from, until)
    }

    /// The zone selection the specification was built with.
    #[must_use]
    pub fn zone(&self) -> &RuleZone {
        &self.zone
    }

    /// The hours of each weekday, Monday first. An empty list means the day is
    /// closed.
    #[must_use]
    pub fn weekly(&self) -> &[Vec<WallRange>; 7] {
        &self.weekly
    }

    /// The date overrides, sorted by start date.
    #[must_use]
    pub fn dates(&self) -> &[DateKey] {
        &self.dates
    }

    /// The hours of the key that governs `date`: its override if it has one,
    /// otherwise its weekday's.
    fn hours_on(&self, date: Date) -> &[WallRange] {
        // Overrides are sorted and disjoint, so at most one can cover the date.
        let index = self.dates.partition_point(|key| key.dates().end() < date);
        if let Some(key) = self.dates.get(index)
            && key.dates().contains(date)
        {
            return key.hours();
        }
        &self.weekly[usize::from(date.weekday().index())]
    }

    /// Membership from the local reading alone, so one rule decides both instant
    /// membership and every segment the interval builder classifies.
    fn contains_local(&self, date: Date, minute: u16) -> bool {
        if self.hours_on(date).iter().any(|range| range.covers(minute)) {
            return true;
        }
        // A crossing range on yesterday's key runs into today. Non-overlapping
        // ranges in `[0, 2880)` allow at most one crossing range per day, and it
        // is always the last one.
        match date.plus_days(-1) {
            Ok(previous) => self
                .hours_on(previous)
                .last()
                .is_some_and(|range| range.spills_over(minute)),
            Err(_) => false,
        }
    }

    /// Breakpoints inside a transition-free slice `[start, end)`.
    ///
    /// With no offset transition inside `[start, end)`, the zone's UTC offset is
    /// constant across the slice, so each local minute on `date` maps to a UTC
    /// minute by integer addition from that date's midnight. Narrowing `hours_on`
    /// to the slice's local time bounds avoids resolving ranges outside short
    /// slices (such as a schedule with many zone periods).
    fn constant_offset_breakpoints(
        &self,
        zone: &Zone,
        start: Instant,
        end: Instant,
    ) -> Vec<Instant> {
        let start_min = start.minutes();
        let end_min = end.minutes();
        let start_local = zone.zoned(start);
        let first = start_local.date();
        let lo_min = start_local.time().minute_of_day();
        let last_instant = Instant::from_minutes(end_min - 1)
            .expect("a slice ends at least one minute after it starts");
        let last_local = zone.zoned(last_instant);
        let last = last_local.date();
        let hi_min = last_local.time().minute_of_day();

        let mut instants = Vec::new();
        let mut date = first;
        let mut midnight = start_min - i64::from(lo_min);
        let push_if_inside = |out: &mut Vec<Instant>, minute: i64| {
            if minute > start_min && minute < end_min {
                out.push(
                    Instant::from_minutes(minute)
                        .expect("a breakpoint strictly inside the slice is representable"),
                );
            }
        };
        loop {
            let day_lo = if date == first { lo_min } else { 0 };
            let day_hi = if date == last {
                hi_min
            } else {
                TimeOfDay::MINUTES_PER_DAY
            };
            push_if_inside(&mut instants, midnight);

            let hours = self.hours_on(date);
            let from_idx = hours.partition_point(|range| range.linear_end() <= day_lo);
            for range in &hours[from_idx..] {
                let range_start = range.start_minutes();
                if range_start > day_hi {
                    break;
                }
                push_if_inside(&mut instants, midnight + i64::from(range_start));
                if !range.crosses_midnight() {
                    let range_end = range.end().minutes();
                    if range_end <= day_hi {
                        push_if_inside(&mut instants, midnight + i64::from(range_end));
                    }
                }
            }
            if let Ok(previous) = date.plus_days(-1)
                && let Some(range) = self.hours_on(previous).last()
                && range.crosses_midnight()
            {
                let spill_end = range.end().minutes();
                if spill_end > day_lo && spill_end <= day_hi {
                    push_if_inside(&mut instants, midnight + i64::from(spill_end));
                }
            }
            if date >= last {
                break;
            }
            date = date
                .plus_days(1)
                .expect("a date strictly before `last` has a representable next date");
            midnight += i64::from(TimeOfDay::MINUTES_PER_DAY);
        }
        instants
    }
}

impl BreakpointSource for WindowSpec {
    fn member(&self, instant: Instant, zone: &Zone) -> bool {
        let local = instant.zoned(zone);
        self.contains_local(local.date(), local.time().minute_of_day())
    }

    fn breakpoints(
        &self,
        zone: &Zone,
        start: Instant,
        end: Instant,
    ) -> Result<Vec<Instant>, TimeError> {
        let transitions = zone.transition_instants(start, end)?;
        if transitions.is_empty() {
            return Ok(self.constant_offset_breakpoints(zone, start, end));
        }
        let (first, last) = breakpoints::dates_covering(zone, start, end, &transitions);
        let mut instants = Vec::new();
        let mut date = first;
        loop {
            // The local date changes at this date's midnight, where the key that
            // governs it changes too.
            breakpoints::push_local(&mut instants, zone, date, TimeOfDay::MIDNIGHT)?;
            for range in self.hours_on(date) {
                breakpoints::push_local(&mut instants, zone, date, range.start())?;
                // A crossing range's end is a boundary on the next date, and is
                // collected there as the previous date's spill.
                if !range.crosses_midnight() {
                    let (end_date, end_time) = range.end_boundary(date)?;
                    breakpoints::push_local(&mut instants, zone, end_date, end_time)?;
                }
            }
            if let Ok(previous) = date.plus_days(-1)
                && let Some(range) = self.hours_on(previous).last()
                && range.crosses_midnight()
            {
                let (end_date, end_time) = range.end_boundary(previous)?;
                breakpoints::push_local(&mut instants, zone, end_date, end_time)?;
            }
            if date >= last {
                break;
            }
            date = date.plus_days(1)?;
        }
        instants.extend(transitions);
        Ok(instants)
    }
}

/// Sorts ranges by start and rejects any that overlap.
///
/// A crossing range is measured on past the day it starts, so two ranges that
/// only touch — one ends where the next starts — are allowed, while a genuine
/// overlap is the error.
fn non_overlapping(mut hours: Vec<WallRange>) -> Result<Vec<WallRange>, WindowError> {
    hours.sort_by_key(|range| range.start_minutes());
    let mut reached_end: Option<u16> = None;
    for range in &hours {
        if let Some(end) = reached_end
            && range.start_minutes() < end
        {
            return Err(WindowError::RangesOverlap { at: range.start() });
        }
        reached_end =
            Some(reached_end.map_or(range.linear_end(), |end| end.max(range.linear_end())));
    }
    Ok(hours)
}

#[cfg(test)]
mod tests {
    use super::{
        DateKey, DateRange, RuleZone, WallEnd, WallRange, Weekdays, WeeklyKey, WindowSpec,
    };
    use crate::time::{
        Date, Instant, LocalResolution, TimeOfDay, Weekday, WindowError, Zone, ZoneName,
        ZoneSchedule,
    };

    /// The instant of a UTC civil date-time, which always exists.
    fn utc(year: i16, month: i8, day: i8, hour: u8, minute: u8) -> Instant {
        let date = Date::new(year, month, day).unwrap();
        let time = TimeOfDay::new(hour, minute).unwrap();
        let LocalResolution::Unique(instant) = Zone::utc().resolve_local(date, time).unwrap()
        else {
            panic!("UTC has no transitions")
        };
        instant
    }

    fn time(hour: u8, minute: u8) -> TimeOfDay {
        TimeOfDay::new(hour, minute).unwrap()
    }

    fn range(start: (u8, u8), end: (u8, u8)) -> WallRange {
        WallRange::new(time(start.0, start.1), WallEnd::Time(time(end.0, end.1))).unwrap()
    }

    fn until_midnight(start: (u8, u8)) -> WallRange {
        WallRange::new(time(start.0, start.1), WallEnd::Midnight).unwrap()
    }

    fn weekly(day: Weekday, hours: Vec<WallRange>) -> WeeklyKey {
        WeeklyKey::new(Weekdays::single(day), hours).unwrap()
    }

    fn spec(weekly: Vec<WeeklyKey>, dates: Vec<DateKey>) -> WindowSpec {
        WindowSpec::new(
            RuleZone::Named(ZoneName::new("UTC").unwrap()),
            weekly,
            dates,
        )
        .unwrap()
    }

    fn minutes(spans: &[crate::intervals::Span]) -> Vec<(i64, i64)> {
        spans
            .iter()
            .map(|span| (span.start(), span.end()))
            .collect()
    }

    /// The built span form, as minute pairs, over `from..until`.
    fn built(spec: &WindowSpec, from: Instant, until: Instant) -> Vec<(i64, i64)> {
        let zones = ZoneSchedule::fixed(Zone::utc());
        minutes(spec.intervals(&zones, from, until).unwrap().spans())
    }

    #[test]
    fn a_weekday_claimed_by_two_keys_is_rejected() {
        let monday = weekly(Weekday::Monday, vec![range((9, 0), (17, 0))]);
        let again = WeeklyKey::new(
            Weekdays::list([Weekday::Monday, Weekday::Tuesday]),
            vec![range((9, 0), (17, 0))],
        )
        .unwrap();
        let error = WindowSpec::new(
            RuleZone::Named(ZoneName::new("UTC").unwrap()),
            vec![monday, again],
            Vec::new(),
        )
        .unwrap_err();
        assert_eq!(
            error,
            WindowError::WeekdayRepeated {
                day: Weekday::Monday
            }
        );
        assert_eq!(error.code(), "window_weekday_repeated");
    }

    #[test]
    fn a_date_claimed_by_two_keys_is_rejected() {
        let date = Date::new(2027, 3, 10).unwrap();
        let first = DateKey::new(
            DateRange::new(date, Date::new(2027, 3, 12).unwrap()).unwrap(),
            Vec::new(),
        )
        .unwrap();
        let second = DateKey::new(
            DateRange::new(
                Date::new(2027, 3, 11).unwrap(),
                Date::new(2027, 3, 14).unwrap(),
            )
            .unwrap(),
            Vec::new(),
        )
        .unwrap();
        let error = WindowSpec::new(
            RuleZone::Named(ZoneName::new("UTC").unwrap()),
            Vec::new(),
            vec![first, second],
        )
        .unwrap_err();
        assert_eq!(
            error,
            WindowError::DateRepeated {
                date: Date::new(2027, 3, 11).unwrap()
            }
        );
        assert_eq!(error.code(), "window_date_repeated");
    }

    #[test]
    fn date_keys_are_matched_whatever_order_they_arrive_in() {
        // `dates` is a JSON object, so the keys arrive in whatever order the
        // caller wrote them. Both the duplicate check and the per-date lookup
        // below it assume the keys are sorted by start date, so an unsorted
        // input must not silently lose an override or miss a collision.
        let later = DateKey::new(
            DateRange::new(
                Date::new(2027, 3, 20).unwrap(),
                Date::new(2027, 3, 25).unwrap(),
            )
            .unwrap(),
            Vec::new(),
        )
        .unwrap();
        let earlier = DateKey::new(
            DateRange::new(
                Date::new(2027, 3, 10).unwrap(),
                Date::new(2027, 3, 21).unwrap(),
            )
            .unwrap(),
            Vec::new(),
        )
        .unwrap();
        // Supplied latest-first, the collision is still found, and the date it
        // reports is the first date both keys claim.
        let error = WindowSpec::new(
            RuleZone::Named(ZoneName::new("UTC").unwrap()),
            Vec::new(),
            vec![later, earlier],
        )
        .unwrap_err();
        assert_eq!(
            error,
            WindowError::DateRepeated {
                date: Date::new(2027, 3, 20).unwrap()
            }
        );

        // Disjoint keys supplied latest-first: each date still finds its own.
        let monday = DateKey::new(
            DateRange::single(Date::new(2027, 3, 15).unwrap()),
            vec![range((9, 0), (17, 0))],
        )
        .unwrap();
        let earlier_monday = DateKey::new(
            DateRange::single(Date::new(2027, 3, 8).unwrap()),
            vec![range((12, 0), (14, 0))],
        )
        .unwrap();
        let spec = spec(Vec::new(), vec![monday, earlier_monday]);
        let zones = ZoneSchedule::fixed(Zone::utc());
        assert!(spec.contains(utc(2027, 3, 8, 13, 0), &zones));
        assert!(!spec.contains(utc(2027, 3, 8, 10, 0), &zones));
        assert!(spec.contains(utc(2027, 3, 15, 13, 0), &zones));
        assert!(!spec.contains(utc(2027, 3, 15, 8, 0), &zones));
    }

    #[test]
    fn overlapping_ranges_within_one_key_are_rejected() {
        let error = WeeklyKey::new(
            Weekdays::single(Weekday::Monday),
            vec![range((9, 0), (17, 0)), range((16, 0), (18, 0))],
        )
        .unwrap_err();
        assert_eq!(error, WindowError::RangesOverlap { at: time(16, 0) });
        assert_eq!(error.code(), "window_ranges_overlap");

        // A crossing range is measured past the end of its day, so it overlaps a
        // later evening range but not an early-morning one.
        let crossing = range((22, 0), (2, 0));
        assert_eq!(
            WeeklyKey::new(
                Weekdays::single(Weekday::Monday),
                vec![crossing, range((23, 0), (1, 0))],
            )
            .unwrap_err()
            .code(),
            "window_ranges_overlap"
        );
        assert!(
            WeeklyKey::new(
                Weekdays::single(Weekday::Monday),
                vec![crossing, range((1, 0), (3, 0))],
            )
            .is_ok()
        );
        // A range that runs to midnight and one that starts at midnight are
        // adjacent, not overlapping.
        assert!(
            WeeklyKey::new(
                Weekdays::single(Weekday::Monday),
                vec![until_midnight((9, 0)), range((0, 0), (3, 0))],
            )
            .is_ok()
        );
    }

    #[test]
    fn a_range_that_starts_where_it_ends_is_rejected() {
        let error = WallRange::new(time(0, 0), WallEnd::Time(time(0, 0))).unwrap_err();
        assert_eq!(error, WindowError::RangeEmpty { start: time(0, 0) });
        assert_eq!(error.code(), "window_range_empty");
        // `24:00` is the end of the day, so a full day is expressible.
        assert!(WallRange::new(time(0, 0), WallEnd::Midnight).is_ok());
    }

    #[test]
    fn a_reversed_date_range_is_rejected() {
        let error = DateRange::new(
            Date::new(2027, 3, 12).unwrap(),
            Date::new(2027, 3, 10).unwrap(),
        )
        .unwrap_err();
        assert_eq!(
            error,
            WindowError::DateRangeReversed {
                start: Date::new(2027, 3, 12).unwrap(),
                end: Date::new(2027, 3, 10).unwrap(),
            }
        );
        assert_eq!(error.code(), "window_date_range_reversed");
        // A single-day range is not reversed.
        assert!(
            DateRange::single(Date::new(2027, 3, 10).unwrap())
                .contains(Date::new(2027, 3, 10).unwrap())
        );
    }

    /// The specification's read accessors expose the normalised form both the
    /// engine and the wire layer read: weekdays indexed Monday first, and date
    /// overrides sorted by start with their hours as supplied.
    #[test]
    fn the_read_accessors_expose_the_normalised_form() {
        let monday = weekly(Weekday::Monday, vec![range((9, 0), (17, 0))]);
        let later = DateKey::new(
            DateRange::single(Date::new(2027, 3, 20).unwrap()),
            Vec::new(),
        )
        .unwrap();
        let earlier = DateKey::new(
            DateRange::single(Date::new(2027, 3, 10).unwrap()),
            vec![range((12, 0), (14, 0))],
        )
        .unwrap();
        let spec = spec(vec![monday], vec![later, earlier]);

        assert_eq!(spec.weekly()[0].len(), 1, "Monday is index 0");
        assert!(spec.weekly()[1..].iter().all(Vec::is_empty));
        assert_eq!(
            spec.dates()
                .iter()
                .map(|key| key.dates().start())
                .collect::<Vec<_>>(),
            [
                Date::new(2027, 3, 10).unwrap(),
                Date::new(2027, 3, 20).unwrap()
            ],
            "date overrides are sorted by start date"
        );
        assert_eq!(spec.zone(), &RuleZone::Named(ZoneName::new("UTC").unwrap()));
    }

    #[test]
    fn a_day_in_no_key_is_closed() {
        // Monday 2027-03-08 and Tuesday 2027-03-09 in UTC.
        let spec = spec(
            vec![weekly(Weekday::Monday, vec![range((9, 0), (17, 0))])],
            Vec::new(),
        );
        assert!(spec.contains(utc(2027, 3, 8, 9, 0), &ZoneSchedule::fixed(Zone::utc()),));
        assert!(!spec.contains(utc(2027, 3, 9, 9, 0), &ZoneSchedule::fixed(Zone::utc()),));
        assert_eq!(
            built(&spec, utc(2027, 3, 9, 0, 0), utc(2027, 3, 10, 0, 0)),
            []
        );
    }

    #[test]
    fn a_midnight_crossing_range_runs_into_the_next_date() {
        // Friday 2027-03-12 opens at 22:00 and runs to 02:15 on Saturday.
        let spec = spec(
            vec![weekly(Weekday::Friday, vec![range((22, 0), (2, 15))])],
            Vec::new(),
        );
        assert_eq!(
            built(&spec, utc(2027, 3, 12, 0, 0), utc(2027, 3, 14, 0, 0)),
            [(
                utc(2027, 3, 12, 22, 0).minutes(),
                utc(2027, 3, 13, 2, 15).minutes()
            )]
        );
        let zones = ZoneSchedule::fixed(Zone::utc());
        assert!(spec.contains(utc(2027, 3, 13, 2, 14), &zones));
        assert!(!spec.contains(utc(2027, 3, 13, 2, 15), &zones));
    }

    #[test]
    fn a_spill_still_reaches_a_date_closed_by_an_override() {
        // The spill belongs to the previous date's key, and a date's key
        // governs its own spill. Closing Saturday therefore closes Saturday's
        // own hours without retracting Friday night's run into it.
        let saturday = DateKey::new(
            DateRange::single(Date::new(2027, 3, 13).unwrap()),
            Vec::new(),
        )
        .unwrap();
        let spec = spec(
            vec![weekly(Weekday::Friday, vec![range((22, 0), (2, 0))])],
            vec![saturday],
        );
        assert_eq!(
            built(&spec, utc(2027, 3, 13, 0, 0), utc(2027, 3, 14, 0, 0)),
            [(
                utc(2027, 3, 13, 0, 0).minutes(),
                utc(2027, 3, 13, 2, 0).minutes()
            )]
        );
    }

    #[test]
    fn a_spill_unions_with_the_next_dates_own_ranges() {
        let spec = spec(
            vec![
                weekly(Weekday::Friday, vec![range((22, 0), (2, 0))]),
                weekly(Weekday::Saturday, vec![range((1, 0), (3, 0))]),
            ],
            Vec::new(),
        );
        assert_eq!(
            built(&spec, utc(2027, 3, 13, 0, 0), utc(2027, 3, 14, 0, 0)),
            [(
                utc(2027, 3, 13, 0, 0).minutes(),
                utc(2027, 3, 13, 3, 0).minutes()
            )]
        );
    }

    #[test]
    fn an_override_on_the_spilling_date_governs_its_own_spill() {
        let friday = DateKey::new(
            DateRange::single(Date::new(2027, 3, 12).unwrap()),
            vec![range((23, 0), (4, 0))],
        )
        .unwrap();
        let spec = spec(
            vec![weekly(Weekday::Friday, vec![range((22, 0), (2, 0))])],
            vec![friday],
        );
        // The weekly Friday hours are gone, and the override's own crossing range
        // runs into Saturday.
        assert_eq!(
            built(&spec, utc(2027, 3, 12, 12, 0), utc(2027, 3, 13, 12, 0)),
            [(
                utc(2027, 3, 12, 23, 0).minutes(),
                utc(2027, 3, 13, 4, 0).minutes()
            )]
        );
    }

    #[test]
    fn an_empty_weekly_with_dates_is_open_on_those_dates_only() {
        let date = DateKey::new(
            DateRange::single(Date::new(2027, 3, 10).unwrap()),
            vec![range((10, 0), (16, 0))],
        )
        .unwrap();
        let spec = spec(Vec::new(), vec![date]);
        assert_eq!(
            built(&spec, utc(2027, 3, 9, 0, 0), utc(2027, 3, 12, 0, 0)),
            [(
                utc(2027, 3, 10, 10, 0).minutes(),
                utc(2027, 3, 10, 16, 0).minutes()
            )]
        );
    }

    #[test]
    fn a_date_override_replaces_the_weekday_hours() {
        let date = DateKey::new(
            DateRange::single(Date::new(2027, 3, 8).unwrap()), // a Monday
            vec![range((12, 0), (14, 0))],
        )
        .unwrap();
        let spec = spec(
            vec![weekly(Weekday::Monday, vec![range((9, 0), (17, 0))])],
            vec![date],
        );
        let zones = ZoneSchedule::fixed(Zone::utc());
        assert!(!spec.contains(utc(2027, 3, 8, 10, 0), &zones));
        assert!(spec.contains(utc(2027, 3, 8, 13, 0), &zones));
        // The next Monday is back on the weekly hours.
        assert!(spec.contains(utc(2027, 3, 15, 10, 0), &zones));
    }

    #[test]
    fn a_window_builds_over_the_requested_range_without_clipping_to_midnight() {
        let spec = spec(
            vec![weekly(Weekday::Monday, vec![range((9, 0), (17, 0))])],
            Vec::new(),
        );
        assert_eq!(
            built(&spec, utc(2027, 3, 8, 10, 30), utc(2027, 3, 8, 12, 30)),
            [(
                utc(2027, 3, 8, 10, 30).minutes(),
                utc(2027, 3, 8, 12, 30).minutes()
            )]
        );
    }

    #[test]
    fn the_interval_set_agrees_with_instant_membership_minute_by_minute() {
        // A week with weekday hours, a crossing Friday night, an override and a
        // closed date, read in UTC where the local reading is the UTC one.
        let spec = spec(
            vec![
                WeeklyKey::new(
                    Weekdays::list([Weekday::Monday, Weekday::Tuesday, Weekday::Wednesday]),
                    vec![range((9, 0), (17, 0)), range((18, 0), (20, 0))],
                )
                .unwrap(),
                weekly(Weekday::Friday, vec![range((22, 0), (2, 15))]),
                weekly(Weekday::Saturday, vec![until_midnight((8, 0))]),
            ],
            vec![
                DateKey::new(
                    DateRange::single(Date::new(2027, 3, 10).unwrap()),
                    vec![range((11, 0), (13, 0))],
                )
                .unwrap(),
                DateKey::new(
                    DateRange::single(Date::new(2027, 3, 12).unwrap()),
                    Vec::new(),
                )
                .unwrap(),
            ],
        );
        let zones = ZoneSchedule::fixed(Zone::utc());
        let from = utc(2027, 3, 8, 0, 0);
        let until = utc(2027, 3, 15, 0, 0);
        let intervals = spec.intervals(&zones, from, until).unwrap();
        for minute in from.minutes()..until.minutes() {
            let instant = Instant::from_minutes(minute).unwrap();
            assert_eq!(
                intervals.contains_minute(minute),
                spec.contains(instant, &zones),
                "{instant}: the breakpoint builder and instant membership disagree"
            );
        }
    }
}