Skip to content

Commit

Permalink
ICU-22991 Remove unnecessary computeGregorianAndDOWFields private
Browse files Browse the repository at this point in the history
Move the DOW calculation into computeWeekFields
  • Loading branch information
FrankYFTang committed Dec 21, 2024
1 parent 4c9ef1a commit 7546622
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 40 deletions.
50 changes: 18 additions & 32 deletions icu4c/source/i18n/calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ void Calendar::computeFields(UErrorCode &ec)
//__FILE__, __LINE__, fFields[UCAL_JULIAN_DAY], localMillis);
#endif

computeGregorianAndDOWFields(fFields[UCAL_JULIAN_DAY], ec);
computeGregorianFields(fFields[UCAL_JULIAN_DAY], ec);

// Call framework method to have subclass compute its fields.
// These must include, at a minimum, MONTH, DAY_OF_MONTH,
Expand Down Expand Up @@ -1538,32 +1538,6 @@ uint8_t Calendar::julianDayToDayOfWeek(int32_t julian)
return result;
}

/**
* Compute the Gregorian calendar year, month, and day of month from
* the given Julian day. These values are not stored in fields, but in
* member variables gregorianXxx. Also compute the DAY_OF_WEEK and
* DOW_LOCAL fields.
*/
void Calendar::computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec)
{
computeGregorianFields(julianDay, ec);
if (U_FAILURE(ec)) {
return;
}

// Compute day of week: JD 0 = Monday
int32_t dow = julianDayToDayOfWeek(julianDay);
internalSet(UCAL_DAY_OF_WEEK,dow);

// Calculate 1-based localized day of week
int32_t dowLocal = dow - getFirstDayOfWeek() + 1;
if (dowLocal < 1) {
dowLocal += 7;
}
internalSet(UCAL_DOW_LOCAL,dowLocal);
fFields[UCAL_DOW_LOCAL] = dowLocal;
}

/**
* Compute the Gregorian calendar year, month, and day of month from the
* Julian day. These values are not stored in fields, but in member
Expand Down Expand Up @@ -1610,8 +1584,19 @@ void Calendar::computeWeekFields(UErrorCode &ec) {
if(U_FAILURE(ec)) {
return;
}

// Compute day of week: JD 0 = Monday
int32_t dayOfWeek = julianDayToDayOfWeek(fFields[UCAL_JULIAN_DAY]);
internalSet(UCAL_DAY_OF_WEEK, dayOfWeek);
int32_t firstDayOfWeek = getFirstDayOfWeek();
// Calculate 1-based localized day of week
int32_t dowLocal = dayOfWeek - firstDayOfWeek + 1;
if (dowLocal < 1) {
dowLocal += 7;
}
internalSet(UCAL_DOW_LOCAL,dowLocal);

int32_t eyear = fFields[UCAL_EXTENDED_YEAR];
int32_t dayOfWeek = fFields[UCAL_DAY_OF_WEEK];
int32_t dayOfYear = fFields[UCAL_DAY_OF_YEAR];

// WEEK_OF_YEAR start
Expand All @@ -1624,10 +1609,11 @@ void Calendar::computeWeekFields(UErrorCode &ec) {
// first week of the next year. ASSUME that the year length is less than
// 7000 days.
int32_t yearOfWeekOfYear = eyear;
int32_t relDow = (dayOfWeek + 7 - getFirstDayOfWeek()) % 7; // 0..6
int32_t relDowJan1 = (dayOfWeek - dayOfYear + 7001 - getFirstDayOfWeek()) % 7; // 0..6
int32_t relDow = (dayOfWeek + 7 - firstDayOfWeek) % 7; // 0..6
int32_t relDowJan1 = (dayOfWeek - dayOfYear + 7001 - firstDayOfWeek) % 7; // 0..6
int32_t woy = (dayOfYear - 1 + relDowJan1) / 7; // 0..53
if ((7 - relDowJan1) >= getMinimalDaysInFirstWeek()) {
int32_t minimalDaysInFirstWeek = getMinimalDaysInFirstWeek();
if ((7 - relDowJan1) >= minimalDaysInFirstWeek) {
++woy;
}

Expand Down Expand Up @@ -1655,7 +1641,7 @@ void Calendar::computeWeekFields(UErrorCode &ec) {
if (lastRelDow < 0) {
lastRelDow += 7;
}
if (((6 - lastRelDow) >= getMinimalDaysInFirstWeek()) &&
if (((6 - lastRelDow) >= minimalDaysInFirstWeek) &&
((dayOfYear + 7 - relDow) > lastDoy)) {
woy = 1;
yearOfWeekOfYear++;
Expand Down
8 changes: 0 additions & 8 deletions icu4c/source/i18n/unicode/calendar.h
Original file line number Diff line number Diff line change
Expand Up @@ -2282,14 +2282,6 @@ class U_I18N_API Calendar : public UObject {

/* calculations */

/**
* Compute the Gregorian calendar year, month, and day of month from
* the given Julian day. These values are not stored in fields, but in
* member variables gregorianXxx. Also compute the DAY_OF_WEEK and
* DOW_LOCAL fields.
*/
void computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec);

protected:

/**
Expand Down

1 comment on commit 7546622

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 7546622 Previous: 7d60bb8 Ratio
TestScan2 91.8092 ns/iter 39.2141 ns/iter 2.34

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.