ICU-5976 Fixed some bugs in TimeZoneNameProviderICU and updated the test case.
X-SVN-Rev: 24115
This commit is contained in:
parent
236d6f86ad
commit
ef6684309b
@ -19,31 +19,44 @@ public class TimeZoneNameTest extends TestFmwk {
|
||||
}
|
||||
|
||||
public void TestTimeZoneNames() {
|
||||
if (skipIfBeforeICU(4,0,0)) {
|
||||
// This test does not work well - likely caused by #6321
|
||||
return;
|
||||
}
|
||||
String[] tzids = TimeZone.getAvailableIDs();
|
||||
for (Locale loc : Locale.getAvailableLocales()) {
|
||||
for (String tzid : tzids) {
|
||||
TimeZone tz = TimeZone.getTimeZone(tzid);
|
||||
com.ibm.icu.util.TimeZone tzIcu = com.ibm.icu.util.TimeZone.getTimeZone(tzid);
|
||||
for (Locale loc : Locale.getAvailableLocales()) {
|
||||
checkDisplayName(false, TimeZone.SHORT, tz, tzIcu, loc);
|
||||
checkDisplayName(true, TimeZone.SHORT, tz, tzIcu, loc);
|
||||
checkDisplayName(false, TimeZone.LONG, tz, tzIcu, loc);
|
||||
checkDisplayName(true, TimeZone.LONG, tz, tzIcu, loc);
|
||||
checkDisplayNamePair(TimeZone.SHORT, tz, tzIcu, loc);
|
||||
checkDisplayNamePair(TimeZone.LONG, tz, tzIcu, loc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkDisplayName(boolean daylight, int style,
|
||||
TimeZone tz, com.ibm.icu.util.TimeZone icuTz, Locale loc) {
|
||||
private void checkDisplayNamePair(int style, TimeZone tz, com.ibm.icu.util.TimeZone icuTz, Locale loc) {
|
||||
/* Note: There are two problems here.
|
||||
*
|
||||
* It looks Java 6 requires a TimeZoneNameProvider to return both standard name and daylight name
|
||||
* for a zone. If the provider implementation only returns either of them, Java 6 also ignore
|
||||
* the other. In ICU, there are zones which do not have daylight names, especially zones which
|
||||
* do not use daylight time. This test case does not check a standard name if its daylight name
|
||||
* is not available because of the Java 6 implementation problem.
|
||||
*
|
||||
* Another problem is that ICU always use a standard name for a zone which does not use daylight
|
||||
* saving time even daylight name is requested.
|
||||
*/
|
||||
|
||||
String icuStdName = getIcuDisplayName(icuTz, false, style, loc);
|
||||
String icuDstName = getIcuDisplayName(icuTz, true, style, loc);
|
||||
if (icuStdName != null && icuDstName != null && !icuStdName.equals(icuDstName)) {
|
||||
checkDisplayName(false, style, tz, loc, icuStdName);
|
||||
checkDisplayName(true, style, tz, loc, icuDstName);
|
||||
}
|
||||
}
|
||||
|
||||
private String getIcuDisplayName(com.ibm.icu.util.TimeZone icuTz, boolean daylight, int style, Locale loc) {
|
||||
ULocale uloc = ULocale.forLocale(loc);
|
||||
boolean shortStyle = (style == TimeZone.SHORT);
|
||||
String icuname = icuTz.getDisplayName(daylight,
|
||||
(shortStyle ? com.ibm.icu.util.TimeZone.SHORT : com.ibm.icu.util.TimeZone.LONG),
|
||||
uloc);
|
||||
|
||||
int numDigits = 0;
|
||||
for (int i = 0; i < icuname.length(); i++) {
|
||||
if (UCharacter.isDigit(icuname.charAt(i))) {
|
||||
@ -52,25 +65,27 @@ public class TimeZoneNameTest extends TestFmwk {
|
||||
}
|
||||
if (numDigits >= 3) {
|
||||
// ICU does not have the localized name
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
return icuname;
|
||||
}
|
||||
|
||||
String name = tz.getDisplayName(daylight, style, loc);
|
||||
private void checkDisplayName(boolean daylight, int style, TimeZone tz, Locale loc, String icuname) {
|
||||
String styleStr = (style == TimeZone.SHORT) ? "SHORT" : "LONG";
|
||||
|
||||
String name = tz.getDisplayName(daylight, style, loc);
|
||||
if (TestUtil.isICUExtendedLocale(loc)) {
|
||||
// The name should be taken from ICU
|
||||
if (!name.equals(icuname)) {
|
||||
errln("FAIL: TimeZone name by ICU is " + icuname + ", but got " + name
|
||||
+ " for time zone " + tz.getID() + " in locale " + loc
|
||||
+ " (daylight=" + daylight + ", style="
|
||||
+ (shortStyle ? "SHORT" : "LONG") + ")");
|
||||
+ " (daylight=" + daylight + ", style=" + styleStr + ")");
|
||||
}
|
||||
} else {
|
||||
if (!name.equals(icuname)) {
|
||||
logln("INFO: TimeZone name by ICU is " + icuname + ", but got " + name
|
||||
+ " for time zone " + tz.getID() + " in locale " + loc
|
||||
+ " (daylight=" + daylight + ", style="
|
||||
+ (shortStyle ? "SHORT" : "LONG") + ")");
|
||||
+ " (daylight=" + daylight + ", style=" + styleStr + ")");
|
||||
}
|
||||
// Try explicit ICU locale (xx_yy_ICU)
|
||||
Locale icuLoc = TestUtil.toICUExtendedLocale(loc);
|
||||
@ -78,8 +93,7 @@ public class TimeZoneNameTest extends TestFmwk {
|
||||
if (!name.equals(icuname)) {
|
||||
errln("FAIL: TimeZone name by ICU is " + icuname + ", but got " + name
|
||||
+ " for time zone " + tz.getID() + " in locale " + icuLoc
|
||||
+ " (daylight=" + daylight + ", style="
|
||||
+ (shortStyle ? "SHORT" : "LONG") + ")");
|
||||
+ " (daylight=" + daylight + ", style=" + styleStr + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,8 @@ public class TimeZoneNameProviderICU extends java.util.spi.TimeZoneNameProvider
|
||||
@Override
|
||||
public String getDisplayName(String ID, boolean daylight, int style, Locale locale) {
|
||||
TimeZone tz = TimeZone.getTimeZone(ID);
|
||||
String disp = tz.getDisplayName(daylight, style, ICULocale.canonicalize(locale));
|
||||
Locale actualLocale = ICULocale.canonicalize(locale);
|
||||
String disp = tz.getDisplayName(daylight, style, actualLocale);
|
||||
if (disp.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
@ -26,7 +27,7 @@ public class TimeZoneNameProviderICU extends java.util.spi.TimeZoneNameProvider
|
||||
int numDigits = 0;
|
||||
for (int i = 0; i < disp.length(); i++) {
|
||||
char c = disp.charAt(i);
|
||||
if (UCharacter.isDefined(c)) {
|
||||
if (UCharacter.isDigit(c)) {
|
||||
numDigits++;
|
||||
}
|
||||
}
|
||||
@ -34,6 +35,17 @@ public class TimeZoneNameProviderICU extends java.util.spi.TimeZoneNameProvider
|
||||
if (numDigits >= 3) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (daylight) {
|
||||
// ICU uses standard name for daylight name when the zone does not use
|
||||
// daylight saving time.
|
||||
|
||||
// This is yet another ugly hack to support the JDK's behavior
|
||||
String stdDisp = tz.getDisplayName(false, style, actualLocale);
|
||||
if (disp.equals(stdDisp)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return disp;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user