findates 0.1.3

Financial date arithmetic: business day calendars, day count conventions, and schedule generation.
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
//! Core financial date functions.
//!
//! All functions are pure: they take dates and conventions by value or
//! reference and return new values without modifying their inputs or
//! maintaining any internal state.

use crate::calendar::Calendar;
use crate::conventions::{AdjustRule, DayCount};
use crate::error::{BusinessDayError, DayCountError};
use chrono::{Datelike, Days, NaiveDate};

/// Returns `true` if `date` is a good business day in `calendar`.
///
/// A date is a business day when it is neither a weekend day nor a holiday.
///
/// # Examples
///
/// ```rust
/// use chrono::NaiveDate;
/// use findates::calendar::basic_calendar;
/// use findates::algebra::is_business_day;
///
/// let cal = basic_calendar();
/// let monday = NaiveDate::from_ymd_opt(2024, 3, 18).unwrap();
/// let saturday = NaiveDate::from_ymd_opt(2024, 3, 16).unwrap();
///
/// assert!(is_business_day(&monday, &cal));
/// assert!(!is_business_day(&saturday, &cal));
/// ```
pub fn is_business_day(date: &NaiveDate, calendar: &Calendar) -> bool {
    if calendar.get_weekend().contains(&date.weekday()) {
        return false;
    }
    !calendar.get_holidays().contains(date)
}

/// Adjusts `date` to a business day according to `calendar` and `adjust_rule`.
///
/// Behaviour by argument combination:
///
/// | `opt_calendar` | `adjust_rule`          | Result                          |
/// |----------------|------------------------|---------------------------------|
/// | `None`         | any                    | `date` unchanged                |
/// | `Some(_)`      | `None`                 | `date` unchanged                |
/// | `Some(_)`      | `Some(Unadjusted)`     | `date` unchanged                |
/// | `Some(_)`      | `Some(_)` (other)      | adjusted to nearest business day|
///
/// If `date` is already a business day it is returned unchanged regardless
/// of the rule.
///
/// # Examples
///
/// ```rust
/// use chrono::NaiveDate;
/// use findates::calendar::basic_calendar;
/// use findates::conventions::AdjustRule;
/// use findates::algebra::adjust;
///
/// let cal = basic_calendar();
/// // 2024-03-16 is Saturday → Following moves to Monday 2024-03-18
/// let sat = NaiveDate::from_ymd_opt(2024, 3, 16).unwrap();
/// let adj = adjust(&sat, Some(&cal), Some(AdjustRule::Following));
/// assert_eq!(adj, NaiveDate::from_ymd_opt(2024, 3, 18).unwrap());
/// ```
pub fn adjust(
    date: &NaiveDate,
    opt_calendar: Option<&Calendar>,
    adjust_rule: Option<AdjustRule>,
) -> NaiveDate {
    let calendar = match opt_calendar {
        None      => return *date,
        Some(cal) => cal,
    };

    if is_business_day(date, calendar) {
        return *date;
    }

    match adjust_rule {
        None | Some(AdjustRule::Unadjusted) => *date,

        Some(AdjustRule::Following) => add_adjust(date, calendar),

        Some(AdjustRule::ModFollowing) => {
            let adj = add_adjust(date, calendar);
            if adj.month() != date.month() { sub_adjust(date, calendar) } else { adj }
        }

        Some(AdjustRule::Preceding) => sub_adjust(date, calendar),

        Some(AdjustRule::ModPreceding) => {
            let adj = sub_adjust(date, calendar);
            if adj.month() != date.month() { add_adjust(date, calendar) } else { adj }
        }

        Some(AdjustRule::HalfMonthModFollowing) => {
            let adj = add_adjust(date, calendar);
            if adj.month() != date.month() || (date.day() <= 15 && adj.day() > 15) {
                sub_adjust(date, calendar)
            } else {
                adj
            }
        }

        Some(AdjustRule::Nearest) => {
            let fwd = add_adjust(date, calendar);
            let bwd = sub_adjust(date, calendar);
            if (fwd - *date).num_days().abs() <= (bwd - *date).num_days().abs() {
                fwd
            } else {
                bwd
            }
        }
    }
}

