-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#3505 Add date modification parsing #5476
base: main
Are you sure you want to change the base?
Conversation
GNU testsuite comparison:
|
src/uu/touch/src/touch.rs
Outdated
return match date_from_modifier(modifier, parsed) { | ||
Ok(new_date) => Ok(datetime_to_filetime(&new_date.and_utc())), | ||
Err(e) => Err(e), | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it could be simplified with:
return match date_from_modifier(modifier, parsed) { | |
Ok(new_date) => Ok(datetime_to_filetime(&new_date.and_utc())), | |
Err(e) => Err(e), | |
}; | |
return date_from_modifier(modifier, parsed).map(|new_date| datetime_to_filetime(&new_date.and_utc())); |
src/uu/touch/src/touch.rs
Outdated
if let Ok((parsed, modifier)) = NaiveDateTime::parse_and_remainder(s, fmt) { | ||
if modifier.is_empty() { | ||
return Ok(datetime_to_filetime(&parsed.and_utc())); | ||
} | ||
return match date_from_modifier(modifier, parsed) { | ||
Ok(new_date) => Ok(datetime_to_filetime(&new_date.and_utc())), | ||
Err(e) => Err(e), | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can probably be simplified too by something like
if let Ok((parsed, modifier)) = NaiveDateTime::parse_and_remainder(s, fmt) { | |
if modifier.is_empty() { | |
return Ok(datetime_to_filetime(&parsed.and_utc())); | |
} | |
return match date_from_modifier(modifier, parsed) { | |
Ok(new_date) => Ok(datetime_to_filetime(&new_date.and_utc())), | |
Err(e) => Err(e), | |
}; | |
if let Ok((parsed, modifier)) = NaiveDateTime::parse_and_remainder(s, fmt) { | |
return if modifier.is_empty() { | |
Ok(datetime_to_filetime(&parsed.and_utc())) | |
} else { | |
date_from_modifier(modifier, parsed).map(|new_date| datetime_to_filetime(&new_date.and_utc())) | |
}; | |
} | |
src/uu/touch/src/touch.rs
Outdated
if modifier.is_empty() { | ||
return Ok(datetime_to_filetime(&parsed)); | ||
} | ||
return match date_from_modifier(modifier, parsed) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same, Err(e) => Err(e),
can often be simplified
Lot of work probably went into this, bravo |
)); | ||
} | ||
date = if time >= 0 { | ||
date.add(Months::new((12 * time) as u32)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
codecov suggests that this line isn't covered, could you please fix it? thanks
if i >= bytes.len() { | ||
break; | ||
} | ||
if let n @ 48..=57 = bytes[i] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please replace 48 & 57 by variable names
} | ||
|
||
fn parse_num(&mut self) -> Result<i64, ParseError> { | ||
while self.cursor < self.haystack.len() && self.haystack[self.cursor].is_ascii_whitespace() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please create a function:
fn skip_whitespace(&mut self) {
while self.cursor < self.haystack.len() && self.haystack[self.cursor].is_ascii_whitespace() {
self.cursor += 1;
}
}
} | ||
|
||
fn parse_unit(&mut self) -> Result<ChronoUnit, ParseError> { | ||
while self.cursor < self.haystack.len() && self.haystack[self.cursor].is_ascii_whitespace() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and replace this one too
} | ||
let bytes = &self.haystack[self.cursor..].to_ascii_lowercase(); | ||
match bytes[0] { | ||
b'd' => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i can probably by simplified by something like:
let units = [
(b'd', "day", ChronoUnit::Day),
(b'w', "week", ChronoUnit::Week),
(b'm', "month", ChronoUnit::Month),
(b'y', "year", ChronoUnit::Year),
(b'h', "hour", ChronoUnit::Hour),
(b's', "second", ChronoUnit::Second),
];
for &(byte, unit_str, chrono_unit) in &units {
if bytes.starts_with(unit_str) {
self.cursor += unit_str.len();
return Ok(chrono_unit);
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for reviewing this PR. I can implement the changes later this evening.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sylvestre, you seem to have a pretty good grasp on this already. Shouldn't this be fixed in parse_datetime
?
I actually did consider this, but I figured it is not technically parsing a date but a modifier to a date. I can move the changes over to |
Yes, that's what parse datetime is for. It's supposed to parse the format described here: https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html, which includes this part I believe. Porting it would be great! |
I made the suggested changes including adding integration testing. I also increased testing to satisfy the code coverage bot, however I'm not sure I was able to get everything. I apologize in advance if the I did something incorrectly; I don't know git very well. |
what do you think about moving it into https://github.com/uutils/parse_datetime/ ? |
Oh, I see. I misunderstood @tertsdiepraam. Yes, I'm willing to move my work to One observation is that In short, I just need to a day or two to reorient myself. But I am confident I can figure it out since I already gained a decent understanding while working on this PR. |
@sylvestre @tertsdiepraam I ran into some interesting issues while working to transfer this PR to |
@dcechano sorry but do you have an update ? thanks |
Yes sir. I am very sorry for the long delay but working out a solution that covers edge cases turned out to be quite challenging. I have been working in the parse_datetime repo as suggested and getting a PR ready. What has been taking so long is that I had to implement the core logic (which was challenging in its own right) then integrate it into the larger library via After the PR, which I have set a hard goal of getting in by the end of this week, |
@sylvestre & @tertsdiepraam I have a PR in that is a major step in addressing #3505. I'm looking forward to hearing what you guys think. Thank you! |
This commit addresses #3505. I added a
parse_date_modification.rs
module touucore::parser
and used it intouch.rs
. I added tests totouch.rs
to prove the parser works and is not buggy. In theory these changes can also help with similar issues indate.rs
but I wanted to keep the PR to one issue at a time.Thank you for considering my pull request and I welcome feedback if you guys have any.