Skip to main content

chrono/format/
parse.rs

1// This is a part of Chrono.
2// Portions copyright (c) 2015, John Nagle.
3// See README.md and LICENSE.txt for details.
4
5//! Date and time parsing routines.
6
7#![allow(deprecated)]
8
9use core::borrow::Borrow;
10use core::usize;
11
12use Weekday;
13
14use super::scan;
15use super::{Parsed, ParseResult, Item, InternalFixed, InternalInternal};
16use super::{OUT_OF_RANGE, INVALID, TOO_SHORT, TOO_LONG, BAD_FORMAT};
17
18fn set_weekday_with_num_days_from_sunday(p: &mut Parsed, v: i64) -> ParseResult<()> {
19    p.set_weekday(match v {
20        0 => Weekday::Sun, 1 => Weekday::Mon, 2 => Weekday::Tue,
21        3 => Weekday::Wed, 4 => Weekday::Thu, 5 => Weekday::Fri,
22        6 => Weekday::Sat, _ => return Err(OUT_OF_RANGE)
23    })
24}
25
26fn set_weekday_with_number_from_monday(p: &mut Parsed, v: i64) -> ParseResult<()> {
27    p.set_weekday(match v {
28        1 => Weekday::Mon, 2 => Weekday::Tue, 3 => Weekday::Wed,
29        4 => Weekday::Thu, 5 => Weekday::Fri, 6 => Weekday::Sat,
30        7 => Weekday::Sun, _ => return Err(OUT_OF_RANGE)
31    })
32}
33
34fn parse_rfc2822<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a str, ())> {
35    macro_rules! try_consume {
36        ($e:expr) => ({ let (s_, v) = $e?; s = s_; v })
37    }
38
39    // an adapted RFC 2822 syntax from Section 3.3 and 4.3:
40    //
41    // date-time   = [ day-of-week "," ] date 1*S time *S
42    // day-of-week = *S day-name *S
43    // day-name    = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
44    // date        = day month year
45    // day         = *S 1*2DIGIT *S
46    // month       = 1*S month-name 1*S
47    // month-name  = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
48    //               "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
49    // year        = *S 2*DIGIT *S
50    // time        = time-of-day 1*S zone
51    // time-of-day = hour ":" minute [ ":" second ]
52    // hour        = *S 2DIGIT *S
53    // minute      = *S 2DIGIT *S
54    // second      = *S 2DIGIT *S
55    // zone        = ( "+" / "-" ) 4DIGIT /
56    //               "UT" / "GMT" /                  ; same to +0000
57    //               "EST" / "CST" / "MST" / "PST" / ; same to -0500 to -0800
58    //               "EDT" / "CDT" / "MDT" / "PDT" / ; same to -0400 to -0700
59    //               1*(%d65-90 / %d97-122)          ; same to -0000
60    //
61    // some notes:
62    //
63    // - quoted characters can be in any mixture of lower and upper cases.
64    //
65    // - we do not recognize a folding white space (FWS) or comment (CFWS).
66    //   for our purposes, instead, we accept any sequence of Unicode
67    //   white space characters (denoted here to `S`). any actual RFC 2822
68    //   parser is expected to parse FWS and/or CFWS themselves and replace
69    //   it with a single SP (`%x20`); this is legitimate.
70    //
71    // - two-digit year < 50 should be interpreted by adding 2000.
72    //   two-digit year >= 50 or three-digit year should be interpreted
73    //   by adding 1900. note that four-or-more-digit years less than 1000
74    //   are *never* affected by this rule.
75    //
76    // - mismatching day-of-week is always an error, which is consistent to
77    //   Chrono's own rules.
78    //
79    // - zones can range from `-9959` to `+9959`, but `FixedOffset` does not
80    //   support offsets larger than 24 hours. this is not *that* problematic
81    //   since we do not directly go to a `DateTime` so one can recover
82    //   the offset information from `Parsed` anyway.
83
84    s = s.trim_left();
85
86    if let Ok((s_, weekday)) = scan::short_weekday(s) {
87        if !s_.starts_with(',') { return Err(INVALID); }
88        s = &s_[1..];
89        parsed.set_weekday(weekday)?;
90    }
91
92    s = s.trim_left();
93    parsed.set_day(try_consume!(scan::number(s, 1, 2)))?;
94    s = scan::space(s)?; // mandatory
95    parsed.set_month(1 + i64::from(try_consume!(scan::short_month0(s))))?;
96    s = scan::space(s)?; // mandatory
97
98    // distinguish two- and three-digit years from four-digit years
99    let prevlen = s.len();
100    let mut year = try_consume!(scan::number(s, 2, usize::MAX));
101    let yearlen = prevlen - s.len();
102    match (yearlen, year) {
103        (2,  0...49) => { year += 2000; } //   47 -> 2047,   05 -> 2005
104        (2, 50...99) => { year += 1900; } //   79 -> 1979
105        (3,       _) => { year += 1900; } //  112 -> 2012,  009 -> 1909
106        (_,       _) => {}                // 1987 -> 1987, 0654 -> 0654
107    }
108    parsed.set_year(year)?;
109
110    s = scan::space(s)?; // mandatory
111    parsed.set_hour(try_consume!(scan::number(s, 2, 2)))?;
112    s = scan::char(s.trim_left(), b':')?.trim_left(); // *S ":" *S
113    parsed.set_minute(try_consume!(scan::number(s, 2, 2)))?;
114    if let Ok(s_) = scan::char(s.trim_left(), b':') { // [ ":" *S 2DIGIT ]
115        parsed.set_second(try_consume!(scan::number(s_, 2, 2)))?;
116    }
117
118    s = scan::space(s)?; // mandatory
119    if let Some(offset) = try_consume!(scan::timezone_offset_2822(s)) {
120        // only set the offset when it is definitely known (i.e. not `-0000`)
121        parsed.set_offset(i64::from(offset))?;
122    }
123
124    Ok((s, ()))
125}
126
127fn parse_rfc3339<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a str, ())> {
128    macro_rules! try_consume {
129        ($e:expr) => ({ let (s_, v) = $e?; s = s_; v })
130    }
131
132    // an adapted RFC 3339 syntax from Section 5.6:
133    //
134    // date-fullyear  = 4DIGIT
135    // date-month     = 2DIGIT ; 01-12
136    // date-mday      = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on month/year
137    // time-hour      = 2DIGIT ; 00-23
138    // time-minute    = 2DIGIT ; 00-59
139    // time-second    = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second rules
140    // time-secfrac   = "." 1*DIGIT
141    // time-numoffset = ("+" / "-") time-hour ":" time-minute
142    // time-offset    = "Z" / time-numoffset
143    // partial-time   = time-hour ":" time-minute ":" time-second [time-secfrac]
144    // full-date      = date-fullyear "-" date-month "-" date-mday
145    // full-time      = partial-time time-offset
146    // date-time      = full-date "T" full-time
147    //
148    // some notes:
149    //
150    // - quoted characters can be in any mixture of lower and upper cases.
151    //
152    // - it may accept any number of fractional digits for seconds.
153    //   for Chrono, this means that we should skip digits past first 9 digits.
154    //
155    // - unlike RFC 2822, the valid offset ranges from -23:59 to +23:59.
156    //   note that this restriction is unique to RFC 3339 and not ISO 8601.
157    //   since this is not a typical Chrono behavior, we check it earlier.
158
159    parsed.set_year(try_consume!(scan::number(s, 4, 4)))?;
160    s = scan::char(s, b'-')?;
161    parsed.set_month(try_consume!(scan::number(s, 2, 2)))?;
162    s = scan::char(s, b'-')?;
163    parsed.set_day(try_consume!(scan::number(s, 2, 2)))?;
164
165    s = match s.as_bytes().first() {
166        Some(&b't') | Some(&b'T') => &s[1..],
167        Some(_) => return Err(INVALID),
168        None => return Err(TOO_SHORT),
169    };
170
171    parsed.set_hour(try_consume!(scan::number(s, 2, 2)))?;
172    s = scan::char(s, b':')?;
173    parsed.set_minute(try_consume!(scan::number(s, 2, 2)))?;
174    s = scan::char(s, b':')?;
175    parsed.set_second(try_consume!(scan::number(s, 2, 2)))?;
176    if s.starts_with('.') {
177        let nanosecond = try_consume!(scan::nanosecond(&s[1..]));
178        parsed.set_nanosecond(nanosecond)?;
179    }
180
181    let offset = try_consume!(scan::timezone_offset_zulu(s, |s| scan::char(s, b':')));
182    if offset <= -86_400 || offset >= 86_400 { return Err(OUT_OF_RANGE); }
183    parsed.set_offset(i64::from(offset))?;
184
185    Ok((s, ()))
186}
187
188/// Tries to parse given string into `parsed` with given formatting items.
189/// Returns `Ok` when the entire string has been parsed (otherwise `parsed` should not be used).
190/// There should be no trailing string after parsing;
191/// use a stray [`Item::Space`](./enum.Item.html#variant.Space) to trim whitespaces.
192///
193/// This particular date and time parser is:
194///
195/// - Greedy. It will consume the longest possible prefix.
196///   For example, `April` is always consumed entirely when the long month name is requested;
197///   it equally accepts `Apr`, but prefers the longer prefix in this case.
198///
199/// - Padding-agnostic (for numeric items).
200///   The [`Pad`](./enum.Pad.html) field is completely ignored,
201///   so one can prepend any number of whitespace then any number of zeroes before numbers.
202///
203/// - (Still) obeying the intrinsic parsing width. This allows, for example, parsing `HHMMSS`.
204pub fn parse<'a, I, B>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<()>
205        where I: Iterator<Item=B>, B: Borrow<Item<'a>> {
206    macro_rules! try_consume {
207        ($e:expr) => ({ let (s_, v) = $e?; s = s_; v })
208    }
209
210    for item in items {
211        match item.borrow() {
212            &Item::Literal(prefix) => {
213                if s.len() < prefix.len() { return Err(TOO_SHORT); }
214                if !s.starts_with(prefix) { return Err(INVALID); }
215                s = &s[prefix.len()..];
216            }
217
218            #[cfg(any(feature = "alloc", feature = "std", test))]
219            &Item::OwnedLiteral(ref prefix) => {
220                if s.len() < prefix.len() { return Err(TOO_SHORT); }
221                if !s.starts_with(&prefix[..]) { return Err(INVALID); }
222                s = &s[prefix.len()..];
223            }
224
225            &Item::Space(_) => {
226                s = s.trim_left();
227            }
228
229            #[cfg(any(feature = "alloc", feature = "std", test))]
230            &Item::OwnedSpace(_) => {
231                s = s.trim_left();
232            }
233
234            &Item::Numeric(ref spec, ref _pad) => {
235                use super::Numeric::*;
236                type Setter = fn(&mut Parsed, i64) -> ParseResult<()>;
237
238                let (width, signed, set): (usize, bool, Setter) = match spec {
239                    &Year           => (4, true, Parsed::set_year),
240                    &YearDiv100     => (2, false, Parsed::set_year_div_100),
241                    &YearMod100     => (2, false, Parsed::set_year_mod_100),
242                    &IsoYear        => (4, true, Parsed::set_isoyear),
243                    &IsoYearDiv100  => (2, false, Parsed::set_isoyear_div_100),
244                    &IsoYearMod100  => (2, false, Parsed::set_isoyear_mod_100),
245                    &Month          => (2, false, Parsed::set_month),
246                    &Day            => (2, false, Parsed::set_day),
247                    &WeekFromSun    => (2, false, Parsed::set_week_from_sun),
248                    &WeekFromMon    => (2, false, Parsed::set_week_from_mon),
249                    &IsoWeek        => (2, false, Parsed::set_isoweek),
250                    &NumDaysFromSun => (1, false, set_weekday_with_num_days_from_sunday),
251                    &WeekdayFromMon => (1, false, set_weekday_with_number_from_monday),
252                    &Ordinal        => (3, false, Parsed::set_ordinal),
253                    &Hour           => (2, false, Parsed::set_hour),
254                    &Hour12         => (2, false, Parsed::set_hour12),
255                    &Minute         => (2, false, Parsed::set_minute),
256                    &Second         => (2, false, Parsed::set_second),
257                    &Nanosecond     => (9, false, Parsed::set_nanosecond),
258                    &Timestamp      => (usize::MAX, false, Parsed::set_timestamp),
259
260                    // for the future expansion
261                    &Internal(ref int) => match int._dummy {},
262                };
263
264                s = s.trim_left();
265                let v = if signed {
266                    if s.starts_with('-') {
267                        let v = try_consume!(scan::number(&s[1..], 1, usize::MAX));
268                        0i64.checked_sub(v).ok_or(OUT_OF_RANGE)?
269                    } else if s.starts_with('+') {
270                        try_consume!(scan::number(&s[1..], 1, usize::MAX))
271                    } else {
272                        // if there is no explicit sign, we respect the original `width`
273                        try_consume!(scan::number(s, 1, width))
274                    }
275                } else {
276                    try_consume!(scan::number(s, 1, width))
277                };
278                set(parsed, v)?;
279            }
280
281            &Item::Fixed(ref spec) => {
282                use super::Fixed::*;
283
284                match spec {
285                    &ShortMonthName => {
286                        let month0 = try_consume!(scan::short_month0(s));
287                        parsed.set_month(i64::from(month0) + 1)?;
288                    }
289
290                    &LongMonthName => {
291                        let month0 = try_consume!(scan::short_or_long_month0(s));
292                        parsed.set_month(i64::from(month0) + 1)?;
293                    }
294
295                    &ShortWeekdayName => {
296                        let weekday = try_consume!(scan::short_weekday(s));
297                        parsed.set_weekday(weekday)?;
298                    }
299
300                    &LongWeekdayName => {
301                        let weekday = try_consume!(scan::short_or_long_weekday(s));
302                        parsed.set_weekday(weekday)?;
303                    }
304
305                    &LowerAmPm | &UpperAmPm => {
306                        if s.len() < 2 { return Err(TOO_SHORT); }
307                        let ampm = match (s.as_bytes()[0] | 32, s.as_bytes()[1] | 32) {
308                            (b'a',b'm') => false,
309                            (b'p',b'm') => true,
310                            _ => return Err(INVALID)
311                        };
312                        parsed.set_ampm(ampm)?;
313                        s = &s[2..];
314                    }
315
316                    &Nanosecond | &Nanosecond3 | &Nanosecond6 | &Nanosecond9 => {
317                        if s.starts_with('.') {
318                            let nano = try_consume!(scan::nanosecond(&s[1..]));
319                            parsed.set_nanosecond(nano)?;
320                        }
321                    }
322
323                    &Internal(InternalFixed { val: InternalInternal::Nanosecond3NoDot }) => {
324                        if s.len() < 3 { return Err(TOO_SHORT); }
325                        let nano = try_consume!(scan::nanosecond_fixed(s, 3));
326                        parsed.set_nanosecond(nano)?;
327                    }
328
329                    &Internal(InternalFixed { val: InternalInternal::Nanosecond6NoDot }) => {
330                        if s.len() < 6 { return Err(TOO_SHORT); }
331                        let nano = try_consume!(scan::nanosecond_fixed(s, 6));
332                        parsed.set_nanosecond(nano)?;
333                    }
334
335                    &Internal(InternalFixed { val: InternalInternal::Nanosecond9NoDot }) => {
336                        if s.len() < 9 { return Err(TOO_SHORT); }
337                        let nano = try_consume!(scan::nanosecond_fixed(s, 9));
338                        parsed.set_nanosecond(nano)?;
339                    }
340
341                    &TimezoneName => return Err(BAD_FORMAT),
342
343                    &TimezoneOffsetColon | &TimezoneOffset => {
344                        let offset = try_consume!(scan::timezone_offset(s.trim_left(),
345                                                                        scan::colon_or_space));
346                        parsed.set_offset(i64::from(offset))?;
347                    }
348
349                    &TimezoneOffsetColonZ | &TimezoneOffsetZ => {
350                        let offset = try_consume!(scan::timezone_offset_zulu(s.trim_left(),
351                                                                             scan::colon_or_space));
352                        parsed.set_offset(i64::from(offset))?;
353                    }
354                    &Internal(InternalFixed { val: InternalInternal::TimezoneOffsetPermissive }) => {
355                        let offset = try_consume!(scan::timezone_offset_permissive(
356                            s.trim_left(), scan::colon_or_space));
357                        parsed.set_offset(i64::from(offset))?;
358                    }
359
360                    &RFC2822 => try_consume!(parse_rfc2822(parsed, s)),
361                    &RFC3339 => try_consume!(parse_rfc3339(parsed, s)),
362                }
363            }
364
365            &Item::Error => {
366                return Err(BAD_FORMAT);
367            }
368        }
369    }
370
371    // if there are trailling chars, it is an error
372    if !s.is_empty() {
373        Err(TOO_LONG)
374    } else {
375        Ok(())
376    }
377}
378
379#[cfg(test)]
380#[test]
381fn test_parse() {
382    use super::*;
383    use super::IMPOSSIBLE;
384
385    // workaround for Rust issue #22255
386    fn parse_all(s: &str, items: &[Item]) -> ParseResult<Parsed> {
387        let mut parsed = Parsed::new();
388        parse(&mut parsed, s, items.iter())?;
389        Ok(parsed)
390    }
391
392    macro_rules! check {
393        ($fmt:expr, $items:expr; $err:tt) => (
394            assert_eq!(parse_all($fmt, &$items), Err($err))
395        );
396        ($fmt:expr, $items:expr; $($k:ident: $v:expr),*) => (#[allow(unused_mut)] {
397            let mut expected = Parsed::new();
398            $(expected.$k = Some($v);)*
399            assert_eq!(parse_all($fmt, &$items), Ok(expected))
400        });
401    }
402
403    // empty string
404    check!("",  []; );
405    check!(" ", []; TOO_LONG);
406    check!("a", []; TOO_LONG);
407
408    // whitespaces
409    check!("",          [sp!("")]; );
410    check!(" ",         [sp!("")]; );
411    check!("\t",        [sp!("")]; );
412    check!(" \n\r  \n", [sp!("")]; );
413    check!("a",         [sp!("")]; TOO_LONG);
414
415    // literal
416    check!("",    [lit!("a")]; TOO_SHORT);
417    check!(" ",   [lit!("a")]; INVALID);
418    check!("a",   [lit!("a")]; );
419    check!("aa",  [lit!("a")]; TOO_LONG);
420    check!("A",   [lit!("a")]; INVALID);
421    check!("xy",  [lit!("xy")]; );
422    check!("xy",  [lit!("x"), lit!("y")]; );
423    check!("x y", [lit!("x"), lit!("y")]; INVALID);
424    check!("xy",  [lit!("x"), sp!(""), lit!("y")]; );
425    check!("x y", [lit!("x"), sp!(""), lit!("y")]; );
426
427    // numeric
428    check!("1987",        [num!(Year)]; year: 1987);
429    check!("1987 ",       [num!(Year)]; TOO_LONG);
430    check!("0x12",        [num!(Year)]; TOO_LONG); // `0` is parsed
431    check!("x123",        [num!(Year)]; INVALID);
432    check!("2015",        [num!(Year)]; year: 2015);
433    check!("0000",        [num!(Year)]; year:    0);
434    check!("9999",        [num!(Year)]; year: 9999);
435    check!(" \t987",      [num!(Year)]; year:  987);
436    check!("5",           [num!(Year)]; year:    5);
437    check!("5\0",         [num!(Year)]; TOO_LONG);
438    check!("\05",         [num!(Year)]; INVALID);
439    check!("",            [num!(Year)]; TOO_SHORT);
440    check!("12345",       [num!(Year), lit!("5")]; year: 1234);
441    check!("12345",       [nums!(Year), lit!("5")]; year: 1234);
442    check!("12345",       [num0!(Year), lit!("5")]; year: 1234);
443    check!("12341234",    [num!(Year), num!(Year)]; year: 1234);
444    check!("1234 1234",   [num!(Year), num!(Year)]; year: 1234);
445    check!("1234 1235",   [num!(Year), num!(Year)]; IMPOSSIBLE);
446    check!("1234 1234",   [num!(Year), lit!("x"), num!(Year)]; INVALID);
447    check!("1234x1234",   [num!(Year), lit!("x"), num!(Year)]; year: 1234);
448    check!("1234xx1234",  [num!(Year), lit!("x"), num!(Year)]; INVALID);
449    check!("1234 x 1234", [num!(Year), lit!("x"), num!(Year)]; INVALID);
450
451    // signed numeric
452    check!("-42",         [num!(Year)]; year: -42);
453    check!("+42",         [num!(Year)]; year: 42);
454    check!("-0042",       [num!(Year)]; year: -42);
455    check!("+0042",       [num!(Year)]; year: 42);
456    check!("-42195",      [num!(Year)]; year: -42195);
457    check!("+42195",      [num!(Year)]; year: 42195);
458    check!("  -42195",    [num!(Year)]; year: -42195);
459    check!("  +42195",    [num!(Year)]; year: 42195);
460    check!("  -   42",    [num!(Year)]; INVALID);
461    check!("  +   42",    [num!(Year)]; INVALID);
462    check!("-",           [num!(Year)]; TOO_SHORT);
463    check!("+",           [num!(Year)]; TOO_SHORT);
464
465    // unsigned numeric
466    check!("345",   [num!(Ordinal)]; ordinal: 345);
467    check!("+345",  [num!(Ordinal)]; INVALID);
468    check!("-345",  [num!(Ordinal)]; INVALID);
469    check!(" 345",  [num!(Ordinal)]; ordinal: 345);
470    check!(" +345", [num!(Ordinal)]; INVALID);
471    check!(" -345", [num!(Ordinal)]; INVALID);
472
473    // various numeric fields
474    check!("1234 5678",
475           [num!(Year), num!(IsoYear)];
476           year: 1234, isoyear: 5678);
477    check!("12 34 56 78",
478           [num!(YearDiv100), num!(YearMod100), num!(IsoYearDiv100), num!(IsoYearMod100)];
479           year_div_100: 12, year_mod_100: 34, isoyear_div_100: 56, isoyear_mod_100: 78);
480    check!("1 2 3 4 5 6",
481           [num!(Month), num!(Day), num!(WeekFromSun), num!(WeekFromMon), num!(IsoWeek),
482            num!(NumDaysFromSun)];
483           month: 1, day: 2, week_from_sun: 3, week_from_mon: 4, isoweek: 5, weekday: Weekday::Sat);
484    check!("7 89 01",
485           [num!(WeekdayFromMon), num!(Ordinal), num!(Hour12)];
486           weekday: Weekday::Sun, ordinal: 89, hour_mod_12: 1);
487    check!("23 45 6 78901234 567890123",
488           [num!(Hour), num!(Minute), num!(Second), num!(Nanosecond), num!(Timestamp)];
489           hour_div_12: 1, hour_mod_12: 11, minute: 45, second: 6, nanosecond: 78_901_234,
490           timestamp: 567_890_123);
491
492    // fixed: month and weekday names
493    check!("apr",       [fix!(ShortMonthName)]; month: 4);
494    check!("Apr",       [fix!(ShortMonthName)]; month: 4);
495    check!("APR",       [fix!(ShortMonthName)]; month: 4);
496    check!("ApR",       [fix!(ShortMonthName)]; month: 4);
497    check!("April",     [fix!(ShortMonthName)]; TOO_LONG); // `Apr` is parsed
498    check!("A",         [fix!(ShortMonthName)]; TOO_SHORT);
499    check!("Sol",       [fix!(ShortMonthName)]; INVALID);
500    check!("Apr",       [fix!(LongMonthName)]; month: 4);
501    check!("Apri",      [fix!(LongMonthName)]; TOO_LONG); // `Apr` is parsed
502    check!("April",     [fix!(LongMonthName)]; month: 4);
503    check!("Aprill",    [fix!(LongMonthName)]; TOO_LONG);
504    check!("Aprill",    [fix!(LongMonthName), lit!("l")]; month: 4);
505    check!("Aprl",      [fix!(LongMonthName), lit!("l")]; month: 4);
506    check!("April",     [fix!(LongMonthName), lit!("il")]; TOO_SHORT); // do not backtrack
507    check!("thu",       [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
508    check!("Thu",       [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
509    check!("THU",       [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
510    check!("tHu",       [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
511    check!("Thursday",  [fix!(ShortWeekdayName)]; TOO_LONG); // `Thu` is parsed
512    check!("T",         [fix!(ShortWeekdayName)]; TOO_SHORT);
513    check!("The",       [fix!(ShortWeekdayName)]; INVALID);
514    check!("Nop",       [fix!(ShortWeekdayName)]; INVALID);
515    check!("Thu",       [fix!(LongWeekdayName)]; weekday: Weekday::Thu);
516    check!("Thur",      [fix!(LongWeekdayName)]; TOO_LONG); // `Thu` is parsed
517    check!("Thurs",     [fix!(LongWeekdayName)]; TOO_LONG); // ditto
518    check!("Thursday",  [fix!(LongWeekdayName)]; weekday: Weekday::Thu);
519    check!("Thursdays", [fix!(LongWeekdayName)]; TOO_LONG);
520    check!("Thursdays", [fix!(LongWeekdayName), lit!("s")]; weekday: Weekday::Thu);
521    check!("Thus",      [fix!(LongWeekdayName), lit!("s")]; weekday: Weekday::Thu);
522    check!("Thursday",  [fix!(LongWeekdayName), lit!("rsday")]; TOO_SHORT); // do not backtrack
523
524    // fixed: am/pm
525    check!("am",  [fix!(LowerAmPm)]; hour_div_12: 0);
526    check!("pm",  [fix!(LowerAmPm)]; hour_div_12: 1);
527    check!("AM",  [fix!(LowerAmPm)]; hour_div_12: 0);
528    check!("PM",  [fix!(LowerAmPm)]; hour_div_12: 1);
529    check!("am",  [fix!(UpperAmPm)]; hour_div_12: 0);
530    check!("pm",  [fix!(UpperAmPm)]; hour_div_12: 1);
531    check!("AM",  [fix!(UpperAmPm)]; hour_div_12: 0);
532    check!("PM",  [fix!(UpperAmPm)]; hour_div_12: 1);
533    check!("Am",  [fix!(LowerAmPm)]; hour_div_12: 0);
534    check!(" Am", [fix!(LowerAmPm)]; INVALID);
535    check!("ame", [fix!(LowerAmPm)]; TOO_LONG); // `am` is parsed
536    check!("a",   [fix!(LowerAmPm)]; TOO_SHORT);
537    check!("p",   [fix!(LowerAmPm)]; TOO_SHORT);
538    check!("x",   [fix!(LowerAmPm)]; TOO_SHORT);
539    check!("xx",  [fix!(LowerAmPm)]; INVALID);
540    check!("",    [fix!(LowerAmPm)]; TOO_SHORT);
541
542    // fixed: dot plus nanoseconds
543    check!("",              [fix!(Nanosecond)]; ); // no field set, but not an error
544    check!("4",             [fix!(Nanosecond)]; TOO_LONG); // never consumes `4`
545    check!("4",             [fix!(Nanosecond), num!(Second)]; second: 4);
546    check!(".0",            [fix!(Nanosecond)]; nanosecond: 0);
547    check!(".4",            [fix!(Nanosecond)]; nanosecond: 400_000_000);
548    check!(".42",           [fix!(Nanosecond)]; nanosecond: 420_000_000);
549    check!(".421",          [fix!(Nanosecond)]; nanosecond: 421_000_000);
550    check!(".42195",        [fix!(Nanosecond)]; nanosecond: 421_950_000);
551    check!(".421950803",    [fix!(Nanosecond)]; nanosecond: 421_950_803);
552    check!(".421950803547", [fix!(Nanosecond)]; nanosecond: 421_950_803);
553    check!(".000000003547", [fix!(Nanosecond)]; nanosecond: 3);
554    check!(".000000000547", [fix!(Nanosecond)]; nanosecond: 0);
555    check!(".",             [fix!(Nanosecond)]; TOO_SHORT);
556    check!(".4x",           [fix!(Nanosecond)]; TOO_LONG);
557    check!(".  4",          [fix!(Nanosecond)]; INVALID);
558    check!("  .4",          [fix!(Nanosecond)]; TOO_LONG); // no automatic trimming
559
560    // fixed: nanoseconds without the dot
561    check!("",             [internal_fix!(Nanosecond3NoDot)]; TOO_SHORT);
562    check!("0",            [internal_fix!(Nanosecond3NoDot)]; TOO_SHORT);
563    check!("4",            [internal_fix!(Nanosecond3NoDot)]; TOO_SHORT);
564    check!("42",           [internal_fix!(Nanosecond3NoDot)]; TOO_SHORT);
565    check!("421",          [internal_fix!(Nanosecond3NoDot)]; nanosecond: 421_000_000);
566    check!("42143",        [internal_fix!(Nanosecond3NoDot), num!(Second)]; nanosecond: 421_000_000, second: 43);
567    check!("42195",        [internal_fix!(Nanosecond3NoDot)]; TOO_LONG);
568    check!("4x",           [internal_fix!(Nanosecond3NoDot)]; TOO_SHORT);
569    check!("  4",          [internal_fix!(Nanosecond3NoDot)]; INVALID);
570    check!(".421",         [internal_fix!(Nanosecond3NoDot)]; INVALID);
571
572    check!("",             [internal_fix!(Nanosecond6NoDot)]; TOO_SHORT);
573    check!("0",            [internal_fix!(Nanosecond6NoDot)]; TOO_SHORT);
574    check!("42195",        [internal_fix!(Nanosecond6NoDot)]; TOO_SHORT);
575    check!("421950",       [internal_fix!(Nanosecond6NoDot)]; nanosecond: 421_950_000);
576    check!("000003",       [internal_fix!(Nanosecond6NoDot)]; nanosecond: 3000);
577    check!("000000",       [internal_fix!(Nanosecond6NoDot)]; nanosecond: 0);
578    check!("4x",           [internal_fix!(Nanosecond6NoDot)]; TOO_SHORT);
579    check!("     4",       [internal_fix!(Nanosecond6NoDot)]; INVALID);
580    check!(".42100",       [internal_fix!(Nanosecond6NoDot)]; INVALID);
581
582    check!("",             [internal_fix!(Nanosecond9NoDot)]; TOO_SHORT);
583    check!("42195",        [internal_fix!(Nanosecond9NoDot)]; TOO_SHORT);
584    check!("421950803",    [internal_fix!(Nanosecond9NoDot)]; nanosecond: 421_950_803);
585    check!("000000003",    [internal_fix!(Nanosecond9NoDot)]; nanosecond: 3);
586    check!("42195080354",  [internal_fix!(Nanosecond9NoDot), num!(Second)]; nanosecond: 421_950_803, second: 54); // don't skip digits that come after the 9
587    check!("421950803547", [internal_fix!(Nanosecond9NoDot)]; TOO_LONG);
588    check!("000000000",    [internal_fix!(Nanosecond9NoDot)]; nanosecond: 0);
589    check!("00000000x",    [internal_fix!(Nanosecond9NoDot)]; INVALID);
590    check!("        4",    [internal_fix!(Nanosecond9NoDot)]; INVALID);
591    check!(".42100000",    [internal_fix!(Nanosecond9NoDot)]; INVALID);
592
593    // fixed: timezone offsets
594    check!("+00:00",    [fix!(TimezoneOffset)]; offset: 0);
595    check!("-00:00",    [fix!(TimezoneOffset)]; offset: 0);
596    check!("+00:01",    [fix!(TimezoneOffset)]; offset: 60);
597    check!("-00:01",    [fix!(TimezoneOffset)]; offset: -60);
598    check!("+00:30",    [fix!(TimezoneOffset)]; offset: 30 * 60);
599    check!("-00:30",    [fix!(TimezoneOffset)]; offset: -30 * 60);
600    check!("+04:56",    [fix!(TimezoneOffset)]; offset: 296 * 60);
601    check!("-04:56",    [fix!(TimezoneOffset)]; offset: -296 * 60);
602    check!("+24:00",    [fix!(TimezoneOffset)]; offset: 24 * 60 * 60);
603    check!("-24:00",    [fix!(TimezoneOffset)]; offset: -24 * 60 * 60);
604    check!("+99:59",    [fix!(TimezoneOffset)]; offset: (100 * 60 - 1) * 60);
605    check!("-99:59",    [fix!(TimezoneOffset)]; offset: -(100 * 60 - 1) * 60);
606    check!("+00:59",    [fix!(TimezoneOffset)]; offset: 59 * 60);
607    check!("+00:60",    [fix!(TimezoneOffset)]; OUT_OF_RANGE);
608    check!("+00:99",    [fix!(TimezoneOffset)]; OUT_OF_RANGE);
609    check!("#12:34",    [fix!(TimezoneOffset)]; INVALID);
610    check!("12:34",     [fix!(TimezoneOffset)]; INVALID);
611    check!("+12:34 ",   [fix!(TimezoneOffset)]; TOO_LONG);
612    check!(" +12:34",   [fix!(TimezoneOffset)]; offset: 754 * 60);
613    check!("\t -12:34", [fix!(TimezoneOffset)]; offset: -754 * 60);
614    check!("",          [fix!(TimezoneOffset)]; TOO_SHORT);
615    check!("+",         [fix!(TimezoneOffset)]; TOO_SHORT);
616    check!("+1",        [fix!(TimezoneOffset)]; TOO_SHORT);
617    check!("+12",       [fix!(TimezoneOffset)]; TOO_SHORT);
618    check!("+123",      [fix!(TimezoneOffset)]; TOO_SHORT);
619    check!("+1234",     [fix!(TimezoneOffset)]; offset: 754 * 60);
620    check!("+12345",    [fix!(TimezoneOffset)]; TOO_LONG);
621    check!("+12345",    [fix!(TimezoneOffset), num!(Day)]; offset: 754 * 60, day: 5);
622    check!("Z",         [fix!(TimezoneOffset)]; INVALID);
623    check!("z",         [fix!(TimezoneOffset)]; INVALID);
624    check!("Z",         [fix!(TimezoneOffsetZ)]; offset: 0);
625    check!("z",         [fix!(TimezoneOffsetZ)]; offset: 0);
626    check!("Y",         [fix!(TimezoneOffsetZ)]; INVALID);
627    check!("Zulu",      [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 0);
628    check!("zulu",      [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 0);
629    check!("+1234ulu",  [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 754 * 60);
630    check!("+12:34ulu", [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 754 * 60);
631    check!("Z",         [internal_fix!(TimezoneOffsetPermissive)]; offset: 0);
632    check!("z",         [internal_fix!(TimezoneOffsetPermissive)]; offset: 0);
633    check!("+12:00",    [internal_fix!(TimezoneOffsetPermissive)]; offset: 12 * 60 * 60);
634    check!("+12",       [internal_fix!(TimezoneOffsetPermissive)]; offset: 12 * 60 * 60);
635    check!("???",       [fix!(TimezoneName)]; BAD_FORMAT); // not allowed
636
637    // some practical examples
638    check!("2015-02-04T14:37:05+09:00",
639           [num!(Year), lit!("-"), num!(Month), lit!("-"), num!(Day), lit!("T"),
640            num!(Hour), lit!(":"), num!(Minute), lit!(":"), num!(Second), fix!(TimezoneOffset)];
641           year: 2015, month: 2, day: 4, hour_div_12: 1, hour_mod_12: 2,
642           minute: 37, second: 5, offset: 32400);
643    check!("20150204143705567",
644            [num!(Year), num!(Month), num!(Day),
645            num!(Hour), num!(Minute), num!(Second), internal_fix!(Nanosecond3NoDot)];
646            year: 2015, month: 2, day: 4, hour_div_12: 1, hour_mod_12: 2,
647            minute: 37, second: 5, nanosecond: 567000000);
648    check!("Mon, 10 Jun 2013 09:32:37 GMT",
649           [fix!(ShortWeekdayName), lit!(","), sp!(" "), num!(Day), sp!(" "),
650            fix!(ShortMonthName), sp!(" "), num!(Year), sp!(" "), num!(Hour), lit!(":"),
651            num!(Minute), lit!(":"), num!(Second), sp!(" "), lit!("GMT")];
652           year: 2013, month: 6, day: 10, weekday: Weekday::Mon,
653           hour_div_12: 0, hour_mod_12: 9, minute: 32, second: 37);
654    check!("20060102150405",
655           [num!(Year), num!(Month), num!(Day), num!(Hour), num!(Minute), num!(Second)];
656           year: 2006, month: 1, day: 2, hour_div_12: 1, hour_mod_12: 3, minute: 4, second: 5);
657    check!("3:14PM",
658           [num!(Hour12), lit!(":"), num!(Minute), fix!(LowerAmPm)];
659           hour_div_12: 1, hour_mod_12: 3, minute: 14);
660    check!("12345678901234.56789",
661           [num!(Timestamp), lit!("."), num!(Nanosecond)];
662           nanosecond: 56_789, timestamp: 12_345_678_901_234);
663    check!("12345678901234.56789",
664           [num!(Timestamp), fix!(Nanosecond)];
665           nanosecond: 567_890_000, timestamp: 12_345_678_901_234);
666}
667
668#[cfg(test)]
669#[test]
670fn test_rfc2822() {
671    use DateTime;
672    use offset::FixedOffset;
673    use super::*;
674    use super::NOT_ENOUGH;
675
676    // Test data - (input, Ok(expected result after parse and format) or Err(error code))
677    let testdates = [
678        ("Tue, 20 Jan 2015 17:35:20 -0800", Ok("Tue, 20 Jan 2015 17:35:20 -0800")), // normal case
679        ("Fri,  2 Jan 2015 17:35:20 -0800", Ok("Fri, 02 Jan 2015 17:35:20 -0800")), // folding whitespace
680        ("Fri, 02 Jan 2015 17:35:20 -0800", Ok("Fri, 02 Jan 2015 17:35:20 -0800")), // leading zero
681        ("20 Jan 2015 17:35:20 -0800", Ok("Tue, 20 Jan 2015 17:35:20 -0800")),  // no day of week
682        ("20 JAN 2015 17:35:20 -0800", Ok("Tue, 20 Jan 2015 17:35:20 -0800")),  // upper case month
683        ("Tue, 20 Jan 2015 17:35 -0800", Ok("Tue, 20 Jan 2015 17:35:00 -0800")), // no second
684        ("11 Sep 2001 09:45:00 EST", Ok("Tue, 11 Sep 2001 09:45:00 -0500")),
685        ("30 Feb 2015 17:35:20 -0800", Err(OUT_OF_RANGE)),      // bad day of month
686        ("Tue, 20 Jan 2015", Err(TOO_SHORT)),                   // omitted fields
687        ("Tue, 20 Avr 2015 17:35:20 -0800", Err(INVALID)),      // bad month name
688        ("Tue, 20 Jan 2015 25:35:20 -0800", Err(OUT_OF_RANGE)), // bad hour
689        ("Tue, 20 Jan 2015 7:35:20 -0800", Err(INVALID)),       // bad # of digits in hour
690        ("Tue, 20 Jan 2015 17:65:20 -0800", Err(OUT_OF_RANGE)), // bad minute
691        ("Tue, 20 Jan 2015 17:35:90 -0800", Err(OUT_OF_RANGE)), // bad second
692        ("Tue, 20 Jan 2015 17:35:20 -0890", Err(OUT_OF_RANGE)), // bad offset
693        ("6 Jun 1944 04:00:00Z", Err(INVALID)),                 // bad offset (zulu not allowed)
694        ("Tue, 20 Jan 2015 17:35:20 HAS", Err(NOT_ENOUGH))      // bad named time zone
695    ];
696
697    fn rfc2822_to_datetime(date: &str) -> ParseResult<DateTime<FixedOffset>> {
698        let mut parsed = Parsed::new();
699        parse(&mut parsed, date, [Item::Fixed(Fixed::RFC2822)].iter())?;
700        parsed.to_datetime()
701    }
702
703    fn fmt_rfc2822_datetime(dt: DateTime<FixedOffset>) -> String {
704        dt.format_with_items([Item::Fixed(Fixed::RFC2822)].iter()).to_string()
705    }
706
707    // Test against test data above
708    for &(date, checkdate) in testdates.iter() {
709        let d = rfc2822_to_datetime(date);          // parse a date
710        let dt = match d {                          // did we get a value?
711            Ok(dt) => Ok(fmt_rfc2822_datetime(dt)), // yes, go on
712            Err(e) => Err(e),                       // otherwise keep an error for the comparison
713        };
714        if dt != checkdate.map(|s| s.to_string()) { // check for expected result
715            panic!("Date conversion failed for {}\nReceived: {:?}\nExpected: {:?}",
716                   date, dt, checkdate);
717        }
718    };
719}
720
721
722
723#[cfg(test)]
724#[test]
725fn parse_rfc850() {
726    use ::{Utc, TimeZone};
727
728    static RFC850_FMT: &'static str =  "%A, %d-%b-%y %T GMT";
729
730    let dt_str = "Sunday, 06-Nov-94 08:49:37 GMT";
731    let dt = Utc.ymd(1994, 11, 6).and_hms(8, 49, 37);
732
733    // Check that the format is what we expect
734    assert_eq!(dt.format(RFC850_FMT).to_string(), dt_str);
735
736    // Check that it parses correctly
737    assert_eq!(Ok(dt), Utc.datetime_from_str("Sunday, 06-Nov-94 08:49:37 GMT", RFC850_FMT));
738
739    // Check that the rest of the weekdays parse correctly (this test originally failed because
740    // Sunday parsed incorrectly).
741    let testdates = [
742        (Utc.ymd(1994, 11, 7).and_hms(8, 49, 37),  "Monday, 07-Nov-94 08:49:37 GMT"),
743        (Utc.ymd(1994, 11, 8).and_hms(8, 49, 37),  "Tuesday, 08-Nov-94 08:49:37 GMT"),
744        (Utc.ymd(1994, 11, 9).and_hms(8, 49, 37),  "Wednesday, 09-Nov-94 08:49:37 GMT"),
745        (Utc.ymd(1994, 11, 10).and_hms(8, 49, 37), "Thursday, 10-Nov-94 08:49:37 GMT"),
746        (Utc.ymd(1994, 11, 11).and_hms(8, 49, 37), "Friday, 11-Nov-94 08:49:37 GMT"),
747        (Utc.ymd(1994, 11, 12).and_hms(8, 49, 37), "Saturday, 12-Nov-94 08:49:37 GMT"),
748    ];
749
750    for val in &testdates {
751        assert_eq!(Ok(val.0), Utc.datetime_from_str(val.1, RFC850_FMT));
752    }
753}
754
755#[cfg(test)]
756#[test]
757fn test_rfc3339() {
758    use DateTime;
759    use offset::FixedOffset;
760    use super::*;
761
762    // Test data - (input, Ok(expected result after parse and format) or Err(error code))
763    let testdates = [
764        ("2015-01-20T17:35:20-08:00", Ok("2015-01-20T17:35:20-08:00")), // normal case
765        ("1944-06-06T04:04:00Z", Ok("1944-06-06T04:04:00+00:00")),      // D-day
766        ("2001-09-11T09:45:00-08:00", Ok("2001-09-11T09:45:00-08:00")),
767        ("2015-01-20T17:35:20.001-08:00", Ok("2015-01-20T17:35:20.001-08:00")),
768        ("2015-01-20T17:35:20.000031-08:00", Ok("2015-01-20T17:35:20.000031-08:00")),
769        ("2015-01-20T17:35:20.000000004-08:00", Ok("2015-01-20T17:35:20.000000004-08:00")),
770        ("2015-01-20T17:35:20.000000000452-08:00", Ok("2015-01-20T17:35:20-08:00")), // too small
771        ("2015-02-30T17:35:20-08:00", Err(OUT_OF_RANGE)),               // bad day of month
772        ("2015-01-20T25:35:20-08:00", Err(OUT_OF_RANGE)),               // bad hour
773        ("2015-01-20T17:65:20-08:00", Err(OUT_OF_RANGE)),               // bad minute
774        ("2015-01-20T17:35:90-08:00", Err(OUT_OF_RANGE)),               // bad second
775        ("2015-01-20T17:35:20-24:00", Err(OUT_OF_RANGE)),               // bad offset
776    ];
777
778    fn rfc3339_to_datetime(date: &str) -> ParseResult<DateTime<FixedOffset>> {
779        let mut parsed = Parsed::new();
780        parse(&mut parsed, date, [Item::Fixed(Fixed::RFC3339)].iter())?;
781        parsed.to_datetime()
782    }
783
784    fn fmt_rfc3339_datetime(dt: DateTime<FixedOffset>) -> String {
785        dt.format_with_items([Item::Fixed(Fixed::RFC3339)].iter()).to_string()
786    }
787
788    // Test against test data above
789    for &(date, checkdate) in testdates.iter() {
790        let d = rfc3339_to_datetime(date);          // parse a date
791        let dt = match d {                          // did we get a value?
792            Ok(dt) => Ok(fmt_rfc3339_datetime(dt)), // yes, go on
793            Err(e) => Err(e),                       // otherwise keep an error for the comparison
794        };
795        if dt != checkdate.map(|s| s.to_string()) { // check for expected result
796            panic!("Date conversion failed for {}\nReceived: {:?}\nExpected: {:?}",
797                   date, dt, checkdate);
798        }
799    };
800}
801