Skip to content

Various improvements to the docs and tests #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
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.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions fuzz/fuzz_targets/parse_datetime.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/parse_timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'a> From<NomError<'a>> for ParseTimestampError {
}
}

pub(crate) fn parse_timestamp(s: &str) -> Result<i64, ParseTimestampError> {
pub fn parse_timestamp(s: &str) -> Result<i64, ParseTimestampError> {
let s = s.trim().to_lowercase();
let s = s.as_str();

Expand Down
2 changes: 1 addition & 1 deletion src/parse_weekday.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ macro_rules! tag_match {
};
}

pub(crate) fn parse_weekday(s: &str) -> Option<Weekday> {
pub fn parse_weekday(s: &str) -> Option<Weekday> {
let s = s.trim().to_lowercase();
let s = s.as_str();

Expand Down
36 changes: 36 additions & 0 deletions tests/simple.rs
Original file line number Diff line number Diff line change
@@ -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);
}