use chrono::Datelike;
use crate::eval::coercion::to_number;
use crate::eval::functions::check_arity;
use crate::eval::functions::date::serial::serial_to_date;
use crate::types::{ErrorKind, Value};
pub fn weekday_fn(args: &[Value]) -> Value {
if let Some(err) = check_arity(args, 1, 2) {
return err;
}
let serial = match to_number(args[0].clone()) { Ok(n) => n, Err(e) => return e };
let return_type = if args.len() > 1 {
match to_number(args[1].clone()) { Ok(n) => n, Err(e) => return e }
} else {
1.0
};
let date = match serial_to_date(serial) {
Some(d) => d,
None => return Value::Error(ErrorKind::Value),
};
let wd = date.weekday().num_days_from_monday() as i32;
let result = match return_type as u32 {
1 | 17 => ((wd + 1) % 7) + 1, 2 | 11 => wd + 1, 3 => wd, 12 => (wd + 6) % 7 + 1, 13 => (wd + 5) % 7 + 1, 14 => (wd + 4) % 7 + 1, 15 => (wd + 3) % 7 + 1, 16 => (wd + 2) % 7 + 1, _ => return Value::Error(ErrorKind::Num),
};
Value::Number(result as f64)
}
#[cfg(test)]
mod tests;