Implement formula evaluation for DATE statement

This commit is contained in:
Joscha 2021-12-06 12:47:19 +00:00
parent c391d76690
commit cb7c87b8b2
7 changed files with 237 additions and 30 deletions

View file

@ -1,14 +1,15 @@
use chrono::{Datelike, NaiveDate};
use chrono::{Datelike, NaiveDate, Weekday};
pub fn is_leap_year(year: i32) -> bool {
NaiveDate::from_ymd_opt(year, 2, 29).is_some()
}
pub fn add_months(year: i32, month: u32, delta: i32) -> (i32, u32) {
let month0 = (month as i32) - 1 + delta;
let year = year + month0.div_euclid(12);
let month = month0.rem_euclid(12) as u32 + 1;
(year, month)
pub fn is_iso_leap_year(year: i32) -> bool {
NaiveDate::from_isoywd_opt(year, 53, Weekday::Sun).is_some()
}
pub fn year_length(year: i32) -> u32 {
NaiveDate::from_ymd(year, 12, 31).ordinal()
}
pub fn month_length(year: i32, month: u32) -> u32 {
@ -17,3 +18,18 @@ pub fn month_length(year: i32, month: u32) -> u32 {
.pred()
.day()
}
pub fn iso_year_length(year: i32) -> u32 {
if is_iso_leap_year(year) {
53 * 7
} else {
52 * 7
}
}
pub fn add_months(year: i32, month: u32, delta: i32) -> (i32, u32) {
let month0 = (month as i32) - 1 + delta;
let year = year + month0.div_euclid(12);
let month = month0.rem_euclid(12) as u32 + 1;
(year, month)
}