Skip to content

Commit 2737b4a

Browse files
Merge pull request #33 from philolo1/fix/datetime-should-parse-relative
Make parse_datetime::parse_datetime::from_str accept relative time
2 parents a738050 + f10749e commit 2737b4a

File tree

11 files changed

+955
-767
lines changed

11 files changed

+955
-767
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,4 @@ jobs:
181181
run: |
182182
## Run it
183183
cd fuzz
184-
cargo +nightly fuzz run fuzz_from_str -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
185-
- name: Run fuzz_parse_datetime_from_str for XX seconds
186-
shell: bash
187-
run: |
188-
## Run it
189-
cd fuzz
190-
cargo +nightly fuzz run fuzz_parse_datetime_from_str -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0
184+
cargo +nightly fuzz run fuzz_parse_datetime -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "parse_datetime"
3-
description = " parsing human-readable relative time strings and converting them to a Duration"
3+
description = "parsing human-readable time strings and converting them to a DateTime"
44
version = "0.4.0"
55
edition = "2021"
66
license = "MIT"

README.md

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/uutils/parse_datetime/blob/main/LICENSE)
55
[![CodeCov](https://codecov.io/gh/uutils/parse_datetime/branch/main/graph/badge.svg)](https://codecov.io/gh/uutils/parse_datetime)
66

7-
A Rust crate for parsing human-readable relative time strings and converting them to a `Duration`, or parsing human-readable datetime strings and converting them to a `DateTime`.
7+
A Rust crate for parsing human-readable relative time strings and human-readable datetime strings and converting them to a `DateTime`.
88

99
## Features
1010

@@ -23,23 +23,23 @@ Add this to your `Cargo.toml`:
2323
parse_datetime = "0.4.0"
2424
```
2525

26-
Then, import the crate and use the `from_str` and `from_str_at_date` functions:
26+
Then, import the crate and use the `parse_datetime_at_date` function:
27+
2728
```rs
28-
use parse_datetime::{from_str, from_str_at_date};
29-
use chrono::Duration;
29+
use chrono::{Duration, Local};
30+
use parse_datetime::parse_datetime_at_date;
3031

31-
let duration = from_str("+3 days");
32-
assert_eq!(duration.unwrap(), Duration::days(3));
32+
let now = Local::now();
33+
let after = parse_datetime_at_date(now, "+3 days");
3334

34-
let today = Utc::today().naive_utc();
35-
let yesterday = today - Duration::days(1);
3635
assert_eq!(
37-
from_str_at_date(yesterday, "2 days").unwrap(),
38-
Duration::days(1)
36+
(now + Duration::days(3)).naive_utc(),
37+
after.unwrap().naive_utc()
3938
);
4039
```
4140

4241
For DateTime parsing, import the `parse_datetime` module:
42+
4343
```rs
4444
use parse_datetime::parse_datetime::from_str;
4545
use chrono::{Local, TimeZone};
@@ -50,7 +50,7 @@ assert_eq!(dt.unwrap(), Local.with_ymd_and_hms(2021, 2, 14, 6, 37, 47).unwrap())
5050

5151
### Supported Formats
5252

53-
The `from_str` and `from_str_at_date` functions support the following formats for relative time:
53+
The `parse_datetime` and `parse_datetime_at_date` functions support absolute datetime and the ollowing relative times:
5454

5555
- `num` `unit` (e.g., "-1 hour", "+3 days")
5656
- `unit` (e.g., "hour", "day")
@@ -60,34 +60,28 @@ The `from_str` and `from_str_at_date` functions support the following formats fo
6060
- use "ago" for the past
6161
- use "next" or "last" with `unit` (e.g., "next week", "last year")
6262
- combined units with "and" or "," (e.g., "2 years and 1 month", "1 day, 2 hours" or "2 weeks 1 second")
63+
- unix timestamps (for example "@0" "@1344000")
6364

6465
`num` can be a positive or negative integer.
6566
`unit` can be one of the following: "fortnight", "week", "day", "hour", "minute", "min", "second", "sec" and their plural forms.
6667

6768
## Return Values
6869

69-
### Duration
70-
71-
The `from_str` and `from_str_at_date` functions return:
72-
73-
- `Ok(Duration)` - If the input string can be parsed as a relative time
74-
- `Err(ParseDurationError)` - If the input string cannot be parsed as a relative time
70+
### parse_datetime and parse_datetime_at_date
7571

76-
This function will return `Err(ParseDurationError::InvalidInput)` if the input string
77-
cannot be parsed as a relative time.
78-
79-
### parse_datetime
80-
81-
The `from_str` function returns:
72+
The `parse_datetime` and `parse_datetime_at_date` function return:
8273

8374
- `Ok(DateTime<FixedOffset>)` - If the input string can be parsed as a datetime
84-
- `Err(ParseDurationError::InvalidInput)` - If the input string cannot be parsed
75+
- `Err(ParseDateTimeError::InvalidInput)` - If the input string cannot be parsed
8576

8677
## Fuzzer
8778

8879
To run the fuzzer:
80+
8981
```
90-
$ cargo fuzz run fuzz_from_str
82+
$ cd fuzz
83+
$ cargo install cargo-fuzz
84+
$ cargo +nightly fuzz run fuzz_parse_datetime
9185
```
9286

9387
## License

fuzz/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
corpus

fuzz/Cargo.toml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "fuzz_from_str"
2+
name = "fuzz_parse_datetime"
33
version = "0.1.0"
44
edition = "2018"
55

@@ -16,13 +16,7 @@ chrono = "0.4"
1616
path = "../"
1717

1818
[[bin]]
19-
name = "fuzz_from_str"
20-
path = "fuzz_targets/from_str.rs"
21-
test = false
22-
doc = false
23-
24-
[[bin]]
25-
name = "fuzz_parse_datetime_from_str"
26-
path = "fuzz_targets/parse_datetime_from_str.rs"
19+
name = "fuzz_parse_datetime"
20+
path = "fuzz_targets/parse_datetime.rs"
2721
test = false
2822
doc = false

fuzz/fuzz_targets/from_str.rs renamed to fuzz/fuzz_targets/parse_datetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ use libfuzzer_sys::fuzz_target;
44

55
fuzz_target!(|data: &[u8]| {
66
let s = std::str::from_utf8(data).unwrap_or("");
7-
let _ = parse_datetime::from_str(s);
7+
let _ = parse_datetime::parse_datetime(s);
88
});

fuzz/fuzz_targets/parse_datetime_from_str.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)