Function from_human_time

Source
pub fn from_human_time(str: &str) -> 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 human_date_parser::{from_human_time, ParseResult};
let date = from_human_time("Last Friday at 19:45").unwrap();
match date {
    ParseResult::DateTime(date) => println!("{date}"),
    _ => unreachable!()
}
Examples found in repository?
examples/stdin.rs (line 14)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn main() {
    let mut buffer = String::new();
    let stdin = stdin();

    println!("Describe a date or time:");
    loop {
        buffer.clear();
        stdin.read_line(&mut buffer).unwrap();
        let result = match human_date_parser::from_human_time(&buffer) {
            Ok(time) => time,
            Err(e) => {
                println!("{e}");
                continue;
            }
        };

        let now = Local::now();

        match result {
            ParseResult::DateTime(datetime) => {
                println!("Time now: {now}");
                println!("Time then: {datetime}\n");
            }
            ParseResult::Date(date) => println!("Date: {date}\n"),
            ParseResult::Time(time) => println!("Time: {time}\n"),
        };
    }
}