From 1d1c4f87c2a70830168a86a577171268ce6e7234 Mon Sep 17 00:00:00 2001 From: Gunnar Noordbruis <13799935+GunnarMorrigan@users.noreply.github.com> Date: Thu, 27 Jul 2023 23:39:46 +0200 Subject: [PATCH] Add support for only hours in time format with tests --- tests/parsing.rs | 24 ++++++++++++++++++------ time/src/parsing/parsed.rs | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/tests/parsing.rs b/tests/parsing.rs index a171f9dee..5dc26d05a 100644 --- a/tests/parsing.rs +++ b/tests/parsing.rs @@ -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", @@ -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( @@ -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!( @@ -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(()) } diff --git a/time/src/parsing/parsed.rs b/time/src/parsing/parsed.rs index b3cdc1492..d33ae6541 100644 --- a/time/src/parsing/parsed.rs +++ b/time/src/parsing/parsed.rs @@ -793,7 +793,7 @@ impl TryFrom 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)?)