date_filter_search 0.1.1

Date Filter Search is a Rust library that makes working with dates a breeze. It seamlessly converts dates to Unix seconds and Unix seconds back to dates, validates and completes partial date inputs, supports a configurable time zone (Local or UTC), and features robust parsing of both absolute and relative search strings for powerful date filtering.
Documentation
use std::env;
use std::process;

use date_filter_search::{date_to_seconds, seconds_to_date, validate_datetime};
use date_filter_search::{get_current_date, set_timezone_option, TimeZoneOption};
use date_filter_search::{parsing_search, search_generic, string_to_date_seconds};

fn main() {
    println!("\n============================");
    println!("  Example Usage of the Library  ");
    println!("      date_filter_search      ");
    println!("============================\n");

    // Set the time zone to UTC. If not set, the default is Local.
    set_timezone_option(TimeZoneOption::Utc);

    // --- Block 1: Current Date ---
    println!("*** CURRENT DATE UTC ***");
    let current_date = get_current_date();
    println!("Current Date: {:?}\n", current_date);

    println!("*** CURRENT DATE IN SECONDS ***");
    let seconds_current = date_to_seconds(current_date);
    println!("Seconds: {}\n", seconds_current);

    // --- Block 2: Convert a Specific Timestamp to Date ---
    println!("*** TIMESTAMP TO DATE CONVERSION ***");
    let timestamp_example = 1744051780;
    let date_from_seconds = seconds_to_date(timestamp_example);
    println!(
        "Timestamp {} corresponds to: {:?}\n",
        timestamp_example, date_from_seconds
    );

    // --- Block 3: Date Validation ---
    println!("*** DATE VALIDATION ***");
    let date_validated = validate_datetime("2021-4", true);
    println!("Validated input \"2021-4\": {:?}\n", date_validated);

    // --- Block 4: Parsing the Search Query ---
    // Examples of search queries:
    //   Relative: "10m" (10 minutes from now)
    //   Absolute: "2021-3 to 2021-6" or "2023-04-05 10:20:30"
    let default_queries = vec![
        "10m".to_string(),
        "2021-3 to 2021-6".to_string(),
        "2023-04-05 10:20:30".to_string(),
    ];

    // Use command line argument as query if provided, otherwise use the default queries.
    let args: Vec<String> = env::args().collect();
    let queries = if args.len() > 1 {
        vec![args[1].clone()]
    } else {
        default_queries
    };

    for query in queries {
        println!("*** PARSING THE SEARCH QUERY ***");
        println!("Query: \"{}\"", query);
        let (start, end) = match parsing_search(&query) {
            Ok((s, e)) => (s, e),
            Err(e) => {
                eprintln!("Error parsing query: {}", e);
                process::exit(1);
            }
        };
        println!(
            "Time Interval (in seconds): Start = {} - End = {}\n",
            start, end
        );

        // --- Block 5: Filtering Dates ---
        // An extended list of dates in various accepted formats.
        let date_list = vec![
            "2023-04-05 10:20:30",
            "2022/12/31 23.59.59",
            "1970|01|01_00,00,00",
            "2000.06.15;12-00-00",
            "2021-04-15 08:30:00",
            "2021-05-10 14:45:20",
            "2021-06-20 22:15:55",
            "2021-03-25 09:05:10",
            "2021-07-04 12:00:00",
            "2021-11-11 11:11:11",
        ];

        println!("*** DATE SEARCH RESULTS ***");
        for date_str in &date_list {
            match string_to_date_seconds(date_str) {
                Ok(timestamp) => {
                    if search_generic(timestamp, start, end) {
                        println!("Found Date: {}", date_str);
                    }
                }
                Err(e) => {
                    eprintln!("Error parsing date '{}': {}", date_str, e);
                }
            }
        }
        println!("\n------------------------------------------\n");
    }
}