Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/items/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ impl DateTimeBuilder {
let need_midnight = self.date.is_some()
|| self.time.is_some()
|| self.weekday.is_some()
|| (zone_seen && self.relative.is_empty());
|| (zone_seen && self.relative.is_empty())
// An empty date string (including one containing only comments or
// ignored signs) means the beginning of today. Explicit relative
// items such as `today` remain distinct and keep the current time.
|| self.relative.is_empty();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes the zone_seen && relative.is_empty() clause redundant, no?
could be simplified like in build_extended, and drop zone_seen/has_timezone (and update the step 3 comment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that got lost during rebase


let mut dt = if need_midnight {
base.with().time(civil::time(0, 0, 0, 0)).build()?
Expand Down Expand Up @@ -394,12 +398,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(tz),
(Some(b), None) => b,
Expand All @@ -410,11 +413,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")?,
Expand Down Expand Up @@ -1026,7 +1026,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::<DateTime>()
.unwrap()
Expand All @@ -1037,7 +1037,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"
);
}

Expand Down
3 changes: 3 additions & 0 deletions src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
13 changes: 7 additions & 6 deletions src/items/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,19 @@ 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>,
{
delimited(
'(',
repeat(0.., alt((none_of(['(', ')']).void(), comment))),
')',
opt(')'),
)
.parse_next(input)
}
Expand Down Expand Up @@ -245,8 +246,8 @@ 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", ""),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a nested unterminated case too, like (a (b

] {
let mut s = input;
space::<ContextError>(&mut s).unwrap();
Expand Down
16 changes: 16 additions & 0 deletions tests/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<DateTime>()
.unwrap()
.to_zoned(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")]
Expand Down
Loading