Function from_human_time

Source
pub fn from_human_time(
    str: &str,
    now: NaiveDateTime,
) -> Result<ParseResult, ParseError>
Expand description

Converts a human expression of a date into a more usable one.

§Errors

This function will return an error if the string contains values than can not be parsed into a date.

§Examples

use chrono::Local;
use human_date_parser::{from_human_time, ParseResult};
let now = Local::now().naive_local();
let date = from_human_time("Last Friday at 19:45", now).unwrap();
match date {
    ParseResult::DateTime(date) => println!("{date}"),
    _ => unreachable!()
}
Examples found in repository?
examples/stdin.rs (line 15)
6fn main() {
7    let mut buffer = String::new();
8    let stdin = stdin();
9
10    println!("Describe a date or time:");
11    loop {
12        buffer.clear();
13        stdin.read_line(&mut buffer).unwrap();
14        let now = Local::now().naive_local();
15        let result = match human_date_parser::from_human_time(&buffer, now) {
16            Ok(time) => time,
17            Err(e) => {
18                println!("{e}");
19                continue;
20            }
21        };
22
23        let now = Local::now();
24
25        match result {
26            ParseResult::DateTime(datetime) => {
27                println!("Time now: {now}");
28                println!("Time then: {datetime}\n");
29            }
30            ParseResult::Date(date) => println!("Date: {date}\n"),
31            ParseResult::Time(time) => println!("Time: {time}\n"),
32        };
33    }
34}