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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use std::ascii::AsciiExt;
use std::collections::HashMap;
use chrono::{
DateTime,
FixedOffset,
};
use chrono::offset::TimeZone;
use super::rfc5322::Rfc5322Parser;
use super::results::{ParsingError, ParsingResult};
static DAYS_OF_WEEK: [&'static str; 7] = [
"mon", "tue", "wed", "thu",
"fri", "sat", "sun",
];
static MONTHS: [&'static str; 12] = [
"jan", "feb", "mar", "apr",
"may", "jun", "jul", "aug",
"sep", "oct", "nov", "dec"
];
lazy_static!{
static ref TZ_DATA: HashMap<&'static str, i32> = {
let mut map = HashMap::new();
map.insert("Z", 0);
map.insert("UT", 0);
map.insert("GMT", 0);
map.insert("PST", -28800);
map.insert("PDT", -25200);
map.insert("MST", -25200);
map.insert("MDT", -21600);
map.insert("CST", -21600);
map.insert("CDT", -18000);
map.insert("EST", -18000);
map.insert("EDT", -14400);
map
};
}
pub struct Rfc822DateParser<'s> {
parser: Rfc5322Parser<'s>,
}
impl<'s> Rfc822DateParser<'s> {
pub fn new(s: &'s str) -> Rfc822DateParser<'s> {
Rfc822DateParser {
parser: Rfc5322Parser::new(s),
}
}
#[inline]
fn consume_u32(&mut self) -> Option<u32> {
match self.parser.consume_word(false) {
Some(s) => match s.parse() {
Ok(x) => Some(x),
Err(_) => None
},
None => None,
}
}
fn consume_time(&mut self) -> ParsingResult<(u32, u32, u32)> {
let hour = match self.consume_u32() {
Some(x) => x,
None => return Err(ParsingError::new(
"Failed to parse time: Expected hour, a number.".to_string()))
};
try!(self.parser.assert_char(':'));
self.parser.consume_char();
let minute = match self.consume_u32() {
Some(x) => x,
None => return Err(ParsingError::new("Failed to parse time: Expected minute.".to_string()))
};
let second = match self.parser.assert_char(':') {
Ok(_) => {
self.parser.consume_char();
self.consume_u32()
},
Err(_) => None
}.unwrap_or(0);
Ok((hour, minute, second))
}
fn consume_timezone_offset(&mut self) -> ParsingResult<i32> {
match self.parser.consume_word(false) {
Some(s) => {
let mut s_slice = &s[..];
s_slice = if s_slice.starts_with("+") {
&s_slice[1..]
} else {
s_slice
};
match s_slice.parse::<i32>() {
Ok(i) => {
let offset_hours = i / 100;
let offset_mins = i % 100;
Ok(offset_hours * 3600 + offset_mins * 60)
},
Err(_) => {
match TZ_DATA.get(s_slice) {
Some(offset) => Ok(offset.clone()),
None => Err(ParsingError::new(format!("Invalid timezone: {}", s_slice)))
}
}
}
},
None => Err(ParsingError::new("Expected timezone offset.".to_string()))
}
}
pub fn consume_datetime(&mut self) -> ParsingResult<DateTime<FixedOffset>> {
self.parser.push_position();
let day_of_week = self.parser.consume_word(false);
if let Some(day_of_week) = day_of_week {
let lower_dow = day_of_week.to_ascii_lowercase();
if DAYS_OF_WEEK.contains(&&lower_dow[..]) {
self.parser.consume_while(|c| { c == ',' || c.is_whitespace() });
} else {
self.parser.pop_position();
};
} else {
self.parser.pop_position();
}
let day_of_month = match self.consume_u32() {
Some(x) => x,
None => return Err(ParsingError::new("Expected day of month, a number.".to_string()))
};
self.parser.consume_linear_whitespace();
let month = try!(self.consume_month());
self.parser.consume_linear_whitespace();
let year = match self.consume_u32() {
Some(i) => {
match i {
0...49 => { i + 2000 },
50...999 => { i + 1900 },
_ => { i },
}
},
None => return Err(ParsingError::new("Expected year.".to_string())),
};
self.parser.consume_linear_whitespace();
let time = try!(self.consume_time());
self.parser.consume_linear_whitespace();
let tz_offset = try!(self.consume_timezone_offset());
let (hour, minute, second) = time;
Ok(FixedOffset::east(tz_offset).ymd(
year as i32, month, day_of_month
).and_hms(
hour, minute, second
))
}
fn consume_month(&mut self) -> ParsingResult<u32> {
match self.parser.consume_word(false) {
Some(s) => {
let lower_month = s.to_ascii_lowercase();
for (i, month) in MONTHS.iter().enumerate() {
if month == &&lower_month[..] {
return Ok((i + 1) as u32);
};
};
return Err(ParsingError::new(format!("Invalid month: {}", lower_month)));
},
None => return Err(ParsingError::new("Expected month.".to_string()))
};
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{
DateTime,
FixedOffset,
};
use chrono::offset::TimeZone;
#[test]
fn test_time_parse() {
struct TimeParseTest<'s> {
input: &'s str,
result: Option<DateTime<FixedOffset>>,
}
let edt = FixedOffset::east(-14400);
let cet = FixedOffset::east(3600);
let napal = FixedOffset::east(20700);
let utc = FixedOffset::east(0);
let tests = vec![
TimeParseTest {
input: "Mon, 20 Jun 1982 10:01:59 EDT",
result: Some(edt.ymd(1982, 6, 20).and_hms(10, 1, 59))
},
TimeParseTest {
input: "Mon, 20 Jun 82 10:01:59 EDT",
result: Some(edt.ymd(1982, 6, 20).and_hms(10, 1, 59))
},
TimeParseTest {
input: "Mon, 20 Jun 02 10:01:59 EDT",
result: Some(edt.ymd(2002, 6, 20).and_hms(10, 1, 59))
},
TimeParseTest {
input: "Mon, 20 Jun 1982 10:01 EDT",
result: Some(edt.ymd(1982, 6, 20).and_hms(10, 1, 0))
},
TimeParseTest {
input: "Mon, 20 Jun 1982 10:01:59 +0100",
result: Some(cet.ymd(1982, 6, 20).and_hms(10, 1, 59))
},
TimeParseTest {
input: "Mon, 20 Jun 1982 10:01:59 -0400",
result: Some(edt.ymd(1982, 6, 20).and_hms(10, 1, 59))
},
TimeParseTest {
input: "Mon, 20 Jun 1982 10:01:59 +0545",
result: Some(napal.ymd(1982, 6, 20).and_hms(10, 1, 59))
},
TimeParseTest {
input: "09 Jan 2012 21:20:00 +0000",
result: Some(utc.ymd(2012, 1, 9).and_hms(21, 20, 00))
},
];
for test in tests.into_iter() {
let mut parser = Rfc822DateParser::new(test.input);
assert_eq!(parser.consume_datetime().ok(), test.result);
}
}
}