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
12 changes: 12 additions & 0 deletions src/parse_relative_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const DAYS_PER_MONTH: [u32; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 3
/// * `date` - A `Date` instance representing the base date for the calculation
/// * `s` - A string slice representing the relative time.
///
/// If `s` is empty, the `date` is returned as-is.
///
/// # Supported formats
///
/// The function supports the following formats for relative time:
Expand Down Expand Up @@ -51,6 +53,10 @@ pub fn parse_relative_time_at_date<T: TimeZone>(
mut datetime: DateTime<T>,
s: &str,
) -> Result<DateTime<T>, ParseDateTimeError> {
let s = s.trim();
if s.is_empty() {
return Ok(datetime);
}
let time_pattern: Regex = Regex::new(
r"(?x)
(?:(?P<value>[-+]?\d*)\s*)?
Expand Down Expand Up @@ -278,6 +284,12 @@ mod tests {
Ok(parsed - now)
}

#[test]
fn test_empty_string() {
let now = Utc::now();
assert_eq!(parse_relative_time_at_date(now, "").unwrap(), now);
}

#[test]
fn test_years() {
let now = Utc::now();
Expand Down