summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordfhoughton <dfhoughton@gmail.com>2019-02-03 14:10:16 -0500
committerdfhoughton <dfhoughton@gmail.com>2019-02-03 14:10:16 -0500
commit0da507aa04c73c9a1f96107f4082232cd72ade83 (patch)
tree9a8c6e476a112e7e763d3a5a4e9ab53406ce19de
parent822e3ddfc9f99cb84355971f3158985d2ecce44d (diff)
downloadtwo-timer-0da507aa04c73c9a1f96107f4082232cd72ade83.zip
added before and after pattern
-rw-r--r--CHANGES.md2
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--README.md1
-rw-r--r--src/lib.rs14
-rw-r--r--tests/tests.rs20
6 files changed, 35 insertions, 6 deletions
diff --git a/CHANGES.md b/CHANGES.md
index b8ef0c5..c4ab7ba 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -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
diff --git a/Cargo.lock b/Cargo.lock
index f95d895..5c69fb6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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)",
diff --git a/Cargo.toml b/Cargo.toml
index 748b2ea..26f7151 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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"
diff --git a/README.md b/README.md
index 11d8314..e37cd53 100644
--- a/README.md
+++ b/README.md
@@ -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/.
diff --git a/src/lib.rs b/src/lib.rs
index dfbf061..1ffd1ee 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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);