@@ -23,23 +23,24 @@ Add this to your `Cargo.toml`:
23
23
parse_datetime = " 0.4.0"
24
24
```
25
25
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
+
27
28
``` 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
+
40
40
```
41
41
42
42
For DateTime parsing, import the ` parse_datetime ` module:
43
+
43
44
``` rs
44
45
use parse_datetime :: parse_datetime :: from_str;
45
46
use chrono :: {Local , TimeZone };
@@ -50,7 +51,7 @@ assert_eq!(dt.unwrap(), Local.with_ymd_and_hms(2021, 2, 14, 6, 37, 47).unwrap())
50
51
51
52
### Supported Formats
52
53
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 :
54
55
55
56
- ` num ` ` unit ` (e.g., "-1 hour", "+3 days")
56
57
- ` unit ` (e.g., "hour", "day")
@@ -60,34 +61,26 @@ The `from_str` and `from_str_at_date` functions support the following formats fo
60
61
- use "ago" for the past
61
62
- use "next" or "last" with ` unit ` (e.g., "next week", "last year")
62
63
- 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 ")
63
65
64
66
` num ` can be a positive or negative integer.
65
67
` unit ` can be one of the following: "fortnight", "week", "day", "hour", "minute", "min", "second", "sec" and their plural forms.
66
68
67
69
## Return Values
68
70
69
- ### Duration
71
+ ### parse_datetime and parse_datetime_at_date
70
72
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:
82
74
83
75
- ` Ok(DateTime<FixedOffset>) ` - If the input string can be prsed as a datetime
84
76
- ` Err(ParseDurationError::InvalidInput) ` - If the input string cannot be parsed
85
77
86
78
## Fuzzer
87
79
88
80
To run the fuzzer:
81
+
89
82
```
90
- $ cargo fuzz run fuzz_from_str
83
+ $ cargo fuzz run fuzz_parse_datetime
91
84
```
92
85
93
86
## License
0 commit comments