fn add_adjust(date: &NaiveDate, calendar: &Calendar) -> NaiveDate {
    let mut t = 1u64;
    loop {
        let candidate = date.checked_add_days(Days::new(t))
            .unwrap_or_else(|| panic!("Date out of range while searching forward for business day"));
        if is_business_day(&candidate, calendar) {
            return candidate;
        }
        t += 1;
    }
}

fn sub_adjust(date: &NaiveDate, calendar: &Calendar) -> NaiveDate {
    let mut t = 1u64;
    loop {
        let candidate = date.checked_sub_days(Days::new(t))
            .unwrap_or_else(|| panic!("Date out of range while searching backward for business day"));
        if is_business_day(&candidate, calendar) {
            return candidate;
        }
        t += 1;
    }
}

/// Generates a sorted vector of every business day from `start_date` to
/// `end_date` inclusive.
///
/// Both endpoints are first adjusted to business days using `adjust_rule`
/// (defaults to [`Following`](AdjustRule::Following) when `None`).
///
/// Consecutive non-business days (e.g. a long holiday period) are handled
/// correctly — the function always steps to the next business day regardless
/// of how many non-working days lie between two valid dates.
///
/// # Examples
///
/// ```rust
/// use chrono::NaiveDate;
/// use findates::calendar::basic_calendar;
/// use findates::algebra::bus_day_schedule;
///
/// let cal   = basic_calendar();
/// let start = NaiveDate::from_ymd_opt(2024, 3, 18).unwrap(); // Monday
/// let end   = NaiveDate::from_ymd_opt(2024, 3, 22).unwrap(); // Friday
/// let days  = bus_day_schedule(&start, &end, &cal, None);
/// assert_eq!(days.len(), 5); // Mon – Fri
/// ```
pub fn bus_day_schedule(
    start_date: &NaiveDate,
    end_date: &NaiveDate,
    calendar: &Calendar,
    adjust_rule: Option<AdjustRule>,
) -> Vec<NaiveDate> {
    let rule = adjust_rule.or(Some(AdjustRule::Following));

    let new_start = adjust(start_date, Some(calendar), rule);
    let new_end   = adjust(end_date,   Some(calendar), rule);

    let mut schedule = vec![new_start];
    let mut prev = new_start;

    while prev < new_end {
        let mut t = 1u64;
        let mut next = adjust(
            &prev.checked_add_days(Days::new(t)).unwrap(),
            Some(calendar),
            rule,
        );
        while next <= prev {
            t += 1;
            next = adjust(
                &prev.checked_add_days(Days::new(t)).unwrap(),
                Some(calendar),
                rule,
            );
        }
        schedule.push(next);
        prev = next;
    }

    schedule
}

/// Counts the number of business days from `start_date` up to but not
/// including `end_date`.
///
/// This follows the common financial convention of including the start date
/// and excluding the end date.  Both endpoints are adjusted as in
/// [`bus_day_schedule`].  The result is equivalent to
/// `bus_day_schedule(...).len() - 1`.
///
/// # Examples
///
/// ```rust
/// use chrono::NaiveDate;
/// use findates::calendar::basic_calendar;
/// use findates::algebra::business_days_between;
///
/// let cal   = basic_calendar();
/// let start = NaiveDate::from_ymd_opt(2024, 3, 18).unwrap(); // Monday
/// let end   = NaiveDate::from_ymd_opt(2024, 3, 22).unwrap(); // Friday
/// // Mon, Tue, Wed, Thu = 4 business days (end excluded)
/// assert_eq!(business_days_between(&start, &end, &cal, None), 4);
/// ```
pub fn business_days_between(
    start_date: &NaiveDate,
    end_date: &NaiveDate,
    calendar: &Calendar,
    adjust_rule: Option<AdjustRule>,
) -> u64 {
    let schedule = bus_day_schedule(start_date, end_date, calendar, adjust_rule);
    schedule.len() as u64 - 1
}

