Debian Codegraph @

debian/rust-vendor/parse_datetime/README.md

raw
1 # parse_datetime
2
3 [![Crates.io](https://img.shields.io/crates/v/parse_datetime.svg)](https://crates.io/crates/parse_datetime)
4 [![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/uutils/parse_datetime/blob/main/LICENSE)
5 [![CodeCov](https://codecov.io/gh/uutils/parse_datetime/branch/main/graph/badge.svg)](https://codecov.io/gh/uutils/parse_datetime)
6 [![CodSpeed](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/uutils/parse_datetime?utm_source=badge)
7
8 A Rust crate for parsing human-readable relative time strings and
9 human-readable datetime strings.
10
11 ## Features
12
13 - Parses a variety of human-readable and standard time formats.
14 - Supports positive and negative durations.
15 - Allows for chaining time units (e.g., "1 hour 2 minutes" or "2 days 2 hours ago").
16 - Calculate durations relative to a specified date.
17 - Relies on Jiff
18
19 ## Usage
20
21 Add `parse_datetime` to your `Cargo.toml` with:
22
23 ```
24 cargo add parse_datetime
25 ```
26
27 Then, import the crate and use the `parse_datetime_at_date` function:
28
29 ```rs
30 use jiff::{ToSpan, Zoned};
31 use parse_datetime::{parse_datetime_at_date, ParsedDateTime};
32
33 let now = Zoned::now();
34 let after = parse_datetime_at_date(now.clone(), "+3 days");
35
36 match after.unwrap() {
37 ParsedDateTime::InRange(z) => assert_eq!(now.checked_add(3.days()).unwrap(), z),
38 ParsedDateTime::Extended(_) => unreachable!("unexpected for this input"),
39 }
40 ```
41
42 For DateTime parsing, import the `parse_datetime` function:
43
44 ```rs
45 use jiff::{civil::{date, time} ,Zoned};
46 use parse_datetime::{parse_datetime, ParsedDateTime};
47
48 let dt = parse_datetime("2021-02-14 06:37:47");
49 match dt.unwrap() {
50 ParsedDateTime::InRange(z) => assert_eq!(z, Zoned::now().with().date(date(2021, 2, 14)).time(time(6, 37, 47, 0)).build().unwrap()),
51 ParsedDateTime::Extended(_) => unreachable!("unexpected for this input"),
52 }
53 ```
54
55 For years beyond jiff's representable range (e.g., year 10000+), the result is an `ExtendedDateTime`:
56
57 ```rs
58 use parse_datetime::{parse_datetime, ParsedDateTime};
59
60 let dt = parse_datetime("12000-01-01").unwrap();
61 match dt {
62 ParsedDateTime::Extended(ext) => {
63 assert_eq!(ext.year, 12000);
64 assert_eq!(ext.month, 1);
65 assert_eq!(ext.day, 1);
66 }
67 ParsedDateTime::InRange(_) => unreachable!("year 12000 is out of jiff range"),
68 }
69 ```
70
71 ### Supported Formats
72
73 The `parse_datetime` and `parse_datetime_at_date` functions support absolute datetime and the following relative times:
74
75 - `num` `unit` (e.g., "-1 hour", "+3 days")
76 - `unit` (e.g., "hour", "day")
77 - "now" or "today"
78 - "yesterday"
79 - "tomorrow"
80 - use "ago" for the past
81 - use "next" or "last" with `unit` (e.g., "next week", "last year")
82 - unix timestamps (for example "@0" "@1344000")
83
84 `num` can be a positive or negative integer.
85 `unit` can be one of the following: "fortnight", "week", "day", "hour", "minute", "min", "second", "sec" and their plural forms.
86
87 ## Return Values
88
89 ### parse_datetime and parse_datetime_at_date
90
91 The `parse_datetime` and `parse_datetime_at_date` function return:
92
93 - `Ok(ParsedDateTime)` - If the input string can be parsed
94 - `ParsedDateTime::InRange(Zoned)` for years supported by `jiff::Zoned`
95 - `ParsedDateTime::Extended(ExtendedDateTime)` for out-of-range years (for example `>9999`)
96 - `Err(ParseDateTimeError::InvalidInput)` - If the input string cannot be parsed
97
98 ## Fuzzer
99
100 To run the fuzzer:
101
102 ```
103 $ cd fuzz
104 $ cargo install cargo-fuzz
105 $ cargo +nightly fuzz run fuzz_parse_datetime
106 ```
107
108 ## License
109
110 This project is licensed under the [MIT License](LICENSE).
111
112 ## Note
113
114 At some point, this crate was called humantime_to_duration.
115 It has been renamed to cover more cases.