Skip to content
Merged
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
22 changes: 19 additions & 3 deletions src/parse_relative_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn parse_relative_time_at_date<T: TimeZone>(
}
let time_pattern: Regex = Regex::new(
r"(?x)
(?:(?P<value>[-+]?\d*)\s*)?
(?:(?P<value>[-+]?\s*\d*)\s*)?
(\s*(?P<direction>next|this|last)?\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*)?
Expand All @@ -73,10 +73,13 @@ pub fn parse_relative_time_at_date<T: TimeZone>(
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 {
Expand Down Expand Up @@ -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");
Expand Down