usestd::convert::TryInto;usechrono::prelude::*;// 0.4.11pubstructMondays{}implMondays{pubfncount(birth:chrono::NaiveDate,curr_date:chrono::NaiveDate)->i64{// start work at age 22letstart_work=birth+chrono::Duration::days(22*365);// retire at age 78letretire=birth+chrono::Duration::days(78*365);// check if they have retired or notletend_work=ifcurr_date>retire{retire}else{curr_date};// check days from monday, if 0 set to 7letstart_from_monday=matchstart_work.weekday().num_days_from_monday(){0=>7,x=>x,};// get first mondayletfirst_monday=start_work+chrono::Duration::days((7-start_from_monday).try_into().unwrap());// get last mondayletlast_monday=end_work-chrono::Duration::days((end_work.weekday().num_days_from_monday()).try_into().unwrap());// count days between mondaysletbetween_mondays=last_monday.signed_duration_since(first_monday).num_days();// if more than 7 days, div by 7ifbetween_mondays>7{between_mondays/7}elseifbetween_mondays>=0{1}else{0}}}#[cfg(test)]modtests{usesuper::*;#[test]fntest_1(){assert_eq!(Mondays::count(chrono::NaiveDate::from_ymd(1995,4,3),chrono::NaiveDate::from_ymd(2017,4,3)),1);}#[test]fntest_2(){assert_eq!(Mondays::count(chrono::NaiveDate::from_ymd(1995,4,2),chrono::NaiveDate::from_ymd(2018,4,2)),53);}#[test]fntest_3(){assert_eq!(Mondays::count(chrono::NaiveDate::from_ymd(1995,4,2),chrono::NaiveDate::from_ymd(2017,3,2)),0);}}
Rust, likely a better way to do this.
Rust Playground
GitHub Gist