Skip to content

Commit

Permalink
Merge pull request #76 from RenjiSann/main
Browse files Browse the repository at this point in the history
Fix single digit date parsing
  • Loading branch information
cakebaker authored May 20, 2024
2 parents 1389991 + 1bb93d7 commit 0f5b6bf
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ pub fn parse_datetime_at_date<S: AsRef<str> + 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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0f5b6bf

Please sign in to comment.