Skip to content

Commit 3f8a500

Browse files
committed
Use parse_rfc3339 directly in DateTime::parse_from_rfc3339
We want this to use the strict parser directly, so we can switch the `RFC3339` item to a relaxed parser.
1 parent ccd7f85 commit 3f8a500

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

src/datetime/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ use std::time::{SystemTime, UNIX_EPOCH};
1616

1717
#[cfg(feature = "unstable-locales")]
1818
use crate::format::Locale;
19-
use crate::format::{parse, parse_and_remainder, ParseError, ParseResult, Parsed, StrftimeItems};
19+
use crate::format::{
20+
parse, parse_and_remainder, parse_rfc3339, Fixed, Item, ParseError, ParseResult, Parsed,
21+
StrftimeItems, TOO_LONG,
22+
};
2023
#[cfg(any(feature = "alloc", feature = "std"))]
2124
use crate::format::{write_rfc3339, DelayedFormat};
22-
use crate::format::{Fixed, Item};
2325
use crate::naive::{Days, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime};
2426
#[cfg(feature = "clock")]
2527
use crate::offset::Local;
@@ -699,9 +701,11 @@ impl DateTime<FixedOffset> {
699701
/// also simultaneously valid RFC 3339 values, but not all RFC 3339 values are valid ISO 8601
700702
/// values (or the other way around).
701703
pub fn parse_from_rfc3339(s: &str) -> ParseResult<DateTime<FixedOffset>> {
702-
const ITEMS: &[Item<'static>] = &[Item::Fixed(Fixed::RFC3339)];
703704
let mut parsed = Parsed::new();
704-
parse(&mut parsed, s, ITEMS.iter())?;
705+
let (s, _) = parse_rfc3339(&mut parsed, s)?;
706+
if !s.is_empty() {
707+
return Err(TOO_LONG);
708+
}
705709
parsed.to_datetime()
706710
}
707711

src/format/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub use formatting::{format_item_localized, format_localized};
6969
pub use locales::Locale;
7070
#[cfg(all(not(feature = "unstable-locales"), any(feature = "alloc", feature = "std")))]
7171
pub(crate) use locales::Locale;
72+
pub(crate) use parse::parse_rfc3339;
7273
pub use parse::{parse, parse_and_remainder};
7374
pub use parsed::Parsed;
7475
pub use strftime::StrftimeItems;
@@ -450,7 +451,7 @@ const IMPOSSIBLE: ParseError = ParseError(ParseErrorKind::Impossible);
450451
const NOT_ENOUGH: ParseError = ParseError(ParseErrorKind::NotEnough);
451452
const INVALID: ParseError = ParseError(ParseErrorKind::Invalid);
452453
const TOO_SHORT: ParseError = ParseError(ParseErrorKind::TooShort);
453-
const TOO_LONG: ParseError = ParseError(ParseErrorKind::TooLong);
454+
pub(crate) const TOO_LONG: ParseError = ParseError(ParseErrorKind::TooLong);
454455
const BAD_FORMAT: ParseError = ParseError(ParseErrorKind::BadFormat);
455456

456457
// this implementation is here only because we need some private code from `scan`

src/format/parse.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn parse_rfc2822<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a st
155155
Ok((s, ()))
156156
}
157157

158-
fn parse_rfc3339<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a str, ())> {
158+
pub(crate) fn parse_rfc3339<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a str, ())> {
159159
macro_rules! try_consume {
160160
($e:expr) => {{
161161
let (s_, v) = $e?;
@@ -1817,15 +1817,9 @@ mod tests {
18171817
("2015-01-20T00:00:1-08:00", Err(INVALID)), // missing complete S
18181818
];
18191819

1820-
fn rfc3339_to_datetime(date: &str) -> ParseResult<DateTime<FixedOffset>> {
1821-
let mut parsed = Parsed::new();
1822-
parse(&mut parsed, date, [Item::Fixed(Fixed::RFC3339)].iter())?;
1823-
parsed.to_datetime()
1824-
}
1825-
18261820
// Test against test data above
18271821
for &(date, checkdate) in testdates.iter() {
1828-
let dt = rfc3339_to_datetime(date); // parse a date
1822+
let dt = DateTime::<FixedOffset>::parse_from_rfc3339(date);
18291823
if dt != checkdate {
18301824
// check for expected result
18311825
panic!(

0 commit comments

Comments
 (0)