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
23 changes: 8 additions & 15 deletions src/items/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Comment thread
anastygnome marked this conversation as resolved.
Resolved

let mut dt = if need_midnight {
base.with().time(civil::time(0, 0, 0, 0)).build()?
Expand Down Expand Up @@ -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,
Expand All @@ -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")?,
Expand Down Expand Up @@ -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::<DateTime>()
.unwrap()
Expand All @@ -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"
);
}

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
14 changes: 8 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,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", ""),
Comment thread
anastygnome marked this conversation as resolved.
Resolved
("(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_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")]
Expand Down
Loading