diff options
| author | 2023-04-04 11:41:00 +0100 | |
|---|---|---|
| committer | 2023-04-04 11:59:44 +0100 | |
| commit | 14539d35bc1ed947b1837ed706c50349fc81c47e (patch) | |
| tree | b585da81b4210e2a36a57dc14fc9bc290f5bb287 | |
| parent | dee01562d00f3f1f64250f09fa7cd6b111541bf6 (diff) | |
a.t.f.DateFormat behaves consistently with j.t.DateFormat in en, en-US locale
DateUtils.formatSameDayTime() test is also updated, because the javadoc
refers to j.t.DateFormat directly. It makes sense to follow
j.t.DateFormat behavior.
The rest of a.t.f.DateUtils is not updated because it's used for
different purposes, e.g. date/time interval, relative date/time, duration, etc.
'\u202f' NNBS character use is acceptable in this case because
it's unlikely being parsed.
Bug: 266731719
Test: atest FrameworksCoreTests:android.text.format
Test: atest CtsTextTestCases:android.text.format
Change-Id: Ib5c916e2b7a66a5faef7bf214be25db88213d91d
3 files changed, 33 insertions, 13 deletions
diff --git a/core/java/android/text/format/DateFormat.java b/core/java/android/text/format/DateFormat.java index d48d566fd860..e6dad27d595b 100755 --- a/core/java/android/text/format/DateFormat.java +++ b/core/java/android/text/format/DateFormat.java @@ -24,12 +24,12 @@ import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; import android.icu.text.DateFormatSymbols; import android.icu.text.DateTimePatternGenerator; +import android.icu.util.ULocale; import android.os.Build; import android.provider.Settings; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.SpannedString; -import android.text.TextUtils; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -265,11 +265,13 @@ public class DateFormat { * @return a string pattern suitable for use with {@link java.text.SimpleDateFormat}. */ 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( DISALLOW_DUPLICATE_FIELD_IN_SKELETON); - return dtpg.getBestPattern(skeleton, DateTimePatternGenerator.MATCH_NO_OPTIONS, + String pattern = dtpg.getBestPattern(skeleton, DateTimePatternGenerator.MATCH_NO_OPTIONS, allowDuplicateFields); + return getCompatibleEnglishPattern(uLocale, pattern); } /** @@ -303,10 +305,11 @@ public class DateFormat { */ @UnsupportedAppUsage public static String getTimeFormatString(Context context, int userHandle) { - DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance( - context.getResources().getConfiguration().locale); - return is24HourFormat(context, userHandle) ? dtpg.getBestPattern("Hm") + ULocale uLocale = ULocale.forLocale(context.getResources().getConfiguration().locale); + DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(uLocale); + String pattern = is24HourFormat(context, userHandle) ? dtpg.getBestPattern("Hm") : dtpg.getBestPattern("hm"); + return getCompatibleEnglishPattern(uLocale, pattern); } /** @@ -713,4 +716,21 @@ public class DateFormat { public static DateFormatSymbols getIcuDateFormatSymbols(Locale 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', ' '); + } } diff --git a/core/tests/coretests/src/android/text/format/DateFormatTest.java b/core/tests/coretests/src/android/text/format/DateFormatTest.java index 8459330cc07b..212cc44eefab 100644 --- a/core/tests/coretests/src/android/text/format/DateFormatTest.java +++ b/core/tests/coretests/src/android/text/format/DateFormatTest.java @@ -156,8 +156,8 @@ public class DateFormatTest { @DisableCompatChanges({DateFormat.DISALLOW_DUPLICATE_FIELD_IN_SKELETON}) public void testGetBestDateTimePattern_enableDuplicateField() { // en-US uses 12-hour format by default. - assertEquals("h:mm\u202fa", DateFormat.getBestDateTimePattern(Locale.US, "jmma")); - assertEquals("h:mm\u202fa", DateFormat.getBestDateTimePattern(Locale.US, "ahmma")); + 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) { diff --git a/core/tests/coretests/src/android/text/format/DateUtilsTest.java b/core/tests/coretests/src/android/text/format/DateUtilsTest.java index 39ed82ef40f3..381c0512c532 100644 --- a/core/tests/coretests/src/android/text/format/DateUtilsTest.java +++ b/core/tests/coretests/src/android/text/format/DateUtilsTest.java @@ -139,16 +139,16 @@ public class DateUtilsTest { fixedTime, java.text.DateFormat.SHORT, java.text.DateFormat.FULL)); 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, 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)); - 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)); - 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)); - 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)); } |