You've already forked parse_datetime
mirror of
https://github.com/uutils/parse_datetime.git
synced 2026-06-10 16:13:15 -07:00
from_str('Thu Jan 01 12:34:00 2015') should fail
This commit is contained in:
+19
-4
@@ -80,15 +80,20 @@ impl From<RegexError> for ParseDurationError {
|
||||
pub fn from_str(s: &str) -> Result<Duration, ParseDurationError> {
|
||||
let time_pattern: Regex = Regex::new(
|
||||
r"(?x)
|
||||
(?P<value>[-+]?\d*)\s*
|
||||
(?P<unit>years?|months?|fortnights?|weeks?|days?|hours?|h|minutes?|mins?|m|seconds?|secs?|s|yesterday|tomorrow|now|today)
|
||||
(\s*(?P<separator>and|,)?\s*)?",
|
||||
(?:(?P<value>[-+]?\d*)\s*)?
|
||||
(?P<unit>years?|months?|fortnights?|weeks?|days?|hours?|h|minutes?|mins?|m|seconds?|secs?|s|yesterday|tomorrow|now|today)
|
||||
(\s*(?P<separator>and|,)?\s*)?
|
||||
(\s*(?P<ago>ago)?)?",
|
||||
)?;
|
||||
|
||||
let mut total_duration = Duration::ZERO;
|
||||
let mut is_ago = s.contains(" ago");
|
||||
let mut captures_processed = 0;
|
||||
let mut total_length = 0;
|
||||
|
||||
for capture in time_pattern.captures_iter(s) {
|
||||
captures_processed += 1;
|
||||
|
||||
let value_str = capture
|
||||
.name("value")
|
||||
.ok_or(ParseDurationError::InvalidInput)?
|
||||
@@ -129,9 +134,19 @@ pub fn from_str(s: &str) -> Result<Duration, ParseDurationError> {
|
||||
Some(duration) => duration,
|
||||
None => return Err(ParseDurationError::InvalidInput),
|
||||
};
|
||||
|
||||
// Calculate the total length of the matched substring
|
||||
if let Some(m) = capture.get(0) {
|
||||
total_length += m.end() - m.start();
|
||||
}
|
||||
}
|
||||
|
||||
if total_duration == Duration::ZERO && !time_pattern.is_match(s) {
|
||||
// Check if the entire input string has been captured
|
||||
if total_length != s.len() {
|
||||
return Err(ParseDurationError::InvalidInput);
|
||||
}
|
||||
|
||||
if captures_processed == 0 {
|
||||
Err(ParseDurationError::InvalidInput)
|
||||
} else {
|
||||
Ok(total_duration)
|
||||
|
||||
+12
-1
@@ -120,7 +120,18 @@ fn test_display_parse_duration_error_through_from_str() {
|
||||
let error = from_str(invalid_input).unwrap_err();
|
||||
|
||||
assert_eq!(
|
||||
format!("{}", error),
|
||||
format!("{error}"),
|
||||
"Invalid input string: cannot be parsed as a relative time"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_display_should_fail() {
|
||||
let invalid_input = "Thu Jan 01 12:34:00 2015";
|
||||
let error = from_str(invalid_input).unwrap_err();
|
||||
|
||||
assert_eq!(
|
||||
format!("{error}"),
|
||||
"Invalid input string: cannot be parsed as a relative time"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user