Skip to content

Commit f8cecbe

Browse files
Clar Charrpitdicker
andcommitted
Make Weekday::num_days_from public, rename to days_since.
Co-authored-by: Paul Dicker <[email protected]>
1 parent 0cfc405 commit f8cecbe

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed

src/format/parsed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,9 +1152,9 @@ fn resolve_week_date(
11521152

11531153
let first_day_of_year = NaiveDate::from_yo_opt(year, 1).ok_or(OUT_OF_RANGE)?;
11541154
// Ordinal of the day at which week 1 starts.
1155-
let first_week_start = 1 + week_start_day.num_days_from(first_day_of_year.weekday()) as i32;
1155+
let first_week_start = 1 + week_start_day.days_since(first_day_of_year.weekday()) as i32;
11561156
// Number of the `weekday`, which is 0 for the first day of the week.
1157-
let weekday = weekday.num_days_from(week_start_day) as i32;
1157+
let weekday = weekday.days_since(week_start_day) as i32;
11581158
let ordinal = first_week_start + (week as i32 - 1) * 7 + weekday;
11591159
if ordinal <= 0 {
11601160
return Err(IMPOSSIBLE);

src/naive/date/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl arbitrary::Arbitrary<'_> for NaiveDate {
122122

123123
impl NaiveDate {
124124
pub(crate) fn weeks_from(&self, day: Weekday) -> i32 {
125-
(self.ordinal() as i32 - self.weekday().num_days_from(day) as i32 + 6) / 7
125+
(self.ordinal() as i32 - self.weekday().days_since(day) as i32 + 6) / 7
126126
}
127127

128128
/// Makes a new `NaiveDate` from year, ordinal and flags.

src/weekday.rs

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl Weekday {
101101
/// `w.number_from_monday()`: | 1 | 2 | 3 | 4 | 5 | 6 | 7
102102
#[inline]
103103
pub const fn number_from_monday(&self) -> u32 {
104-
self.num_days_from(Weekday::Mon) + 1
104+
self.days_since(Weekday::Mon) + 1
105105
}
106106

107107
/// Returns a day-of-week number starting from Sunday = 1.
@@ -111,7 +111,7 @@ impl Weekday {
111111
/// `w.number_from_sunday()`: | 2 | 3 | 4 | 5 | 6 | 7 | 1
112112
#[inline]
113113
pub const fn number_from_sunday(&self) -> u32 {
114-
self.num_days_from(Weekday::Sun) + 1
114+
self.days_since(Weekday::Sun) + 1
115115
}
116116

117117
/// Returns a day-of-week number starting from Monday = 0.
@@ -135,7 +135,7 @@ impl Weekday {
135135
/// ```
136136
#[inline]
137137
pub const fn num_days_from_monday(&self) -> u32 {
138-
self.num_days_from(Weekday::Mon)
138+
self.days_since(Weekday::Mon)
139139
}
140140

141141
/// Returns a day-of-week number starting from Sunday = 0.
@@ -145,17 +145,27 @@ impl Weekday {
145145
/// `w.num_days_from_sunday()`: | 1 | 2 | 3 | 4 | 5 | 6 | 0
146146
#[inline]
147147
pub const fn num_days_from_sunday(&self) -> u32 {
148-
self.num_days_from(Weekday::Sun)
148+
self.days_since(Weekday::Sun)
149149
}
150150

151-
/// Returns a day-of-week number starting from the parameter `day` (D) = 0.
151+
/// The number of days since the given day.
152152
///
153-
/// `w`: | `D` | `D+1` | `D+2` | `D+3` | `D+4` | `D+5` | `D+6`
154-
/// --------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
155-
/// `w.num_days_from(wd)`: | 0 | 1 | 2 | 3 | 4 | 5 | 6
156-
#[inline]
157-
pub(crate) const fn num_days_from(&self, day: Weekday) -> u32 {
158-
(*self as u32 + 7 - day as u32) % 7
153+
/// # Examples
154+
///
155+
/// ```
156+
/// use chrono::Weekday::*;
157+
/// assert_eq!(Mon.days_since(Mon), 0);
158+
/// assert_eq!(Sun.days_since(Tue), 5);
159+
/// assert_eq!(Wed.days_since(Sun), 3);
160+
/// ```
161+
pub const fn days_since(&self, other: Weekday) -> u32 {
162+
let lhs = *self as u32;
163+
let rhs = other as u32;
164+
if lhs < rhs {
165+
7 + lhs - rhs
166+
} else {
167+
lhs - rhs
168+
}
159169
}
160170
}
161171

@@ -296,34 +306,28 @@ mod tests {
296306
use super::Weekday;
297307

298308
#[test]
299-
fn test_num_days_from() {
309+
fn test_days_since() {
300310
for i in 0..7 {
301311
let base_day = Weekday::try_from(i).unwrap();
302312

303-
assert_eq!(base_day.num_days_from_monday(), base_day.num_days_from(Weekday::Mon));
304-
assert_eq!(base_day.num_days_from_sunday(), base_day.num_days_from(Weekday::Sun));
305-
306-
assert_eq!(base_day.num_days_from(base_day), 0);
307-
308-
assert_eq!(base_day.num_days_from(base_day.pred()), 1);
309-
assert_eq!(base_day.num_days_from(base_day.pred().pred()), 2);
310-
assert_eq!(base_day.num_days_from(base_day.pred().pred().pred()), 3);
311-
assert_eq!(base_day.num_days_from(base_day.pred().pred().pred().pred()), 4);
312-
assert_eq!(base_day.num_days_from(base_day.pred().pred().pred().pred().pred()), 5);
313-
assert_eq!(
314-
base_day.num_days_from(base_day.pred().pred().pred().pred().pred().pred()),
315-
6
316-
);
317-
318-
assert_eq!(base_day.num_days_from(base_day.succ()), 6);
319-
assert_eq!(base_day.num_days_from(base_day.succ().succ()), 5);
320-
assert_eq!(base_day.num_days_from(base_day.succ().succ().succ()), 4);
321-
assert_eq!(base_day.num_days_from(base_day.succ().succ().succ().succ()), 3);
322-
assert_eq!(base_day.num_days_from(base_day.succ().succ().succ().succ().succ()), 2);
323-
assert_eq!(
324-
base_day.num_days_from(base_day.succ().succ().succ().succ().succ().succ()),
325-
1
326-
);
313+
assert_eq!(base_day.num_days_from_monday(), base_day.days_since(Weekday::Mon));
314+
assert_eq!(base_day.num_days_from_sunday(), base_day.days_since(Weekday::Sun));
315+
316+
assert_eq!(base_day.days_since(base_day), 0);
317+
318+
assert_eq!(base_day.days_since(base_day.pred()), 1);
319+
assert_eq!(base_day.days_since(base_day.pred().pred()), 2);
320+
assert_eq!(base_day.days_since(base_day.pred().pred().pred()), 3);
321+
assert_eq!(base_day.days_since(base_day.pred().pred().pred().pred()), 4);
322+
assert_eq!(base_day.days_since(base_day.pred().pred().pred().pred().pred()), 5);
323+
assert_eq!(base_day.days_since(base_day.pred().pred().pred().pred().pred().pred()), 6);
324+
325+
assert_eq!(base_day.days_since(base_day.succ()), 6);
326+
assert_eq!(base_day.days_since(base_day.succ().succ()), 5);
327+
assert_eq!(base_day.days_since(base_day.succ().succ().succ()), 4);
328+
assert_eq!(base_day.days_since(base_day.succ().succ().succ().succ()), 3);
329+
assert_eq!(base_day.days_since(base_day.succ().succ().succ().succ().succ()), 2);
330+
assert_eq!(base_day.days_since(base_day.succ().succ().succ().succ().succ().succ()), 1);
327331
}
328332
}
329333

0 commit comments

Comments
 (0)