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 => {
411 try_consume!(scan::timezone_name_skip(s));
412 }
413
414 &TimezoneOffsetColon | &TimezoneOffset => {
415 let offset = try_consume!(scan::timezone_offset(
416 s.trim_left(),
417 scan::colon_or_space
418 ));
419 parsed.set_offset(i64::from(offset)).map_err(|e| (s, e))?;
420 }
421
422 &TimezoneOffsetColonZ | &TimezoneOffsetZ => {
423 let offset = try_consume!(scan::timezone_offset_zulu(
424 s.trim_left(),
425 scan::colon_or_space
426 ));
427 parsed.set_offset(i64::from(offset)).map_err(|e| (s, e))?;
428 }
429 &Internal(InternalFixed {
430 val: InternalInternal::TimezoneOffsetPermissive,
431 }) => {
432 let offset = try_consume!(scan::timezone_offset_permissive(
433 s.trim_left(),
434 scan::colon_or_space
435 ));
436 parsed.set_offset(i64::from(offset)).map_err(|e| (s, e))?;
437 }
438
439 &RFC2822 => try_consume!(parse_rfc2822(parsed, s)),
440 &RFC3339 => try_consume!(parse_rfc3339(parsed, s)),
441 }
442 }
443
444 Item::Error => {
445 return Err((s, BAD_FORMAT));
446 }
447 }
448 }
449
450 // if there are trailling chars, it is an error
451 if !s.is_empty() {
452 Err((s, TOO_LONG))
453 } else {
454 Ok(s)
455 }
456 }
457
458 impl str::FromStr for DateTime<FixedOffset> {
459 type Err = ParseError;
460
from_str(s: &str) -> ParseResult<DateTime<FixedOffset>>461 fn from_str(s: &str) -> ParseResult<DateTime<FixedOffset>> {
462 const DATE_ITEMS: &'static [Item<'static>] = &[
463 Item::Numeric(Numeric::Year, Pad::Zero),
464 Item::Space(""),
465 Item::Literal("-"),
466 Item::Numeric(Numeric::Month, Pad::Zero),
467 Item::Space(""),
468 Item::Literal("-"),
469 Item::Numeric(Numeric::Day, Pad::Zero),
470 ];
471 const TIME_ITEMS: &'static [Item<'static>] = &[
472 Item::Numeric(Numeric::Hour, Pad::Zero),
473 Item::Space(""),
474 Item::Literal(":"),
475 Item::Numeric(Numeric::Minute, Pad::Zero),
476 Item::Space(""),
477 Item::Literal(":"),
478 Item::Numeric(Numeric::Second, Pad::Zero),
479 Item::Fixed(Fixed::Nanosecond),
480 Item::Space(""),
481 Item::Fixed(Fixed::TimezoneOffsetZ),
482 Item::Space(""),
483 ];
484
485 let mut parsed = Parsed::new();
486 match parse_internal(&mut parsed, s, DATE_ITEMS.iter()) {
487 Err((remainder, e)) if e.0 == ParseErrorKind::TooLong => {
488 if remainder.starts_with('T') || remainder.starts_with(' ') {
489 parse(&mut parsed, &remainder[1..], TIME_ITEMS.iter())?;
490 } else {
491 Err(INVALID)?;
492 }
493 }
494 Err((_s, e)) => Err(e)?,
495 Ok(_) => Err(NOT_ENOUGH)?,
496 };
497 parsed.to_datetime()
498 }
499 }
500
501 #[cfg(test)]
502 #[test]
test_parse()503 fn test_parse() {
504 use super::IMPOSSIBLE;
505 use super::*;
506
507 // workaround for Rust issue #22255
508 fn parse_all(s: &str, items: &[Item]) -> ParseResult<Parsed> {
509 let mut parsed = Parsed::new();
510 parse(&mut parsed, s, items.iter())?;
511 Ok(parsed)
512 }
513
514 macro_rules! check {
515 ($fmt:expr, $items:expr; $err:tt) => (
516 assert_eq!(parse_all($fmt, &$items), Err($err))
517 );
518 ($fmt:expr, $items:expr; $($k:ident: $v:expr),*) => (#[allow(unused_mut)] {
519 let mut expected = Parsed::new();
520 $(expected.$k = Some($v);)*
521 assert_eq!(parse_all($fmt, &$items), Ok(expected))
522 });
523 }
524
525 // empty string
526 check!("", []; );
527 check!(" ", []; TOO_LONG);
528 check!("a", []; TOO_LONG);
529
530 // whitespaces
531 check!("", [sp!("")]; );
532 check!(" ", [sp!("")]; );
533 check!("\t", [sp!("")]; );
534 check!(" \n\r \n", [sp!("")]; );
535 check!("a", [sp!("")]; TOO_LONG);
536
537 // literal
538 check!("", [lit!("a")]; TOO_SHORT);
539 check!(" ", [lit!("a")]; INVALID);
540 check!("a", [lit!("a")]; );
541 check!("aa", [lit!("a")]; TOO_LONG);
542 check!("A", [lit!("a")]; INVALID);
543 check!("xy", [lit!("xy")]; );
544 check!("xy", [lit!("x"), lit!("y")]; );
545 check!("x y", [lit!("x"), lit!("y")]; INVALID);
546 check!("xy", [lit!("x"), sp!(""), lit!("y")]; );
547 check!("x y", [lit!("x"), sp!(""), lit!("y")]; );
548
549 // numeric
550 check!("1987", [num!(Year)]; year: 1987);
551 check!("1987 ", [num!(Year)]; TOO_LONG);
552 check!("0x12", [num!(Year)]; TOO_LONG); // `0` is parsed
553 check!("x123", [num!(Year)]; INVALID);
554 check!("2015", [num!(Year)]; year: 2015);
555 check!("0000", [num!(Year)]; year: 0);
556 check!("9999", [num!(Year)]; year: 9999);
557 check!(" \t987", [num!(Year)]; year: 987);
558 check!("5", [num!(Year)]; year: 5);
559 check!("5\0", [num!(Year)]; TOO_LONG);
560 check!("\05", [num!(Year)]; INVALID);
561 check!("", [num!(Year)]; TOO_SHORT);
562 check!("12345", [num!(Year), lit!("5")]; year: 1234);
563 check!("12345", [nums!(Year), lit!("5")]; year: 1234);
564 check!("12345", [num0!(Year), lit!("5")]; year: 1234);
565 check!("12341234", [num!(Year), num!(Year)]; year: 1234);
566 check!("1234 1234", [num!(Year), num!(Year)]; year: 1234);
567 check!("1234 1235", [num!(Year), num!(Year)]; IMPOSSIBLE);
568 check!("1234 1234", [num!(Year), lit!("x"), num!(Year)]; INVALID);
569 check!("1234x1234", [num!(Year), lit!("x"), num!(Year)]; year: 1234);
570 check!("1234xx1234", [num!(Year), lit!("x"), num!(Year)]; INVALID);
571 check!("1234 x 1234", [num!(Year), lit!("x"), num!(Year)]; INVALID);
572
573 // signed numeric
574 check!("-42", [num!(Year)]; year: -42);
575 check!("+42", [num!(Year)]; year: 42);
576 check!("-0042", [num!(Year)]; year: -42);
577 check!("+0042", [num!(Year)]; year: 42);
578 check!("-42195", [num!(Year)]; year: -42195);
579 check!("+42195", [num!(Year)]; year: 42195);
580 check!(" -42195", [num!(Year)]; year: -42195);
581 check!(" +42195", [num!(Year)]; year: 42195);
582 check!(" - 42", [num!(Year)]; INVALID);
583 check!(" + 42", [num!(Year)]; INVALID);
584 check!("-", [num!(Year)]; TOO_SHORT);
585 check!("+", [num!(Year)]; TOO_SHORT);
586
587 // unsigned numeric
588 check!("345", [num!(Ordinal)]; ordinal: 345);
589 check!("+345", [num!(Ordinal)]; INVALID);
590 check!("-345", [num!(Ordinal)]; INVALID);
591 check!(" 345", [num!(Ordinal)]; ordinal: 345);
592 check!(" +345", [num!(Ordinal)]; INVALID);
593 check!(" -345", [num!(Ordinal)]; INVALID);
594
595 // various numeric fields
596 check!("1234 5678",
597 [num!(Year), num!(IsoYear)];
598 year: 1234, isoyear: 5678);
599 check!("12 34 56 78",
600 [num!(YearDiv100), num!(YearMod100), num!(IsoYearDiv100), num!(IsoYearMod100)];
601 year_div_100: 12, year_mod_100: 34, isoyear_div_100: 56, isoyear_mod_100: 78);
602 check!("1 2 3 4 5 6",
603 [num!(Month), num!(Day), num!(WeekFromSun), num!(WeekFromMon), num!(IsoWeek),
604 num!(NumDaysFromSun)];
605 month: 1, day: 2, week_from_sun: 3, week_from_mon: 4, isoweek: 5, weekday: Weekday::Sat);
606 check!("7 89 01",
607 [num!(WeekdayFromMon), num!(Ordinal), num!(Hour12)];
608 weekday: Weekday::Sun, ordinal: 89, hour_mod_12: 1);
609 check!("23 45 6 78901234 567890123",
610 [num!(Hour), num!(Minute), num!(Second), num!(Nanosecond), num!(Timestamp)];
611 hour_div_12: 1, hour_mod_12: 11, minute: 45, second: 6, nanosecond: 78_901_234,
612 timestamp: 567_890_123);
613
614 // fixed: month and weekday names
615 check!("apr", [fix!(ShortMonthName)]; month: 4);
616 check!("Apr", [fix!(ShortMonthName)]; month: 4);
617 check!("APR", [fix!(ShortMonthName)]; month: 4);
618 check!("ApR", [fix!(ShortMonthName)]; month: 4);
619 check!("April", [fix!(ShortMonthName)]; TOO_LONG); // `Apr` is parsed
620 check!("A", [fix!(ShortMonthName)]; TOO_SHORT);
621 check!("Sol", [fix!(ShortMonthName)]; INVALID);
622 check!("Apr", [fix!(LongMonthName)]; month: 4);
623 check!("Apri", [fix!(LongMonthName)]; TOO_LONG); // `Apr` is parsed
624 check!("April", [fix!(LongMonthName)]; month: 4);
625 check!("Aprill", [fix!(LongMonthName)]; TOO_LONG);
626 check!("Aprill", [fix!(LongMonthName), lit!("l")]; month: 4);
627 check!("Aprl", [fix!(LongMonthName), lit!("l")]; month: 4);
628 check!("April", [fix!(LongMonthName), lit!("il")]; TOO_SHORT); // do not backtrack
629 check!("thu", [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
630 check!("Thu", [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
631 check!("THU", [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
632 check!("tHu", [fix!(ShortWeekdayName)]; weekday: Weekday::Thu);
633 check!("Thursday", [fix!(ShortWeekdayName)]; TOO_LONG); // `Thu` is parsed
634 check!("T", [fix!(ShortWeekdayName)]; TOO_SHORT);
635 check!("The", [fix!(ShortWeekdayName)]; INVALID);
636 check!("Nop", [fix!(ShortWeekdayName)]; INVALID);
637 check!("Thu", [fix!(LongWeekdayName)]; weekday: Weekday::Thu);
638 check!("Thur", [fix!(LongWeekdayName)]; TOO_LONG); // `Thu` is parsed
639 check!("Thurs", [fix!(LongWeekdayName)]; TOO_LONG); // ditto
640 check!("Thursday", [fix!(LongWeekdayName)]; weekday: Weekday::Thu);
641 check!("Thursdays", [fix!(LongWeekdayName)]; TOO_LONG);
642 check!("Thursdays", [fix!(LongWeekdayName), lit!("s")]; weekday: Weekday::Thu);
643 check!("Thus", [fix!(LongWeekdayName), lit!("s")]; weekday: Weekday::Thu);
644 check!("Thursday", [fix!(LongWeekdayName), lit!("rsday")]; TOO_SHORT); // do not backtrack
645
646 // fixed: am/pm
647 check!("am", [fix!(LowerAmPm)]; hour_div_12: 0);
648 check!("pm", [fix!(LowerAmPm)]; hour_div_12: 1);
649 check!("AM", [fix!(LowerAmPm)]; hour_div_12: 0);
650 check!("PM", [fix!(LowerAmPm)]; 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!(UpperAmPm)]; hour_div_12: 0);
654 check!("PM", [fix!(UpperAmPm)]; hour_div_12: 1);
655 check!("Am", [fix!(LowerAmPm)]; hour_div_12: 0);
656 check!(" Am", [fix!(LowerAmPm)]; INVALID);
657 check!("ame", [fix!(LowerAmPm)]; TOO_LONG); // `am` is parsed
658 check!("a", [fix!(LowerAmPm)]; TOO_SHORT);
659 check!("p", [fix!(LowerAmPm)]; TOO_SHORT);
660 check!("x", [fix!(LowerAmPm)]; TOO_SHORT);
661 check!("xx", [fix!(LowerAmPm)]; INVALID);
662 check!("", [fix!(LowerAmPm)]; TOO_SHORT);
663
664 // fixed: dot plus nanoseconds
665 check!("", [fix!(Nanosecond)]; ); // no field set, but not an error
666 check!("4", [fix!(Nanosecond)]; TOO_LONG); // never consumes `4`
667 check!("4", [fix!(Nanosecond), num!(Second)]; second: 4);
668 check!(".0", [fix!(Nanosecond)]; nanosecond: 0);
669 check!(".4", [fix!(Nanosecond)]; nanosecond: 400_000_000);
670 check!(".42", [fix!(Nanosecond)]; nanosecond: 420_000_000);
671 check!(".421", [fix!(Nanosecond)]; nanosecond: 421_000_000);
672 check!(".42195", [fix!(Nanosecond)]; nanosecond: 421_950_000);
673 check!(".421950803", [fix!(Nanosecond)]; nanosecond: 421_950_803);
674 check!(".421950803547", [fix!(Nanosecond)]; nanosecond: 421_950_803);
675 check!(".000000003547", [fix!(Nanosecond)]; nanosecond: 3);
676 check!(".000000000547", [fix!(Nanosecond)]; nanosecond: 0);
677 check!(".", [fix!(Nanosecond)]; TOO_SHORT);
678 check!(".4x", [fix!(Nanosecond)]; TOO_LONG);
679 check!(". 4", [fix!(Nanosecond)]; INVALID);
680 check!(" .4", [fix!(Nanosecond)]; TOO_LONG); // no automatic trimming
681
682 // fixed: nanoseconds without the dot
683 check!("", [internal_fix!(Nanosecond3NoDot)]; TOO_SHORT);
684 check!("0", [internal_fix!(Nanosecond3NoDot)]; TOO_SHORT);
685 check!("4", [internal_fix!(Nanosecond3NoDot)]; TOO_SHORT);
686 check!("42", [internal_fix!(Nanosecond3NoDot)]; TOO_SHORT);
687 check!("421", [internal_fix!(Nanosecond3NoDot)]; nanosecond: 421_000_000);
688 check!("42143", [internal_fix!(Nanosecond3NoDot), num!(Second)]; nanosecond: 421_000_000, second: 43);
689 check!("42195", [internal_fix!(Nanosecond3NoDot)]; TOO_LONG);
690 check!("4x", [internal_fix!(Nanosecond3NoDot)]; TOO_SHORT);
691 check!(" 4", [internal_fix!(Nanosecond3NoDot)]; INVALID);
692 check!(".421", [internal_fix!(Nanosecond3NoDot)]; INVALID);
693
694 check!("", [internal_fix!(Nanosecond6NoDot)]; TOO_SHORT);
695 check!("0", [internal_fix!(Nanosecond6NoDot)]; TOO_SHORT);
696 check!("42195", [internal_fix!(Nanosecond6NoDot)]; TOO_SHORT);
697 check!("421950", [internal_fix!(Nanosecond6NoDot)]; nanosecond: 421_950_000);
698 check!("000003", [internal_fix!(Nanosecond6NoDot)]; nanosecond: 3000);
699 check!("000000", [internal_fix!(Nanosecond6NoDot)]; nanosecond: 0);
700 check!("4x", [internal_fix!(Nanosecond6NoDot)]; TOO_SHORT);
701 check!(" 4", [internal_fix!(Nanosecond6NoDot)]; INVALID);
702 check!(".42100", [internal_fix!(Nanosecond6NoDot)]; INVALID);
703
704 check!("", [internal_fix!(Nanosecond9NoDot)]; TOO_SHORT);
705 check!("42195", [internal_fix!(Nanosecond9NoDot)]; TOO_SHORT);
706 check!("421950803", [internal_fix!(Nanosecond9NoDot)]; nanosecond: 421_950_803);
707 check!("000000003", [internal_fix!(Nanosecond9NoDot)]; nanosecond: 3);
708 check!("42195080354", [internal_fix!(Nanosecond9NoDot), num!(Second)]; nanosecond: 421_950_803, second: 54); // don't skip digits that come after the 9
709 check!("421950803547", [internal_fix!(Nanosecond9NoDot)]; TOO_LONG);
710 check!("000000000", [internal_fix!(Nanosecond9NoDot)]; nanosecond: 0);
711 check!("00000000x", [internal_fix!(Nanosecond9NoDot)]; INVALID);
712 check!(" 4", [internal_fix!(Nanosecond9NoDot)]; INVALID);
713 check!(".42100000", [internal_fix!(Nanosecond9NoDot)]; INVALID);
714
715 // fixed: timezone offsets
716 check!("+00:00", [fix!(TimezoneOffset)]; offset: 0);
717 check!("-00:00", [fix!(TimezoneOffset)]; offset: 0);
718 check!("+00:01", [fix!(TimezoneOffset)]; offset: 60);
719 check!("-00:01", [fix!(TimezoneOffset)]; offset: -60);
720 check!("+00:30", [fix!(TimezoneOffset)]; offset: 30 * 60);
721 check!("-00:30", [fix!(TimezoneOffset)]; offset: -30 * 60);
722 check!("+04:56", [fix!(TimezoneOffset)]; offset: 296 * 60);
723 check!("-04:56", [fix!(TimezoneOffset)]; offset: -296 * 60);
724 check!("+24:00", [fix!(TimezoneOffset)]; offset: 24 * 60 * 60);
725 check!("-24:00", [fix!(TimezoneOffset)]; offset: -24 * 60 * 60);
726 check!("+99:59", [fix!(TimezoneOffset)]; offset: (100 * 60 - 1) * 60);
727 check!("-99:59", [fix!(TimezoneOffset)]; offset: -(100 * 60 - 1) * 60);
728 check!("+00:59", [fix!(TimezoneOffset)]; offset: 59 * 60);
729 check!("+00:60", [fix!(TimezoneOffset)]; OUT_OF_RANGE);
730 check!("+00:99", [fix!(TimezoneOffset)]; OUT_OF_RANGE);
731 check!("#12:34", [fix!(TimezoneOffset)]; INVALID);
732 check!("12:34", [fix!(TimezoneOffset)]; INVALID);
733 check!("+12:34 ", [fix!(TimezoneOffset)]; TOO_LONG);
734 check!(" +12:34", [fix!(TimezoneOffset)]; offset: 754 * 60);
735 check!("\t -12:34", [fix!(TimezoneOffset)]; offset: -754 * 60);
736 check!("", [fix!(TimezoneOffset)]; TOO_SHORT);
737 check!("+", [fix!(TimezoneOffset)]; TOO_SHORT);
738 check!("+1", [fix!(TimezoneOffset)]; TOO_SHORT);
739 check!("+12", [fix!(TimezoneOffset)]; TOO_SHORT);
740 check!("+123", [fix!(TimezoneOffset)]; TOO_SHORT);
741 check!("+1234", [fix!(TimezoneOffset)]; offset: 754 * 60);
742 check!("+12345", [fix!(TimezoneOffset)]; TOO_LONG);
743 check!("+12345", [fix!(TimezoneOffset), num!(Day)]; offset: 754 * 60, day: 5);
744 check!("Z", [fix!(TimezoneOffset)]; INVALID);
745 check!("z", [fix!(TimezoneOffset)]; INVALID);
746 check!("Z", [fix!(TimezoneOffsetZ)]; offset: 0);
747 check!("z", [fix!(TimezoneOffsetZ)]; offset: 0);
748 check!("Y", [fix!(TimezoneOffsetZ)]; INVALID);
749 check!("Zulu", [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 0);
750 check!("zulu", [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 0);
751 check!("+1234ulu", [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 754 * 60);
752 check!("+12:34ulu", [fix!(TimezoneOffsetZ), lit!("ulu")]; offset: 754 * 60);
753 check!("Z", [internal_fix!(TimezoneOffsetPermissive)]; offset: 0);
754 check!("z", [internal_fix!(TimezoneOffsetPermissive)]; offset: 0);
755 check!("+12:00", [internal_fix!(TimezoneOffsetPermissive)]; offset: 12 * 60 * 60);
756 check!("+12", [internal_fix!(TimezoneOffsetPermissive)]; offset: 12 * 60 * 60);
757 check!("CEST 5", [fix!(TimezoneName), lit!(" "), num!(Day)]; day: 5);
758
759 // some practical examples
760 check!("2015-02-04T14:37:05+09:00",
761 [num!(Year), lit!("-"), num!(Month), lit!("-"), num!(Day), lit!("T"),
762 num!(Hour), lit!(":"), num!(Minute), lit!(":"), num!(Second), fix!(TimezoneOffset)];
763 year: 2015, month: 2, day: 4, hour_div_12: 1, hour_mod_12: 2,
764 minute: 37, second: 5, offset: 32400);
765 check!("20150204143705567",
766 [num!(Year), num!(Month), num!(Day),
767 num!(Hour), num!(Minute), num!(Second), internal_fix!(Nanosecond3NoDot)];
768 year: 2015, month: 2, day: 4, hour_div_12: 1, hour_mod_12: 2,
769 minute: 37, second: 5, nanosecond: 567000000);
770 check!("Mon, 10 Jun 2013 09:32:37 GMT",
771 [fix!(ShortWeekdayName), lit!(","), sp!(" "), num!(Day), sp!(" "),
772 fix!(ShortMonthName), sp!(" "), num!(Year), sp!(" "), num!(Hour), lit!(":"),
773 num!(Minute), lit!(":"), num!(Second), sp!(" "), lit!("GMT")];
774 year: 2013, month: 6, day: 10, weekday: Weekday::Mon,
775 hour_div_12: 0, hour_mod_12: 9, minute: 32, second: 37);
776 check!("Sun Aug 02 13:39:15 CEST 2020",
777 [fix!(ShortWeekdayName), sp!(" "), fix!(ShortMonthName), sp!(" "),
778 num!(Day), sp!(" "), num!(Hour), lit!(":"), num!(Minute), lit!(":"),
779 num!(Second), sp!(" "), fix!(TimezoneName), sp!(" "), num!(Year)];
780 year: 2020, month: 8, day: 2, weekday: Weekday::Sun,
781 hour_div_12: 1, hour_mod_12: 1, minute: 39, second: 15);
782 check!("20060102150405",
783 [num!(Year), num!(Month), num!(Day), num!(Hour), num!(Minute), num!(Second)];
784 year: 2006, month: 1, day: 2, hour_div_12: 1, hour_mod_12: 3, minute: 4, second: 5);
785 check!("3:14PM",
786 [num!(Hour12), lit!(":"), num!(Minute), fix!(LowerAmPm)];
787 hour_div_12: 1, hour_mod_12: 3, minute: 14);
788 check!("12345678901234.56789",
789 [num!(Timestamp), lit!("."), num!(Nanosecond)];
790 nanosecond: 56_789, timestamp: 12_345_678_901_234);
791 check!("12345678901234.56789",
792 [num!(Timestamp), fix!(Nanosecond)];
793 nanosecond: 567_890_000, timestamp: 12_345_678_901_234);
794 }
795
796 #[cfg(test)]
797 #[test]
test_rfc2822()798 fn test_rfc2822() {
799 use super::NOT_ENOUGH;
800 use super::*;
801 use offset::FixedOffset;
802 use DateTime;
803
804 // Test data - (input, Ok(expected result after parse and format) or Err(error code))
805 let testdates = [
806 ("Tue, 20 Jan 2015 17:35:20 -0800", Ok("Tue, 20 Jan 2015 17:35:20 -0800")), // normal case
807 ("Fri, 2 Jan 2015 17:35:20 -0800", Ok("Fri, 02 Jan 2015 17:35:20 -0800")), // folding whitespace
808 ("Fri, 02 Jan 2015 17:35:20 -0800", Ok("Fri, 02 Jan 2015 17:35:20 -0800")), // leading zero
809 ("20 Jan 2015 17:35:20 -0800", Ok("Tue, 20 Jan 2015 17:35:20 -0800")), // no day of week
810 ("20 JAN 2015 17:35:20 -0800", Ok("Tue, 20 Jan 2015 17:35:20 -0800")), // upper case month
811 ("Tue, 20 Jan 2015 17:35 -0800", Ok("Tue, 20 Jan 2015 17:35:00 -0800")), // no second
812 ("11 Sep 2001 09:45:00 EST", Ok("Tue, 11 Sep 2001 09:45:00 -0500")),
813 ("30 Feb 2015 17:35:20 -0800", Err(OUT_OF_RANGE)), // bad day of month
814 ("Tue, 20 Jan 2015", Err(TOO_SHORT)), // omitted fields
815 ("Tue, 20 Avr 2015 17:35:20 -0800", Err(INVALID)), // bad month name
816 ("Tue, 20 Jan 2015 25:35:20 -0800", Err(OUT_OF_RANGE)), // bad hour
817 ("Tue, 20 Jan 2015 7:35:20 -0800", Err(INVALID)), // bad # of digits in hour
818 ("Tue, 20 Jan 2015 17:65:20 -0800", Err(OUT_OF_RANGE)), // bad minute
819 ("Tue, 20 Jan 2015 17:35:90 -0800", Err(OUT_OF_RANGE)), // bad second
820 ("Tue, 20 Jan 2015 17:35:20 -0890", Err(OUT_OF_RANGE)), // bad offset
821 ("6 Jun 1944 04:00:00Z", Err(INVALID)), // bad offset (zulu not allowed)
822 ("Tue, 20 Jan 2015 17:35:20 HAS", Err(NOT_ENOUGH)), // bad named time zone
823 ];
824
825 fn rfc2822_to_datetime(date: &str) -> ParseResult<DateTime<FixedOffset>> {
826 let mut parsed = Parsed::new();
827 parse(&mut parsed, date, [Item::Fixed(Fixed::RFC2822)].iter())?;
828 parsed.to_datetime()
829 }
830
831 fn fmt_rfc2822_datetime(dt: DateTime<FixedOffset>) -> String {
832 dt.format_with_items([Item::Fixed(Fixed::RFC2822)].iter()).to_string()
833 }
834
835 // Test against test data above
836 for &(date, checkdate) in testdates.iter() {
837 let d = rfc2822_to_datetime(date); // parse a date
838 let dt = match d {
839 // did we get a value?
840 Ok(dt) => Ok(fmt_rfc2822_datetime(dt)), // yes, go on
841 Err(e) => Err(e), // otherwise keep an error for the comparison
842 };
843 if dt != checkdate.map(|s| s.to_string()) {
844 // check for expected result
845 panic!(
846 "Date conversion failed for {}\nReceived: {:?}\nExpected: {:?}",
847 date, dt, checkdate
848 );
849 }
850 }
851 }
852
853 #[cfg(test)]
854 #[test]
parse_rfc850()855 fn parse_rfc850() {
856 use {TimeZone, Utc};
857
858 static RFC850_FMT: &'static str = "%A, %d-%b-%y %T GMT";
859
860 let dt_str = "Sunday, 06-Nov-94 08:49:37 GMT";
861 let dt = Utc.ymd(1994, 11, 6).and_hms(8, 49, 37);
862
863 // Check that the format is what we expect
864 assert_eq!(dt.format(RFC850_FMT).to_string(), dt_str);
865
866 // Check that it parses correctly
867 assert_eq!(Ok(dt), Utc.datetime_from_str("Sunday, 06-Nov-94 08:49:37 GMT", RFC850_FMT));
868
869 // Check that the rest of the weekdays parse correctly (this test originally failed because
870 // Sunday parsed incorrectly).
871 let testdates = [
872 (Utc.ymd(1994, 11, 7).and_hms(8, 49, 37), "Monday, 07-Nov-94 08:49:37 GMT"),
873 (Utc.ymd(1994, 11, 8).and_hms(8, 49, 37), "Tuesday, 08-Nov-94 08:49:37 GMT"),
874 (Utc.ymd(1994, 11, 9).and_hms(8, 49, 37), "Wednesday, 09-Nov-94 08:49:37 GMT"),
875 (Utc.ymd(1994, 11, 10).and_hms(8, 49, 37), "Thursday, 10-Nov-94 08:49:37 GMT"),
876 (Utc.ymd(1994, 11, 11).and_hms(8, 49, 37), "Friday, 11-Nov-94 08:49:37 GMT"),
877 (Utc.ymd(1994, 11, 12).and_hms(8, 49, 37), "Saturday, 12-Nov-94 08:49:37 GMT"),
878 ];
879
880 for val in &testdates {
881 assert_eq!(Ok(val.0), Utc.datetime_from_str(val.1, RFC850_FMT));
882 }
883 }
884
885 #[cfg(test)]
886 #[test]
test_rfc3339()887 fn test_rfc3339() {
888 use super::*;
889 use offset::FixedOffset;
890 use DateTime;
891
892 // Test data - (input, Ok(expected result after parse and format) or Err(error code))
893 let testdates = [
894 ("2015-01-20T17:35:20-08:00", Ok("2015-01-20T17:35:20-08:00")), // normal case
895 ("1944-06-06T04:04:00Z", Ok("1944-06-06T04:04:00+00:00")), // D-day
896 ("2001-09-11T09:45:00-08:00", Ok("2001-09-11T09:45:00-08:00")),
897 ("2015-01-20T17:35:20.001-08:00", Ok("2015-01-20T17:35:20.001-08:00")),
898 ("2015-01-20T17:35:20.000031-08:00", Ok("2015-01-20T17:35:20.000031-08:00")),
899 ("2015-01-20T17:35:20.000000004-08:00", Ok("2015-01-20T17:35:20.000000004-08:00")),
900 ("2015-01-20T17:35:20.000000000452-08:00", Ok("2015-01-20T17:35:20-08:00")), // too small
901 ("2015-02-30T17:35:20-08:00", Err(OUT_OF_RANGE)), // bad day of month
902 ("2015-01-20T25:35:20-08:00", Err(OUT_OF_RANGE)), // bad hour
903 ("2015-01-20T17:65:20-08:00", Err(OUT_OF_RANGE)), // bad minute
904 ("2015-01-20T17:35:90-08:00", Err(OUT_OF_RANGE)), // bad second
905 ("2015-01-20T17:35:20-24:00", Err(OUT_OF_RANGE)), // bad offset
906 ];
907
908 fn rfc3339_to_datetime(date: &str) -> ParseResult<DateTime<FixedOffset>> {
909 let mut parsed = Parsed::new();
910 parse(&mut parsed, date, [Item::Fixed(Fixed::RFC3339)].iter())?;
911 parsed.to_datetime()
912 }
913
914 fn fmt_rfc3339_datetime(dt: DateTime<FixedOffset>) -> String {
915 dt.format_with_items([Item::Fixed(Fixed::RFC3339)].iter()).to_string()
916 }
917
918 // Test against test data above
919 for &(date, checkdate) in testdates.iter() {
920 let d = rfc3339_to_datetime(date); // parse a date
921 let dt = match d {
922 // did we get a value?
923 Ok(dt) => Ok(fmt_rfc3339_datetime(dt)), // yes, go on
924 Err(e) => Err(e), // otherwise keep an error for the comparison
925 };
926 if dt != checkdate.map(|s| s.to_string()) {
927 // check for expected result
928 panic!(
929 "Date conversion failed for {}\nReceived: {:?}\nExpected: {:?}",
930 date, dt, checkdate
931 );
932 }
933 }
934 }
935