You've already forked parse_datetime
mirror of
https://github.com/uutils/parse_datetime.git
synced 2026-06-10 16:13:15 -07:00
Merge pull request #114 from jfinkels/parse-empty-string
Allow empty string in parse_relative_time_at_date
This commit is contained in:
@@ -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:
|
||||
@@ -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*)?
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user