Skip to content

Commit b5d2087

Browse files
committed
back to i64
1 parent 90576c8 commit b5d2087

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

components/calendar/src/cal/chinese/simple.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,19 @@ impl super::LunarChineseYearData {
8383
base_moment: LocalMoment,
8484
duration: Milliseconds,
8585
) -> LocalMoment {
86-
let diff_millis = (rata_die - base_moment.rata_die) as i128
87-
* MILLISECONDS_IN_EPHEMERIS_DAY as i128
88-
- base_moment.local_milliseconds as i128;
86+
let diff_millis = (rata_die - base_moment.rata_die) * MILLISECONDS_IN_EPHEMERIS_DAY
87+
- base_moment.local_milliseconds as i64;
8988

90-
let num_periods = (diff_millis + MILLISECONDS_IN_EPHEMERIS_DAY as i128 - 1)
91-
.div_euclid(duration.0 as i128);
89+
let num_periods =
90+
(diff_millis + MILLISECONDS_IN_EPHEMERIS_DAY - 1).div_euclid(duration.0);
9291

93-
let millis = base_moment.rata_die.to_i64_date() as i128
94-
* MILLISECONDS_IN_EPHEMERIS_DAY as i128
95-
+ base_moment.local_milliseconds as i128
96-
+ num_periods * duration.0 as i128;
92+
let millis = base_moment.rata_die.to_i64_date() * MILLISECONDS_IN_EPHEMERIS_DAY
93+
+ base_moment.local_milliseconds as i64
94+
+ num_periods * duration.0;
9795

98-
let rata_die =
99-
(millis / MILLISECONDS_IN_EPHEMERIS_DAY as i128) as i64 - (millis < 0) as i64;
100-
let local_milliseconds = (millis % MILLISECONDS_IN_EPHEMERIS_DAY as i128) as i64
101-
+ (millis < 0) as i64 * MILLISECONDS_IN_EPHEMERIS_DAY as i64;
96+
let rata_die = millis / MILLISECONDS_IN_EPHEMERIS_DAY - (millis < 0) as i64;
97+
let local_milliseconds = millis % MILLISECONDS_IN_EPHEMERIS_DAY
98+
+ (millis < 0) as i64 * MILLISECONDS_IN_EPHEMERIS_DAY;
10299

103100
LocalMoment {
104101
rata_die: RataDie::new(rata_die),
@@ -168,3 +165,18 @@ impl super::LunarChineseYearData {
168165
LunarChineseYearData::new(related_iso, start_day, month_lengths, leap_month)
169166
}
170167
}
168+
169+
#[test]
170+
fn bounds() {
171+
LunarChineseYearData::simple(UTC_PLUS_9, 292_277_025);
172+
assert!(
173+
std::panic::catch_unwind(|| LunarChineseYearData::simple(UTC_PLUS_9, 292_277_026)).is_err()
174+
);
175+
176+
LunarChineseYearData::simple(BEIJING_UTC_OFFSET, -292_275_024);
177+
assert!(std::panic::catch_unwind(|| LunarChineseYearData::simple(
178+
BEIJING_UTC_OFFSET,
179+
-292_275_025
180+
))
181+
.is_err());
182+
}

components/calendar/src/calendar_arithmetic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use core::marker::PhantomData;
1515
use core::ops::RangeInclusive;
1616
use tinystr::tinystr;
1717

18-
/// The range ±2³⁰. We use i32::MIN since it is -2³¹
19-
const VALID_YEAR_RANGE: RangeInclusive<i32> = (i32::MIN / 2)..=-(i32::MIN / 2);
18+
/// The range ±2²⁷. We use i32::MIN since it is -2³¹
19+
const VALID_YEAR_RANGE: RangeInclusive<i32> = (i32::MIN / 16)..=-(i32::MIN / 16);
2020

2121
#[derive(Debug)]
2222
#[allow(clippy::exhaustive_structs)] // this type is stable

components/calendar/src/date.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<A: AsCalendar> Date<A> {
126126
///
127127
/// The year is `extended_year` if no era is provided.
128128
///
129-
/// This function will not accept year/extended_year values that are outside of the range `[-2³⁰, 2³⁰]`,
129+
/// This function will not accept year/extended_year values that are outside of the range `[-2²⁷, 2²⁷]`,
130130
/// regardless of the calendar, instead returning a [`DateError::Range`]. See [`Date::try_from_fields()`] for more
131131
/// information.
132132
#[inline]
@@ -150,8 +150,8 @@ impl<A: AsCalendar> Date<A> {
150150
/// and fill in missing fields. See [`DateFromFieldsOptions`] for more information.
151151
///
152152
/// This function will not accept year/extended_year values that are outside of the range `[-2²⁷, 2²⁷]`,
153-
/// regardless of the calendar, instead returning a [`DateError::Range`]. This allows us to to keep
154-
/// all operations on [`Date`]s infallible by staying clear of integer limits.
153+
/// regardless of the calendar, instead returning a [`DateError::Range`]. This is to prevent
154+
/// overflowing behaviors near the extreme values of the `i32` range.
155155
/// Currently, calendar-specific `Date::try_new_calendarname()` constructors
156156
/// do not do this, and it is possible to obtain such extreme dates via calendar conversion or arithmetic,
157157
/// though [we may change that behavior in the future](https://github.com/unicode-org/icu4x/issues/7076).

0 commit comments

Comments
 (0)