celers-beat 0.3.0

Periodic task scheduler for CeleRS (Celery Beat equivalent)
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
//! Working-calendar utilities: recurring holidays and business-day arithmetic.
//!
//! This module builds on the [`HolidayCalendar`](crate::schedule::HolidayCalendar)
//! and [`DayOfWeek`] types from
//! [`crate::schedule`] and adds two capabilities required by the scheduling
//! roadmap:
//!
//! 1. **Recurring (annual) holidays** — a [`WorkingCalendar`] that knows about
//!    both fixed-date holidays (a single calendar date) and recurring holidays
//!    that fall on the same month/day every year, plus a configurable set of
//!    weekend days.
//! 2. **Business-day calculations** — [`WorkingCalendar::is_business_day`],
//!    [`WorkingCalendar::next_business_day`],
//!    [`WorkingCalendar::previous_business_day`],
//!    [`WorkingCalendar::add_business_days`] (the count may be negative) and
//!    [`WorkingCalendar::business_days_between`], all of which honour both the
//!    weekend definition and the holiday set.
//!
//! Everything is implemented with [`chrono`] and pure Rust — there are no
//! external calendar dependencies and no shortcuts.
//!
//! # Example
//!
//! ```
//! use celers_beat::calendar::WorkingCalendar;
//! use chrono::NaiveDate;
//!
//! let mut cal = WorkingCalendar::new();
//! // New Year's Day every year.
//! cal.add_recurring_holiday("New Year's Day", 1, 1);
//! // A one-off company holiday in 2026 only.
//! cal.add_fixed_holiday("Company Offsite", 2026, 3, 16);
//!
//! // 2026-01-01 is a Thursday but it is a recurring holiday.
//! let new_year = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();
//! assert!(cal.is_holiday(new_year));
//! assert!(!cal.is_business_day(new_year));
//!
//! // The first business day strictly after New Year's Day 2026 (a Thursday).
//! assert_eq!(
//!     cal.next_business_day(new_year),
//!     NaiveDate::from_ymd_opt(2026, 1, 2)
//! );
//! ```

use crate::schedule::DayOfWeek;
use chrono::{DateTime, Datelike, NaiveDate, Utc};
use serde::{Deserialize, Serialize};

/// Maximum number of days the business-day search routines will scan before
/// giving up. This guards against pathological calendars (e.g. every day a
/// holiday) so the routines always terminate. Ten years of calendar days is
/// far larger than any realistic run of consecutive non-business days.
const MAX_SCAN_DAYS: i64 = 3660;

/// A named holiday that may be either a fixed calendar date or a recurring
/// annual (month/day) holiday.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum CalendarHoliday {
    /// A holiday that occurs once on a specific calendar date.
    Fixed {
        /// Human-readable name of the holiday.
        name: String,
        /// Year of the holiday.
        year: i32,
        /// Month of the holiday (1-12).
        month: u32,
        /// Day of the holiday (1-31).
        day: u32,
    },
    /// A holiday that recurs on the same month/day every year.
    Recurring {
        /// Human-readable name of the holiday.
        name: String,
        /// Month of the holiday (1-12).
        month: u32,
        /// Day of the holiday (1-31).
        day: u32,
    },
}

impl CalendarHoliday {
    /// The display name of the holiday.
    pub fn name(&self) -> &str {
        match self {
            CalendarHoliday::Fixed { name, .. } => name,
            CalendarHoliday::Recurring { name, .. } => name,
        }
    }

    /// Returns `true` when this holiday falls on `date`.
    pub fn matches(&self, date: NaiveDate) -> bool {
        match self {
            CalendarHoliday::Fixed {
                year, month, day, ..
            } => date.year() == *year && date.month() == *month && date.day() == *day,
            CalendarHoliday::Recurring { month, day, .. } => {
                date.month() == *month && date.day() == *day
            }
        }
    }

    /// Returns `true` for recurring (annual) holidays.
    pub fn is_recurring(&self) -> bool {
        matches!(self, CalendarHoliday::Recurring { .. })
    }
}

