Skip to content

Commit 7773727

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 efee20b commit 7773727

File tree

7 files changed

+883
-646
lines changed

7 files changed

+883
-646
lines changed

README.md

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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,26 @@ 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 prsed as a datetime
8476
- `Err(ParseDurationError::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+
$ cargo fuzz run fuzz_parse_datetime
9184
```
9285

9386
## License

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)