diff --git a/src/items/builder.rs b/src/items/builder.rs index 4fd6aca..3762aa6 100644 --- a/src/items/builder.rs +++ b/src/items/builder.rs @@ -255,7 +255,6 @@ impl DateTimeBuilder { // 1. Choose the base instant. // If a TZ="..." prefix was parsed, it should override the base's timezone // while keeping the base's timestamp for relative date calculations. - let has_timezone = self.timezone.is_some(); let base = match (self.base, self.timezone) { (Some(b), Some(tz)) => b.timestamp().to_zoned_datetime(tz), (Some(b), None) => b, @@ -270,14 +269,12 @@ impl DateTimeBuilder { } // 3. Determine whether to truncate the time of day. Like GNU, a - // relative item on its own keeps the current time of day, while a date, - // a weekday, or a timezone item without any relative item resets it to - // midnight. - let zone_seen = self.offset.is_some() || self.local_zone || has_timezone; + // relative item on its own keeps the current time of day. All other + // inputs, including an empty or comment-only input, start at midnight. let need_midnight = self.date.is_some() || self.time.is_some() || self.weekday.is_some() - || (zone_seen && self.relative.is_empty()); + || self.relative.is_empty(); let mut dt = if need_midnight { base.with().time(civil::time(0, 0, 0, 0)).build()? @@ -394,12 +391,11 @@ impl DateTimeBuilder { time, weekday, offset, - local_zone, + local_zone: _, timezone, relative, } = self; - let has_timezone = timezone.is_some(); let base = match (base, timezone) { (Some(b), Some(tz)) => b.timestamp().to_zoned_datetime(tz), (Some(b), None) => b, @@ -410,11 +406,8 @@ impl DateTimeBuilder { // See `build_in_range` for why a relative item alone keeps the // current time of day. - let zone_seen = offset.is_some() || local_zone || has_timezone; - let need_midnight = date.is_some() - || time.is_some() - || weekday.is_some() - || (zone_seen && relative.is_empty()); + let need_midnight = + date.is_some() || time.is_some() || weekday.is_some() || relative.is_empty(); let mut dt = ExtendedDateTime::new( DateParts { year: u32::try_from(base.year()).map_err(|_| "year must be non-negative")?, @@ -1028,7 +1021,7 @@ mod tests { } #[test] - fn build_extended_preserves_base_time_when_not_truncated() { + fn build_extended_starts_empty_input_at_midnight() { let base = "2000-01-01 10:11:12.123456789" .parse::() .unwrap() @@ -1039,7 +1032,7 @@ mod tests { let z = builder.build_extended().unwrap().expect_in_range(); assert_eq!( z.strftime("%Y-%m-%d %H:%M:%S%.9f").to_string(), - "2000-01-01 10:11:12.123456789" + "2000-01-01 00:00:00.000000000" ); } diff --git a/src/items/mod.rs b/src/items/mod.rs index ebf58e9..8c8a2e2 100644 --- a/src/items/mod.rs +++ b/src/items/mod.rs @@ -791,6 +791,9 @@ mod tests { .unwrap(); for (input, expected) in [ + ("", "2000-06-15 00:00:00+00:00"), + ("(foo)", "2000-06-15 00:00:00+00:00"), + ("(foo", "2000-06-15 00:00:00+00:00"), ("1997-01-01 (foo) 10:00", "1997-01-01 10:00:00+00:00"), ("1997-01-01 (a (b)) 10:00", "1997-01-01 10:00:00+00:00"), ("(foo) 1997-01-01", "1997-01-01 00:00:00+00:00"), diff --git a/src/items/primitive.rs b/src/items/primitive.rs index f8e0709..fb5377e 100644 --- a/src/items/primitive.rs +++ b/src/items/primitive.rs @@ -87,10 +87,11 @@ where .parse_next(input) } -/// Parse a comment +/// Parse a comment. /// -/// A comment is given between parentheses, which must be balanced. Any other -/// tokens can be within the comment. +/// A comment is given between parentheses. Any other tokens can be within the +/// comment. GNU's lexer treats an unmatched opening parenthesis as a comment +/// through the end of the input, so the closing parenthesis is optional here. fn comment<'a, E>(input: &mut &'a str) -> winnow::Result<(), E> where E: ParserError<&'a str>, @@ -98,7 +99,7 @@ where delimited( '(', repeat(0.., alt((none_of(['(', ')']).void(), comment))), - ')', + opt(')'), ) .parse_next(input) } @@ -245,8 +246,9 @@ mod tests { // A sign followed by a digit is not ignored. ("- 12", "- 12"), (" +12", "+12"), - // An unbalanced comment is not skipped. - ("(foo abc", "(foo abc"), + // An unbalanced comment runs through the end of the input. + ("(foo abc", ""), + ("(a (b", ""), ] { let mut s = input; space::(&mut s).unwrap(); diff --git a/tests/date.rs b/tests/date.rs index 0ef2f01..fe9a81b 100644 --- a/tests/date.rs +++ b/tests/date.rs @@ -88,6 +88,22 @@ fn test_whitespace_between_items(#[case] input: &str, #[case] expected: &str) { check_absolute(input, expected); } +// GNU treats an unmatched opening parenthesis as a comment through the end +// of the input, and comment-only input starts today at midnight. +#[rstest] +#[case::unterminated_after_date("2024-02-29 12:34:56 (unterminated", "2024-02-29 12:34:56+00:00")] +#[case::comment_only("(comment only)", "2000-06-15 00:00:00+00:00")] +#[case::unterminated_comment_only("(comment only", "2000-06-15 00:00:00+00:00")] +#[case::comment_before_date("(ignored) 1997-01-01", "1997-01-01 00:00:00+00:00")] +fn test_comments(#[case] input: &str, #[case] expected: &str) { + let base = "2000-06-15 12:34:56" + .parse::() + .unwrap() + .to_zoned_datetime(TimeZone::UTC) + .unwrap(); + check_relative(base, input, expected); +} + #[rstest] #[case::us_style("11/14", 2022, "2022-11-14 00:00:00+00:00")] #[case::alphabetical_full_month_in_front("november 14", 2022, "2022-11-14 00:00:00+00:00")]