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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
extern crate chrono;
extern crate regex;

use chrono::{DateTime, FixedOffset, ParseResult};
use regex::Regex;

use std::collections::HashMap;

// TODO: Clean up
// TODO: Optimize
// TODO: Setup Error-chain
pub fn sanitize_rfc822_like_date(s: &str) -> String {
    let weekdays = vec![
        "Mon,",
        "Tue,",
        "Wed,",
        "Thu,",
        "Fri,",
        "Sat,",
        "Sun,",
        "Monday,",
        "Tuesday,",
        "Wednesday,",
        "Thursday,",
        "Friday,",
        "Saturday,",
        "Sunday,",
    ];

    let mut months = HashMap::new();
    months.insert("January", "Jan");
    months.insert("February", "Feb");
    months.insert("March", "Mar");
    months.insert("April ", "Apr");
    months.insert("May", "May");
    months.insert("June", "Jun");
    months.insert("July", "Jul");
    months.insert("August", "Aug");
    months.insert("September", "Sep");
    months.insert("October", "Oct");
    months.insert("November", "Nov");
    months.insert("December", "Dec");

    let mut foo = String::from(s);

    // Pad HH:MM:SS with exta zero if needed.
    // Check if it matchers a patter of 2:2:2, and skip if it dows
    let re = Regex::new(r"(\d{1,2}):(\d{1,2}):(\d{1,2})").unwrap();
    // hours, minutes, seconds = cap[1], cap[2], cap[3]
    let cap = re.captures(&s).unwrap();
    let mut newtime = Vec::new();

    cap.iter()
        .skip(1)
        .map(|x| if let Some(y) = x {
            // if y.end() - y.start() == 1 {
            if y.as_str().len() == 1 {
                newtime.push(format!("0{}", y.as_str()));
            } else {
                newtime.push(y.as_str().to_string());
            }
        })
        .fold((), |(), _| ());

    let ntime = &newtime.join(":");
    foo = foo.replace(cap.get(0).unwrap().as_str(), ntime);

    // Weekday name is not required for rfc2822
    // for stable, replace for_each with map and add
    // .fold((), |(),_|()) or .collect()
    weekdays
        .iter()
        .map(|x| if foo.starts_with(x) {
            // TODO: handle to lower etc.
            // For sure someone has a weird feed with the day in lowercase
            foo = format!("{}", &foo[x.len()..]);
            foo = foo.trim().to_string();
        })
        .fold((), |(), _| ());

    // Replace long month names with 3 letter Abr.
    months
        .iter()
        .map(|(k, v)| if s.contains(k) {
            foo = foo.replace(k, v);
        })
        .fold((), |(), _| ());

    // See #102, https://github.com/chronotope/chrono/issues/102
    // Handle -0000 as +0000
    if s.ends_with("-0000") {
        foo = format!("{}+0000", &foo[..foo.len() - 5]);
    }

    // println!("{}", foo);
    foo
}

/// World is full of broken code and invalid rfc822/rfc2822 daytimes.
/// This function acts like the normal DateTime::parse_from_rfc2822 would at first.
/// It calls DateTime::parse_from_rfc2822(s), if it succedes It returns the normal result,
/// But if It fails, It will try to sanitize the String s, and fix common ways date generators
/// misshandle rfc822/rfc2822, And it will then try to parse it again as DayTime.
/// BEWARE OF THE PERFORMANCE PENALTIES.
pub fn parse_from_rfc2822_with_fallback(s: &str) -> ParseResult<DateTime<FixedOffset>> {
    let date = DateTime::parse_from_rfc2822(&s);
    match date {
        Ok(_) => date,
        Err(err) => {
            let san = sanitize_rfc822_like_date(s);
            let dt = DateTime::parse_from_rfc2822(&san);
            if let Ok(_) = dt {
                return dt;
            } else {
                return Err(err);
            }
        }
    }
}


#[cfg(test)]
mod tests {
    use super::*;
    use chrono::DateTime;

