Skip to content

Commit

Permalink
Fix first_day implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sestrella committed May 17, 2022
1 parent fee4da4 commit 785dd1c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
15 changes: 9 additions & 6 deletions src/date.rs
Expand Up @@ -525,10 +525,10 @@ where

#[cfg(test)]
mod tests {
use crate::{Datelike, TimeZone, Utc, Weekday, Weeklike};
use crate::{Local, Weekday, Weeklike};

#[test]
fn test_week_first_day() {
fn test_week() {
let weekdays = [
Weekday::Mon,
Weekday::Tue,
Expand All @@ -538,10 +538,13 @@ mod tests {
Weekday::Sat,
Weekday::Sun,
];
let date = Utc.ymd(2022, 4, 18);
assert_eq!(date.weekday(), Weekday::Mon);
for weekday in weekdays {
assert!(date.week(weekday).first_day() <= date);
let date = Local::today();
for start in weekdays {
let week = date.week(start);
let days = week.days();
assert!(week.first_day() <= date);
assert!(week.last_day() >= date);
assert!(days.contains(&date));
}
}
}
48 changes: 27 additions & 21 deletions src/naive/date.rs
Expand Up @@ -19,8 +19,7 @@ use crate::format::{parse, ParseError, ParseResult, Parsed, StrftimeItems};
use crate::format::{Item, Numeric, Pad};
use crate::naive::{IsoWeek, NaiveDateTime, NaiveTime};
use crate::oldtime::Duration as OldDuration;
use crate::traits::Weeklike;
use crate::{Datelike, Duration, Weekday};
use crate::{Datelike, Duration, Weekday, Weeklike};

use super::internals::{self, DateImpl, Mdf, Of, YearFlags};
use super::isoweek;
Expand Down Expand Up @@ -64,10 +63,10 @@ impl Weeklike for NaiveWeek {

#[inline]
fn first_day(&self) -> Self::Day {
let start = self.date.weekday().num_days_from_monday();
let end = self.start.num_days_from_monday();
let days = if start > end { start - end } else { end - start };
self.date - Duration::days(7 - days as i64)
let start = self.start.num_days_from_monday();
let end = self.date.weekday().num_days_from_monday();
let days = if start > end { 7 - end } else { end - start };
self.date - Duration::days(days.into())
}
}

Expand Down Expand Up @@ -2441,20 +2440,27 @@ mod tests {
}

#[test]
fn test_naiveweek_first_day() {
let weekdays = [
Weekday::Mon,
Weekday::Tue,
Weekday::Wed,
Weekday::Thu,
Weekday::Fri,
Weekday::Sat,
Weekday::Sun,
];
let date = NaiveDate::from_ymd(2022, 4, 18);
assert_eq!(date.weekday(), Weekday::Mon);
for start in weekdays {
assert!(date.week(start).first_day() <= date);
}
fn test_naiveweek_less_than_weekday() {
let date = NaiveDate::from_ymd(2022, 5, 18);
let week = date.week(Weekday::Mon);
assert_eq!(week.first_day(), NaiveDate::from_ymd(2022, 5, 16));
assert_eq!(week.last_day(), NaiveDate::from_ymd(2022, 5, 22));
}

#[test]
fn test_naiveweek_equal_to_weekday() {
let date = NaiveDate::from_ymd(2022, 5, 18);
let week = date.week(Weekday::Wed);
assert_eq!(week.first_day(), date);
assert_eq!(week.last_day(), NaiveDate::from_ymd(2022, 5, 24));
}

/// TODO: Fix this test
#[test]
fn test_naiveweek_greater_than_weekday() {
let date = NaiveDate::from_ymd(2022, 5, 18);
let week = date.week(Weekday::Fri);
assert_eq!(week.first_day(), NaiveDate::from_ymd(2022, 5, 13));
assert_eq!(week.last_day(), NaiveDate::from_ymd(2022, 5, 19));
}
}
12 changes: 6 additions & 6 deletions src/traits.rs
Expand Up @@ -313,20 +313,20 @@ mod tests {

#[test]
fn test_weeklike_last_day() {
let first_day = NaiveDate::from_ymd(2022, 4, 18);
let week = FakeWeek { date: first_day };
let date = NaiveDate::from_ymd(2022, 4, 18);
let week = FakeWeek { date };
let last_day = week.last_day();
assert_eq!(first_day.weekday(), Weekday::Mon);
assert_eq!(date.weekday(), Weekday::Mon);
assert_eq!(last_day.weekday(), Weekday::Sun);
assert_eq!(last_day, NaiveDate::from_ymd(2022, 4, 24));
}

#[test]
fn test_weeklike_days() {
let first_day = NaiveDate::from_ymd(2022, 4, 18);
let week = FakeWeek { date: first_day };
let date = NaiveDate::from_ymd(2022, 4, 18);
let week = FakeWeek { date };
let days = week.days();
assert_eq!(days.start(), &first_day);
assert_eq!(days.start(), &week.first_day());
assert_eq!(days.end(), &week.last_day());
}
}

0 comments on commit 785dd1c

Please sign in to comment.