use super::serial::{date_to_serial, serial_to_date};
use crate::args::ArgSchema;
use crate::function::Function;
use crate::traits::{ArgumentHandle, FunctionContext};
use chrono::{Datelike, NaiveDate};
use formualizer_common::{ExcelError, LiteralValue};
use formualizer_macros::func_caps;
fn coerce_to_serial(arg: &ArgumentHandle) -> Result<f64, ExcelError> {
let v = arg.value()?.into_literal();
if let LiteralValue::Error(e) = v {
return Err(e);
}
crate::coercion::to_number_lenient(&v).map_err(|_| {
ExcelError::new_value()
.with_message("EDATE/EOMONTH expects numeric, date, or text-numeric arguments")
})
}
fn coerce_to_int(arg: &ArgumentHandle) -> Result<i32, ExcelError> {
let v = arg.value()?.into_literal();
if let LiteralValue::Error(e) = v {
return Err(e);
}
crate::coercion::to_number_lenient(&v)
.map(|f| f.trunc() as i32)
.map_err(|_| {
ExcelError::new_value()
.with_message("EDATE/EOMONTH months argument is not a valid number")
})
}
#[derive(Debug)]
pub struct EdateFn;
impl Function for EdateFn {
func_caps!(PURE);
fn name(&self) -> &'static str {
"EDATE"
}
fn min_args(&self) -> usize {
2
}
fn arg_schema(&self) -> &'static [ArgSchema] {
use std::sync::LazyLock;
static TWO: LazyLock<Vec<ArgSchema>> = LazyLock::new(|| {
vec![
ArgSchema::number_lenient_scalar(),
ArgSchema::number_lenient_scalar(),
]
});
&TWO[..]
}
fn eval<'a, 'b, 'c>(
&self,
args: &'c [ArgumentHandle<'a, 'b>],
_ctx: &dyn FunctionContext<'b>,
) -> Result<crate::traits::CalcValue<'b>, ExcelError> {
let start_serial = coerce_to_serial(&args[0])?;
let months = coerce_to_int(&args[1])?;
let start_date = serial_to_date(start_serial)?;
let total_months =
start_date.year() as i64 * 12 + start_date.month() as i64 + months as i64;
let tm = total_months - 1;
let target_year = tm.div_euclid(12) as i32;
let target_month = (tm.rem_euclid(12) + 1) as u32;
let max_day = last_day_of_month(target_year, target_month);
let target_day = start_date.day().min(max_day);
let target_date = NaiveDate::from_ymd_opt(target_year, target_month, target_day)
.ok_or_else(ExcelError::new_num)?;
Ok(crate::traits::CalcValue::Scalar(LiteralValue::Number(
date_to_serial(&target_date),
)))
}
}
#[derive(Debug)]
pub struct EomonthFn;
impl Function for EomonthFn {
func_caps!(PURE);
fn name(&self) -> &'static str {
"EOMONTH"
}
fn min_args(&self) -> usize {
2
}
fn arg_schema(&self) -> &'static [ArgSchema] {
use std::sync::LazyLock;
static TWO: LazyLock<Vec<ArgSchema>> = LazyLock::new(|| {
vec![
ArgSchema::number_lenient_scalar(),
ArgSchema::number_lenient_scalar(),
]
});
&TWO[..]
}
fn eval<'a, 'b, 'c>(
&self,
args: &'c [ArgumentHandle<'a, 'b>],
_ctx: &dyn FunctionContext<'b>,
) -> Result<crate::traits::CalcValue<'b>, ExcelError> {
let start_serial = coerce_to_serial(&args[0])?;
let months = coerce_to_int(&args[1])?;
let start_date = serial_to_date(start_serial)?;
let total_months =
start_date.year() as i64 * 12 + start_date.month() as i64 + months as i64;
let tm = total_months - 1;
let target_year = tm.div_euclid(12) as i32;
let target_month = (tm.rem_euclid(12) + 1) as u32;
let last_day = last_day_of_month(target_year, target_month);
let target_date = NaiveDate::from_ymd_opt(target_year, target_month, last_day)
.ok_or_else(ExcelError::new_num)?;
Ok(crate::traits::CalcValue::Scalar(LiteralValue::Number(
date_to_serial(&target_date),
)))
}
}
fn last_day_of_month(year: i32, month: u32) -> u32 {
for day in (28..=31).rev() {
if NaiveDate::from_ymd_opt(year, month, day).is_some() {
return day;
}
}
28 }
pub fn register_builtins() {
use std::sync::Arc;
crate::function_registry::register_function(Arc::new(EdateFn));
crate::function_registry::register_function(Arc::new(EomonthFn));
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_workbook::TestWorkbook;
use formualizer_parse::parser::{ASTNode, ASTNodeType};
use std::sync::Arc;
fn lit(v: LiteralValue) -> ASTNode {
ASTNode::new(ASTNodeType::Literal(v), None)
}
#[test]
fn test_edate_basic() {
let wb = TestWorkbook::new().with_function(Arc::new(EdateFn));
let ctx = wb.interpreter();
let f = ctx.context.get_function("", "EDATE").unwrap();
let start = lit(LiteralValue::Number(44927.0));
let months = lit(LiteralValue::Int(3));
let result = f
.dispatch(
&[
ArgumentHandle::new(&start, &ctx),
ArgumentHandle::new(&months, &ctx),
],
&ctx.function_context(None),
)
.unwrap()
.into_literal();
assert!(matches!(result, LiteralValue::Number(_)));
}
#[test]
fn test_edate_negative_months() {
let wb = TestWorkbook::new().with_function(Arc::new(EdateFn));
let ctx = wb.interpreter();
let f = ctx.context.get_function("", "EDATE").unwrap();
let start = lit(LiteralValue::Number(44927.0)); let months = lit(LiteralValue::Int(-2));
let result = f
.dispatch(
&[
ArgumentHandle::new(&start, &ctx),
ArgumentHandle::new(&months, &ctx),
],
&ctx.function_context(None),
)
.unwrap()
.into_literal();
assert!(matches!(result, LiteralValue::Number(_)));
}
#[test]
fn test_eomonth_basic() {
let wb = TestWorkbook::new().with_function(Arc::new(EomonthFn));
let ctx = wb.interpreter();
let f = ctx.context.get_function("", "EOMONTH").unwrap();
let start = lit(LiteralValue::Number(44927.0)); let months = lit(LiteralValue::Int(0));
let result = f
.dispatch(
&[
ArgumentHandle::new(&start, &ctx),
ArgumentHandle::new(&months, &ctx),
],
&ctx.function_context(None),
)
.unwrap()
.into_literal();
assert!(matches!(result, LiteralValue::Number(_)));
}
#[test]
fn test_eomonth_february() {
let wb = TestWorkbook::new().with_function(Arc::new(EomonthFn));
let ctx = wb.interpreter();
let f = ctx.context.get_function("", "EOMONTH").unwrap();
let start = lit(LiteralValue::Number(44927.0)); let months = lit(LiteralValue::Int(1));
let result = f
.dispatch(
&[
ArgumentHandle::new(&start, &ctx),
ArgumentHandle::new(&months, &ctx),
],
&ctx.function_context(None),
)
.unwrap()
.into_literal();
assert!(matches!(result, LiteralValue::Number(_)));
}
}