From 1bb93d7fe936aca546f63ad99401eba11a451fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Sat, 18 May 2024 11:11:05 +0200 Subject: [PATCH] Fix single digit date parsing --- src/lib.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b0e7a00..4d634f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -204,10 +204,10 @@ pub fn parse_datetime_at_date + Clone>( } } - let ts = s.as_ref().to_owned() + "0000"; + let ts = s.as_ref().to_owned() + " 0000"; // Parse date only formats - assume midnight local timezone for fmt in [format::ISO_8601, format::ISO_8601_NO_SEP] { - let f = fmt.to_owned() + "%H%M"; + let f = fmt.to_owned() + " %H%M"; if let Ok(parsed) = NaiveDateTime::parse_from_str(&ts, &f) { if let Ok(dt) = naive_dt_to_fixed_offset(date, parsed) { return Ok(dt); @@ -327,6 +327,23 @@ mod tests { } } + #[cfg(test)] + mod formats { + use crate::parse_datetime; + use chrono::{DateTime, Local, TimeZone}; + + #[test] + fn single_digit_month_day() { + let x = Local.with_ymd_and_hms(1987, 5, 7, 0, 0, 0).unwrap(); + let expected = DateTime::fixed_offset(&x); + + assert_eq!(Ok(expected), parse_datetime("1987-05-07")); + assert_eq!(Ok(expected), parse_datetime("1987-5-07")); + assert_eq!(Ok(expected), parse_datetime("1987-05-7")); + assert_eq!(Ok(expected), parse_datetime("1987-5-7")); + } + } + #[cfg(test)] mod offsets { use chrono::Local;