diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-09-27 20:50:45 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-27 20:50:45 +0200 |
commit | 5fed7376f530d5c2fcbb7e3dd4fe59533bf4860b (patch) | |
tree | 498dbea17e30a1da866d72884a6e211307692c6f | |
parent | ba211d419e78cd9148803868096d98a1de38cce3 (diff) | |
parent | da0e5e0222bf3a39a02f984bc0456e87b363ad3c (diff) |
Merge branch 'fixes'
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | src/indicator.rs | 16 | ||||
-rw-r--r-- | src/iter.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 7 | ||||
-rw-r--r-- | src/timetype.rs | 75 | ||||
-rw-r--r-- | src/util.rs | 6 |
6 files changed, 107 insertions, 5 deletions
@@ -18,6 +18,10 @@ error-chain = "0.10" filters = { version = "0.1.1", optional = true } +[dev-dependencies] +env_logger = "0.4" +log = "0.3" + [features] default = [] with-filters = [ "filters" ] diff --git a/src/indicator.rs b/src/indicator.rs index 72e1136..fa9bf63 100644 --- a/src/indicator.rs +++ b/src/indicator.rs @@ -1,3 +1,15 @@ +#[cfg(feature = "with-filters")] +use filters::filter::Filter; + +#[cfg(feature = "with-filters")] +use filters::filter::IntoFilter; + +#[cfg(feature = "with-filters")] +use timetype::TimeType; + +#[cfg(feature = "with-filters")] +use chrono::Datelike; + #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug)] pub enum Day { Monday, @@ -64,7 +76,7 @@ pub struct DayFilter(Day); #[cfg(feature = "with-filters")] impl Filter<TimeType> for DayFilter { fn filter(&self, tt: &TimeType) -> bool { - tt.get_moment(|mom| mom.weekday() == self.0.into()).unwrap_or(false) + tt.get_moment().map(|mom| mom.weekday() == self.0.clone().into()).unwrap_or(false) } } @@ -85,7 +97,7 @@ pub struct MonthFilter(Month); #[cfg(feature = "with-filters")] impl Filter<TimeType> for MonthFilter { fn filter(&self, tt: &TimeType) -> bool { - tt.get_moment(|mom| mom.month() == self.0.into()).unwrap_or(false) + tt.get_moment().map(|mom| mom.month() == self.0.clone().into()).unwrap_or(false) } } diff --git a/src/iter.rs b/src/iter.rs index 9713e51..4e7c18c 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -381,6 +381,8 @@ mod type_tests { mod type_tests_filter_interface { use super::*; use super::extensions::*; + use filters::filter::Filter; + use filters::filter::IntoFilter; #[test] fn test_compile() { @@ -388,7 +390,7 @@ mod type_tests_filter_interface { let _ = TimeType::today() .yearly(1) .unwrap() - .every(::indicator::Day::Monday.or(::indicator::Month::January)) + .every(::indicator::Day::Monday.into_filter().or(::indicator::Month::January)) .collect::<Vec<_>>(); } } @@ -5,6 +5,13 @@ extern crate chrono; #[cfg(feature = "with-filters")] extern crate filters; +#[cfg(test)] +extern crate env_logger; + +#[cfg(test)] +#[macro_use] +extern crate log; + pub mod error; pub mod iter; pub mod result; diff --git a/src/timetype.rs b/src/timetype.rs index cfabe1e..0d2081b 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -1987,6 +1987,7 @@ mod timetype_value_tests { #[cfg(test)] mod moment_plus_amount_tests { + use env_logger; use super::TimeType as TT; use chrono::NaiveDate; use chrono::Timelike; @@ -2002,8 +2003,13 @@ mod moment_plus_amount_tests { } => { #[test] fn $name() { + let _ = env_logger::init(); + let base = TT::moment($base); + debug!("Using base = {:?}", base); + debug!(" + {:?}", $amount); let result = $op(base, $amount).calculate(); + debug!(" -> = {:?}", result); assert!(result.is_ok(), "Operation failed: {:?}", result); let result = result.unwrap(); let expected = $exp; @@ -2203,6 +2209,75 @@ mod moment_plus_amount_tests { expected = NaiveDate::from_ymd(1999, 1, 1).and_hms(0, 0, 0); } + generate_test_moment_plus_amount! { + name = test_moment_plus_more_than_one_minute_in_seconds; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::seconds(130); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 2, 10); + } + + generate_test_moment_plus_amount! { + name = test_moment_plus_more_than_one_hour_in_minutes; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::minutes(130); + expected = NaiveDate::from_ymd(2000, 1, 1).and_hms(2, 10, 00); + } + + generate_test_moment_plus_amount! { + name = test_moment_plus_more_than_one_day_in_hours_1; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::hours(50); + expected = NaiveDate::from_ymd(2000, 1, 3).and_hms(2, 0, 0); + } + + generate_test_moment_plus_amount! { + name = test_moment_plus_more_than_one_day_in_hours_2; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::hours(170); + expected = NaiveDate::from_ymd(2000, 1, 8).and_hms(2, 0, 0); + } + + generate_test_moment_plus_amount! { + name = test_moment_plus_more_than_one_month_in_days_1; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::days(80); + expected = NaiveDate::from_ymd(2000, 3,21).and_hms(0, 0, 0); + } + + generate_test_moment_plus_amount! { + name = test_moment_plus_more_than_one_month_in_days_2; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::days(120); + expected = NaiveDate::from_ymd(2000, 4,30).and_hms(0, 0, 0); + } + + generate_test_moment_plus_amount! { + name = test_moment_plus_more_than_one_month_in_days_3; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::days(150); + expected = NaiveDate::from_ymd(2000, 5,30).and_hms(0, 0, 0); + } + + generate_test_moment_plus_amount! { + name = test_moment_plus_more_than_one_year_in_months_1; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::months(15); + expected = NaiveDate::from_ymd(2001, 4, 1).and_hms(0, 0, 0); + } + + generate_test_moment_plus_amount! { + name = test_moment_plus_more_than_one_year_in_months_2; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::months(25); + expected = NaiveDate::from_ymd(2002, 2, 1).and_hms(0, 0, 0); + } + + generate_test_moment_plus_amount! { + name = test_moment_plus_more_than_one_year_in_months_3; + base = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0); + amount = TT::months(78); + expected = NaiveDate::from_ymd(2006, 7, 1).and_hms(0, 0, 0); + } } #[cfg(test)] diff --git a/src/util.rs b/src/util.rs index cd6e038..86437be 100644 --- a/src/util.rs +++ b/src/util.rs @@ -19,8 +19,10 @@ 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 } - let adjust = get_num_of_days_in_month(y, mo); - fix! { d , adjust, mo } + while d > get_num_of_days_in_month(y, mo) { + d -= get_num_of_days_in_month(y, mo); + mo += 1; + } fix! { mo, 12, y } |