2023-06-09 10:15:23 +02:00
|
|
|
# parse_datetime
|
2023-04-23 18:34:54 +02:00
|
|
|
|
2023-06-09 10:15:23 +02:00
|
|
|
[](https://crates.io/crates/parse_datetime)
|
|
|
|
|
[](https://github.com/uutils/parse_datetime/blob/main/LICENSE)
|
|
|
|
|
[](https://codecov.io/gh/uutils/parse_datetime)
|
2026-03-31 23:48:09 +02:00
|
|
|
[](https://codspeed.io/uutils/parse_datetime?utm_source=badge)
|
2023-04-23 20:39:08 +02:00
|
|
|
|
2026-03-31 03:51:07 -04:00
|
|
|
A Rust crate for parsing human-readable relative time strings and
|
|
|
|
|
human-readable datetime strings.
|
2023-04-23 18:34:54 +02:00
|
|
|
|
|
|
|
|
## Features
|
|
|
|
|
|
2023-06-06 10:05:14 -07:00
|
|
|
- Parses a variety of human-readable and standard time formats.
|
2023-04-23 18:34:54 +02:00
|
|
|
- Supports positive and negative durations.
|
2025-07-24 14:13:01 +00:00
|
|
|
- Allows for chaining time units (e.g., "1 hour 2 minutes" or "2 days 2 hours ago").
|
2023-05-11 17:16:02 +02:00
|
|
|
- Calculate durations relative to a specified date.
|
2025-08-16 23:17:41 +08:00
|
|
|
- Relies on Jiff
|
2023-04-23 18:34:54 +02:00
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
2025-02-17 14:51:34 +01:00
|
|
|
Add `parse_datetime` to your `Cargo.toml` with:
|
2023-04-23 18:34:54 +02:00
|
|
|
|
2025-02-17 11:24:36 +01:00
|
|
|
```
|
|
|
|
|
cargo add parse_datetime
|
2023-04-23 18:34:54 +02:00
|
|
|
```
|
|
|
|
|
|
2023-08-23 08:51:58 +09:00
|
|
|
Then, import the crate and use the `parse_datetime_at_date` function:
|
|
|
|
|
|
2023-06-05 20:22:57 +08:00
|
|
|
```rs
|
2025-08-16 23:17:41 +08:00
|
|
|
use jiff::{ToSpan, Zoned};
|
2026-03-31 03:51:07 -04:00
|
|
|
use parse_datetime::{parse_datetime_at_date, ParsedDateTime};
|
2023-04-23 18:34:54 +02:00
|
|
|
|
2025-08-16 23:17:41 +08:00
|
|
|
let now = Zoned::now();
|
|
|
|
|
let after = parse_datetime_at_date(now.clone(), "+3 days");
|
2023-05-11 17:16:02 +02:00
|
|
|
|
2026-03-31 03:51:07 -04:00
|
|
|
match after.unwrap() {
|
|
|
|
|
ParsedDateTime::InRange(z) => assert_eq!(now.checked_add(3.days()).unwrap(), z),
|
|
|
|
|
ParsedDateTime::Extended(_) => unreachable!("unexpected for this input"),
|
|
|
|
|
}
|
2023-04-23 18:34:54 +02:00
|
|
|
```
|
|
|
|
|
|
2024-01-31 15:13:23 -08:00
|
|
|
For DateTime parsing, import the `parse_datetime` function:
|
2023-08-23 08:51:58 +09:00
|
|
|
|
2023-06-07 15:01:25 +02:00
|
|
|
```rs
|
2025-08-16 23:17:41 +08:00
|
|
|
use jiff::{civil::{date, time} ,Zoned};
|
2026-03-31 03:51:07 -04:00
|
|
|
use parse_datetime::{parse_datetime, ParsedDateTime};
|
2023-06-06 10:05:14 -07:00
|
|
|
|
2024-01-31 15:13:23 -08:00
|
|
|
let dt = parse_datetime("2021-02-14 06:37:47");
|
2026-03-31 03:51:07 -04:00
|
|
|
match dt.unwrap() {
|
|
|
|
|
ParsedDateTime::InRange(z) => assert_eq!(z, Zoned::now().with().date(date(2021, 2, 14)).time(time(6, 37, 47, 0)).build().unwrap()),
|
|
|
|
|
ParsedDateTime::Extended(_) => unreachable!("unexpected for this input"),
|
|
|
|
|
}
|
2023-06-06 10:05:14 -07:00
|
|
|
```
|
|
|
|
|
|
2026-03-31 10:23:17 +02:00
|
|
|
For years beyond jiff's representable range (e.g., year 10000+), the result is an `ExtendedDateTime`:
|
|
|
|
|
|
|
|
|
|
```rs
|
|
|
|
|
use parse_datetime::{parse_datetime, ParsedDateTime};
|
|
|
|
|
|
|
|
|
|
let dt = parse_datetime("12000-01-01").unwrap();
|
|
|
|
|
match dt {
|
|
|
|
|
ParsedDateTime::Extended(ext) => {
|
|
|
|
|
assert_eq!(ext.year, 12000);
|
2026-04-01 10:01:47 +02:00
|
|
|
assert_eq!(ext.month, 1);
|
|
|
|
|
assert_eq!(ext.day, 1);
|
2026-03-31 10:23:17 +02:00
|
|
|
}
|
|
|
|
|
ParsedDateTime::InRange(_) => unreachable!("year 12000 is out of jiff range"),
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-23 18:34:54 +02:00
|
|
|
### Supported Formats
|
|
|
|
|
|
2023-08-30 14:39:34 +09:00
|
|
|
The `parse_datetime` and `parse_datetime_at_date` functions support absolute datetime and the following relative times:
|
2023-04-23 18:34:54 +02:00
|
|
|
|
|
|
|
|
- `num` `unit` (e.g., "-1 hour", "+3 days")
|
|
|
|
|
- `unit` (e.g., "hour", "day")
|
|
|
|
|
- "now" or "today"
|
|
|
|
|
- "yesterday"
|
|
|
|
|
- "tomorrow"
|
|
|
|
|
- use "ago" for the past
|
2023-06-26 15:27:05 +04:00
|
|
|
- use "next" or "last" with `unit` (e.g., "next week", "last year")
|
2023-08-23 08:51:58 +09:00
|
|
|
- unix timestamps (for example "@0" "@1344000")
|
2023-04-23 18:34:54 +02:00
|
|
|
|
|
|
|
|
`num` can be a positive or negative integer.
|
|
|
|
|
`unit` can be one of the following: "fortnight", "week", "day", "hour", "minute", "min", "second", "sec" and their plural forms.
|
|
|
|
|
|
|
|
|
|
## Return Values
|
|
|
|
|
|
2023-08-23 08:51:58 +09:00
|
|
|
### parse_datetime and parse_datetime_at_date
|
2023-06-06 10:05:14 -07:00
|
|
|
|
2023-08-23 08:51:58 +09:00
|
|
|
The `parse_datetime` and `parse_datetime_at_date` function return:
|
2023-06-06 10:05:14 -07:00
|
|
|
|
2026-03-31 03:51:07 -04:00
|
|
|
- `Ok(ParsedDateTime)` - If the input string can be parsed
|
|
|
|
|
- `ParsedDateTime::InRange(Zoned)` for years supported by `jiff::Zoned`
|
|
|
|
|
- `ParsedDateTime::Extended(ExtendedDateTime)` for out-of-range years (for example `>9999`)
|
2023-08-23 08:51:58 +09:00
|
|
|
- `Err(ParseDateTimeError::InvalidInput)` - If the input string cannot be parsed
|
2023-06-06 10:05:14 -07:00
|
|
|
|
2023-04-23 22:12:41 +02:00
|
|
|
## Fuzzer
|
2023-04-23 20:26:01 +02:00
|
|
|
|
|
|
|
|
To run the fuzzer:
|
2023-08-23 08:51:58 +09:00
|
|
|
|
2023-04-23 20:26:01 +02:00
|
|
|
```
|
2023-08-23 08:51:58 +09:00
|
|
|
$ cd fuzz
|
|
|
|
|
$ cargo install cargo-fuzz
|
|
|
|
|
$ cargo +nightly fuzz run fuzz_parse_datetime
|
2023-04-23 20:26:01 +02:00
|
|
|
```
|
2023-04-23 18:34:54 +02:00
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
|
|
This project is licensed under the [MIT License](LICENSE).
|
2023-06-09 13:43:04 +02:00
|
|
|
|
|
|
|
|
## Note
|
|
|
|
|
|
|
|
|
|
At some point, this crate was called humantime_to_duration.
|
|
|
|
|
It has been renamed to cover more cases.
|