Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 34 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl From<RegexError> for ParseDurationError {
/// * "tomorrow"
/// * use "ago" for the past
///
/// [num] can be a positive or negative integer.
/// `[num]` can be a positive or negative integer.
/// [unit] can be one of the following: "fortnight", "week", "day", "hour",
/// "minute", "min", "second", "sec" and their plural forms.
///
Expand All @@ -89,6 +89,35 @@ impl From<RegexError> for ParseDurationError {
/// assert!(matches!(from_str("invalid"), Err(ParseDurationError::InvalidInput)));
/// ```
pub fn from_str(s: &str) -> Result<Duration, ParseDurationError> {
from_str_at_date(OffsetDateTime::now_utc().date(), s)
}

/// Parses a duration string and returns a `Duration` instance, with the duration
/// calculated from the specified date.
///
/// # Arguments
///
/// * `date` - A `Date` instance representing the base date for the calculation
/// * `s` - A string slice representing the relative time.
///
/// # Errors
///
/// This function will return `Err(ParseDurationError::InvalidInput)` if the input string
/// cannot be parsed as a relative time.
///
/// # Examples
///
/// ```
/// use time::{Date, Duration, OffsetDateTime};
/// use humantime_to_duration::{from_str_at_date, ParseDurationError};
/// let today = OffsetDateTime::now_utc().date();
/// let yesterday = today - Duration::days(1);
/// assert_eq!(
/// from_str_at_date(yesterday, "2 days").unwrap(),
/// Duration::days(1) // 1 day from the specified date + 1 day from the input string
/// );
/// ```
pub fn from_str_at_date(date: Date, s: &str) -> Result<Duration, ParseDurationError> {
let time_pattern: Regex = Regex::new(
r"(?x)
(?:(?P<value>[-+]?\d*)\s*)?
Expand Down Expand Up @@ -160,35 +189,11 @@ pub fn from_str(s: &str) -> Result<Duration, ParseDurationError> {
if captures_processed == 0 {
Err(ParseDurationError::InvalidInput)
} else {
Ok(total_duration)
}
}

/// Parses a duration string and returns a `Duration` instance, with the duration
/// calculated from the specified date.
///
/// # Arguments
///
/// * `date` - A `Date` instance representing the base date for the calculation
/// * `s` - A string slice representing the relative time.
///
/// # Examples
///
/// ```
/// use time::{Date, Duration, OffsetDateTime};
/// use humantime_to_duration::{from_str_at_date, ParseDurationError};
/// let today = OffsetDateTime::now_utc().date();
/// let yesterday = today - Duration::days(1);
/// assert_eq!(
/// from_str_at_date(yesterday, "2 days").unwrap(),
/// Duration::days(1) // 1 day from the specified date + 1 day from the input string
/// );
/// ```
pub fn from_str_at_date(date: Date, s: &str) -> Result<Duration, ParseDurationError> {
let time_now = OffsetDateTime::now_utc().date();
let date_duration = date - time_now;
let time_now = OffsetDateTime::now_utc().date();
let date_duration = date - time_now;

Ok(from_str(s)? + date_duration)
Ok(total_duration + date_duration)
}
}

#[cfg(test)]
Expand Down