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