Skip to content

Commit

Permalink
Fix single digit date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
RenjiSann committed May 20, 2024
1 parent 1389991 commit 1bb93d7
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 {

Check warning on line 331 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L331

Added line #L331 was not covered by tests
use crate::parse_datetime;
use chrono::{DateTime, Local, TimeZone};

Check warning on line 333 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L333

Added line #L333 was not covered by tests

#[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"));
}
}

Check warning on line 345 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L345

Added line #L345 was not covered by tests

#[cfg(test)]
mod offsets {
use chrono::Local;
Expand Down

0 comments on commit 1bb93d7

Please sign in to comment.