You've already forked parse_datetime
mirror of
https://github.com/uutils/parse_datetime.git
synced 2026-06-10 16:13:15 -07:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bf6f14bc09 | |||
| 6a52551dea | |||
| 21e7e01fc8 | |||
| 1359dc9349 | |||
| daec32d5c4 | |||
| 7a3d0bafca | |||
| 537df73bdd | |||
| 3e4c6e3b6c |
@@ -0,0 +1 @@
|
||||
target/
|
||||
Generated
+1
-1
@@ -13,7 +13,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "humantime_to_duration"
|
||||
version = "0.1.4"
|
||||
version = "0.2.1"
|
||||
dependencies = [
|
||||
"regex",
|
||||
"time",
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "humantime_to_duration"
|
||||
description = " parsing human-readable relative time strings and converting them to a Duration"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
repository = "https://github.com/uutils/humantime_to_duration"
|
||||
|
||||
@@ -19,7 +19,7 @@ Add this to your `Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
humantime_to_duration = "0.1.1"
|
||||
humantime_to_duration = "0.2.1"
|
||||
```
|
||||
|
||||
Then, import the crate and use the `from_str` and `from_str_at_date` functions:
|
||||
|
||||
+35
-30
@@ -6,7 +6,7 @@ use std::error::Error;
|
||||
use std::fmt::{self, Display};
|
||||
use time::{Date, Duration, OffsetDateTime};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum ParseDurationError {
|
||||
InvalidRegex(RegexError),
|
||||
InvalidInput,
|
||||
@@ -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.
|
||||
///
|
||||
@@ -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*)?
|
||||
@@ -160,37 +189,13 @@ pub fn from_str(s: &str) -> Result<Duration, ParseDurationError> {
|
||||
if captures_processed == 0 {
|
||||
Err(ParseDurationError::InvalidInput)
|
||||
} else {
|
||||
Ok(total_duration)
|
||||
let time_now = OffsetDateTime::now_utc().date();
|
||||
let date_duration = date - time_now;
|
||||
|
||||
Ok(total_duration + date_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;
|
||||
|
||||
Ok(from_str(s)? + date_duration)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
|
||||
+4
-16
@@ -5,16 +5,10 @@ use time::{Duration, OffsetDateTime};
|
||||
fn test_invalid_input() {
|
||||
let result = from_str("foobar");
|
||||
println!("{result:?}");
|
||||
match result {
|
||||
Err(ParseDurationError::InvalidInput) => assert!(true),
|
||||
_ => assert!(false),
|
||||
}
|
||||
assert_eq!(result, Err(ParseDurationError::InvalidInput));
|
||||
|
||||
let result = from_str("invalid 1");
|
||||
match result {
|
||||
Err(ParseDurationError::InvalidInput) => assert!(true),
|
||||
_ => assert!(false),
|
||||
}
|
||||
assert_eq!(result, Err(ParseDurationError::InvalidInput));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -151,14 +145,8 @@ fn test_invalid_input_at_date() {
|
||||
let today = OffsetDateTime::now_utc().date();
|
||||
let result = from_str_at_date(today, "foobar");
|
||||
println!("{result:?}");
|
||||
match result {
|
||||
Err(ParseDurationError::InvalidInput) => assert!(true),
|
||||
_ => assert!(false),
|
||||
}
|
||||
assert_eq!(result, Err(ParseDurationError::InvalidInput));
|
||||
|
||||
let result = from_str_at_date(today, "invalid 1r");
|
||||
match result {
|
||||
Err(ParseDurationError::InvalidInput) => assert!(true),
|
||||
_ => assert!(false),
|
||||
}
|
||||
assert_eq!(result, Err(ParseDurationError::InvalidInput));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user