/// Computes the day count fraction between two dates using the given convention.
///
/// If `calendar` is `None`, no date adjustment is performed.  If `calendar`
/// is provided and `adjust_rule` is `None`, the adjustment rule defaults to
/// [`Following`](AdjustRule::Following) before computing the fraction.
/// To suppress adjustment while still providing a calendar (e.g. for
/// [`Bd252`](DayCount::Bd252)), pass `Some(AdjustRule::Unadjusted)`.
///
/// If `end_date` is before `start_date` the fraction is computed on the
/// absolute time difference.
///
/// # Errors
///
/// Returns [`Err(DayCountError::MissingCalendar)`](DayCountError::MissingCalendar)
/// if `daycount` is [`Bd252`](DayCount::Bd252) and `calendar` is `None`.
///
/// # Examples
///
/// ```rust
/// use chrono::NaiveDate;
/// use findates::algebra::day_count_fraction;
/// use findates::conventions::DayCount;
///
/// // 2023 is not a leap year: exactly 365 days between these dates.
/// let start = NaiveDate::from_ymd_opt(2023, 1, 1).unwrap();
/// let end   = NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
///
/// // Act/365 over a full non-leap year = exactly 1.0
/// let dcf = day_count_fraction(&start, &end, DayCount::Act365, None, None).unwrap();
/// assert!((dcf - 1.0).abs() < 1e-9);
///
/// // Act/360 over 365 days
/// let dcf360 = day_count_fraction(&start, &end, DayCount::Act360, None, None).unwrap();
/// assert!((dcf360 - 365.0 / 360.0).abs() < 1e-9);
/// ```
pub fn day_count_fraction(
    start_date: &NaiveDate,
    end_date: &NaiveDate,
    daycount: DayCount,
    calendar: Option<&Calendar>,
    adjust_rule: Option<AdjustRule>,
) -> Result<f64, DayCountError> {
    let (start_adjusted, end_adjusted, some_adjust_rule, delta) = if calendar.is_none() {
        (
            *start_date,
            *end_date,
            adjust_rule,
            (*end_date - *start_date).num_days().abs(),
        )
    } else {
        let rule = if adjust_rule.is_none() {
            Some(AdjustRule::Following)
        } else {
            adjust_rule
        };
        let s = adjust(start_date, calendar, rule);
        let e = adjust(end_date, calendar, rule);
        let d = (s - e).num_days().abs();
        (s, e, rule, d)
    };

    let start_year:  i32 = start_adjusted.year();
    let start_month: i32 = start_adjusted.month() as i32;
    let mut start_day: i32 = start_adjusted.day() as i32;
    let end_year:    i32 = end_adjusted.year();
    let end_month:   i32 = end_adjusted.month() as i32;
    let mut end_day: i32 = end_adjusted.day() as i32;

    match daycount {
        DayCount::Act360 => Ok(delta as f64 / 360.0),

        DayCount::Act365 => Ok(delta as f64 / 365.0),

        DayCount::ActActISDA => {
            if start_adjusted == end_adjusted {
                return Ok(0.0);
            }
            if start_year == end_year && is_leap_year(start_year) {
                return Ok(delta as f64 / 366.0);
            }
            if start_year == end_year {
                return Ok(delta as f64 / 365.0);
            }
            if start_adjusted > end_adjusted {
                return day_count_fraction(
                    &end_adjusted,
                    &start_adjusted,
                    DayCount::ActActISDA,
                    calendar,
                    some_adjust_rule,
                );
            }
            let dcf = end_year as f64 - start_year as f64 - 1.0;
            let base1 = if is_leap_year(start_year) { 366 } else { 365 };
            let base2 = if is_leap_year(end_year)   { 366 } else { 365 };
            let dcf1 = (NaiveDate::from_ymd_opt(start_year + 1, 1, 1).unwrap()
                - start_adjusted).num_days() as f64
                / base1 as f64;
            let dcf2 = (end_adjusted
                - NaiveDate::from_ymd_opt(end_year, 1, 1).unwrap()).num_days() as f64
                / base2 as f64;
            Ok(dcf + dcf1 + dcf2)
        }

        DayCount::D30360Euro => {
            if start_day == 31 { start_day = 30; }
            if end_day   == 31 { end_day   = 30; }
            let res = 360 * (end_year - start_year)
                + 30 * (end_month - start_month)
                + (end_day - start_day);
            Ok(res as f64 / 360.0)
        }

        DayCount::D30365 => {
            let res = 360.0 * (end_year - start_year) as f64
                + 30.0 * (end_month - start_month) as f64
                + (end_day - start_day) as f64;
            Ok(res / 365.0)
        }

        DayCount::Bd252 => {
            let cal = calendar.ok_or(DayCountError::MissingCalendar)?;
            Ok(business_days_between(
                &start_adjusted,
                &end_adjusted,
                cal,
                some_adjust_rule,
            ) as f64 / 252.0)
        }
    }
}

