use super::funejson::Value; use super::dates_add_days::{is_leap_year, parse_iso_date}; /// Completed years on `on_date`. A person attains an age at the start of their /// birthday; for a 29 February birthday in a year without one, the birthday is /// taken to be 1 March (Age of Legal Capacity (Scotland) Act 1991 s.6(2), and /// the UK reading of Family Law Reform Act 1969 s.9), not 28 February. /// /// # Panics /// Panics on a malformed or impossible date, or `on_date` before `birth_date`. pub fn age(birth_date: &str, on_date: &str) -> i64 { let birth = parse_iso_date(birth_date); let on = parse_iso_date(on_date); if on_date < birth_date { panic!("onDate {} is before birthDate {}", on_date, birth_date); } let (mut birthday_month, mut birthday_day) = (birth.month, birth.day); if birth.month == 2 && birth.day == 29 && !is_leap_year(on.year) { birthday_month = 3; birthday_day = 1; } let mut years = on.year - birth.year; if (on.month, on.day) < (birthday_month, birthday_day) { years -= 1; } years } pub fn fune_vector(args: &[Value]) -> Value { Value::Int(age(args[0].as_str(), args[1].as_str())) }