Skip to content

Commit

Permalink
ICU-22991 Simplified Grego code
Browse files Browse the repository at this point in the history
Use timeToFields instead of dayToFields
  • Loading branch information
FrankYFTang committed Jan 8, 2025
1 parent 9eafd8c commit 7c1fb6b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
13 changes: 10 additions & 3 deletions icu4c/source/i18n/gregoimp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ double ClockMath::floorDivide(double dividend, double divisor,
r = dividend - (quotient * divisor);
}
}
U_ASSERT(0 <= r && r < divisor);
if (remainder != nullptr) {
*remainder = r;
}
Expand Down Expand Up @@ -204,15 +203,23 @@ void Grego::timeToFields(UDate time, int32_t& year, int8_t& month,
int8_t& dom, int8_t& dow, int16_t& doy, int32_t& mid, UErrorCode& status) {
if (U_FAILURE(status)) return;
double millisInDay;
double day = ClockMath::floorDivide(static_cast<double>(time), static_cast<double>(U_MILLIS_PER_DAY), &millisInDay);
double day = ClockMath::floorDivide(time, U_MILLIS_PER_DAY, &millisInDay);
if (day > INT32_MAX || day < INT32_MIN) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
mid = static_cast<int32_t>(millisInDay);
dayToFields(day, year, month, dom, dow, doy, status);
}

int32_t Grego::timeToYear(UDate time, UErrorCode& status) {
if (U_FAILURE(status)) return 0;
double millisInDay;
int32_t day = ClockMath::floorDivide(static_cast<double>(time), static_cast<double>(U_MILLIS_PER_DAY), &millisInDay);
double day = ClockMath::floorDivide(time, U_MILLIS_PER_DAY, &millisInDay);
if (day > INT32_MAX || day < INT32_MIN) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
return Grego::dayToYear(day, status);
}

Expand Down
11 changes: 2 additions & 9 deletions icu4c/source/i18n/simpletz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,8 @@ SimpleTimeZone::getOffsetFromLocal(UDate date, UTimeZoneLocalOption nonExistingT
rawOffsetGMT = getRawOffset();
int32_t year, millis;
int8_t month, dom, dow;
double dday = ClockMath::floorDivide(date, U_MILLIS_PER_DAY, &millis);
if (dday > INT32_MAX || dday < INT32_MIN) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
int32_t day = dday;

Grego::dayToFields(day, year, month, dom, dow, status);
Grego::timeToFields(date, year, month, dom, dow, millis, status);
if (U_FAILURE(status)) return;

savingsDST = getOffset(GregorianCalendar::AD, year, month, dom,
Expand Down Expand Up @@ -555,8 +549,7 @@ SimpleTimeZone::getOffsetFromLocal(UDate date, UTimeZoneLocalOption nonExistingT
}
}
if (recalc) {
day = ClockMath::floorDivide(date, U_MILLIS_PER_DAY, &millis);
Grego::dayToFields(day, year, month, dom, dow, status);
Grego::timeToFields(date, year, month, dom, dow, millis, status);
if (U_FAILURE(status)) return;
savingsDST = getOffset(GregorianCalendar::AD, year, month, dom,
static_cast<uint8_t>(dow), millis,
Expand Down
9 changes: 1 addition & 8 deletions icu4c/source/i18n/timezone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,14 +732,7 @@ void TimeZone::getOffset(UDate date, UBool local, int32_t& rawOffset,
for (int32_t pass=0; ; ++pass) {
int32_t year, millis;
int8_t month, dom, dow;
double day = ClockMath::floorDivide(date, U_MILLIS_PER_DAY, &millis);

// out of the range
if (day < INT32_MIN || day > INT32_MAX) {
ec = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
Grego::dayToFields(day, year, month, dom, dow, ec);
Grego::timeToFields(date, year, month, dom, dow, millis, ec);
if (U_FAILURE(ec)) return;

dstOffset = getOffset(GregorianCalendar::AD, year, month, dom,
Expand Down

0 comments on commit 7c1fb6b

Please sign in to comment.