Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorten integer type of day_of_month, week_of_month, week_of_year #5702

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Compiled data updated to CLDR 45 and ICU 75 (unicode-org#4782)
- `icu_calendar`
- Consistently name calendar-specific `Date`/`DateTime` functions that have a calendar argument (https://github.com/unicode-org/icu4x/pull/5692)
- 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
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion components/calendar/src/dangi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,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
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
8 changes: 4 additions & 4 deletions ffi/capi/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub mod ffi {
/// Returns the 1-indexed day in the month for this date
#[diplomat::rust_link(icu::calendar::Date::day_of_month, FnInStruct)]
#[diplomat::attr(auto, getter)]
pub fn day_of_month(&self) -> u32 {
pub fn day_of_month(&self) -> u8 {
self.0.day_of_month().0
}

Expand All @@ -102,7 +102,7 @@ pub mod ffi {
FnInStruct,
hidden
)]
pub fn week_of_month(&self, first_weekday: IsoWeekday) -> u32 {
pub fn week_of_month(&self, first_weekday: IsoWeekday) -> u8 {
self.0.week_of_month(first_weekday.into()).0
}

Expand Down Expand Up @@ -251,7 +251,7 @@ pub mod ffi {
/// Returns the 1-indexed day in the month for this date
#[diplomat::rust_link(icu::calendar::Date::day_of_month, FnInStruct)]
#[diplomat::attr(auto, getter)]
pub fn day_of_month(&self) -> u32 {
pub fn day_of_month(&self) -> u8 {
self.0.day_of_month().0
}

Expand All @@ -272,7 +272,7 @@ pub mod ffi {
FnInStruct,
hidden
)]
pub fn week_of_month(&self, first_weekday: IsoWeekday) -> u32 {
pub fn week_of_month(&self, first_weekday: IsoWeekday) -> u8 {
self.0.week_of_month(first_weekday.into()).0
}

Expand Down
8 changes: 4 additions & 4 deletions ffi/capi/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub mod ffi {
/// Returns the 1-indexed day in the month for this date
#[diplomat::rust_link(icu::calendar::Date::day_of_month, FnInStruct)]
#[diplomat::attr(auto, getter)]
pub fn day_of_month(&self) -> u32 {
pub fn day_of_month(&self) -> u8 {
self.0.date.day_of_month().0
}

Expand All @@ -174,7 +174,7 @@ pub mod ffi {
FnInStruct,
hidden
)]
pub fn week_of_month(&self, first_weekday: IsoWeekday) -> u32 {
pub fn week_of_month(&self, first_weekday: IsoWeekday) -> u8 {
self.0.date.week_of_month(first_weekday.into()).0
}

Expand Down Expand Up @@ -386,7 +386,7 @@ pub mod ffi {
/// Returns the 1-indexed day in the month for this date
#[diplomat::rust_link(icu::calendar::Date::day_of_month, FnInStruct)]
#[diplomat::attr(auto, getter)]
pub fn day_of_month(&self) -> u32 {
pub fn day_of_month(&self) -> u8 {
self.0.date.day_of_month().0
}

Expand All @@ -407,7 +407,7 @@ pub mod ffi {
FnInStruct,
hidden
)]
pub fn week_of_month(&self, first_weekday: IsoWeekday) -> u32 {
pub fn week_of_month(&self, first_weekday: IsoWeekday) -> u8 {
self.0.date.week_of_month(first_weekday.into()).0
}

Expand Down
2 changes: 1 addition & 1 deletion ffi/capi/src/week.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub mod ffi {
#[diplomat::rust_link(icu::calendar::week::WeekOf, Struct)]
#[diplomat::out]
pub struct WeekOf {
pub week: u16,
pub week: u8,
pub unit: WeekRelativeUnit,
}
/// A Week calculator, useful to be passed in to `week_of_year()` on Date and DateTime types
Expand Down
Loading