Skip to content

Commit

Permalink
Reject missing component between valid components
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Jul 28, 2023
1 parent bbea8cd commit 72d4f94
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 13 additions & 1 deletion tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,18 @@ fn parse_time_err() -> time::Result<()> {
error::TryFromParsed::InsufficientInformation { .. }
))
));
assert!(matches!(
Time::parse("12:34", &fd::parse("[hour]:[second]")?),
Err(error::Parse::TryFromParsed(
error::TryFromParsed::InsufficientInformation { .. }
))
));
assert!(matches!(
Time::parse("12:34", &fd::parse("[hour]:[subsecond]")?),
Err(error::Parse::TryFromParsed(
error::TryFromParsed::InsufficientInformation { .. }
))
));
assert!(matches!(
Time::parse("13 PM", &fd::parse("[hour repr:12] [period]")?),
Err(error::Parse::ParseFromDescription(
Expand Down Expand Up @@ -1033,7 +1045,7 @@ fn parse_primitive_date_time_err() -> time::Result<()> {
));
assert!(matches!(
PrimitiveDateTime::parse(
"2023-07-27 23:30",
"2023-07-27 23:30",
&fd::parse("[year]-[month]-[day] [hour]")?
),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
Expand Down
16 changes: 12 additions & 4 deletions time/src/parsing/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ impl TryFrom<Parsed> for Time {
(_, Some(hour), Some(true)) => hour.get() + 12,
_ => return Err(InsufficientInformation),
};

if parsed.hour_24().is_none()
&& parsed.hour_12().is_some()
&& parsed.hour_12_is_pm().is_some()
Expand All @@ -793,10 +794,17 @@ impl TryFrom<Parsed> for Time {
{
return Ok(Self::from_hms_nano(hour, 0, 0, 0)?);
}
let minute = parsed.minute().unwrap_or(0);
let second = parsed.second().unwrap_or(0);
let subsecond = parsed.subsecond().unwrap_or(0);
Ok(Self::from_hms_nano(hour, minute, second, subsecond)?)

// Reject combinations such as hour-second with minute omitted.
match (parsed.minute(), parsed.second(), parsed.subsecond()) {
(None, None, None) => Ok(Self::from_hms_nano(hour, 0, 0, 0)?),
(Some(minute), None, None) => Ok(Self::from_hms_nano(hour, minute, 0, 0)?),
(Some(minute), Some(second), None) => Ok(Self::from_hms_nano(hour, minute, second, 0)?),
(Some(minute), Some(second), Some(subsecond)) => {
Ok(Self::from_hms_nano(hour, minute, second, subsecond)?)
}
_ => Err(InsufficientInformation),
}
}
}

Expand Down

0 comments on commit 72d4f94

Please sign in to comment.