From 8197700ccdbb77a355e47ef8f4d9720ef41ff564 Mon Sep 17 00:00:00 2001 From: Eric Sheppard Date: Tue, 14 Feb 2023 11:41:14 +0000 Subject: [PATCH] apply same fix to parsing and add failing test cases as per issue #961 --- src/format/parsed.rs | 4 ++-- src/naive/date.rs | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/format/parsed.rs b/src/format/parsed.rs index fc4c4540bd..eb697e12bf 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -380,8 +380,8 @@ impl Parsed { let verify_ordinal = |date: NaiveDate| { let ordinal = date.ordinal(); let weekday = date.weekday(); - let week_from_sun = (ordinal as i32 - weekday.num_days_from_sunday() as i32 + 7) / 7; - let week_from_mon = (ordinal as i32 - weekday.num_days_from_monday() as i32 + 7) / 7; + let week_from_sun = (ordinal as i32 - weekday.num_days_from_sunday() as i32 + 6) / 7; + let week_from_mon = (ordinal as i32 - weekday.num_days_from_monday() as i32 + 6) / 7; self.ordinal.unwrap_or(ordinal) == ordinal && self.week_from_sun.map_or(week_from_sun, |v| v as i32) == week_from_sun && self.week_from_mon.map_or(week_from_mon, |v| v as i32) == week_from_mon diff --git a/src/naive/date.rs b/src/naive/date.rs index e3cc9b6c6a..eccbcf2ff4 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -2831,6 +2831,16 @@ mod tests { assert!(NaiveDate::parse_from_str("Sat, 09 Aug 2013", "%a, %d %b %Y").is_err()); assert!(NaiveDate::parse_from_str("2014-57", "%Y-%m-%d").is_err()); assert!(NaiveDate::parse_from_str("2014", "%Y").is_err()); // insufficient + + assert_eq!( + NaiveDate::parse_from_str("2020-01-0", "%Y-%W-%w").ok(), + NaiveDate::from_ymd_opt(2020, 1, 12), + ); + + assert_eq!( + NaiveDate::parse_from_str("2019-01-0", "%Y-%W-%w").ok(), + NaiveDate::from_ymd_opt(2019, 1, 13), + ); } #[test]