1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use chrono::naive::NaiveTime as ChronoNaiveTime;
use parse::Parse;
pub struct Time {
hour: u32,
minute: u32,
second: u32,
}
impl Time {
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
}
}
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> {
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 minute = capts
.name("m")
.and_then(|o| FromStr::from_str(o.as_str()).ok())
.unwrap_or(0);
let second = capts
.name("s")
.and_then(|o| FromStr::from_str(o.as_str()).ok())
.unwrap_or(0);
let hour = match capts
.name("h")
.and_then(|o| FromStr::from_str(o.as_str()).ok())
{
None => {
debug!("No hour");
return None;
},
Some(x) => x,
};
Some(Time::new(hour, minute, second))
})
}
}
#[cfg(test)]
mod test {
use super::Time;
use parse::Parse;
#[test]
fn test_valid() {
let s = "2016-12-12T20:01:02";
let t = Time::parse(s);
assert!(t.is_some());
let t = t.unwrap();
assert_eq!(20, t.hour());
assert_eq!(1, t.minute());
assert_eq!(2, t.second());
}
#[test]
fn test_valid_without_sec() {
let s = "2016-12-12T20:01";
let t = Time::parse(s);
assert!(t.is_some());
let t = t.unwrap();
assert_eq!(20, t.hour());
assert_eq!(1, t.minute());
assert_eq!(0, t.second());
}
#[test]
fn test_valid_without_min() {
let s = "2016-12-12T20";
let t = Time::parse(s);
assert!(t.is_some());
let t = t.unwrap();
assert_eq!(20, t.hour());
assert_eq!(0, t.minute());
assert_eq!(0, t.second());
}
#[test]
fn test_invalid() {
assert!(Time::parse("2015-12-12T").is_none());
assert!(Time::parse("2015-12-12T200").is_none());
assert!(Time::parse("2015-12-12T20-20").is_none());
assert!(Time::parse("2015-12-12T20:200").is_none());
assert!(Time::parse("2015-12-12T20:20:200").is_none());
assert!(Time::parse("2015-12-12T20:20:").is_none());
assert!(Time::parse("2015-12-12T20:").is_none());
assert!(Time::parse("2015-12-12T2:20:21").is_none());
assert!(Time::parse("2015-12-12T2:2:20").is_none());
assert!(Time::parse("2015-12-12T2:2:2").is_none());
}
}
|