finish human time parsing

This commit is contained in:
Abraham Toriz 2021-07-05 12:14:09 -05:00
parent 1b34ab89af
commit f0e6a2b47d
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
2 changed files with 8 additions and 2 deletions

View File

@ -207,7 +207,9 @@ mod tests {
} }
fn time_diff(t1: DateTime<Utc>, t2: DateTime<Local>) { fn time_diff(t1: DateTime<Utc>, t2: DateTime<Local>) {
assert!((t1 - Utc.from_utc_datetime(&t2.naive_utc())).num_seconds() < 1, "too different"); let diff = (t1 - dbg!(Utc.from_utc_datetime(&dbg!(t2.naive_utc())))).num_seconds();
assert!(diff < 1, "too different: {} s", diff);
} }
#[test] #[test]
@ -242,5 +244,8 @@ mod tests {
time_diff(parse_time("a m ago").unwrap(), Local::now() - Duration::minutes(1)); time_diff(parse_time("a m ago").unwrap(), Local::now() - Duration::minutes(1));
time_diff(parse_time("an hr ago").unwrap(), Local::now() - Duration::hours(1)); time_diff(parse_time("an hr ago").unwrap(), Local::now() - Duration::hours(1));
time_diff(parse_time("a min ago").unwrap(), Local::now() - Duration::minutes(1)); time_diff(parse_time("a min ago").unwrap(), Local::now() - Duration::minutes(1));
time_diff(parse_time("two hours and fifteen minuts ago").unwrap(), Local::now() - Duration::minutes(2 * 60 + 15));
time_diff(parse_time("two hours and thirty minuts ago").unwrap(), Local::now() - Duration::minutes(2 * 60 + 30));
// time_diff(parse_time("two and a half hours ago").unwrap(), Local::now() - Duration::minutes(150));
} }
} }

View File

@ -3,6 +3,7 @@ use std::collections::HashMap;
use regex::Regex; use regex::Regex;
lazy_static! { lazy_static! {
// https://regex101.com/r/V9zYQu/1/
pub static ref HUMAN_REGEX: Regex = Regex::new(r"(?xi) pub static ref HUMAN_REGEX: Regex = Regex::new(r"(?xi)
(?P<hour> (?P<hour>
(?P<hnum> (?P<hnum>
@ -29,7 +30,7 @@ lazy_static! {
)| )|
(?P<mtextualnum>\d+) (?P<mtextualnum>\d+)
) )
(\s+)?m(in)?(ute)?s? (\s+)?m(in)?(ute?)?s?
)? )?
\s+ago \s+ago
").unwrap(); ").unwrap();