Skip to content

Commit

Permalink
Add support for only hours in time format with tests (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnarMorrigan authored Jul 28, 2023
1 parent facb587 commit bbea8cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ fn iso_8601_error() {
fn parse_time() -> time::Result<()> {
let format_input_output = [
(fd::parse("[hour repr:12] [period]")?, "01 PM", time!(1 PM)),
(fd::parse("[hour]")?, "12", time!(12:00)),
(
fd::parse("[hour]:[minute]:[second]")?,
"13:02:03",
Expand Down Expand Up @@ -672,12 +673,6 @@ fn parse_time_err() -> time::Result<()> {
error::TryFromParsed::InsufficientInformation { .. }
))
));
assert!(matches!(
Time::parse("12", &fd::parse("[hour]")?),
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 @@ -1009,6 +1004,16 @@ fn parse_offset_err() -> time::Result<()> {
Ok(())
}

#[test]
fn parse_primitive_date_time() -> time::Result<()> {
assert_eq!(
PrimitiveDateTime::parse("2023-07-27 23", &fd::parse("[year]-[month]-[day] [hour]")?),
Ok(datetime!(2023-07-27 23:00))
);

Ok(())
}

#[test]
fn parse_primitive_date_time_err() -> time::Result<()> {
assert!(matches!(
Expand All @@ -1026,6 +1031,13 @@ fn parse_primitive_date_time_err() -> time::Result<()> {
error::ParseFromDescription::InvalidComponent("hour")
))
));
assert!(matches!(
PrimitiveDateTime::parse(
"2023-07-27 23:30",
&fd::parse("[year]-[month]-[day] [hour]")?
),
Err(error::Parse::UnexpectedTrailingCharacters { .. })
));

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion time/src/parsing/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ impl TryFrom<Parsed> for Time {
{
return Ok(Self::from_hms_nano(hour, 0, 0, 0)?);
}
let minute = parsed.minute().ok_or(InsufficientInformation)?;
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)?)
Expand Down

0 comments on commit bbea8cd

Please sign in to comment.