From b4487663aa5d809fb5d6ed59a2f8a1218eb09951 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Sat, 24 Oct 2026 17:10:13 +0200 Subject: [PATCH] Fix YEARFRAC basis 0 for February-end start and 31st end Found while testing crates/functions/src/date.rs:443-457 (yearfrac_core, basis 0) Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/functions/src/date.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/crates/functions/src/date.rs b/crates/functions/src/date.rs index 05638b2b7..30130d1cf 100644 --- a/crates/functions/src/date.rs +++ b/crates/functions/src/date.rs @@ -441,18 +441,14 @@ pub(crate) fn yearfrac_core(sys: DateSystem, s: i64, e: i64, basis: i64) -> R { let (mut a, mut b) = (d1.day as i64, d2.day as i64); - if a == 31 && b == 31 { - a = 30; + if last_of_feb(&d1) && last_of_feb(&d2) { b = 30; - } else if a == 31 { + } + if last_of_feb(&d1) || a == 31 { a = 30; - } else if a == 30 && b == 31 { + } + if a == 30 && b == 31 { b = 30; - } else if last_of_feb(&d1) && last_of_feb(&d2) { - a = 30; - b = 30; - } else if last_of_feb(&d1) { - a = 30; } ((d2.year as i64 - d1.year as i64) * 360 + (d2.month as i64 - d1.month as i64) * 30 + b - a) as f64 / 360.0 } @@ -811,6 +807,13 @@ mod tests { close(ev("YEARFRAC", vec![s, n(d(2012.0, 1.0, 1.0))]), 0.0); } + #[test] + fn yearfrac_basis0_feb_end_to_31() { + close(ev("YEARFRAC", vec![n(d(2021.0, 2.0, 28.0)), n(d(2021.0, 3.0, 31.0)), n(0.0)]), 30.0 / 360.0); + close(ev("YEARFRAC", vec![n(d(2020.0, 2.0, 29.0)), n(d(2020.0, 3.0, 31.0)), n(0.0)]), 30.0 / 360.0); + close(ev("YEARFRAC", vec![n(d(2021.0, 2.0, 28.0)), n(d(2021.0, 2.0, 28.0)), n(0.0)]), 0.0); + } + #[test] fn date_lifting() { let v = ev("YEAR", vec![av(row(&[43831.0, 46302.0]))]);