From e4048bbb5c3848056c830eda6af3f67e060abdde Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 16 Sep 2017 11:08:09 +0200 Subject: Use get_num_of_days_in_month() for adjusting --- src/timetype.rs | 2 +- src/util.rs | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/timetype.rs b/src/timetype.rs index cbb3ee0..063a27f 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -396,7 +396,7 @@ fn end_of_month(tt: TimeType) -> Result { els @ TT::Addition(_, _) | els @ TT::Subtraction(_, _) => Err(KE::from_kind(KEK::CannotCalculateEndOfMonthOn(els))), TT::Moment(m) => { - let last_day = get_num_of_days_in_month(m.year(), m.month()); + let last_day = get_num_of_days_in_month(m.year() as i64, m.month() as i64) as u32; Ok(TT::moment(NaiveDate::from_ymd(m.year(), m.month(), last_day).and_hms(0, 0, 0))) }, TT::EndOfYear(e) => do_calculate(*e), diff --git a/src/util.rs b/src/util.rs index d946f08..ec62a7f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -19,11 +19,8 @@ pub fn adjust_times_add(mut y: i64, mut mo: i64, mut d: i64, mut h: i64, mut mi: fix! { mi, 60, h } fix! { h , 24, d } - if mo == 1 || mo == 3 || mo == 5 || mo == 7 || mo == 8 || mo == 10 || mo == 12 { - fix! { d , 31, mo } - } else { - fix! { d , 30, mo } - } + let adjust = get_num_of_days_in_month(y, mo); + fix! { d , adjust, mo } fix! { mo, 12, y } @@ -63,11 +60,11 @@ pub fn adjust_times_sub(mut y: i64, mut mo: i64, mut d: i64, mut h: i64, mut mi: } #[inline] -pub fn get_num_of_days_in_month(y: i32, m: u32) -> u32 { +pub fn get_num_of_days_in_month(y: i64, m: i64) -> i64 { if m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12 { 31 } else if m == 2 { - if is_leap_year(y) { + if is_leap_year(y as i32) { 29 } else { 28 -- cgit v1.2.3