/// A working calendar that combines a weekend definition with fixed and
/// recurring holidays, and exposes business-day arithmetic.
///
/// The weekend defaults to Saturday and Sunday but may be customised (for
/// example to model a Friday/Saturday weekend, or a six-day working week).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkingCalendar {
    /// All holidays known to the calendar.
    holidays: Vec<CalendarHoliday>,
    /// The set of weekdays treated as the weekend (non-business days).
    weekend: Vec<DayOfWeek>,
}

impl Default for WorkingCalendar {
    fn default() -> Self {
        Self::new()
    }
}

impl WorkingCalendar {
    /// Create a new calendar with no holidays and the default Saturday/Sunday
    /// weekend.
    pub fn new() -> Self {
        Self {
            holidays: Vec::new(),
            weekend: vec![DayOfWeek::Saturday, DayOfWeek::Sunday],
        }
    }

    /// Create a calendar with an explicit weekend definition and no holidays.
    ///
    /// Duplicate entries in `weekend` are removed so membership tests stay
    /// well-defined.
    pub fn with_weekend(weekend: impl IntoIterator<Item = DayOfWeek>) -> Self {
        let mut cal = Self {
            holidays: Vec::new(),
            weekend: Vec::new(),
        };
        cal.set_weekend(weekend);
        cal
    }

    /// Replace the weekend definition. Duplicate days are removed.
    pub fn set_weekend(&mut self, weekend: impl IntoIterator<Item = DayOfWeek>) {
        let mut days: Vec<DayOfWeek> = Vec::new();
        for day in weekend {
            if !days.contains(&day) {
                days.push(day);
            }
        }
        self.weekend = days;
    }

    /// The current weekend definition.
    pub fn weekend(&self) -> &[DayOfWeek] {
        &self.weekend
    }

    /// Returns `true` when `day` is part of the configured weekend.
    pub fn is_weekend_day(&self, day: DayOfWeek) -> bool {
        self.weekend.contains(&day)
    }

    /// Add a fixed-date (one-off) holiday.
    pub fn add_fixed_holiday(&mut self, name: impl Into<String>, year: i32, month: u32, day: u32) {
        self.holidays.push(CalendarHoliday::Fixed {
            name: name.into(),
            year,
            month,
            day,
        });
    }

    /// Add a recurring (annual) holiday that falls on the same month/day every
    /// year.
    pub fn add_recurring_holiday(&mut self, name: impl Into<String>, month: u32, day: u32) {
        self.holidays.push(CalendarHoliday::Recurring {
            name: name.into(),
            month,
            day,
        });
    }

    /// Add an already-constructed [`CalendarHoliday`].
    pub fn add_holiday(&mut self, holiday: CalendarHoliday) {
        self.holidays.push(holiday);
    }

    /// Remove every holiday whose name equals `name` (case-sensitive).
    ///
    /// Returns the number of holiday entries removed.
    pub fn remove_holiday_by_name(&mut self, name: &str) -> usize {
        let before = self.holidays.len();
        self.holidays.retain(|h| h.name() != name);
        before - self.holidays.len()
    }

    /// Remove every holiday that falls on `date` (fixed or recurring).
    ///
    /// Returns the number of holiday entries removed.
    pub fn remove_holiday_on(&mut self, date: NaiveDate) -> usize {
        let before = self.holidays.len();
        self.holidays.retain(|h| !h.matches(date));
        before - self.holidays.len()
    }

    /// The holidays currently held by the calendar.
    pub fn holidays(&self) -> &[CalendarHoliday] {
        &self.holidays
    }

    /// Number of holiday entries.
    pub fn len(&self) -> usize {
        self.holidays.len()
    }

    /// Returns `true` when no holidays are configured.
    pub fn is_empty(&self) -> bool {
        self.holidays.is_empty()
    }

    /// Returns `true` when `date` is a holiday (fixed or recurring).
    pub fn is_holiday(&self, date: NaiveDate) -> bool {
        self.holidays.iter().any(|h| h.matches(date))
    }

