diff options
author | dfhoughton <dfhoughton@gmail.com> | 2019-02-03 14:10:16 -0500 |
---|---|---|
committer | dfhoughton <dfhoughton@gmail.com> | 2019-02-03 14:10:16 -0500 |
commit | 0da507aa04c73c9a1f96107f4082232cd72ade83 (patch) | |
tree | 9a8c6e476a112e7e763d3a5a4e9ab53406ce19de | |
parent | 822e3ddfc9f99cb84355971f3158985d2ecce44d (diff) | |
download | two-timer-0da507aa04c73c9a1f96107f4082232cd72ade83.zip |
added before and after pattern
-rw-r--r-- | CHANGES.md | 2 | ||||
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | src/lib.rs | 14 | ||||
-rw-r--r-- | tests/tests.rs | 20 |
6 files changed, 35 insertions, 6 deletions
@@ -20,3 +20,5 @@ * added `<count>` `<periods>` from now/ago ## 1.0.5 * better organization and documentation of grammar +## 1.06 +* added "before and after"
\ No newline at end of file @@ -108,7 +108,7 @@ dependencies = [ [[package]] name = "two_timer" -version = "1.0.5" +version = "1.0.6" dependencies = [ "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1,6 +1,6 @@ [package] name = "two_timer" -version = "1.0.5" +version = "1.0.6" authors = ["dfhoughton <dfhoughton@gmail.com>"] description="parser for English time expressions" homepage="https://github.com/dfhoughton/two-timer" @@ -35,5 +35,6 @@ Some expressions it can handle: * Friday the 13th * 2 weeks ago * ten seconds from now +* 5 minutes before and after midnight The complete API is available at https://docs.rs/two_timer/0.1.0/two_timer/. @@ -296,7 +296,7 @@ lazy_static! { am_pm => (?-i) [["am", "AM", "pm", "PM", "a.m.", "A.M.", "p.m.", "P.M."]] bce => (?-ib) [["bce", "b.c.e.", "bc", "b.c.", "BCE", "B.C.E.", "BC", "B.C."]] ce => (?-ib) [["ce", "c.e.", "ad", "a.d.", "CE", "C.E.", "AD", "A.D."]] - direction -> [["before", "after", "around"]] + direction -> [["before", "after", "around", "before and after"]] displacement => [["week", "day", "hour", "minute", "second"]] ("s")? // not handling variable-width periods like months or years from_now_or_ago => [["from now", "ago"]] h12 => [(1..=12).into_iter().collect::<Vec<_>>()] @@ -858,7 +858,7 @@ fn handle_specific_period( }; let span = match period { Period::Week => (d, d + Duration::weeks(1)), - _ => moment_to_period(d, &period, config) + _ => moment_to_period(d, &period, config), }; return Ok(span); } @@ -1614,8 +1614,14 @@ fn adjust(d1: NaiveDateTime, d2: NaiveDateTime, m: &Match) -> (NaiveDateTime, Na let direction = adjustment.name("direction").unwrap().as_str(); match direction.chars().nth(0).unwrap() { 'b' | 'B' => { - let d = d1 - unit; - (d, d) + if direction.len() == 6 { + // before + let d = d1 - unit; + (d, d) + } else { + // before and after + (d1 - unit, d1 + unit) + } } _ => match direction.chars().nth(1).unwrap() { 'f' | 'F' => { diff --git a/tests/tests.rs b/tests/tests.rs index 67d9fe5..9278522 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1260,6 +1260,26 @@ fn one_week_after_may_6_1969() { } #[test] +fn one_week_before_and_after_may_6_1969() { + let d = NaiveDate::from_ymd(1969, 5, 6).and_hms(0, 0, 0); + let d1 = d - Duration::days(7); + let d2 = d + Duration::days(7); + let patterns = ["one week before and after May 6, 1969", "1 week before and after May 6, 1969"]; + for p in patterns.iter() { + match parse(p, None) { + Ok((start, end, _)) => { + assert_eq!(d1, start); + assert_eq!(d2, end); + } + Err(e) => { + println!("{:?}", e); + assert!(false, "didn't match"); + } + } + } +} + +#[test] fn one_week_around_may_6_1969() { let d1 = NaiveDate::from_ymd(1969, 5, 6).and_hms(0, 0, 0) - Duration::milliseconds(7 * 24 * 60 * 60 * 1000 / 2); |