/// Adds `years_to_add` years to `date`, returning `None` if the result is out
/// of range (e.g. Feb 29 in a non-leap target year).
///
/// chrono does not provide year arithmetic directly; this function fills the gap.
///
/// # Examples
///
/// ```rust
/// use chrono::NaiveDate;
/// use findates::algebra::checked_add_years;
///
/// let d = NaiveDate::from_ymd_opt(2023, 8, 15).unwrap();
/// assert_eq!(
///     checked_add_years(&d, 1),
///     NaiveDate::from_ymd_opt(2024, 8, 15)
/// );
///
/// // Feb 29 in a leap year → non-leap target year returns None
/// let leap_day = NaiveDate::from_ymd_opt(2024, 2, 29).unwrap();
/// assert!(checked_add_years(&leap_day, 1).is_none());
/// ```
pub fn checked_add_years(date: &NaiveDate, years_to_add: i32) -> Option<NaiveDate> {
    NaiveDate::from_ymd_opt(
        date.year() + years_to_add,
        date.month(),
        date.day(),
    )
}

/// Moves `date` forward by `n` business days in `calendar`.
///
/// **Precondition**: `date` must already be a business day.  If it is not,
/// [`Err(BusinessDayError::InvalidStartDate)`](BusinessDayError::InvalidStartDate)
/// is returned — including when `n = 0`.  The recommended workflow is to
/// adjust the start date first, then call this function:
///
/// ```rust
/// use chrono::NaiveDate;
/// use findates::calendar::basic_calendar;
/// use findates::conventions::AdjustRule;
/// use findates::algebra::{adjust, add_business_days};
///
/// let cal = basic_calendar();
/// // Start from a Saturday — adjust to Monday first, then step forward.
/// let saturday = NaiveDate::from_ymd_opt(2024, 3, 16).unwrap();
/// let monday   = adjust(&saturday, Some(&cal), Some(AdjustRule::Following));
/// let result   = add_business_days(&monday, 3, &cal).unwrap();
/// assert_eq!(result, NaiveDate::from_ymd_opt(2024, 3, 21).unwrap()); // Thursday
/// ```
///
/// When `n = 0` and `date` is a business day, `date` is returned unchanged.
pub fn add_business_days(
    date: &NaiveDate,
    n: u32,
    calendar: &Calendar,
) -> Result<NaiveDate, BusinessDayError> {
    if !is_business_day(date, calendar) {
        return Err(BusinessDayError::InvalidStartDate);
    }
    let mut current = *date;
    for _ in 0..n {
        current = add_adjust(&current, calendar);
    }
    Ok(current)
}

/// Moves `date` backward by `n` business days in `calendar`.
///
/// **Precondition**: `date` must already be a business day.  If it is not,
/// [`Err(BusinessDayError::InvalidStartDate)`](BusinessDayError::InvalidStartDate)
/// is returned — including when `n = 0`.  The recommended workflow is to
/// adjust the start date first, then call this function:
///
/// ```rust
/// use chrono::NaiveDate;
/// use findates::calendar::basic_calendar;
/// use findates::conventions::AdjustRule;
/// use findates::algebra::{adjust, subtract_business_days};
///
/// let cal = basic_calendar();
/// // Start from a Saturday — adjust to Monday first, then step backward.
/// let saturday = NaiveDate::from_ymd_opt(2024, 3, 16).unwrap();
/// let monday   = adjust(&saturday, Some(&cal), Some(AdjustRule::Following));
/// let result   = subtract_business_days(&monday, 1, &cal).unwrap();
/// assert_eq!(result, NaiveDate::from_ymd_opt(2024, 3, 15).unwrap()); // Friday
/// ```
///
/// When `n = 0` and `date` is a business day, `date` is returned unchanged.
pub fn subtract_business_days(
    date: &NaiveDate,
    n: u32,
    calendar: &Calendar,
) -> Result<NaiveDate, BusinessDayError> {
    if !is_business_day(date, calendar) {
        return Err(BusinessDayError::InvalidStartDate);
    }
    let mut current = *date;
    for _ in 0..n {
        current = sub_adjust(&current, calendar);
    }
    Ok(current)
}

fn is_leap_year(year: i32) -> bool {
    NaiveDate::from_ymd_opt(year, 2, 29).is_some()
}