Skip to content

Commit e7ed14e

Browse files
Allow spaces between +/- and number (#129)
* Allow spaces between +/- and number * Remove space to make fmt job pass --------- Co-authored-by: Daniel Hofstetter <[email protected]>
1 parent 9184428 commit e7ed14e

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/parse_relative_time.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn parse_relative_time_at_date<T: TimeZone>(
5959
}
6060
let time_pattern: Regex = Regex::new(
6161
r"(?x)
62-
(?:(?P<value>[-+]?\d*)\s*)?
62+
(?:(?P<value>[-+]?\s*\d*)\s*)?
6363
(\s*(?P<direction>next|this|last)?\s*)?
6464
(?P<unit>years?|months?|fortnights?|weeks?|days?|hours?|h|minutes?|mins?|m|seconds?|secs?|s|yesterday|tomorrow|now|today)
6565
(\s*(?P<separator>and|,)?\s*)?
@@ -73,10 +73,13 @@ pub fn parse_relative_time_at_date<T: TimeZone>(
7373
for capture in time_pattern.captures_iter(s) {
7474
captures_processed += 1;
7575

76-
let value_str = capture
76+
let value_str: String = capture
7777
.name("value")
7878
.ok_or(ParseDateTimeError::InvalidInput)?
79-
.as_str();
79+
.as_str()
80+
.chars()
81+
.filter(|c| !c.is_whitespace()) // Remove potential space between +/- and number
82+
.collect();
8083
let value = if value_str.is_empty() {
8184
1
8285
} else {
@@ -510,6 +513,19 @@ mod tests {
510513
);
511514
}
512515

516+
#[test]
517+
fn test_spaces() {
518+
let now = Utc::now();
519+
assert_eq!(
520+
parse_relative_time_at_date(now, "+ 1 hour").unwrap(),
521+
now.checked_add_signed(Duration::hours(1)).unwrap()
522+
);
523+
assert_eq!(
524+
parse_relative_time_at_date(now, "- 1 hour").unwrap(),
525+
now.checked_sub_signed(Duration::hours(1)).unwrap()
526+
);
527+
}
528+
513529
#[test]
514530
fn test_invalid_input() {
515531
let result = parse_duration("foobar");

0 commit comments

Comments
 (0)