fuzzy_datetime/
detect.rs

1use crate::{guess::{guess_date_order, guess_date_splitter, DateOrderGuess}, DateOptions, DateOrder};
2
3/// This assumes all date strings are in the same format
4/// and deduces through elimination
5pub fn detect_date_format_from_list(date_list: &[&str]) -> DateOptions {
6    detect_date_format_from_generic_list(date_list, |&x| Some(x.to_string()))
7  }
8  
9  /// This assumes all objects in the list have a date string
10  /// and deduces through elimination
11  pub fn detect_date_format_from_generic_list<T, F>(date_list: &[T], extract_date: F) -> DateOptions 
12  where 
13      F: Fn(&T) -> Option<String>,
14  {
15    let mut order = DateOrder::YMD;
16  
17    for row in date_list {
18      if let Some(dt_str) = extract_date(row) {
19        if dt_str.trim().is_empty() {
20          continue; // Skip empty string
21        }
22        let split_char = guess_date_splitter(&dt_str);
23        let guess = guess_date_order(&dt_str, split_char);
24        match guess {
25            DateOrderGuess::YearFirst => {
26                order = DateOrder::YMD;
27                return DateOptions(order, split_char);
28            },
29            DateOrderGuess::DayFirst => {
30                order = DateOrder::DMY;
31                return DateOptions(order, split_char);
32            },
33            DateOrderGuess::MonthFirst => {
34                order = DateOrder::MDY;
35                return DateOptions(order, split_char);
36            },
37            _ => continue, // NonDate or ambiguous format, keep looking
38        }
39      }
40    }
41    // If we didn't find a conclusive format, we might want to handle this case better
42    DateOptions(order, None)
43  }