diff options
-rw-r--r-- | CHANGES.md | 4 | ||||
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/lib.rs | 9 | ||||
-rw-r--r-- | tests/tests.rs | 32 |
5 files changed, 34 insertions, 15 deletions
@@ -7,4 +7,6 @@ ## 1.0.1 * removing some documentation ## 1.0.2 -* added `msg` method to `TimeError`
\ No newline at end of file +* added `msg` method to `TimeError` +## 1.0.3 +* fixed "12 pm" bug
\ No newline at end of file @@ -108,7 +108,7 @@ dependencies = [ [[package]] name = "two_timer" -version = "1.0.2" +version = "1.0.3" 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.2" +version = "1.0.3" authors = ["dfhoughton <dfhoughton@gmail.com>"] description="parser for English time expressions" homepage="https://github.com/dfhoughton/two-timer" @@ -972,14 +972,19 @@ fn time(m: &Match) -> (u32, Option<u32>, Option<u32>) { let hour = if let Some(hour_24) = m.name("hour_24") { s_to_n(hour_24.name("h24").unwrap().as_str()) } else if let Some(hour_12) = m.name("hour_12") { - let hour = s_to_n(hour_12.name("h12").unwrap().as_str()); - if let Some(am_pm) = m.name("am_pm") { + let mut hour = s_to_n(hour_12.name("h12").unwrap().as_str()); + hour = if let Some(am_pm) = m.name("am_pm") { match am_pm.as_str().chars().nth(0).expect("empty string") { 'a' | 'A' => hour, _ => hour + 12, } } else { hour + }; + if hour == 24 { + 0 + } else { + hour } } else { unreachable!() diff --git a/tests/tests.rs b/tests/tests.rs index c77eb1a..87d348b 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -17,7 +17,7 @@ fn always() { "from beginning to end", "from the beginning to the end", ] - .iter() + .iter() { let (start, end, _) = parse(phrase, None).unwrap(); assert_eq!(alpha, start); @@ -74,7 +74,7 @@ fn day_5_6_69_at_3_30_pm() { "at 15:30 on 5-6-69", "15:30 on 5-6-69", ] - .iter() + .iter() { let (start, end, _) = parse(phrase, None).unwrap(); assert_eq!(then, start); @@ -91,7 +91,7 @@ fn day_5_6_69_at_3_pm() { "at 15 on 5-6-69", "15 on 5-6-69", ] - .iter() + .iter() { let (start, end, _) = parse(phrase, None).unwrap(); assert_eq!(then, start); @@ -108,7 +108,7 @@ fn day_5_6_69_at_3_30_00_pm() { "at 15:30:00 on 5-6-69", "15:30:00 on 5-6-69", ] - .iter() + .iter() { let (start, end, _) = parse(phrase, None).unwrap(); assert_eq!(then, start); @@ -125,7 +125,7 @@ fn day_5_6_69_at_3_30_01_pm() { "at 15:30:01 on 5-6-69", "15:30:01 on 5-6-69", ] - .iter() + .iter() { let (start, end, _) = parse(phrase, None).unwrap(); assert_eq!(then, start); @@ -142,7 +142,7 @@ fn day_5_6_69_at_3_30_01_am() { "at 3:30:01 on 5-6-69", "3:30:01 on 5-6-69", ] - .iter() + .iter() { let (start, end, _) = parse(phrase, None).unwrap(); assert_eq!(then, start); @@ -229,7 +229,7 @@ fn alphabetic_5_6_69() { "T, May 6, '69", "T, May 6, 69", ] - .iter() + .iter() { let (start, end, _) = parse(phrase, None).unwrap(); assert_eq!(then, start); @@ -314,7 +314,7 @@ fn ymd_5_31_69() { "'69/31/05", "'69.31.05", ] - .iter() + .iter() { let (start, end, _) = parse(phrase, None).unwrap(); assert_eq!(then, start); @@ -667,7 +667,7 @@ fn dawn_of_time() { "the big bang", "the birth of the universe", ] - .iter() + .iter() { let (start, end, _) = parse(phrase, None).unwrap(); assert_eq!(then, start); @@ -698,7 +698,7 @@ fn the_crack_of_doom() { "ever after", "the last syllable of recorded time", ] - .iter() + .iter() { let (_, end, _) = parse(phrase, None).unwrap(); assert_eq!(then, end); @@ -1042,3 +1042,15 @@ fn next_weekend_on_saturday_when_sunday_starts_week() { assert_eq!(d1, start); assert_eq!(d2, end); } + +#[test] +fn regression_12pm() { + let d1 = NaiveDate::from_ymd(2018, 5, 21).and_hms(0, 0, 0); + let d2 = NaiveDate::from_ymd(2018, 5, 21).and_hms(1, 0, 0); + if let Ok((start, end, _)) = parse("12 pm on May 21, 2018", None) { + assert_eq!(d1, start); + assert_eq!(d2, end); + } else { + assert!(false); + } +} |