Skip to content

Commit d0dceb6

Browse files
authored
Merge pull request #114 from jfinkels/parse-empty-string
Allow empty string in parse_relative_time_at_date
2 parents 51ddcb7 + 45b9d9f commit d0dceb6

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/parse_relative_time.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const DAYS_PER_MONTH: [u32; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 3
2020
/// * `date` - A `Date` instance representing the base date for the calculation
2121
/// * `s` - A string slice representing the relative time.
2222
///
23+
/// If `s` is empty, the `date` is returned as-is.
24+
///
2325
/// # Supported formats
2426
///
2527
/// The function supports the following formats for relative time:
@@ -51,6 +53,10 @@ pub fn parse_relative_time_at_date<T: TimeZone>(
5153
mut datetime: DateTime<T>,
5254
s: &str,
5355
) -> Result<DateTime<T>, ParseDateTimeError> {
56+
let s = s.trim();
57+
if s.is_empty() {
58+
return Ok(datetime);
59+
}
5460
let time_pattern: Regex = Regex::new(
5561
r"(?x)
5662
(?:(?P<value>[-+]?\d*)\s*)?
@@ -278,6 +284,12 @@ mod tests {
278284
Ok(parsed - now)
279285
}
280286

287+
#[test]
288+
fn test_empty_string() {
289+
let now = Utc::now();
290+
assert_eq!(parse_relative_time_at_date(now, "").unwrap(), now);
291+
}
292+
281293
#[test]
282294
fn test_years() {
283295
let now = Utc::now();

0 commit comments

Comments
 (0)