Skip to content

Commit

Permalink
Error if there are too many fields for NaiveTime
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed May 28, 2023
1 parent 4c6f0a7 commit 0fea267
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
20 changes: 20 additions & 0 deletions src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,23 @@ impl Parsed {
|| self.timestamp.is_some()
}

fn has_date_fields(&self) -> bool {
self.year.is_some()
|| self.year_div_100.is_some()
|| self.year_mod_100.is_some()
|| self.isoyear.is_some()
|| self.isoyear_div_100.is_some()
|| self.isoyear_mod_100.is_some()
|| self.month.is_some()
|| self.week_from_sun.is_some()
|| self.week_from_mon.is_some()
|| self.isoweek.is_some()
|| self.weekday.is_some()
|| self.ordinal.is_some()
|| self.day.is_some()
|| self.timestamp.is_some()
}

/// Returns a parsed naive date out of given fields.
///
/// This method is able to determine the date from given subset of fields:
Expand Down Expand Up @@ -434,6 +451,9 @@ impl Parsed {
///
/// It is able to handle leap seconds when given second is 60.
pub fn to_naive_time(&self) -> ParseResult<NaiveTime> {
if self.has_date_fields() || self.offset.is_some() {
return Err(BAD_FORMAT);
}
self.to_naive_time_inner(true)
}

Expand Down
15 changes: 10 additions & 5 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,18 @@ impl NaiveTime {
/// Ok(NaiveTime::from_hms_micro_opt(13, 23, 45, 678_900).unwrap()));
/// ```
///
/// Date and offset is ignored for the purpose of parsing.
/// Date and offset will return an error, parse to a [`NaiveDateTime`] or
/// `DateTime<FixedOffset>` first.
///
/// ```
/// # use chrono::NaiveTime;
/// # let parse_from_str = NaiveTime::parse_from_str;
/// assert_eq!(parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
/// Ok(NaiveTime::from_hms_opt(12, 34, 56).unwrap()));
/// use chrono::{DateTime, FixedOffset, NaiveTime};
/// use chrono::format::ParseErrorKind;
///
/// let result = NaiveTime::parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z");
/// assert_eq!(result.unwrap_err().kind(), ParseErrorKind::BadFormat);
/// let dt =
/// DateTime::<FixedOffset>::parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z");
/// assert_eq!(dt.unwrap().time(), NaiveTime::from_hms_opt(12, 34, 56).unwrap());
/// ```
///
/// [Leap seconds](#leap-second-handling) are correctly handled by
Expand Down
10 changes: 6 additions & 4 deletions src/naive/time/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,6 @@ fn test_date_from_str() {
#[test]
fn test_time_parse_from_str() {
let hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();
assert_eq!(
NaiveTime::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
Ok(hms(12, 34, 56))
); // ignore date and offset
assert_eq!(NaiveTime::parse_from_str("PM 12:59", "%P %H:%M"), Ok(hms(12, 59, 0)));
assert_eq!(NaiveTime::parse_from_str("12:59 \n\t PM", "%H:%M \n\t %P"), Ok(hms(12, 59, 0)));
assert_eq!(NaiveTime::parse_from_str("\t\t12:59\tPM\t", "\t\t%H:%M\t%P\t"), Ok(hms(12, 59, 0)));
Expand All @@ -343,6 +339,12 @@ fn test_time_parse_from_str() {
assert!(NaiveTime::parse_from_str("\t\t12:59 PM\t", "\t\t%H:%M\t%P\t").is_err());
assert!(NaiveTime::parse_from_str("12:59 PM", "%H:%M %P").is_err());
assert!(NaiveTime::parse_from_str("12:3456", "%H:%M:%S").is_err());

assert!(NaiveTime::parse_from_str("2014-5-7T12:34:56", "%Y-%m-%dT%H:%M:%S").is_err());
assert!(NaiveTime::parse_from_str("12:34:56+09:30", "%%H:%M:%S%z").is_err());
assert!(
NaiveTime::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z").is_err()
);
}

#[test]
Expand Down

0 comments on commit 0fea267

Please sign in to comment.