Skip to content

Commit 74b79c6

Browse files
committedDec 11, 2020
8257964: Broken Calendar#getMinimalDaysInFirstWeek with java.locale.providers=HOST
Reviewed-by: joehw
1 parent f9c9bf0 commit 74b79c6

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed
 

‎src/java.base/windows/classes/sun/util/locale/provider/HostLocaleProviderAdapterImpl.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class HostLocaleProviderAdapterImpl {
7575

7676
// CalendarData value types
7777
private static final int CD_FIRSTDAYOFWEEK = 0;
78-
private static final int CD_MINIMALDAYSINFIRSTWEEK = 1;
78+
private static final int CD_FIRSTWEEKOFYEAR = 1;
7979

8080
// Currency/Locale display name types
8181
private static final int DN_CURRENCY_NAME = 0;
@@ -366,7 +366,15 @@ public int getFirstDayOfWeek(Locale locale) {
366366

367367
@Override
368368
public int getMinimalDaysInFirstWeek(Locale locale) {
369-
return 0;
369+
int firstWeek = getCalendarDataValue(
370+
removeExtensions(locale).toLanguageTag(),
371+
CD_FIRSTWEEKOFYEAR);
372+
// Interpret the value from Windows LOCALE_IFIRSTWEEKOFYEAR setting
373+
return switch (firstWeek) {
374+
case 1 -> 7; // First full week following 1/1 is the first week of the year.
375+
case 2 -> 4; // First week containing at least four days is the first week of the year.
376+
default -> 1; // First week can be a single day, if 1/1 falls on the last day of the week.
377+
};
370378
}
371379
};
372380
}

‎src/java.base/windows/native/libjava/HostLocaleProviderAdapter_md.c

+5
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,11 @@ JNIEXPORT jint JNICALL Java_sun_util_locale_provider_HostLocaleProviderAdapterIm
647647
LOCALE_IFIRSTDAYOFWEEK | LOCALE_RETURN_NUMBER,
648648
(LPWSTR)&num, sizeof(num));
649649
break;
650+
case sun_util_locale_provider_HostLocaleProviderAdapterImpl_CD_FIRSTWEEKOFYEAR:
651+
got = getLocaleInfoWrapper(langtag,
652+
LOCALE_IFIRSTWEEKOFYEAR | LOCALE_RETURN_NUMBER,
653+
(LPWSTR)&num, sizeof(num));
654+
break;
650655
}
651656

652657
(*env)->ReleaseStringChars(env, jlangtag, langtag);

‎test/jdk/java/util/Locale/LocaleProviders.java

+35
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.time.ZonedDateTime;
2828
import java.time.format.DateTimeFormatter;
2929
import java.time.format.FormatStyle;
30+
import java.time.temporal.WeekFields;
3031
import java.util.*;
3132
import java.util.logging.Level;
3233
import java.util.logging.LogRecord;
@@ -110,6 +111,10 @@ public static void main(String[] args) {
110111
bug8248695Test();
111112
break;
112113

114+
case "bug8257964Test":
115+
bug8257964Test();
116+
break;
117+
113118
default:
114119
throw new RuntimeException("Test method '"+methodName+"' not found.");
115120
}
@@ -433,4 +438,34 @@ static void bug8248695Test() {
433438
System.out.println(dtf.format(zdt));
434439
}
435440
}
441+
442+
// Run only if the platform locale is en-GB
443+
static void bug8257964Test() {
444+
var defLoc = Locale.getDefault(Locale.Category.FORMAT);
445+
var type = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, Locale.UK)
446+
.getAdapterType();
447+
if (defLoc.equals(Locale.UK) &&
448+
type == LocaleProviderAdapter.Type.HOST &&
449+
(IS_WINDOWS || IS_MAC)) {
450+
Calendar instance = Calendar.getInstance(Locale.UK);
451+
int result = instance.getMinimalDaysInFirstWeek();
452+
if (result != 4) {
453+
throw new RuntimeException("MinimalDaysInFirstWeek for Locale.UK is incorrect. " +
454+
"returned: " + result);
455+
}
456+
457+
LocalDate date = LocalDate.of(2020,12,31);
458+
result = date.get(WeekFields.of(Locale.UK).weekOfWeekBasedYear());
459+
if (result != 53) {
460+
throw new RuntimeException("weekNumber is incorrect. " +
461+
"returned: " + result);
462+
}
463+
System.out.println("bug8257964Test succeeded.");
464+
} else {
465+
System.out.println("Test ignored. Either :-\n" +
466+
"Default format locale is not Locale.UK: " + defLoc + ", or\n" +
467+
"OS is neither macOS/Windows, or\n" +
468+
"provider is not HOST: " + type);
469+
}
470+
}
436471
}

‎test/jdk/java/util/Locale/LocaleProvidersRun.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @bug 6336885 7196799 7197573 7198834 8000245 8000615 8001440 8008577
2727
* 8010666 8013086 8013233 8013903 8015960 8028771 8054482 8062006
2828
* 8150432 8215913 8220227 8228465 8232871 8232860 8236495 8245241
29-
* 8246721 8248695
29+
* 8246721 8248695 8257964
3030
* @summary tests for "java.locale.providers" system property
3131
* @library /test/lib
3232
* @build LocaleProviders
@@ -172,6 +172,9 @@ public static void main(String[] args) throws Throwable {
172172

173173
//testing 8248695 fix.
174174
testRun("HOST", "bug8248695Test", "", "", "");
175+
176+
//testing 8257964 fix. (macOS/Windows only)
177+
testRun("HOST", "bug8257964Test", "", "", "");
175178
}
176179

177180
private static void testRun(String prefList, String methodName,

0 commit comments

Comments
 (0)
Please sign in to comment.