fuzzy_datetime/
from_fuzzy_iso_string.rs

1use chrono::NaiveDateTime;
2
3use crate::iso_fuzzy_string_to_datetime;
4
5/// This trait may be implemented by any Date or DateTime object
6/// An implementation for chrono::NaiveDateTime is provided below
7/// 
8/// It Will be removed in 0.4.0. The functionality has moved to the fuzzy-datetime crate
9/// with more advanced date-time parsing and correction options
10pub trait FromFuzzyISOString {
11  
12  ///
13  /// Convert from any ISO-8601-like string (yyyy-mm-dd HH:MM:SS) to a DateTime object
14  /// Valid formats
15  /// Full date-time: e.g. 2023-11-15T17:53:26
16  /// with optional millisecends (ignored): e.g. 2023-11-15T17:53:26.383Z
17  /// with space rather than T: 2023-11-15 17:53:26
18  /// without seconds: 2023-11-15T17:53 (rounded to the start of the minute)
19  /// without minutes: 2023-11-15T17 (rounded to the top of the hour)
20  /// without time: 2023-11-15 (rounded to the start of the day)
21  /// without the month day: 2023-11 (rounded to the start of the month)
22  /// Year only: 2023 (rounded to the year start)
23  ///
24  fn from_fuzzy_iso_string(dt_str: &str) -> Option<Self>  where Self: Sized;
25
26}
27
28/// Implement the FromFuzzyISOString trait for NaiveDateTime
29/// Use the fuzzy-datetime crate for more robust date-time parsing and correction
30impl FromFuzzyISOString for NaiveDateTime {
31  /// construct a DateTime object from an exact or approximate ISO-8601-compatible string
32  fn from_fuzzy_iso_string(dt_str: &str) -> Option<Self> {
33    if let Ok(dt) = iso_fuzzy_string_to_datetime(dt_str) {
34      Some(dt)
35    } else {
36      None
37    }
38  }
39}