|
| 1 | +use core::ops::RangeInclusive; |
| 2 | + |
| 3 | +use crate::expect; |
| 4 | +use crate::naive::NaiveDate; |
| 5 | +use crate::Weekday; |
| 6 | + |
| 7 | +/// A week represented by a [`NaiveDate`] and a [`Weekday`] which is the first |
| 8 | +/// day of the week. |
| 9 | +#[derive(Debug)] |
| 10 | +pub struct NaiveWeek { |
| 11 | + date: NaiveDate, |
| 12 | + start: Weekday, |
| 13 | +} |
| 14 | + |
| 15 | +impl NaiveWeek { |
| 16 | + /// Create a new `NaiveWeek` |
| 17 | + pub(crate) const fn new(date: NaiveDate, start: Weekday) -> Self { |
| 18 | + Self { date, start } |
| 19 | + } |
| 20 | + |
| 21 | + /// Returns a date representing the first day of the week. |
| 22 | + /// |
| 23 | + /// # Panics |
| 24 | + /// |
| 25 | + /// Panics if the first day of the week happens to fall just out of range of `NaiveDate` |
| 26 | + /// (more than ca. 262,000 years away from common era). |
| 27 | + /// |
| 28 | + /// # Examples |
| 29 | + /// |
| 30 | + /// ``` |
| 31 | + /// use chrono::{NaiveDate, Weekday}; |
| 32 | + /// |
| 33 | + /// let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap(); |
| 34 | + /// let week = date.week(Weekday::Mon); |
| 35 | + /// assert!(week.first_day() <= date); |
| 36 | + /// ``` |
| 37 | + #[inline] |
| 38 | + #[must_use] |
| 39 | + pub const fn first_day(&self) -> NaiveDate { |
| 40 | + let start = self.start.num_days_from_monday() as i32; |
| 41 | + let ref_day = self.date.weekday().num_days_from_monday() as i32; |
| 42 | + // Calculate the number of days to subtract from `self.date`. |
| 43 | + // Do not construct an intermediate date beyond `self.date`, because that may be out of |
| 44 | + // range if `date` is close to `NaiveDate::MAX`. |
| 45 | + let days = start - ref_day - if start > ref_day { 7 } else { 0 }; |
| 46 | + expect!(self.date.add_days(days), "first weekday out of range for `NaiveDate`") |
| 47 | + } |
| 48 | + |
| 49 | + /// Returns a date representing the last day of the week. |
| 50 | + /// |
| 51 | + /// # Panics |
| 52 | + /// |
| 53 | + /// Panics if the last day of the week happens to fall just out of range of `NaiveDate` |
| 54 | + /// (more than ca. 262,000 years away from common era). |
| 55 | + /// |
| 56 | + /// # Examples |
| 57 | + /// |
| 58 | + /// ``` |
| 59 | + /// use chrono::{NaiveDate, Weekday}; |
| 60 | + /// |
| 61 | + /// let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap(); |
| 62 | + /// let week = date.week(Weekday::Mon); |
| 63 | + /// assert!(week.last_day() >= date); |
| 64 | + /// ``` |
| 65 | + #[inline] |
| 66 | + #[must_use] |
| 67 | + pub const fn last_day(&self) -> NaiveDate { |
| 68 | + let end = self.start.pred().num_days_from_monday() as i32; |
| 69 | + let ref_day = self.date.weekday().num_days_from_monday() as i32; |
| 70 | + // Calculate the number of days to add to `self.date`. |
| 71 | + // Do not construct an intermediate date before `self.date` (like with `first_day()`), |
| 72 | + // because that may be out of range if `date` is close to `NaiveDate::MIN`. |
| 73 | + let days = end - ref_day + if end < ref_day { 7 } else { 0 }; |
| 74 | + expect!(self.date.add_days(days), "last weekday out of range for `NaiveDate`") |
| 75 | + } |
| 76 | + |
| 77 | + /// Returns a [`RangeInclusive<T>`] representing the whole week bounded by |
| 78 | + /// [first_day](NaiveWeek::first_day) and [last_day](NaiveWeek::last_day) functions. |
| 79 | + /// |
| 80 | + /// # Panics |
| 81 | + /// |
| 82 | + /// Panics if the either the first or last day of the week happens to fall just out of range of |
| 83 | + /// `NaiveDate` (more than ca. 262,000 years away from common era). |
| 84 | + /// |
| 85 | + /// # Examples |
| 86 | + /// |
| 87 | + /// ``` |
| 88 | + /// use chrono::{NaiveDate, Weekday}; |
| 89 | + /// |
| 90 | + /// let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap(); |
| 91 | + /// let week = date.week(Weekday::Mon); |
| 92 | + /// let days = week.days(); |
| 93 | + /// assert!(days.contains(&date)); |
| 94 | + /// ``` |
| 95 | + #[inline] |
| 96 | + #[must_use] |
| 97 | + pub const fn days(&self) -> RangeInclusive<NaiveDate> { |
| 98 | + self.first_day()..=self.last_day() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +mod test { |
| 104 | + use crate::{NaiveDate, Weekday}; |
| 105 | + #[test] |
| 106 | + fn test_naiveweek() { |
| 107 | + let date = NaiveDate::from_ymd_opt(2022, 5, 18).unwrap(); |
| 108 | + let asserts = [ |
| 109 | + (Weekday::Mon, "Mon 2022-05-16", "Sun 2022-05-22"), |
| 110 | + (Weekday::Tue, "Tue 2022-05-17", "Mon 2022-05-23"), |
| 111 | + (Weekday::Wed, "Wed 2022-05-18", "Tue 2022-05-24"), |
| 112 | + (Weekday::Thu, "Thu 2022-05-12", "Wed 2022-05-18"), |
| 113 | + (Weekday::Fri, "Fri 2022-05-13", "Thu 2022-05-19"), |
| 114 | + (Weekday::Sat, "Sat 2022-05-14", "Fri 2022-05-20"), |
| 115 | + (Weekday::Sun, "Sun 2022-05-15", "Sat 2022-05-21"), |
| 116 | + ]; |
| 117 | + for (start, first_day, last_day) in asserts { |
| 118 | + let week = date.week(start); |
| 119 | + let days = week.days(); |
| 120 | + assert_eq!(Ok(week.first_day()), NaiveDate::parse_from_str(first_day, "%a %Y-%m-%d")); |
| 121 | + assert_eq!(Ok(week.last_day()), NaiveDate::parse_from_str(last_day, "%a %Y-%m-%d")); |
| 122 | + assert!(days.contains(&date)); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn test_naiveweek_min_max() { |
| 128 | + let date_max = NaiveDate::MAX; |
| 129 | + assert!(date_max.week(Weekday::Mon).first_day() <= date_max); |
| 130 | + let date_min = NaiveDate::MIN; |
| 131 | + assert!(date_min.week(Weekday::Mon).last_day() >= date_min); |
| 132 | + } |
| 133 | +} |
0 commit comments