2016-05-28 15:53:20 +00:00
|
|
|
use chrono::naive::time::NaiveTime as ChronoNaiveTime;
|
|
|
|
|
|
|
|
use parse::Parse;
|
|
|
|
|
|
|
|
pub struct Time {
|
|
|
|
hour: u32,
|
|
|
|
minute: u32,
|
|
|
|
second: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Time {
|
|
|
|
|
2016-05-28 18:36:43 +00:00
|
|
|
pub fn new(hour: u32, minute: u32, second: u32) -> Time {
|
|
|
|
Time {
|
|
|
|
hour: hour,
|
|
|
|
minute: minute,
|
|
|
|
second: second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hour(&self) -> u32 {
|
|
|
|
self.hour
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn minute(&self) -> u32 {
|
|
|
|
self.minute
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn second(&self) -> u32 {
|
|
|
|
self.second
|
2016-05-28 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<ChronoNaiveTime> for Time {
|
|
|
|
|
|
|
|
fn into(self) -> ChronoNaiveTime {
|
|
|
|
ChronoNaiveTime::from_hms(self.hour, self.minute, self.second)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for Time {
|
|
|
|
|
|
|
|
fn parse(s: &str) -> Option<Time> {
|
2016-05-28 18:19:09 +00:00
|
|
|
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))
|
|
|
|
})
|
2016-05-28 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|