    /// Returns the name of the holiday falling on `date`, if any. When several
    /// holidays coincide the first one added wins.
    pub fn holiday_name(&self, date: NaiveDate) -> Option<&str> {
        self.holidays
            .iter()
            .find(|h| h.matches(date))
            .map(|h| h.name())
    }

    /// All holidays observed in `year`, returned as `(date, name)` pairs sorted
    /// by date. Recurring holidays are materialised for the requested year.
    pub fn holidays_in_year(&self, year: i32) -> Vec<(NaiveDate, String)> {
        let mut result: Vec<(NaiveDate, String)> = Vec::new();
        for holiday in &self.holidays {
            let candidate = match holiday {
                CalendarHoliday::Fixed {
                    year: hy,
                    month,
                    day,
                    name,
                } => {
                    if *hy == year {
                        NaiveDate::from_ymd_opt(year, *month, *day).map(|d| (d, name.clone()))
                    } else {
                        None
                    }
                }
                CalendarHoliday::Recurring { month, day, name } => {
                    NaiveDate::from_ymd_opt(year, *month, *day).map(|d| (d, name.clone()))
                }
            };
            if let Some(entry) = candidate {
                result.push(entry);
            }
        }
        result.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1)));
        result
    }

    /// Returns `true` when `date` is a business day: neither a weekend day nor
    /// a holiday.
    pub fn is_business_day(&self, date: NaiveDate) -> bool {
        let day = DayOfWeek::from_chrono(date.weekday());
        !self.is_weekend_day(day) && !self.is_holiday(date)
    }

    /// The first business day strictly **after** `date`.
    ///
    /// Returns `None` only if no business day can be found within
    /// `MAX_SCAN_DAYS` (an effectively impossible calendar).
    pub fn next_business_day(&self, date: NaiveDate) -> Option<NaiveDate> {
        let mut current = date;
        for _ in 0..MAX_SCAN_DAYS {
            current = current.succ_opt()?;
            if self.is_business_day(current) {
                return Some(current);
            }
        }
        None
    }

    /// The first business day strictly **before** `date`.
    ///
    /// Returns `None` only if no business day can be found within
    /// `MAX_SCAN_DAYS`.
    pub fn previous_business_day(&self, date: NaiveDate) -> Option<NaiveDate> {
        let mut current = date;
        for _ in 0..MAX_SCAN_DAYS {
            current = current.pred_opt()?;
            if self.is_business_day(current) {
                return Some(current);
            }
        }
        None
    }

    /// The first business day on or **after** `date` (returns `date` itself
    /// when it is already a business day).
    pub fn business_day_on_or_after(&self, date: NaiveDate) -> Option<NaiveDate> {
        if self.is_business_day(date) {
            return Some(date);
        }
        self.next_business_day(date)
    }

    /// The first business day on or **before** `date` (returns `date` itself
    /// when it is already a business day).
    pub fn business_day_on_or_before(&self, date: NaiveDate) -> Option<NaiveDate> {
        if self.is_business_day(date) {
            return Some(date);
        }
        self.previous_business_day(date)
    }

    /// Add `n` business days to `date`.
    ///
    /// * `n == 0` returns `date` unchanged (even when `date` is a weekend or
    ///   holiday — no normalisation is performed for a zero offset).
    /// * `n > 0` steps forward `n` business days.
    /// * `n < 0` steps backward `|n|` business days.
    ///
    /// Returns `None` if the search exceeds `MAX_SCAN_DAYS` or runs off the
    /// representable date range.
    pub fn add_business_days(&self, date: NaiveDate, n: i64) -> Option<NaiveDate> {
        if n == 0 {
            return Some(date);
        }
        let mut remaining = n.unsigned_abs();
        let mut current = date;
        let forward = n > 0;
        for _ in 0..MAX_SCAN_DAYS {
            current = if forward {
                current.succ_opt()?
            } else {
                current.pred_opt()?
            };
            if self.is_business_day(current) {
                remaining -= 1;
                if remaining == 0 {
                    return Some(current);
                }
            }
        }
        None
    }

    /// Count the number of business days in the half-open interval
    /// `(start, end]` — i.e. business days strictly after `start` up to and
    /// including `end`.
    ///
    /// The result is **signed**: when `end` precedes `start` the count is
    /// negative and counts business days in `(end, start]` with a flipped sign.
    /// When `start == end` the result is `0`. This convention makes
    /// `add_business_days(start, business_days_between(start, end))` land on the
    /// next business day on/before `end` and round-trips cleanly for
    /// business-day endpoints.
    ///
    /// Returns `None` if the span exceeds `MAX_SCAN_DAYS`.
    pub fn business_days_between(&self, start: NaiveDate, end: NaiveDate) -> Option<i64> {
        use std::cmp::Ordering;
        match start.cmp(&end) {
            Ordering::Equal => Some(0),
            Ordering::Less => self.count_business_days_exclusive_inclusive(start, end),
            Ordering::Greater => self
                .count_business_days_exclusive_inclusive(end, start)
                .map(|c| -c),
        }
    }

    /// Count business days in `(lo, hi]` where `lo < hi`.
    fn count_business_days_exclusive_inclusive(&self, lo: NaiveDate, hi: NaiveDate) -> Option<i64> {
        let mut count = 0i64;
        let mut current = lo;
        for _ in 0..MAX_SCAN_DAYS {
            current = current.succ_opt()?;
            if current > hi {
                break;
            }
            if self.is_business_day(current) {
                count += 1;
            }
        }
        if current < hi {
            // We exhausted the scan budget before reaching `hi`.
            return None;
        }
        Some(count)
    }
}

