Skip to content

Commit 0ebe3fd

Browse files
Replace Date::day_of_week by Date::weekday (#7288)
#7228 Also replace other usages of "day of week" that are not the numeric day of the week (pretty much all of them).
1 parent 7d33cbd commit 0ebe3fd

File tree

37 files changed

+358
-186
lines changed

37 files changed

+358
-186
lines changed

components/calendar/README.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/calendar/fuzz/fuzz_targets/construction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct FuzzInput {
2121
fuzz_target!(|data: FuzzInput| {
2222
if let Some(date) = data.ymd.to_date(data.cal, data.overflow_constrain) {
2323
let _ = date.day_of_month();
24-
let _ = date.day_of_week();
24+
let _ = date.weekday();
2525
let _ = date.day_of_year();
2626
let _ = date.days_in_month();
2727
let _ = date.days_in_year();

components/calendar/src/cal/hebrew.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ impl Date<Hebrew> {
365365

366366
#[cfg(test)]
367367
mod tests {
368-
369368
use super::*;
369+
use crate::types::Weekday;
370370

371371
pub const TISHREI: Month = Month::new(1);
372372
pub const ḤESHVAN: Month = Month::new(2);
@@ -508,6 +508,6 @@ mod tests {
508508

509509
// Should be Saturday per:
510510
// https://www.hebcal.com/converter?hd=1&hm=Tishrei&hy=3760&h2g=1
511-
assert_eq!(6, dt.day_of_week() as usize);
511+
assert_eq!(dt.weekday(), Weekday::Saturday);
512512
}
513513
}

components/calendar/src/cal/iso.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,20 @@ mod test {
200200
}
201201

202202
#[test]
203-
fn test_day_of_week() {
203+
fn test_weekday() {
204204
// June 23, 2021 is a Wednesday
205205
assert_eq!(
206-
Date::try_new_iso(2021, 6, 23).unwrap().day_of_week(),
206+
Date::try_new_iso(2021, 6, 23).unwrap().weekday(),
207207
Weekday::Wednesday,
208208
);
209209
// Feb 2, 1983 was a Wednesday
210210
assert_eq!(
211-
Date::try_new_iso(1983, 2, 2).unwrap().day_of_week(),
211+
Date::try_new_iso(1983, 2, 2).unwrap().weekday(),
212212
Weekday::Wednesday,
213213
);
214214
// Jan 21, 2021 was a Tuesday
215215
assert_eq!(
216-
Date::try_new_iso(2020, 1, 21).unwrap().day_of_week(),
216+
Date::try_new_iso(2020, 1, 21).unwrap().weekday(),
217217
Weekday::Tuesday,
218218
);
219219
}

components/calendar/src/date.rs

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,69 @@ impl<A: AsCalendar> Date<A> {
243243
Date { inner, calendar }
244244
}
245245

246-
/// The number of months in the year of this date.
246+
/// The day-of-month of this date.
247247
#[inline]
248-
pub fn months_in_year(&self) -> u8 {
249-
self.calendar.as_calendar().months_in_year(self.inner())
248+
pub fn day_of_month(&self) -> types::DayOfMonth {
249+
self.calendar.as_calendar().day_of_month(&self.inner)
250250
}
251251

252-
/// The number of days in the year of this date.
252+
/// The day-of-year of this date.
253253
#[inline]
254-
pub fn days_in_year(&self) -> u16 {
255-
self.calendar.as_calendar().days_in_year(self.inner())
254+
pub fn day_of_year(&self) -> types::DayOfYear {
255+
self.calendar.as_calendar().day_of_year(&self.inner)
256+
}
257+
258+
/// The weekday of this date.
259+
#[inline]
260+
pub fn weekday(&self) -> types::Weekday {
261+
self.to_rata_die().into()
262+
}
263+
264+
/// The weekday of this date.
265+
///
266+
/// This is *not* the day of the week, an ordinal number that is locale
267+
/// dependent.
268+
#[deprecated(since = "2.2.0", note = "use `Date::weekday")]
269+
pub fn day_of_week(&self) -> types::Weekday {
270+
self.to_rata_die().into()
271+
}
272+
273+
/// The month of this date.
274+
#[inline]
275+
pub fn month(&self) -> types::MonthInfo {
276+
self.calendar.as_calendar().month(&self.inner)
277+
}
278+
279+
/// The year of this date.
280+
///
281+
/// This returns an enum, see [`Date::era_year()`] and [`Date::cyclic_year()`] which are available
282+
/// for concrete calendar types and return concrete types.
283+
#[inline]
284+
pub fn year(&self) -> types::YearInfo {
285+
self.calendar.as_calendar().year_info(&self.inner).into()
286+
}
287+
288+
/// The "extended year" of this date.
289+
///
290+
/// This year number can be used when you need a simple numeric representation
291+
/// of the year, and can be meaningfully compared with extended years from other
292+
/// eras or used in arithmetic.
293+
///
294+
/// For calendars defined in Temporal, this will match the "arithmetic year"
295+
/// as defined in <https://tc39.es/proposal-intl-era-monthcode/>.
296+
/// This is typically anchored with year 1 as the year 1 of either the most modern or
297+
/// otherwise some "major" era for the calendar.
298+
///
299+
/// See [`Self::year()`] for more information about the year.
300+
#[inline]
301+
pub fn extended_year(&self) -> i32 {
302+
self.year().extended_year()
303+
}
304+
305+
/// Whether this date is in a leap year.
306+
#[inline]
307+
pub fn is_in_leap_year(&self) -> bool {
308+
self.calendar.as_calendar().is_in_leap_year(&self.inner)
256309
}
257310

258311
/// The number of days in the month of this date.
@@ -261,10 +314,16 @@ impl<A: AsCalendar> Date<A> {
261314
self.calendar.as_calendar().days_in_month(self.inner())
262315
}
263316

264-
/// The day of the week of this date.
317+
/// The number of days in the year of this date.
265318
#[inline]
266-
pub fn day_of_week(&self) -> types::Weekday {
267-
self.to_rata_die().into()
319+
pub fn days_in_year(&self) -> u16 {
320+
self.calendar.as_calendar().days_in_year(self.inner())
321+
}
322+
323+
/// The number of months in the year of this date.
324+
#[inline]
325+
pub fn months_in_year(&self) -> u8 {
326+
self.calendar.as_calendar().months_in_year(self.inner())
268327
}
269328

270329
/// Add a `duration` to this date, mutating it
@@ -359,56 +418,6 @@ impl<A: AsCalendar> Date<A> {
359418
.until(self.inner(), other.inner(), options)
360419
}
361420

362-
/// The year of this date.
363-
///
364-
/// This returns an enum, see [`Date::era_year()`] and [`Date::cyclic_year()`] which are available
365-
/// for concrete calendar types and return concrete types.
366-
#[inline]
367-
pub fn year(&self) -> types::YearInfo {
368-
self.calendar.as_calendar().year_info(&self.inner).into()
369-
}
370-
371-
/// The "extended year" of this date.
372-
///
373-
/// This year number can be used when you need a simple numeric representation
374-
/// of the year, and can be meaningfully compared with extended years from other
375-
/// eras or used in arithmetic.
376-
///
377-
/// For calendars defined in Temporal, this will match the "arithmetic year"
378-
/// as defined in <https://tc39.es/proposal-intl-era-monthcode/>.
379-
/// This is typically anchored with year 1 as the year 1 of either the most modern or
380-
/// otherwise some "major" era for the calendar.
381-
///
382-
/// See [`Self::year()`] for more information about the year.
383-
#[inline]
384-
pub fn extended_year(&self) -> i32 {
385-
self.year().extended_year()
386-
}
387-
388-
/// Whether this date is in a leap year.
389-
#[inline]
390-
pub fn is_in_leap_year(&self) -> bool {
391-
self.calendar.as_calendar().is_in_leap_year(&self.inner)
392-
}
393-
394-
/// The month of this date.
395-
#[inline]
396-
pub fn month(&self) -> types::MonthInfo {
397-
self.calendar.as_calendar().month(&self.inner)
398-
}
399-
400-
/// The day-of-month of this date.
401-
#[inline]
402-
pub fn day_of_month(&self) -> types::DayOfMonth {
403-
self.calendar.as_calendar().day_of_month(&self.inner)
404-
}
405-
406-
/// The day-of-month of this date.
407-
#[inline]
408-
pub fn day_of_year(&self) -> types::DayOfYear {
409-
self.calendar.as_calendar().day_of_year(&self.inner)
410-
}
411-
412421
/// Construct a date from raw values for a given calendar. This does not check any
413422
/// invariants for the date and calendar, and should only be called by calendar implementations.
414423
///
@@ -483,7 +492,7 @@ impl Date<Iso> {
483492
as u16,
484493
self.days_in_year(),
485494
self.day_of_year().0,
486-
self.day_of_week(),
495+
self.weekday(),
487496
)
488497
.unwrap_or_else(|_| {
489498
// ISO calendar has more than 14 days per year
@@ -648,20 +657,20 @@ mod tests {
648657
}
649658

650659
#[test]
651-
fn test_day_of_week() {
660+
fn test_weekday() {
652661
// June 23, 2021 is a Wednesday
653662
assert_eq!(
654-
Date::try_new_iso(2021, 6, 23).unwrap().day_of_week(),
663+
Date::try_new_iso(2021, 6, 23).unwrap().weekday(),
655664
Weekday::Wednesday,
656665
);
657666
// Feb 2, 1983 was a Wednesday
658667
assert_eq!(
659-
Date::try_new_iso(1983, 2, 2).unwrap().day_of_week(),
668+
Date::try_new_iso(1983, 2, 2).unwrap().weekday(),
660669
Weekday::Wednesday,
661670
);
662671
// Jan 21, 2021 was a Tuesday
663672
assert_eq!(
664-
Date::try_new_iso(2020, 1, 21).unwrap().day_of_week(),
673+
Date::try_new_iso(2020, 1, 21).unwrap().weekday(),
665674
Weekday::Tuesday,
666675
);
667676
}

components/calendar/src/duration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/// let mut date_iso = Date::try_new_iso(1992, 9, 2)
2727
/// .expect("Failed to initialize ISO Date instance.");
2828
///
29-
/// assert_eq!(date_iso.day_of_week(), Weekday::Wednesday);
29+
/// assert_eq!(date_iso.weekday(), Weekday::Wednesday);
3030
/// assert_eq!(date_iso.era_year().year, 1992);
3131
/// assert_eq!(date_iso.month().ordinal, 9);
3232
/// assert_eq!(date_iso.day_of_month().0, 2);

components/calendar/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//! let mut date_iso = Date::try_new_iso(1992, 9, 2)
3434
//! .expect("Failed to initialize ISO Date instance.");
3535
//!
36-
//! assert_eq!(date_iso.day_of_week(), Weekday::Wednesday);
36+
//! assert_eq!(date_iso.weekday(), Weekday::Wednesday);
3737
//! assert_eq!(date_iso.era_year().year, 1992);
3838
//! assert_eq!(date_iso.month().ordinal, 9);
3939
//! assert_eq!(date_iso.day_of_month().0, 2);

components/calendar/src/tests/continuity_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn check_continuity<A: AsCalendar>(mut date: Date<A>, years_to_check: usize) {
88
let duration = DateDuration::for_days(1);
99

1010
let mut rata_die = date.to_rata_die();
11-
let mut weekday = date.day_of_week();
11+
let mut weekday = date.weekday();
1212
let mut year = date.year();
1313
let mut is_in_leap_year = date.is_in_leap_year();
1414

@@ -18,7 +18,7 @@ fn check_continuity<A: AsCalendar>(mut date: Date<A>, years_to_check: usize) {
1818
.unwrap();
1919
let next_rata_die = next_date.to_iso().to_rata_die();
2020
assert_eq!(next_rata_die, rata_die + 1, "{next_date:?}");
21-
let next_weekday = next_date.day_of_week();
21+
let next_weekday = next_date.weekday();
2222
let next_year = next_date.year();
2323
let next_is_in_leap_year = next_date.is_in_leap_year();
2424
assert_eq!(

components/calendar/src/week.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ impl WeekCalculator {
103103
num_days_in_previous_unit: u16,
104104
num_days_in_unit: u16,
105105
day: u16,
106-
week_day: Weekday,
106+
weekday: Weekday,
107107
) -> Result<WeekOf, RangeError> {
108108
let current = UnitInfo::new(
109109
// The first day of this month/year is (day - 1) days from `day`.
110-
add_to_weekday(week_day, 1 - i32::from(day)),
110+
add_to_weekday(weekday, 1 - i32::from(day)),
111111
num_days_in_unit,
112112
)?;
113113

@@ -465,7 +465,7 @@ mod tests {
465465
u16::from(previous_month.days_in_month()),
466466
u16::from(date.days_in_month()),
467467
u16::from(day),
468-
date.day_of_week(),
468+
date.weekday(),
469469
)
470470
}
471471

components/datetime/src/builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@ pub enum DateFields {
117117
/// The year, month, and day of the month, as in
118118
/// “January 1st, 2000”.
119119
YMD,
120-
/// The day of the month and day of the week, as in
120+
/// The day of the month and weekday, as in
121121
/// “Saturday 1st”.
122122
DE,
123-
/// The month, day of the month, and day of the week, as in
123+
/// The month, day of the month, and weekday, as in
124124
/// “Saturday, January 1st”.
125125
MDE,
126-
/// The year, month, day of the month, and day of the week, as in
126+
/// The year, month, day of the month, and weekday, as in
127127
/// “Saturday, January 1st, 2000”.
128128
YMDE,
129-
/// The day of the week alone, as in
129+
/// The weekday alone, as in
130130
/// “Saturday”.
131131
E,
132132
/// A standalone month, as in

0 commit comments

Comments
 (0)