Skip to content

Commit ce6ea28

Browse files
committed
Refactor parse_datetime to only expose parse_datetime function
Create new file parse_relative_time.rs with the relative time helper function. Renames from_str to parse_datetime and parse_relative time. Adds function parse_datetime_at_date.
1 parent b615eff commit ce6ea28

File tree

12 files changed

+967
-739
lines changed

12 files changed

+967
-739
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 relative and absolute time strings and converting them to a DateTime"
44
version = "0.4.0"
55
edition = "2021"
66
license = "MIT"

README.md

Lines changed: 24 additions & 29 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,24 @@ 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;
30-
31-
let duration = from_str("+3 days");
32-
assert_eq!(duration.unwrap(), Duration::days(3));
33-
34-
let today = Utc::today().naive_utc();
35-
let yesterday = today - Duration::days(1);
36-
assert_eq!(
37-
from_str_at_date(yesterday, "2 days").unwrap(),
38-
Duration::days(1)
39-
);
29+
use chrono::{Duration, Local};
30+
use parse_datetime::parse_datetime_at_date;
31+
32+
let now = Local::now();
33+
let after = parse_datetime_at_date(now, "+3 days");
34+
35+
assert_eq!(
36+
(now + Duration::days(3)).naive_utc(),
37+
after.unwrap().naive_utc()
38+
);
39+
4040
```
4141

4242
For DateTime parsing, import the `parse_datetime` module:
43+
4344
```rs
4445
use parse_datetime::parse_datetime::from_str;
4546
use chrono::{Local, TimeZone};
@@ -50,7 +51,7 @@ assert_eq!(dt.unwrap(), Local.with_ymd_and_hms(2021, 2, 14, 6, 37, 47).unwrap())
5051

5152
### Supported Formats
5253

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

5556
- `num` `unit` (e.g., "-1 hour", "+3 days")
5657
- `unit` (e.g., "hour", "day")
@@ -60,34 +61,28 @@ The `from_str` and `from_str_at_date` functions support the following formats fo
6061
- use "ago" for the past
6162
- use "next" or "last" with `unit` (e.g., "next week", "last year")
6263
- combined units with "and" or "," (e.g., "2 years and 1 month", "1 day, 2 hours" or "2 weeks 1 second")
64+
- unix timestamps (for example "@0" "@1344000")
6365

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

6769
## Return Values
6870

69-
### Duration
71+
### parse_datetime and parse_datetime_at_date
7072

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
75-
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:
73+
The `parse_datetime` and `parse_datetime_at_date` function return:
8274

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

8678
## Fuzzer
8779

8880
To run the fuzzer:
81+
8982
```
90-
$ cargo fuzz run fuzz_from_str
83+
$ cd fuzz
84+
$ cargo install cargo-fuzz
85+
$ cargo +nightly fuzz run fuzz_parse_datetime
9186
```
9287

9388
## License

fuzz/.gitignore

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

0 commit comments

Comments
 (0)