Files
komp_ac/server/src/shared/date_utils.rs
2025-02-22 23:23:07 +01:00

35 lines
1.2 KiB
Rust

// src/shared/date_utils.rs
use chrono::NaiveDate;
/// Attempts to parse the date string with multiple commonly used formats in Slovakia
pub fn parse_date_with_multiple_formats(date_str: &str) -> Option<NaiveDate> {
// Define common date formats used in Slovakia
let formats = [
"%d:%m:%Y", // Original format: 01:01:2023
"%d.%m.%Y", // Standard Slovak format: 01.01.2023
"%d-%m-%Y", // Dash format: 01-01-2023
"%d/%m/%Y", // Slash format: 01/01/2023
"%Y-%m-%d", // ISO format: 2023-01-01
"%Y/%m/%d", // Alternative ISO: 2023/01/01
"%Y.%m.%d", // Dot ISO: 2023.01.01
"%d.%m.%y", // Short year with dot: 01.01.23
"%d-%m-%y", // Short year with dash: 01-01-23
"%d/%m/%y", // Short year with slash: 01/01/23
];
// Try each format until one works
for format in formats {
if let Ok(date) = NaiveDate::parse_from_str(date_str, format) {
return Some(date);
}
}
// If none of the formats worked, try to handle potential whitespace issues
let trimmed = date_str.trim();
if trimmed != date_str {
return parse_date_with_multiple_formats(trimmed);
}
None
}