diff options
author | dfhoughton <dfhoughton@gmail.com> | 2019-02-10 16:35:23 -0500 |
---|---|---|
committer | dfhoughton <dfhoughton@gmail.com> | 2019-02-10 16:35:23 -0500 |
commit | 72efcceb945fb80a576775a513b64e104078e9f0 (patch) | |
tree | 8988e40d08276cf6f8e723cf5fcb79af63b1eb7b | |
parent | 46b9401a350645472ded762adce28db7545895c0 (diff) | |
download | two-timer-72efcceb945fb80a576775a513b64e104078e9f0.zip |
added specific_time rule
-rw-r--r-- | CHANGES.md | 4 | ||||
-rw-r--r-- | Cargo.lock | 4 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | src/lib.rs | 54 | ||||
-rw-r--r-- | tests/tests.rs | 16 |
6 files changed, 67 insertions, 14 deletions
@@ -22,4 +22,6 @@ * better organization and documentation of grammar ## 1.06 * added "before and after" -* fixed "Friday the 13th" and "the 31st" to scan back through the calendar to the nearest match
\ No newline at end of file +* fixed "Friday the 13th" and "the 31st" to scan back through the calendar to the nearest match +## 1.0.7 +* added `<specific_time>` pattern: e.g., 1969-05-06 12:03:05
\ No newline at end of file @@ -1,3 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. [[package]] name = "aho-corasick" version = "0.6.9" @@ -108,7 +110,7 @@ dependencies = [ [[package]] name = "two_timer" -version = "1.0.6" +version = "1.0.7" 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.6" +version = "1.0.7" authors = ["dfhoughton <dfhoughton@gmail.com>"] description="parser for English time expressions" homepage="https://github.com/dfhoughton/two-timer" @@ -36,5 +36,6 @@ Some expressions it can handle: * 2 weeks ago * ten seconds from now * 5 minutes before and after midnight +* 1969-05-06 12:03:05 The complete API is available at https://docs.rs/two_timer/0.1.0/two_timer/. @@ -269,7 +269,9 @@ lazy_static! { at_time -> ("at") <time> - specific_time => <first_time> | <last_time> + specific_time => <first_time> | <last_time> | <precise_time> + + precise_time -> <n_date> <hour_24> time -> <hour_12> <am_pm>? | <hour_24> | <named_time> @@ -723,6 +725,19 @@ fn specific(m: &Match) -> bool { m.has("specific_day") || m.has("specific_period") || m.has("specific_time") } +fn n_date(date: &Match, config: &Config) -> Result<NaiveDate, TimeError> { + let year = year(date, &config.now); + let month = n_month(date); + let day = n_day(date); + match NaiveDate::from_ymd_opt(year, month, day) { + None => Err(TimeError::ImpossibleDate(format!( + "cannot construct date with year {}, month {}, and day {}", + year, month, day + ))), + Some(d) => Ok(d), + } +} + fn handle_specific_day( m: &Match, config: &Config, @@ -764,16 +779,9 @@ fn handle_specific_day( } if let Some(date) = m.name("date_with_year") { if let Some(date) = date.name("n_date") { - let year = year(date, &now); - let month = n_month(date); - let day = n_day(date); - let d_opt = NaiveDate::from_ymd_opt(year, month, day); - return match d_opt { - None => Err(TimeError::ImpossibleDate(format!( - "cannot construct date with year {}, month {}, and day {}", - year, month, day - ))), - Some(d1) => { + return match n_date(date, config) { + Err(s) => Err(s), + Ok(d1) => { let d1 = d1.and_hms(0, 0, 0); Ok(moment_and_time( &Config::new().now(d1).period(Period::Day), @@ -1032,6 +1040,30 @@ fn handle_specific_time( moment: &Match, config: &Config, ) -> Result<(NaiveDateTime, NaiveDateTime), TimeError> { + if let Some(moment) = moment.name("precise_time") { + return match n_date(moment, config) { + Err(s) => Err(s), + Ok(d) => { + let (hour, minute, second, _) = time(moment); + let period = if second.is_some() { + Period::Second + } else if minute.is_some() { + Period::Minute + } else { + Period::Hour + }; + let m = d + .and_hms(0, 0, 0) + .with_hour(hour) + .unwrap() + .with_minute(minute.unwrap_or(0)) + .unwrap() + .with_second(second.unwrap_or(0)) + .unwrap(); + Ok(moment_to_period(m, &period, config)) + } + }; + } return if moment.has("first_time") { Ok(moment_to_period(first_moment(), &config.period, config)) } else { diff --git a/tests/tests.rs b/tests/tests.rs index b4c5b5d..7ee0325 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1448,3 +1448,19 @@ fn the_31st() { } } } + +#[test] +fn specific_time() { + let d1 = NaiveDate::from_ymd(1969, 5, 6).and_hms(12, 3, 5); + let d2 = d1 + Duration::seconds(1); + match parse("1969-05-06 12:03:05", None) { + Ok((start, end, _)) => { + assert_eq!(d1, start); + assert_eq!(d2, end); + } + Err(e) => { + println!("{:?}", e); + assert!(false, "didn't match"); + } + } +}
\ No newline at end of file |