diff --git a/Cargo.lock b/Cargo.lock index cd3b98a..24a9be7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,7 +151,7 @@ checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "parse_datetime" -version = "0.5.0" +version = "0.5.1" dependencies = [ "chrono", "nom", diff --git a/Cargo.toml b/Cargo.toml index 5255ddb..d56241d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "parse_datetime" description = "parsing human-readable time strings and converting them to a DateTime" -version = "0.5.0" +version = "0.5.1" edition = "2021" license = "MIT" repository = "https://github.com/uutils/parse_datetime" diff --git a/README.md b/README.md index 31a1147..f3b2f82 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Add this to your `Cargo.toml`: ```toml [dependencies] -parse_datetime = "0.4.0" +parse_datetime = "0.5.1" ``` Then, import the crate and use the `parse_datetime_at_date` function: @@ -41,11 +41,34 @@ assert_eq!( For DateTime parsing, import the `parse_datetime` module: ```rs -use parse_datetime::parse_datetime::from_str; use chrono::{Local, TimeZone}; +use parse_datetime::parse_relative_time; -let dt = from_str("2021-02-14 06:37:47"); -assert_eq!(dt.unwrap(), Local.with_ymd_and_hms(2021, 2, 14, 6, 37, 47).unwrap()); +let one_minute = parse_relative_time("1 minute").unwrap(); +assert_eq!( + one_minute, + Duration::seconds(60) +); +``` + +For timestamp parsing, import the `parse_timpestamp` module: + +```rs +use chrono::{Local, TimeZone}; +use parse_datetime::parse_timpestamp; + +let ts = parse_timestamp("@1234").unwrap(); +assert_eq!(ts, 1234); +``` + +For weekday parsing, import the `parse_weekday` module: + +```rs +use chrono::{Local, TimeZone}; +use parse_datetime::parse_weekday; + +let mon = parse_weekday("monday").unwrap(); +assert_eq!(mon, Weekday::Mon); ``` ### Supported Formats diff --git a/fuzz/fuzz_targets/parse_datetime.rs b/fuzz/fuzz_targets/parse_datetime.rs index 289bbb3..f3f98cb 100644 --- a/fuzz/fuzz_targets/parse_datetime.rs +++ b/fuzz/fuzz_targets/parse_datetime.rs @@ -1,3 +1,6 @@ +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + #![no_main] use libfuzzer_sys::fuzz_target; diff --git a/src/lib.rs b/src/lib.rs index de65b56..9c987c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,11 +12,11 @@ use regex::Error as RegexError; use std::error::Error; use std::fmt::{self, Display}; -// Expose parse_datetime -mod parse_relative_time; -mod parse_timestamp; +// Expose other features from this crate +pub mod parse_relative_time; +pub mod parse_timestamp; -mod parse_weekday; +pub mod parse_weekday; use chrono::{ DateTime, Datelike, Duration, FixedOffset, Local, LocalResult, NaiveDateTime, TimeZone, diff --git a/src/parse_timestamp.rs b/src/parse_timestamp.rs index 732558d..5e42c98 100644 --- a/src/parse_timestamp.rs +++ b/src/parse_timestamp.rs @@ -49,7 +49,7 @@ impl<'a> From> for ParseTimestampError { } } -pub(crate) fn parse_timestamp(s: &str) -> Result { +pub fn parse_timestamp(s: &str) -> Result { let s = s.trim().to_lowercase(); let s = s.as_str(); diff --git a/src/parse_weekday.rs b/src/parse_weekday.rs index d61aca7..23fb6fa 100644 --- a/src/parse_weekday.rs +++ b/src/parse_weekday.rs @@ -13,7 +13,7 @@ macro_rules! tag_match { }; } -pub(crate) fn parse_weekday(s: &str) -> Option { +pub fn parse_weekday(s: &str) -> Option { let s = s.trim().to_lowercase(); let s = s.as_str(); diff --git a/tests/simple.rs b/tests/simple.rs index 8b13789..8187ab1 100644 --- a/tests/simple.rs +++ b/tests/simple.rs @@ -1 +1,37 @@ +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. +use chrono::{Duration, Local, Weekday}; +use parse_datetime::{ + parse_datetime_at_date, parse_relative_time::parse_relative_time, + parse_timestamp::parse_timestamp, parse_weekday::parse_weekday, +}; + +#[test] +fn test_parse_datetime_at_date() { + let now = Local::now(); + let after = parse_datetime_at_date(now, "+3 days"); + + assert_eq!( + (now + Duration::days(3)).naive_utc(), + after.unwrap().naive_utc() + ); +} + +#[test] +fn test_parse_relative_time() { + let one_minute = parse_relative_time("1 minute").unwrap(); + assert_eq!(one_minute, Duration::seconds(60)); +} + +#[test] +fn test_parse_timestamp() { + let ts = parse_timestamp("@1234").unwrap(); + assert_eq!(ts, 1234); +} + +#[test] +fn test_weekday() { + let mon = parse_weekday("monday").unwrap(); + assert_eq!(mon, Weekday::Mon); +}