    #[test]
    fn test_invalid_dates() {
        // left is raw date extracted from rss feeds.
        // right is corresponding valid rfc2822
        let dates = vec![
            ("Thu, 6 July 2017 15:30:00 PDT", "6 Jul 2017 15:30:00 PDT"),
            ("Mon, 10 July 2017 16:00:00 PDT", "10 Jul 2017 16:00:00 PDT"),
            ("Mon, 17 July 2017 17:00:00 PDT", "17 Jul 2017 17:00:00 PDT"),
            ("Mon, 24 July 2017 16:00:00 PDT", "24 Jul 2017 16:00:00 PDT"),
            ("Mon, 31 July 2017 16:00:00 PDT", "31 Jul 2017 16:00:00 PDT"),
            ("Thu, 30 Aug 2017 1:30:00 PDT", "30 Aug 2017 01:30:00 PDT"),
            (
                "Wed, 20 Sep 2017 10:00:00 -0000",
                "20 Sep 2017 10:00:00 +0000",
            ),
            (
                "Wed, 13 Sep 2017 10:00:00 -0000",
                "13 Sep 2017 10:00:00 +0000",
            ),
            (
                "Wed, 09 Aug 2017 10:00:00 -0000",
                "09 Aug 2017 10:00:00 +0000",
            ),
            (
                "Wed, 02 Aug 2017 10:00:00 -0000",
                "02 Aug 2017 10:00:00 +0000",
            ),
            (
                "Wed, 26 Jul 2017 10:00:00 -0000",
                "26 Jul 2017 10:00:00 +0000",
            ),
            (
                "Wed, 19 Jul 2017 10:00:00 -0000",
                "19 Jul 2017 10:00:00 +0000",
            ),
            (
                "Wed, 12 Jul 2017 10:00:00 -0000",
                "12 Jul 2017 10:00:00 +0000",
            ),
            (
                "Wed, 28 Jun 2017 10:00:00 -0000",
                "28 Jun 2017 10:00:00 +0000",
            ),
            (
                "Wed, 21 Jun 2017 10:00:00 -0000",
                "21 Jun 2017 10:00:00 +0000",
            ),
            (
                "Wed, 14 Jun 2017 10:00:00 -0000",
                "14 Jun 2017 10:00:00 +0000",
            ),
            (
                "Wed, 07 Jun 2017 10:00:00 -0000",
                "07 Jun 2017 10:00:00 +0000",
            ),
            (
                "Wed, 31 May 2017 10:00:00 -0000",
                "31 May 2017 10:00:00 +0000",
            ),
            (
                "Wed, 24 May 2017 10:00:00 -0000",
                "24 May 2017 10:00:00 +0000",
            ),
            (
                "Wed, 17 May 2017 10:00:00 -0000",
                "17 May 2017 10:00:00 +0000",
            ),
            (
                "Wed, 10 May 2017 10:00:00 -0000",
                "10 May 2017 10:00:00 +0000",
            ),
            (
                "Wed, 03 May 2017 10:00:00 -0000",
                "03 May 2017 10:00:00 +0000",
            ),
            (
                "Wed, 19 Apr 2017 10:00:00 -0000",
                "19 Apr 2017 10:00:00 +0000",
            ),
            (
                "Wed, 12 Apr 2017 10:00:00 -0000",
                "12 Apr 2017 10:00:00 +0000",
            ),
            (
                "Wed, 05 Apr 2017 10:00:00 -0000",
                "05 Apr 2017 10:00:00 +0000",
            ),
            (
                "Wed, 29 Mar 2017 10:00:00 -0000",
                "29 Mar 2017 10:00:00 +0000",
            ),
            (
                "Wed, 22 Mar 2017 10:00:00 -0000",
                "22 Mar 2017 10:00:00 +0000",
            ),
            (
                "Wed, 15 Mar 2017 10:00:00 -0000",
                "15 Mar 2017 10:00:00 +0000",
            ),
            (
                "Wed, 08 Mar 2017 11:00:00 -0000",
                "08 Mar 2017 11:00:00 +0000",
            ),
            (
                "Wed, 01 Mar 2017 11:00:00 -0000",
                "01 Mar 2017 11:00:00 +0000",
            ),
            (
                "Wed, 22 Feb 2017 11:00:00 -0000",
                "22 Feb 2017 11:00:00 +0000",
            ),
            (
                "Wed, 15 Feb 2017 11:00:00 -0000",
                "15 Feb 2017 11:00:00 +0000",
            ),
            (
                "Wed, 08 Feb 2017 11:00:00 -0000",
                "08 Feb 2017 11:00:00 +0000",
            ),
            (
                "Wed, 01 Feb 2017 11:00:00 -0000",
                "01 Feb 2017 11:00:00 +0000",
            ),
            (
                "Wed, 25 Jan 2017 11:00:00 -0000",
                "25 Jan 2017 11:00:00 +0000",
            ),
            (
                "Fri, 13 Jan 2017 18:38:00 -0000",
                "13 Jan 2017 18:38:00 +0000",
            ),
            (
                "Wed, 20 Sep 2017 03:30:00 -0000",
                "20 Sep 2017 03:30:00 +0000",
            ),
            (
                "Wed, 13 Sep 2017 03:15:00 -0000",
                "13 Sep 2017 03:15:00 +0000",
            ),
            (
                "Wed, 06 Sep 2017 03:15:00 -0000",
                "06 Sep 2017 03:15:00 +0000",
            ),
            (
                "Wed, 30 Aug 2017 03:15:00 -0000",
                "30 Aug 2017 03:15:00 +0000",
            ),
            (
                "Wed, 23 Aug 2017 03:15:00 -0000",
                "23 Aug 2017 03:15:00 +0000",
            ),
            (
                "Wed, 16 Aug 2017 03:15:00 -0000",
                "16 Aug 2017 03:15:00 +0000",
            ),
            (
                "Wed, 09 Aug 2017 03:15:00 -0000",
                "09 Aug 2017 03:15:00 +0000",
            ),
            (
                "Wed, 02 Aug 2017 03:00:00 -0000",
                "02 Aug 2017 03:00:00 +0000",
            ),
            (
                "Tue, 11 Jul 2017 17:14:45 -0000",
                "11 Jul 2017 17:14:45 +0000",
            ),
            (
                "Thu, 03 August 2017 06:00:00 -0400",
                "03 Aug 2017 06:00:00 -0400",
            ),
            (
                "Thu, 27 July 2017 06:00:00 -0400",
                "27 Jul 2017 06:00:00 -0400",
            ),
            (
                "Thu, 20 July 2017 06:00:00 -0400",
                "20 Jul 2017 06:00:00 -0400",
            ),
            (
                "Thu, 13 July 2017 06:00:00 -0400",
                "13 Jul 2017 06:00:00 -0400",
            ),
            (
                "Thu, 06 July 2017 06:00:00 -0400",
                "06 Jul 2017 06:00:00 -0400",
            ),
            (
                "Thu, 28 June 2017 06:00:00 -0400",
                "28 Jun 2017 06:00:00 -0400",
            ),
            (
                "Thu, 17 Jul 2013 06:00:03 -0400",
                "17 Jul 2013 06:00:03 -0400",
            ),
            (
                "Thu, 02 Apr 2014 06:00:03 -0400",
                "02 Apr 2014 06:00:03 -0400",
            ),
            (
                "Wed, 14 Jan 2016 06:00:03 -0400",
                "14 Jan 2016 06:00:03 -0400",
            ),
            (
                "Thu, 22 June 2017 06:00:00 -0400",
                "22 Jun 2017 06:00:00 -0400",
            ),
            (
                "Thu, 15 June 2017 06:00:00 -0400",
                "15 Jun 2017 06:00:00 -0400",
            ),
            (
                "Thu, 7 June 2017 06:00:00 -0400",
                "7 Jun 2017 06:00:00 -0400",
            ),
            (
                "Thu, 1 June 2017 06:00:00 -0400",
                "1 Jun 2017 06:00:00 -0400",
            ),
            (
                "Thu, 23 Dec 2015 06:00:03 -0400",
                "23 Dec 2015 06:00:03 -0400",
            ),
            (
                "Thu, 14 Feb 2014 06:00:03 -0400",
                "14 Feb 2014 06:00:03 -0400",
            ),
            (
                "Thu, 04 Dec 2013 06:00:03 -0400",
                "04 Dec 2013 06:00:03 -0400",
            ),
            (
                "Thu, 20 Dec 2016 06:00:00 -0400",
                "20 Dec 2016 06:00:00 -0400",
            ),
            (
                "Thu, 23 Nov 2016 06:00:00 -0400",
                "23 Nov 2016 06:00:00 -0400",
            ),
            (
                "Thu, 05 Aug 2016 06:00:00 -0400",
                "05 Aug 2016 06:00:00 -0400",
            ),
            (
                "Fri, 09 Jun 2016 12:00:00 -0400",
                "09 Jun 2016 12:00:00 -0400",
            ),
            (
                "Thu, 10 May 2017 06:00:00 -0400",
                "10 May 2017 06:00:00 -0400",
            ),
            (
                "Thu, 22 Feb 2017 06:00:00 -0400",
                "22 Feb 2017 06:00:00 -0400",
            ),
            (
                "Thu, 15 Feb 2017 06:00:00 -0400",
                "15 Feb 2017 06:00:00 -0400",
            ),
        ];

        for (bad, good) in dates {
            // let f = DateTime::parse_from_rfc2822(good).unwrap();
            // println!("\"{}\",", f.to_rfc2822());

            assert_eq!(
                parse_from_rfc2822_with_fallback(bad),
                DateTime::parse_from_rfc2822(good)
            )
        }
    }

}