Skip to content

Commit

Permalink
Shorten integer type of day_of_month, week_of_month, week_of_year (#5702
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Manishearth authored Oct 18, 2024
1 parent 5df7f49 commit 3b2f425
Show file tree
Hide file tree
Showing 36 changed files with 87 additions and 83 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `icu_calendar`
- Consistently name calendar-specific `Date`/`DateTime` functions that have a calendar argument (https://github.com/unicode-org/icu4x/pull/5692)
- Move all calendar types to `cal` module (https://github.com/unicode-org/icu4x/pull/5701)
- Shorten integer types returned by `day_of_month()`, `week_of_month()`, and `week_of_year()` to `u8` (https://github.com/unicode-org/icu4x/pull/5702)
- `icu_collections`
- `icu_normalizer`
- `icu_datetime`
Expand Down
3 changes: 1 addition & 2 deletions components/calendar/src/any_calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,6 @@ mod tests {

use super::*;
use crate::Ref;
use core::convert::TryInto;

fn single_test_roundtrip(
calendar: Ref<AnyCalendar>,
Expand All @@ -1889,7 +1888,7 @@ mod tests {
let roundtrip_era = roundtrip_year.formatting_era().unwrap_or(era);
let roundtrip_year = roundtrip_year.era_year_or_extended();
let roundtrip_month = date.month().standard_code;
let roundtrip_day = date.day_of_month().0.try_into().expect("Must fit in u8");
let roundtrip_day = date.day_of_month().0;

assert_eq!(
(era, year, month, day),
Expand Down
4 changes: 2 additions & 2 deletions components/calendar/src/buddhist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ mod test {
);
assert_eq!(
buddhist1.day_of_month().0,
buddhist_day as u32,
buddhist_day,
"Iso -> Buddhist day check failed for case: {case:?}"
);

Expand All @@ -361,7 +361,7 @@ mod test {
);
assert_eq!(
iso2.day_of_month().0,
iso_day as u32,
iso_day,
"Buddhist -> Iso day check failed for case: {case:?}"
);
}
Expand Down
2 changes: 1 addition & 1 deletion components/calendar/src/calendar_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl<C: CalendarArithmetic> ArithmeticDate<C> {

#[inline]
pub fn day_of_month(&self) -> types::DayOfMonth {
types::DayOfMonth(self.day.into())
types::DayOfMonth(self.day)
}

/// The [`types::MonthInfo`] for the current month (with month code) for a solar calendar
Expand Down
6 changes: 3 additions & 3 deletions components/calendar/src/chinese.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl Calendar for Chinese {

/// The calendar-specific day-of-month represented by `date`
fn day_of_month(&self, date: &Self::DateInner) -> types::DayOfMonth {
types::DayOfMonth(date.0 .0.day as u32)
types::DayOfMonth(date.0 .0.day)
}

/// Information of the day of the year
Expand Down Expand Up @@ -666,7 +666,7 @@ mod test {
iso_day: u8,
expected_year: i32,
expected_month: u8,
expected_day: u32,
expected_day: u8,
}

let cases = [
Expand Down Expand Up @@ -1006,7 +1006,7 @@ mod test {
expected_rel_iso: i32,
expected_cyclic: u8,
expected_month: u8,
expected_day: u32,
expected_day: u8,
}

let cases = [
Expand Down
4 changes: 2 additions & 2 deletions components/calendar/src/dangi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl Calendar for Dangi {
}

fn day_of_month(&self, date: &Self::DateInner) -> crate::types::DayOfMonth {
types::DayOfMonth(date.0 .0.day as u32)
types::DayOfMonth(date.0 .0.day)
}

fn day_of_year_info(&self, date: &Self::DateInner) -> crate::types::DayOfYearInfo {
Expand Down Expand Up @@ -492,7 +492,7 @@ mod test {
expected_rel_iso: i32,
expected_cyclic: u8,
expected_month: u8,
expected_day: u32,
expected_day: u8,
}

let cases = [
Expand Down
2 changes: 1 addition & 1 deletion components/calendar/src/gregorian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ mod test {
expected_year: i32,
expected_era: Era,
expected_month: u8,
expected_day: u32,
expected_day: u8,
}

fn check_test_case(case: TestCase) {
Expand Down
4 changes: 2 additions & 2 deletions components/calendar/src/indian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ mod tests {
"{y}-{m}-{d}: ISO month did not match"
);
assert_eq!(
iso.day_of_month().0 as u8,
iso.day_of_month().0,
iso_d,
"{y}-{m}-{d}: ISO day did not match"
);
Expand Down Expand Up @@ -376,7 +376,7 @@ mod tests {
iso_day: u8,
expected_year: i32,
expected_month: u8,
expected_day: u32,
expected_day: u8,
}

fn check_case(case: TestCase) {
Expand Down
2 changes: 1 addition & 1 deletion components/calendar/src/julian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ mod test {
expected_year: i32,
expected_era: Era,
expected_month: u8,
expected_day: u32,
expected_day: u8,
}

let cases = [
Expand Down
2 changes: 1 addition & 1 deletion components/calendar/src/persian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ mod tests {

// From https://calendar.ut.ac.ir/Fa/News/Data/Doc/KabiseShamsi1206-1498-new.pdf
// Plain text version at https://github.com/roozbehp/persiancalendar/blob/main/kabise.txt
static CALENDAR_UT_AC_IR_TEST_DATA: [(i32, bool, i32, u8, u32); 293] = [
static CALENDAR_UT_AC_IR_TEST_DATA: [(i32, bool, i32, u8, u8); 293] = [
(1206, false, 1827, 3, 22),
(1207, false, 1828, 3, 21),
(1208, false, 1829, 3, 21),
Expand Down
2 changes: 1 addition & 1 deletion components/calendar/src/roc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ mod test {
expected_year: i32,
expected_era: Era,
expected_month: u8,
expected_day: u32,
expected_day: u8,
}

fn check_test_case(case: TestCase) {
Expand Down
8 changes: 4 additions & 4 deletions components/calendar/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,22 +331,22 @@ pub struct DayOfYearInfo {
/// A day number in a month. Usually 1-based.
#[allow(clippy::exhaustive_structs)] // this is a newtype
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DayOfMonth(pub u32);
pub struct DayOfMonth(pub u8);

/// A week number in a month. Usually 1-based.
#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(clippy::exhaustive_structs)] // this is a newtype
pub struct WeekOfMonth(pub u32);
pub struct WeekOfMonth(pub u8);

/// A week number in a year. Usually 1-based.
#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(clippy::exhaustive_structs)] // this is a newtype
pub struct WeekOfYear(pub u32);
pub struct WeekOfYear(pub u8);

/// A day of week in month. 1-based.
#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(clippy::exhaustive_structs)] // this is a newtype
pub struct DayOfWeekInMonth(pub u32);
pub struct DayOfWeekInMonth(pub u8);

impl From<DayOfMonth> for DayOfWeekInMonth {
fn from(day_of_month: DayOfMonth) -> Self {
Expand Down
18 changes: 11 additions & 7 deletions components/calendar/src/week_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ impl WeekCalculator {
///
/// [1]: https://www.unicode.org/reports/tr35/tr35-55/tr35-dates.html#Date_Patterns_Week_Of_Year
pub fn week_of_month(&self, day_of_month: DayOfMonth, iso_weekday: IsoWeekday) -> WeekOfMonth {
WeekOfMonth(simple_week_of(self.first_weekday, day_of_month.0 as u16, iso_weekday) as u32)
WeekOfMonth(simple_week_of(
self.first_weekday,
day_of_month.0 as u16,
iso_weekday,
))
}

/// Returns the week of year according to the weekday and [`DayOfYearInfo`].
Expand Down Expand Up @@ -169,7 +173,7 @@ enum RelativeWeek {
/// A week that is assigned to the last week of the previous year/month. e.g. 2021-01-01 is week 54 of 2020 per the ISO calendar.
LastWeekOfPreviousUnit,
/// A week that's assigned to the current year/month. The offset is 1-based. e.g. 2021-01-11 is week 2 of 2021 per the ISO calendar so would be WeekOfCurrentUnit(2).
WeekOfCurrentUnit(u16),
WeekOfCurrentUnit(u8),
/// A week that is assigned to the first week of the next year/month. e.g. 2019-12-31 is week 1 of 2020 per the ISO calendar.
FirstWeekOfNextUnit,
}
Expand Down Expand Up @@ -213,15 +217,15 @@ impl UnitInfo {
}

/// Returns the number of weeks in this unit according to `calendar`.
fn num_weeks(&self, calendar: &WeekCalculator) -> u16 {
fn num_weeks(&self, calendar: &WeekCalculator) -> u8 {
let first_week_offset = self.first_week_offset(calendar);
let num_days_including_first_week =
(self.duration_days as i32) - (first_week_offset as i32);
debug_assert!(
num_days_including_first_week >= 0,
"Unit is shorter than a week."
);
((num_days_including_first_week + 7 - (calendar.min_week_days as i32)) / 7) as u16
((num_days_including_first_week + 7 - (calendar.min_week_days as i32)) / 7) as u8
}

/// Returns the week number for the given day in this unit.
Expand All @@ -232,7 +236,7 @@ impl UnitInfo {
return RelativeWeek::LastWeekOfPreviousUnit;
}

let week_number = (1 + days_since_first_week / 7) as u16;
let week_number = (1 + days_since_first_week / 7) as u8;
if week_number > self.num_weeks(calendar) {
return RelativeWeek::FirstWeekOfNextUnit;
}
Expand All @@ -257,7 +261,7 @@ pub enum RelativeUnit {
#[allow(clippy::exhaustive_structs)] // this type is stable
pub struct WeekOf {
/// Week of month/year. 1 based.
pub week: u16,
pub week: u8,
/// The month/year that this week is in, relative to the month/year of the input date.
pub unit: RelativeUnit,
}
Expand Down Expand Up @@ -316,7 +320,7 @@ pub fn week_of(
/// - first_weekday: The first day of a week.
/// - day: 1-based day of the month or year.
/// - week_day: The weekday of `day`.
pub fn simple_week_of(first_weekday: IsoWeekday, day: u16, week_day: IsoWeekday) -> u16 {
pub fn simple_week_of(first_weekday: IsoWeekday, day: u16, week_day: IsoWeekday) -> u8 {
let calendar = WeekCalculator {
first_weekday,
min_week_days: 1,
Expand Down
4 changes: 2 additions & 2 deletions ffi/capi/bindings/c/Date.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ffi/capi/bindings/c/DateTime.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ffi/capi/bindings/c/IsoDate.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ffi/capi/bindings/c/IsoDateTime.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ffi/capi/bindings/c/WeekOf.d.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ffi/capi/bindings/cpp/icu4x/Date.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ffi/capi/bindings/cpp/icu4x/Date.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ffi/capi/bindings/cpp/icu4x/DateTime.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3b2f425

Please sign in to comment.