diff --git a/README.md b/README.md index 9cab9aa..92dcf13 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ The `from_str` and `from_str_at_date` functions support the following formats fo - "yesterday" - "tomorrow" - use "ago" for the past +- use "next" or "last" with `unit` (e.g., "next week", "last year") - combined units with "and" or "," (e.g., "2 years and 1 month", "1 day, 2 hours" or "2 weeks 1 second") `num` can be a positive or negative integer. diff --git a/src/lib.rs b/src/lib.rs index 8a104da..614b355 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -124,6 +124,7 @@ pub fn from_str_at_date(date: NaiveDate, s: &str) -> Result[-+]?\d*)\s*)? + (\s*(?Pnext|last)?\s*)? (?Pyears?|months?|fortnights?|weeks?|days?|hours?|h|minutes?|mins?|m|seconds?|secs?|s|yesterday|tomorrow|now|today) (\s*(?Pand|,)?\s*)? (\s*(?Pago)?)?", @@ -148,6 +149,13 @@ pub fn from_str_at_date(date: NaiveDate, s: &str) -> Result() .map_err(|_| ParseDurationError::InvalidInput)? }; + + if let Some(direction) = capture.name("direction") { + if direction.as_str() == "last" { + is_ago = true; + } + } + let unit = capture .name("unit") .ok_or(ParseDurationError::InvalidInput)? @@ -366,4 +374,12 @@ mod tests { Err(ParseDurationError::InvalidInput) )); } + + #[test] + fn test_direction() { + assert_eq!(from_str("last hour").unwrap(), Duration::seconds(-3600)); + assert_eq!(from_str("next year").unwrap(), Duration::days(365)); + assert_eq!(from_str("next week").unwrap(), Duration::days(7)); + assert_eq!(from_str("last month").unwrap(), Duration::days(-30)); + } }