/// Convenience: extract the `NaiveDate` (in UTC) of a timestamp so callers that
/// work with [`DateTime<Utc>`] can feed the date-based calendar API directly.
pub fn date_of(timestamp: DateTime<Utc>) -> NaiveDate {
    timestamp.date_naive()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schedule::DayOfWeek;

    fn d(y: i32, m: u32, day: u32) -> NaiveDate {
        NaiveDate::from_ymd_opt(y, m, day).expect("valid test date")
    }

    #[test]
    fn weekend_defaults_to_sat_sun() {
        let cal = WorkingCalendar::new();
        // 2026-06-13 is a Saturday, 2026-06-14 is a Sunday, 2026-06-12 a Friday.
        assert!(cal.is_weekend_day(DayOfWeek::Saturday));
        assert!(cal.is_weekend_day(DayOfWeek::Sunday));
        assert!(!cal.is_weekend_day(DayOfWeek::Monday));
        assert!(!cal.is_business_day(d(2026, 6, 13)));
        assert!(!cal.is_business_day(d(2026, 6, 14)));
        assert!(cal.is_business_day(d(2026, 6, 12)));
    }

    #[test]
    fn custom_weekend_friday_saturday() {
        let cal = WorkingCalendar::with_weekend([DayOfWeek::Friday, DayOfWeek::Saturday]);
        // 2026-06-12 Friday, 2026-06-13 Saturday, 2026-06-14 Sunday.
        assert!(!cal.is_business_day(d(2026, 6, 12))); // Friday weekend
        assert!(!cal.is_business_day(d(2026, 6, 13))); // Saturday weekend
        assert!(cal.is_business_day(d(2026, 6, 14))); // Sunday is a work day
    }

    #[test]
    fn set_weekend_dedups() {
        let mut cal = WorkingCalendar::new();
        cal.set_weekend([DayOfWeek::Sunday, DayOfWeek::Sunday, DayOfWeek::Saturday]);
        assert_eq!(cal.weekend().len(), 2);
    }

    #[test]
    fn fixed_and_recurring_holidays() {
        let mut cal = WorkingCalendar::new();
        cal.add_recurring_holiday("New Year's Day", 1, 1);
        cal.add_fixed_holiday("Company Offsite", 2026, 3, 16);

        // Recurring matches every year.
        assert!(cal.is_holiday(d(2025, 1, 1)));
        assert!(cal.is_holiday(d(2026, 1, 1)));
        assert!(cal.is_holiday(d(2030, 1, 1)));
        assert_eq!(cal.holiday_name(d(2026, 1, 1)), Some("New Year's Day"));

        // Fixed matches only that year.
        assert!(cal.is_holiday(d(2026, 3, 16)));
        assert!(!cal.is_holiday(d(2027, 3, 16)));
    }

    #[test]
    fn remove_holiday_by_name_and_date() {
        let mut cal = WorkingCalendar::new();
        cal.add_recurring_holiday("New Year's Day", 1, 1);
        cal.add_fixed_holiday("Offsite", 2026, 3, 16);
        cal.add_fixed_holiday("Offsite", 2027, 3, 16);

        assert_eq!(cal.remove_holiday_by_name("Offsite"), 2);
        assert!(cal.is_holiday(d(2026, 1, 1)));
        assert!(!cal.is_holiday(d(2026, 3, 16)));

        assert_eq!(cal.remove_holiday_on(d(2026, 1, 1)), 1);
        assert!(!cal.is_holiday(d(2026, 1, 1)));
        assert!(cal.is_empty());
    }

    #[test]
    fn holidays_in_year_materialises_recurring() {
        let mut cal = WorkingCalendar::new();
        cal.add_recurring_holiday("New Year's Day", 1, 1);
        cal.add_recurring_holiday("Christmas", 12, 25);
        cal.add_fixed_holiday("One-off 2026", 2026, 6, 10);
        cal.add_fixed_holiday("One-off 2025", 2025, 6, 10);

        let in_2026 = cal.holidays_in_year(2026);
        assert_eq!(in_2026.len(), 3);
        // Sorted by date: Jan 1, Jun 10, Dec 25.
        assert_eq!(in_2026[0].0, d(2026, 1, 1));
        assert_eq!(in_2026[1].0, d(2026, 6, 10));
        assert_eq!(in_2026[1].1, "One-off 2026");
        assert_eq!(in_2026[2].0, d(2026, 12, 25));
    }

    #[test]
    fn business_day_skips_weekend() {
        let cal = WorkingCalendar::new();
        // Friday 2026-06-12 -> next business day is Monday 2026-06-15.
        assert_eq!(cal.next_business_day(d(2026, 6, 12)), Some(d(2026, 6, 15)));
        // Monday 2026-06-15 -> previous business day is Friday 2026-06-12.
        assert_eq!(
            cal.previous_business_day(d(2026, 6, 15)),
            Some(d(2026, 6, 12))
        );
    }

    #[test]
    fn business_day_skips_holiday() {
        let mut cal = WorkingCalendar::new();
        // 2026-07-03 (Friday) declared a holiday; 2026-07-04 Sat, 07-05 Sun.
        cal.add_fixed_holiday("Independence Day (observed)", 2026, 7, 3);
        // Thursday 2026-07-02 -> next business day skips holiday Fri + weekend
        // -> Monday 2026-07-06.
        assert_eq!(cal.next_business_day(d(2026, 7, 2)), Some(d(2026, 7, 6)));
    }

    #[test]
    fn on_or_after_and_before() {
        let cal = WorkingCalendar::new();
        // Saturday 2026-06-13.
        assert_eq!(
            cal.business_day_on_or_after(d(2026, 6, 13)),
            Some(d(2026, 6, 15))
        );
        assert_eq!(
            cal.business_day_on_or_before(d(2026, 6, 13)),
            Some(d(2026, 6, 12))
        );
        // A weekday returns itself.
        assert_eq!(
            cal.business_day_on_or_after(d(2026, 6, 12)),
            Some(d(2026, 6, 12))
        );
    }

    #[test]
    fn add_business_days_positive() {
        let cal = WorkingCalendar::new();
        // Monday 2026-06-08 + 5 business days = Monday 2026-06-15.
        assert_eq!(
            cal.add_business_days(d(2026, 6, 8), 5),
            Some(d(2026, 6, 15))
        );
        // Friday 2026-06-12 + 1 business day skips the weekend -> Mon 06-15.
        assert_eq!(
            cal.add_business_days(d(2026, 6, 12), 1),
            Some(d(2026, 6, 15))
        );
    }

    #[test]
    fn add_business_days_negative() {
        let cal = WorkingCalendar::new();
        // Monday 2026-06-15 - 1 business day -> Friday 2026-06-12.
        assert_eq!(
            cal.add_business_days(d(2026, 6, 15), -1),
            Some(d(2026, 6, 12))
        );
        // Monday 2026-06-15 - 5 business days -> Monday 2026-06-08.
        assert_eq!(
            cal.add_business_days(d(2026, 6, 15), -5),
            Some(d(2026, 6, 8))
        );
    }

    #[test]
    fn add_business_days_zero_is_identity() {
        let cal = WorkingCalendar::new();
        // Even on a Saturday, n == 0 returns the same date untouched.
        assert_eq!(
            cal.add_business_days(d(2026, 6, 13), 0),
            Some(d(2026, 6, 13))
        );
    }

    #[test]
    fn add_business_days_with_holiday() {
        let mut cal = WorkingCalendar::new();
        cal.add_fixed_holiday("Holiday", 2026, 6, 10); // Wednesday
                                                       // Monday 2026-06-08 + 3 business days: Tue 09, [Wed 10 holiday skipped],
                                                       // Thu 11, Fri 12 -> 2026-06-12.
        assert_eq!(
            cal.add_business_days(d(2026, 6, 8), 3),
            Some(d(2026, 6, 12))
        );
    }

    #[test]
    fn business_days_between_basic() {
        let cal = WorkingCalendar::new();
        // (Mon 06-08, Fri 06-12] -> Tue,Wed,Thu,Fri = 4 business days.
        assert_eq!(
            cal.business_days_between(d(2026, 6, 8), d(2026, 6, 12)),
            Some(4)
        );
        // Across a weekend: (Fri 06-12, Mon 06-15] -> only Mon = 1.
        assert_eq!(
            cal.business_days_between(d(2026, 6, 12), d(2026, 6, 15)),
            Some(1)
        );
        // Same day -> 0.
        assert_eq!(
            cal.business_days_between(d(2026, 6, 12), d(2026, 6, 12)),
            Some(0)
        );
    }

    #[test]
    fn business_days_between_is_signed() {
        let cal = WorkingCalendar::new();
        let forward = cal
            .business_days_between(d(2026, 6, 8), d(2026, 6, 12))
            .expect("forward count");
        let backward = cal
            .business_days_between(d(2026, 6, 12), d(2026, 6, 8))
            .expect("backward count");
        assert_eq!(forward, 4);
        assert_eq!(backward, -4);
    }

    #[test]
    fn business_days_between_excludes_holidays() {
        let mut cal = WorkingCalendar::new();
        cal.add_fixed_holiday("Holiday", 2026, 6, 10); // Wednesday
                                                       // (Mon 06-08, Fri 06-12] minus the Wed holiday = Tue,Thu,Fri = 3.
        assert_eq!(
            cal.business_days_between(d(2026, 6, 8), d(2026, 6, 12)),
            Some(3)
        );
    }

    #[test]
    fn add_then_between_round_trips_for_business_days() {
        let cal = WorkingCalendar::new();
        let start = d(2026, 6, 8); // Monday, a business day.
        for n in 1..=10i64 {
            let target = cal.add_business_days(start, n).expect("add");
            assert_eq!(
                cal.business_days_between(start, target),
                Some(n),
                "round-trip failed for n={n}"
            );
        }
    }

    #[test]
    fn date_of_extracts_utc_date() {
        use chrono::TimeZone;
        let ts = Utc.with_ymd_and_hms(2026, 6, 13, 23, 59, 0).unwrap();
        assert_eq!(date_of(ts), d(2026, 6, 13));
    }
}