Merge "Allow the skeleton of "jmma" in DateFormat.getBestDateTimePattern()"

This commit is contained in:
vichang
2020-10-15 18:30:21 +00:00
committed by Gerrit Code Review
2 changed files with 51 additions and 1 deletions

View File

@@ -17,10 +17,14 @@
package android.text.format; package android.text.format;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.app.compat.CompatChanges;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledAfter;
import android.compat.annotation.UnsupportedAppUsage; import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context; import android.content.Context;
import android.icu.text.DateFormatSymbols; import android.icu.text.DateFormatSymbols;
import android.icu.text.DateTimePatternGenerator; import android.icu.text.DateTimePatternGenerator;
import android.os.Build;
import android.provider.Settings; import android.provider.Settings;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
@@ -158,6 +162,16 @@ public class DateFormat {
private static Locale sIs24HourLocale; private static Locale sIs24HourLocale;
private static boolean sIs24Hour; private static boolean sIs24Hour;
/**
* {@link #getBestDateTimePattern(Locale, String)} does not allow non-consecutive repeated
* symbol in the skeleton. For example, please use a skeleton of {@code "jmm"} or
* {@code "hmma"} instead of {@code "ahmma"} or {@code "jmma"}, because the field 'j' could
* mean using 12-hour in some locales and, in this case, is duplicated as the 'a' field.
*/
@ChangeId
@EnabledAfter(targetSdkVersion = Build.VERSION_CODES.R)
static final long DISALLOW_DUPLICATE_FIELD_IN_SKELETON = 170233598L;
/** /**
* Returns true if times should be formatted as 24 hour times, false if times should be * Returns true if times should be formatted as 24 hour times, false if times should be
* formatted as 12 hour (AM/PM) times. Based on the user's chosen locale and other preferences. * formatted as 12 hour (AM/PM) times. Based on the user's chosen locale and other preferences.
@@ -251,7 +265,9 @@ public class DateFormat {
*/ */
public static String getBestDateTimePattern(Locale locale, String skeleton) { public static String getBestDateTimePattern(Locale locale, String skeleton) {
DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(locale); DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(locale);
return dtpg.getBestPattern(skeleton); boolean allowDuplicateFields = !CompatChanges.isChangeEnabled(
DISALLOW_DUPLICATE_FIELD_IN_SKELETON);
return dtpg.getBestPattern(skeleton, allowDuplicateFields);
} }
/** /**

View File

@@ -21,13 +21,19 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import android.compat.testing.PlatformCompatChangeRule;
import android.icu.text.DateFormatSymbols; import android.icu.text.DateFormatSymbols;
import android.platform.test.annotations.Presubmit; import android.platform.test.annotations.Presubmit;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
import libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import java.util.Arrays; import java.util.Arrays;
@@ -38,6 +44,9 @@ import java.util.Locale;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class DateFormatTest { public class DateFormatTest {
@Rule
public TestRule compatChangeRule = new PlatformCompatChangeRule();
@Test @Test
public void testHasDesignator() { public void testHasDesignator() {
assertTrue(DateFormat.hasDesignator("hh:mm:ss", DateFormat.MINUTE)); assertTrue(DateFormat.hasDesignator("hh:mm:ss", DateFormat.MINUTE));
@@ -135,4 +144,29 @@ public class DateFormatTest {
private static String best(Locale l, String skeleton) { private static String best(Locale l, String skeleton) {
return DateFormat.getBestDateTimePattern(l, skeleton); return DateFormat.getBestDateTimePattern(l, skeleton);
} }
@Test
@EnableCompatChanges({DateFormat.DISALLOW_DUPLICATE_FIELD_IN_SKELETON})
public void testGetBestDateTimePattern_disableDuplicateField() {
assertIllegalArgumentException(Locale.US, "jmma");
assertIllegalArgumentException(Locale.US, "ahmma");
}
@Test
@DisableCompatChanges({DateFormat.DISALLOW_DUPLICATE_FIELD_IN_SKELETON})
public void testGetBestDateTimePattern_enableDuplicateField() {
// en-US uses 12-hour format by default.
assertEquals("h:mm a", DateFormat.getBestDateTimePattern(Locale.US, "jmma"));
assertEquals("h:mm a", DateFormat.getBestDateTimePattern(Locale.US, "ahmma"));
}
private static void assertIllegalArgumentException(Locale l, String skeleton) {
try {
DateFormat.getBestDateTimePattern(l, skeleton);
fail("getBestDateTimePattern() does not fail with Locale: " + l
+ " skeleton: " + skeleton);
} catch (IllegalArgumentException expected) {
// ignored
}
}
} }