diff --git a/src/parse_relative_time.rs b/src/parse_relative_time.rs index 689af5b..782949b 100644 --- a/src/parse_relative_time.rs +++ b/src/parse_relative_time.rs @@ -59,7 +59,7 @@ pub fn parse_relative_time_at_date( } let time_pattern: Regex = Regex::new( r"(?x) - (?:(?P[-+]?\d*)\s*)? + (?:(?P[-+]?\s*\d*)\s*)? (\s*(?Pnext|this|last)?\s*)? (?Pyears?|months?|fortnights?|weeks?|days?|hours?|h|minutes?|mins?|m|seconds?|secs?|s|yesterday|tomorrow|now|today) (\s*(?Pand|,)?\s*)? @@ -73,10 +73,13 @@ pub fn parse_relative_time_at_date( for capture in time_pattern.captures_iter(s) { captures_processed += 1; - let value_str = capture + let value_str: String = capture .name("value") .ok_or(ParseDateTimeError::InvalidInput)? - .as_str(); + .as_str() + .chars() + .filter(|c| !c.is_whitespace()) // Remove potential space between +/- and number + .collect(); let value = if value_str.is_empty() { 1 } else { @@ -510,6 +513,19 @@ mod tests { ); } + #[test] + fn test_spaces() { + let now = Utc::now(); + assert_eq!( + parse_relative_time_at_date(now, "+ 1 hour").unwrap(), + now.checked_add_signed(Duration::hours(1)).unwrap() + ); + assert_eq!( + parse_relative_time_at_date(now, "- 1 hour").unwrap(), + now.checked_sub_signed(Duration::hours(1)).unwrap() + ); + } + #[test] fn test_invalid_input() { let result = parse_duration("foobar");