8257964: Broken Calendar#getMinimalDaysInFirstWeek with java.locale.providers=HOST

Reviewed-by: joehw
This commit is contained in:
Naoto Sato 2020-12-11 21:26:16 +00:00
parent f9c9bf03a8
commit 74b79c6e19
4 changed files with 54 additions and 3 deletions

View File

@ -75,7 +75,7 @@ public class HostLocaleProviderAdapterImpl {
// CalendarData value types
private static final int CD_FIRSTDAYOFWEEK = 0;
private static final int CD_MINIMALDAYSINFIRSTWEEK = 1;
private static final int CD_FIRSTWEEKOFYEAR = 1;
// Currency/Locale display name types
private static final int DN_CURRENCY_NAME = 0;
@ -366,7 +366,15 @@ public class HostLocaleProviderAdapterImpl {
@Override
public int getMinimalDaysInFirstWeek(Locale locale) {
return 0;
int firstWeek = getCalendarDataValue(
removeExtensions(locale).toLanguageTag(),
CD_FIRSTWEEKOFYEAR);
// Interpret the value from Windows LOCALE_IFIRSTWEEKOFYEAR setting
return switch (firstWeek) {
case 1 -> 7; // First full week following 1/1 is the first week of the year.
case 2 -> 4; // First week containing at least four days is the first week of the year.
default -> 1; // First week can be a single day, if 1/1 falls on the last day of the week.
};
}
};
}

View File

@ -647,6 +647,11 @@ JNIEXPORT jint JNICALL Java_sun_util_locale_provider_HostLocaleProviderAdapterIm
LOCALE_IFIRSTDAYOFWEEK | LOCALE_RETURN_NUMBER,
(LPWSTR)&num, sizeof(num));
break;
case sun_util_locale_provider_HostLocaleProviderAdapterImpl_CD_FIRSTWEEKOFYEAR:
got = getLocaleInfoWrapper(langtag,
LOCALE_IFIRSTWEEKOFYEAR | LOCALE_RETURN_NUMBER,
(LPWSTR)&num, sizeof(num));
break;
}
(*env)->ReleaseStringChars(env, jlangtag, langtag);

View File

@ -27,6 +27,7 @@ import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.WeekFields;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.LogRecord;
@ -110,6 +111,10 @@ public class LocaleProviders {
bug8248695Test();
break;
case "bug8257964Test":
bug8257964Test();
break;
default:
throw new RuntimeException("Test method '"+methodName+"' not found.");
}
@ -433,4 +438,34 @@ public class LocaleProviders {
System.out.println(dtf.format(zdt));
}
}
// Run only if the platform locale is en-GB
static void bug8257964Test() {
var defLoc = Locale.getDefault(Locale.Category.FORMAT);
var type = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, Locale.UK)
.getAdapterType();
if (defLoc.equals(Locale.UK) &&
type == LocaleProviderAdapter.Type.HOST &&
(IS_WINDOWS || IS_MAC)) {
Calendar instance = Calendar.getInstance(Locale.UK);
int result = instance.getMinimalDaysInFirstWeek();
if (result != 4) {
throw new RuntimeException("MinimalDaysInFirstWeek for Locale.UK is incorrect. " +
"returned: " + result);
}
LocalDate date = LocalDate.of(2020,12,31);
result = date.get(WeekFields.of(Locale.UK).weekOfWeekBasedYear());
if (result != 53) {
throw new RuntimeException("weekNumber is incorrect. " +
"returned: " + result);
}
System.out.println("bug8257964Test succeeded.");
} else {
System.out.println("Test ignored. Either :-\n" +
"Default format locale is not Locale.UK: " + defLoc + ", or\n" +
"OS is neither macOS/Windows, or\n" +
"provider is not HOST: " + type);
}
}
}

View File

@ -26,7 +26,7 @@
* @bug 6336885 7196799 7197573 7198834 8000245 8000615 8001440 8008577
* 8010666 8013086 8013233 8013903 8015960 8028771 8054482 8062006
* 8150432 8215913 8220227 8228465 8232871 8232860 8236495 8245241
* 8246721 8248695
* 8246721 8248695 8257964
* @summary tests for "java.locale.providers" system property
* @library /test/lib
* @build LocaleProviders
@ -172,6 +172,9 @@ public class LocaleProvidersRun {
//testing 8248695 fix.
testRun("HOST", "bug8248695Test", "", "", "");
//testing 8257964 fix. (macOS/Windows only)
testRun("HOST", "bug8257964Test", "", "", "");
}
private static void testRun(String prefList, String methodName,