Impl Parse::parse for Time
This commit is contained in:
parent
e25ab854ee
commit
fbb576d51a
1 changed files with 21 additions and 1 deletions
|
@ -27,7 +27,27 @@ impl Into<ChronoNaiveTime> for Time {
|
|||
impl Parse for Time {
|
||||
|
||||
fn parse(s: &str) -> Option<Time> {
|
||||
unimplemented!()
|
||||
use std::str::FromStr;
|
||||
use regex::Regex;
|
||||
use parse::time_parse_regex;
|
||||
|
||||
lazy_static! {
|
||||
static ref R: Regex = Regex::new(time_parse_regex()).unwrap();
|
||||
}
|
||||
|
||||
R.captures(s)
|
||||
.and_then(|capts| {
|
||||
let hour = capts.name("h").and_then(|o| FromStr::from_str(o).ok());
|
||||
let minute = capts.name("m").and_then(|o| FromStr::from_str(o).ok()).unwrap_or(0);
|
||||
let second = capts.name("s").and_then(|o| FromStr::from_str(o).ok()).unwrap_or(0);
|
||||
|
||||
if hour.is_none() {
|
||||
debug!("No hour");
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Time::new(hour.unwrap(), minute, second))
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue