Skip to content

Commit

Permalink
ICU-22991 Fix Java
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Jan 9, 2025
1 parent bad0a71 commit faa81a7
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions icu4j/main/core/src/main/java/com/ibm/icu/impl/Grego.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,23 @@ public static long fieldsToDay(int year, int month, int dom) {
* @return the day of week
*/
public static int dayOfWeek(long day) {
long[] remainder = new long[1];
floorDivide(day + 5 /* Calendar.THURSDAY */, 7, remainder);
int dayOfWeek = (int)remainder[0];
Pair<Long, Integer> result = floorDivideAndRemainer(day + 5 /* Calendar.THURSDAY */, 7);
int dayOfWeek = result.second;
dayOfWeek = (dayOfWeek == 0) ? 7 : dayOfWeek;
return dayOfWeek;
}

public static Pair<Integer, Integer> dayToYear(long day) {
// Convert from 1970 CE epoch to 1 CE epoch (Gregorian calendar)
day += JULIAN_1970_CE - JULIAN_1_CE;
long[] rem = new long[1];
long n400 = floorDivide(day, 146097, rem);
long n100 = floorDivide(rem[0], 36524, rem);
long n4 = floorDivide(rem[0], 1461, rem);
long n1 = floorDivide(rem[0], 365, rem);

int year = (int)(400 * n400 + 100 * n100 + 4 * n4 + n1);
int dayOfYear = (int)rem[0];
if (n100 == 4 || n1 == 4) {
Pair<Long, Integer> n400 = floorDivideAndRemainer(day, 146097);
Pair<Long, Integer> n100 = floorDivideAndRemainer(n400.second, 36524);
Pair<Long, Integer> n4 = floorDivideAndRemainer(n100.second, 1461);
Pair<Long, Integer> n1 = floorDivideAndRemainer(n4.second, 365);

int year = (int)(400 * n400.first + 100 * n100.first + 4 * n4.first + n1.first);
int dayOfYear = n1.second;
if (n100.first == 4 || n1.first == 4) {
dayOfYear = 365; // Dec 31 at end of 4- or 400-yr cycle
}
else {
Expand Down Expand Up @@ -182,15 +180,14 @@ public static int[] timeToFields(long time, int[] fields) {
if (fields == null || fields.length < 6) {
fields = new int[6];
}
long[] remainder = new long[1];
long day = floorDivide(time, 24*60*60*1000 /* milliseconds per day */, remainder);
dayToFields(day, fields);
fields[5] = (int)remainder[0];
Pair<Long, Integer> result = floorDivideAndRemainer(time, 24*60*60*1000 /* milliseconds per day */);
dayToFields(result.first, fields);
fields[5] = result.second;
return fields;
}

public static int timeToYear(long time) {
return dayToYear(floorDivide(time, 24*60*60*1000 /* milliseconds per day */, new long[1])).first;
return dayToYear(floorDivideAndRemainer(time, 24*60*60*1000 /* milliseconds per day */).first).first;
}

public static long floorDivide(long numerator, long denominator) {
Expand All @@ -201,14 +198,12 @@ public static long floorDivide(long numerator, long denominator) {
((numerator + 1) / denominator) - 1;
}

private static long floorDivide(long numerator, long denominator, long[] remainder) {
private static Pair<Long, Integer> floorDivideAndRemainer(long numerator, int denominator) {
if (numerator >= 0) {
remainder[0] = numerator % denominator;
return numerator / denominator;
return new Pair<Long, Integer>(floorDivide(numerator, denominator), (int)(numerator % denominator));
}
long quotient = ((numerator + 1) / denominator) - 1;
remainder[0] = numerator - (quotient * denominator);
return quotient;
long quotient = floorDivide(numerator, denominator);
return new Pair<Long, Integer>(quotient, (int)(numerator - (quotient * denominator)));
}

/*
Expand Down

0 comments on commit faa81a7

Please sign in to comment.