Fuzzy DateTime
This library crate provides functions to detect, normalize, and complete date and datetime strings for bulk conversion to ISO 8601 compatible formats. It can fill in missing time, day, and month components if the year and other longer units are detected.
In all cases, dates must come first and may be separated from the optional time component by a space or the letter 'T'.
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Core Functions
fuzzy_to_datetime(dt: &str, date_opts: Option<DateOptions>, time_separator: Option<char>) -> Result<NaiveDateTime, ParseError>
This is the most versatile function. If the last two arguments are None, the function will attempt to guess the format, which might lead to ambiguity between m/d/Y and d/m/Y. For processing large arrays of date-like strings, it's recommended to use format detection functions first.
let date_opts = dmy;
if let Ok = fuzzy_to_datetime
fuzzy_to_datetime_string(dt: &str, date_opts: Option<DateOptions>, time_separator: Option<char>) -> Option<String>
If you only need a normalized ISO 8601 compatible string for direct output or database storage, this function bypasses the chrono crate:
let date_opts = dmy;
if let Some = fuzzy_to_datetime_string println!; // should be 2019-11-09T17:30:45.000Z
iso_fuzzy_string_to_datetime(dt: &str) -> Result<NaiveDateTime, ParsedError>
This assumes a Y-m-d date order and is fully compatible with the original function used with the julian day- converter crate.
let date_opts = dmy;
if let Ok = iso_fuzzy_string_to_datetime
detect_date_format_from_generic_list(date_list: &[&str]) -> DateOptions and detect_date_format_from_generic_list<T, F>(date_list: &[T], extract_date: F) -> DateOption
These functions identify the correct format by analyzing as many sample dates as necessary. They help determine the date format so you can apply the correct date order and separator options before converting large datasets.
In the following example, the function iterates lazily over a vector of objects, stopping at the second date which can only be interpreted as m/d/Y. The returned DateOptions can then be used with fuzzy_to_datetime() to convert all date strings in the list, skipping entries that do not match the detected format.
let rows: = vec!;
let date_opts_special = detect_date_format_from_generic_list;
assert_eq!;
Simple ISO date-time to naive dateTime conversion
let datetime_str = "1876-9-25 15:45"; // incomplete without zero-padding
if let Some = from_fuzzy_iso_string