Merge "a.t.f.DateFormat behaves consistently with j.t.DateFormat in en, en-US locale"
This commit is contained in:
@@ -24,12 +24,12 @@ 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.icu.util.ULocale;
|
||||||
import android.os.Build;
|
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;
|
||||||
import android.text.SpannedString;
|
import android.text.SpannedString;
|
||||||
import android.text.TextUtils;
|
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
@@ -265,11 +265,13 @@ public class DateFormat {
|
|||||||
* @return a string pattern suitable for use with {@link java.text.SimpleDateFormat}.
|
* @return a string pattern suitable for use with {@link java.text.SimpleDateFormat}.
|
||||||
*/
|
*/
|
||||||
public static String getBestDateTimePattern(Locale locale, String skeleton) {
|
public static String getBestDateTimePattern(Locale locale, String skeleton) {
|
||||||
DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(locale);
|
ULocale uLocale = ULocale.forLocale(locale);
|
||||||
|
DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(uLocale);
|
||||||
boolean allowDuplicateFields = !CompatChanges.isChangeEnabled(
|
boolean allowDuplicateFields = !CompatChanges.isChangeEnabled(
|
||||||
DISALLOW_DUPLICATE_FIELD_IN_SKELETON);
|
DISALLOW_DUPLICATE_FIELD_IN_SKELETON);
|
||||||
return dtpg.getBestPattern(skeleton, DateTimePatternGenerator.MATCH_NO_OPTIONS,
|
String pattern = dtpg.getBestPattern(skeleton, DateTimePatternGenerator.MATCH_NO_OPTIONS,
|
||||||
allowDuplicateFields);
|
allowDuplicateFields);
|
||||||
|
return getCompatibleEnglishPattern(uLocale, pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -303,10 +305,11 @@ public class DateFormat {
|
|||||||
*/
|
*/
|
||||||
@UnsupportedAppUsage
|
@UnsupportedAppUsage
|
||||||
public static String getTimeFormatString(Context context, int userHandle) {
|
public static String getTimeFormatString(Context context, int userHandle) {
|
||||||
DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(
|
ULocale uLocale = ULocale.forLocale(context.getResources().getConfiguration().locale);
|
||||||
context.getResources().getConfiguration().locale);
|
DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(uLocale);
|
||||||
return is24HourFormat(context, userHandle) ? dtpg.getBestPattern("Hm")
|
String pattern = is24HourFormat(context, userHandle) ? dtpg.getBestPattern("Hm")
|
||||||
: dtpg.getBestPattern("hm");
|
: dtpg.getBestPattern("hm");
|
||||||
|
return getCompatibleEnglishPattern(uLocale, pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -713,4 +716,21 @@ public class DateFormat {
|
|||||||
public static DateFormatSymbols getIcuDateFormatSymbols(Locale locale) {
|
public static DateFormatSymbols getIcuDateFormatSymbols(Locale locale) {
|
||||||
return new DateFormatSymbols(android.icu.util.GregorianCalendar.class, locale);
|
return new DateFormatSymbols(android.icu.util.GregorianCalendar.class, locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See http://b/266731719. It mirrors the implementation in
|
||||||
|
* {@link libcore.icu.SimpleDateFormatData.DateTimeFormatStringGenerator#postProcessPattern}
|
||||||
|
*/
|
||||||
|
private static String getCompatibleEnglishPattern(ULocale locale, String pattern) {
|
||||||
|
if (pattern == null || locale == null || !"en".equals(locale.getLanguage())) {
|
||||||
|
return pattern;
|
||||||
|
}
|
||||||
|
|
||||||
|
String region = locale.getCountry();
|
||||||
|
if (region != null && !region.isEmpty() && !"US".equals(region)) {
|
||||||
|
return pattern;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pattern.replace('\u202f', ' ');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -156,8 +156,8 @@ public class DateFormatTest {
|
|||||||
@DisableCompatChanges({DateFormat.DISALLOW_DUPLICATE_FIELD_IN_SKELETON})
|
@DisableCompatChanges({DateFormat.DISALLOW_DUPLICATE_FIELD_IN_SKELETON})
|
||||||
public void testGetBestDateTimePattern_enableDuplicateField() {
|
public void testGetBestDateTimePattern_enableDuplicateField() {
|
||||||
// en-US uses 12-hour format by default.
|
// en-US uses 12-hour format by default.
|
||||||
assertEquals("h:mm\u202fa", DateFormat.getBestDateTimePattern(Locale.US, "jmma"));
|
assertEquals("h:mm a", DateFormat.getBestDateTimePattern(Locale.US, "jmma"));
|
||||||
assertEquals("h:mm\u202fa", DateFormat.getBestDateTimePattern(Locale.US, "ahmma"));
|
assertEquals("h:mm a", DateFormat.getBestDateTimePattern(Locale.US, "ahmma"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertIllegalArgumentException(Locale l, String skeleton) {
|
private static void assertIllegalArgumentException(Locale l, String skeleton) {
|
||||||
|
|||||||
@@ -139,16 +139,16 @@ public class DateUtilsTest {
|
|||||||
fixedTime, java.text.DateFormat.SHORT, java.text.DateFormat.FULL));
|
fixedTime, java.text.DateFormat.SHORT, java.text.DateFormat.FULL));
|
||||||
|
|
||||||
final long hourDuration = 2 * 60 * 60 * 1000;
|
final long hourDuration = 2 * 60 * 60 * 1000;
|
||||||
assertEquals("5:30:15\u202fAM Greenwich Mean Time", DateUtils.formatSameDayTime(
|
assertEquals("5:30:15 AM Greenwich Mean Time", DateUtils.formatSameDayTime(
|
||||||
fixedTime + hourDuration, fixedTime, java.text.DateFormat.FULL,
|
fixedTime + hourDuration, fixedTime, java.text.DateFormat.FULL,
|
||||||
java.text.DateFormat.FULL));
|
java.text.DateFormat.FULL));
|
||||||
assertEquals("5:30:15\u202fAM", DateUtils.formatSameDayTime(fixedTime + hourDuration,
|
assertEquals("5:30:15 AM", DateUtils.formatSameDayTime(fixedTime + hourDuration,
|
||||||
fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.DEFAULT));
|
fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.DEFAULT));
|
||||||
assertEquals("5:30:15\u202fAM GMT", DateUtils.formatSameDayTime(fixedTime + hourDuration,
|
assertEquals("5:30:15 AM GMT", DateUtils.formatSameDayTime(fixedTime + hourDuration,
|
||||||
fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.LONG));
|
fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.LONG));
|
||||||
assertEquals("5:30:15\u202fAM", DateUtils.formatSameDayTime(fixedTime + hourDuration,
|
assertEquals("5:30:15 AM", DateUtils.formatSameDayTime(fixedTime + hourDuration,
|
||||||
fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.MEDIUM));
|
fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.MEDIUM));
|
||||||
assertEquals("5:30\u202fAM", DateUtils.formatSameDayTime(fixedTime + hourDuration,
|
assertEquals("5:30 AM", DateUtils.formatSameDayTime(fixedTime + hourDuration,
|
||||||
fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.SHORT));
|
fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.SHORT));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user