Skip to content

Commit 7834d6d

Browse files
committed
Move NaiveWeek to separate module
1 parent c8c8ec4 commit 7834d6d

File tree

4 files changed

+139
-128
lines changed

4 files changed

+139
-128
lines changed

src/naive/date/mod.rs

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#[cfg(feature = "alloc")]
1717
use core::borrow::Borrow;
1818
use core::iter::FusedIterator;
19-
use core::ops::{Add, AddAssign, RangeInclusive, Sub, SubAssign};
19+
use core::ops::{Add, AddAssign, Sub, SubAssign};
2020
use core::{fmt, str};
2121

2222
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
@@ -33,7 +33,7 @@ use crate::format::{
3333
Parsed, StrftimeItems,
3434
};
3535
use crate::month::Months;
36-
use crate::naive::{IsoWeek, NaiveDateTime, NaiveTime};
36+
use crate::naive::{IsoWeek, NaiveDateTime, NaiveTime, NaiveWeek};
3737
use crate::{expect, try_opt};
3838
use crate::{Datelike, TimeDelta, Weekday};
3939

@@ -48,101 +48,6 @@ pub(crate) mod serde;
4848
#[cfg(test)]
4949
mod tests;
5050

51-
/// A week represented by a [`NaiveDate`] and a [`Weekday`] which is the first
52-
/// day of the week.
53-
#[derive(Debug)]
54-
pub struct NaiveWeek {
55-
date: NaiveDate,
56-
start: Weekday,
57-
}
58-
59-
impl NaiveWeek {
60-
/// Create a new `NaiveWeek`
61-
pub(crate) const fn new(date: NaiveDate, start: Weekday) -> Self {
62-
Self { date, start }
63-
}
64-
65-
/// Returns a date representing the first day of the week.
66-
///
67-
/// # Panics
68-
///
69-
/// Panics if the first day of the week happens to fall just out of range of `NaiveDate`
70-
/// (more than ca. 262,000 years away from common era).
71-
///
72-
/// # Examples
73-
///
74-
/// ```
75-
/// use chrono::{NaiveDate, Weekday};
76-
///
77-
/// let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap();
78-
/// let week = date.week(Weekday::Mon);
79-
/// assert!(week.first_day() <= date);
80-
/// ```
81-
#[inline]
82-
#[must_use]
83-
pub const fn first_day(&self) -> NaiveDate {
84-
let start = self.start.num_days_from_monday() as i32;
85-
let ref_day = self.date.weekday().num_days_from_monday() as i32;
86-
// Calculate the number of days to subtract from `self.date`.
87-
// Do not construct an intermediate date beyond `self.date`, because that may be out of
88-
// range if `date` is close to `NaiveDate::MAX`.
89-
let days = start - ref_day - if start > ref_day { 7 } else { 0 };
90-
expect!(self.date.add_days(days), "first weekday out of range for `NaiveDate`")
91-
}
92-
93-
/// Returns a date representing the last day of the week.
94-
///
95-
/// # Panics
96-
///
97-
/// Panics if the last day of the week happens to fall just out of range of `NaiveDate`
98-
/// (more than ca. 262,000 years away from common era).
99-
///
100-
/// # Examples
101-
///
102-
/// ```
103-
/// use chrono::{NaiveDate, Weekday};
104-
///
105-
/// let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap();
106-
/// let week = date.week(Weekday::Mon);
107-
/// assert!(week.last_day() >= date);
108-
/// ```
109-
#[inline]
110-
#[must_use]
111-
pub const fn last_day(&self) -> NaiveDate {
112-
let end = self.start.pred().num_days_from_monday() as i32;
113-
let ref_day = self.date.weekday().num_days_from_monday() as i32;
114-
// Calculate the number of days to add to `self.date`.
115-
// Do not construct an intermediate date before `self.date` (like with `first_day()`),
116-
// because that may be out of range if `date` is close to `NaiveDate::MIN`.
117-
let days = end - ref_day + if end < ref_day { 7 } else { 0 };
118-
expect!(self.date.add_days(days), "last weekday out of range for `NaiveDate`")
119-
}
120-
121-
/// Returns a [`RangeInclusive<T>`] representing the whole week bounded by
122-
/// [first_day](NaiveWeek::first_day) and [last_day](NaiveWeek::last_day) functions.
123-
///
124-
/// # Panics
125-
///
126-
/// Panics if the either the first or last day of the week happens to fall just out of range of
127-
/// `NaiveDate` (more than ca. 262,000 years away from common era).
128-
///
129-
/// # Examples
130-
///
131-
/// ```
132-
/// use chrono::{NaiveDate, Weekday};
133-
///
134-
/// let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap();
135-
/// let week = date.week(Weekday::Mon);
136-
/// let days = week.days();
137-
/// assert!(days.contains(&date));
138-
/// ```
139-
#[inline]
140-
#[must_use]
141-
pub const fn days(&self) -> RangeInclusive<NaiveDate> {
142-
self.first_day()..=self.last_day()
143-
}
144-
}
145-
14651
/// A duration in calendar days.
14752
///
14853
/// This is useful because when using `TimeDelta` it is possible that adding `TimeDelta::days(1)`
@@ -1478,7 +1383,7 @@ impl NaiveDate {
14781383
/// Returns the day of week.
14791384
// This duplicates `Datelike::weekday()`, because trait methods can't be const yet.
14801385
#[inline]
1481-
const fn weekday(&self) -> Weekday {
1386+
pub(super) const fn weekday(&self) -> Weekday {
14821387
match (((self.yof & ORDINAL_MASK) >> 4) + (self.yof & WEEKDAY_FLAGS_MASK)) % 7 {
14831388
0 => Weekday::Mon,
14841389
1 => Weekday::Tue,

src/naive/date/tests.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -687,35 +687,6 @@ fn test_week_iterator_limit() {
687687
);
688688
}
689689

690-
#[test]
691-
fn test_naiveweek() {
692-
let date = NaiveDate::from_ymd_opt(2022, 5, 18).unwrap();
693-
let asserts = [
694-
(Weekday::Mon, "Mon 2022-05-16", "Sun 2022-05-22"),
695-
(Weekday::Tue, "Tue 2022-05-17", "Mon 2022-05-23"),
696-
(Weekday::Wed, "Wed 2022-05-18", "Tue 2022-05-24"),
697-
(Weekday::Thu, "Thu 2022-05-12", "Wed 2022-05-18"),
698-
(Weekday::Fri, "Fri 2022-05-13", "Thu 2022-05-19"),
699-
(Weekday::Sat, "Sat 2022-05-14", "Fri 2022-05-20"),
700-
(Weekday::Sun, "Sun 2022-05-15", "Sat 2022-05-21"),
701-
];
702-
for (start, first_day, last_day) in asserts {
703-
let week = date.week(start);
704-
let days = week.days();
705-
assert_eq!(Ok(week.first_day()), NaiveDate::parse_from_str(first_day, "%a %Y-%m-%d"));
706-
assert_eq!(Ok(week.last_day()), NaiveDate::parse_from_str(last_day, "%a %Y-%m-%d"));
707-
assert!(days.contains(&date));
708-
}
709-
}
710-
711-
#[test]
712-
fn test_naiveweek_min_max() {
713-
let date_max = NaiveDate::MAX;
714-
assert!(date_max.week(Weekday::Mon).first_day() <= date_max);
715-
let date_min = NaiveDate::MIN;
716-
assert!(date_min.week(Weekday::Mon).last_day() >= date_min);
717-
}
718-
719690
#[test]
720691
fn test_weeks_from() {
721692
// tests per: https://github.com/chronotope/chrono/issues/961

src/naive/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ pub(crate) mod datetime;
99
mod internals;
1010
pub(crate) mod isoweek;
1111
pub(crate) mod time;
12+
mod week;
1213

13-
pub use self::date::{Days, NaiveDate, NaiveDateDaysIterator, NaiveDateWeeksIterator, NaiveWeek};
14+
pub use self::date::{Days, NaiveDate, NaiveDateDaysIterator, NaiveDateWeeksIterator};
1415
#[allow(deprecated)]
1516
pub use self::date::{MAX_DATE, MIN_DATE};
1617
#[cfg(feature = "rustc-serialize")]
@@ -20,6 +21,7 @@ pub use self::datetime::rustc_serialize::TsSeconds;
2021
pub use self::datetime::{NaiveDateTime, MAX_DATETIME, MIN_DATETIME};
2122
pub use self::isoweek::IsoWeek;
2223
pub use self::time::NaiveTime;
24+
pub use self::week::NaiveWeek;
2325

2426
#[cfg(feature = "__internal_bench")]
2527
#[doc(hidden)]

src/naive/week.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)