diff options
author | Neil Fuller <nfuller@google.com> | 2015-04-23 23:15:44 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-04-23 23:15:44 +0000 |
commit | acd9b0b85150feff5a4be4e696f411aa98fb4da1 (patch) | |
tree | 395811659f1138700003c7273e66559ddf46b569 /luni | |
parent | 99c2aa4c5ea183d615187d6db457adc32a205697 (diff) | |
parent | bd7c7451634fa46aef554dd6e09421ce2d6033a6 (diff) | |
download | libcore-acd9b0b85150feff5a4be4e696f411aa98fb4da1.zip libcore-acd9b0b85150feff5a4be4e696f411aa98fb4da1.tar.gz libcore-acd9b0b85150feff5a4be4e696f411aa98fb4da1.tar.bz2 |
am bd7c7451: am 726db7ae: Merge "Ensure consistency of capitalization in RelativeDateTimeFormatter"
* commit 'bd7c7451634fa46aef554dd6e09421ce2d6033a6':
Ensure consistency of capitalization in RelativeDateTimeFormatter
Diffstat (limited to 'luni')
6 files changed, 423 insertions, 326 deletions
diff --git a/luni/src/main/java/libcore/icu/DateIntervalFormat.java b/luni/src/main/java/libcore/icu/DateIntervalFormat.java index 10e3e6a..509d0a0 100644 --- a/luni/src/main/java/libcore/icu/DateIntervalFormat.java +++ b/luni/src/main/java/libcore/icu/DateIntervalFormat.java @@ -16,38 +16,20 @@ package libcore.icu; -import com.ibm.icu.impl.JavaTimeZone; import com.ibm.icu.util.Calendar; -import com.ibm.icu.util.GregorianCalendar; import com.ibm.icu.util.ULocale; import java.text.FieldPosition; import java.util.TimeZone; import libcore.util.BasicLruCache; +import static libcore.icu.DateUtilsBridge.FORMAT_UTC; + /** * Exposes icu4j's DateIntervalFormat. */ public final class DateIntervalFormat { - // These are all public API in DateUtils. There are others, but they're either for use with - // other methods (like FORMAT_ABBREV_RELATIVE), don't internationalize (like FORMAT_CAP_AMPM), - // or have never been implemented anyway. - public static final int FORMAT_SHOW_TIME = 0x00001; - public static final int FORMAT_SHOW_WEEKDAY = 0x00002; - public static final int FORMAT_SHOW_YEAR = 0x00004; - public static final int FORMAT_NO_YEAR = 0x00008; - public static final int FORMAT_SHOW_DATE = 0x00010; - public static final int FORMAT_NO_MONTH_DAY = 0x00020; - public static final int FORMAT_12HOUR = 0x00040; - public static final int FORMAT_24HOUR = 0x00080; - public static final int FORMAT_UTC = 0x02000; - public static final int FORMAT_ABBREV_TIME = 0x04000; - public static final int FORMAT_ABBREV_WEEKDAY = 0x08000; - public static final int FORMAT_ABBREV_MONTH = 0x10000; - public static final int FORMAT_NUMERIC_DATE = 0x20000; - public static final int FORMAT_ABBREV_ALL = 0x80000; - private static final FormatterCache CACHED_FORMATTERS = new FormatterCache(); static class FormatterCache extends BasicLruCache<String, com.ibm.icu.text.DateIntervalFormat> { @@ -67,7 +49,7 @@ public final class DateIntervalFormat { // We create a java.util.TimeZone here to use libcore's data and libcore's olson ID / pseudo-tz // logic. TimeZone tz = (olsonId != null) ? TimeZone.getTimeZone(olsonId) : TimeZone.getDefault(); - com.ibm.icu.util.TimeZone icuTimeZone = icuTimeZone(tz); + com.ibm.icu.util.TimeZone icuTimeZone = DateUtilsBridge.icuTimeZone(tz); ULocale icuLocale = ULocale.getDefault(); return formatDateRange(icuLocale, icuTimeZone, startMs, endMs, flags); } @@ -76,12 +58,12 @@ public final class DateIntervalFormat { // skeleton instead of int flags.) public static String formatDateRange(ULocale icuLocale, com.ibm.icu.util.TimeZone icuTimeZone, long startMs, long endMs, int flags) { - Calendar startCalendar = createIcuCalendar(icuTimeZone, icuLocale, startMs); + Calendar startCalendar = DateUtilsBridge.createIcuCalendar(icuTimeZone, icuLocale, startMs); Calendar endCalendar; if (startMs == endMs) { endCalendar = startCalendar; } else { - endCalendar = createIcuCalendar(icuTimeZone, icuLocale, endMs); + endCalendar = DateUtilsBridge.createIcuCalendar(icuTimeZone, icuLocale, endMs); } boolean endsAtMidnight = isMidnight(endCalendar); @@ -91,11 +73,12 @@ public final class DateIntervalFormat { // This is not the behavior of icu4j's DateIntervalFormat, but it's the historical behavior // of Android's DateUtils.formatDateRange. if (startMs != endMs && endsAtMidnight && - ((flags & FORMAT_SHOW_TIME) == 0 || dayDistance(startCalendar, endCalendar) <= 1)) { + ((flags & DateUtilsBridge.FORMAT_SHOW_TIME) == 0 + || DateUtilsBridge.dayDistance(startCalendar, endCalendar) <= 1)) { endCalendar.roll(Calendar.DAY_OF_MONTH, false); } - String skeleton = toSkeleton(startCalendar, endCalendar, flags); + String skeleton = DateUtilsBridge.toSkeleton(startCalendar, endCalendar, flags); synchronized (CACHED_FORMATTERS) { com.ibm.icu.text.DateIntervalFormat formatter = getFormatter(skeleton, icuLocale, icuTimeZone); @@ -117,86 +100,6 @@ public final class DateIntervalFormat { return formatter; } - private static String toSkeleton(Calendar startCalendar, Calendar endCalendar, int flags) { - if ((flags & FORMAT_ABBREV_ALL) != 0) { - flags |= FORMAT_ABBREV_MONTH | FORMAT_ABBREV_TIME | FORMAT_ABBREV_WEEKDAY; - } - - String monthPart = "MMMM"; - if ((flags & FORMAT_NUMERIC_DATE) != 0) { - monthPart = "M"; - } else if ((flags & FORMAT_ABBREV_MONTH) != 0) { - monthPart = "MMM"; - } - - String weekPart = "EEEE"; - if ((flags & FORMAT_ABBREV_WEEKDAY) != 0) { - weekPart = "EEE"; - } - - String timePart = "j"; // "j" means choose 12 or 24 hour based on current locale. - if ((flags & FORMAT_24HOUR) != 0) { - timePart = "H"; - } else if ((flags & FORMAT_12HOUR) != 0) { - timePart = "h"; - } - - // If we've not been asked to abbreviate times, or we're using the 24-hour clock (where it - // never makes sense to leave out the minutes), include minutes. This gets us times like - // "4 PM" while avoiding times like "16" (for "16:00"). - if ((flags & FORMAT_ABBREV_TIME) == 0 || (flags & FORMAT_24HOUR) != 0) { - timePart += "m"; - } else { - // Otherwise, we're abbreviating a 12-hour time, and should only show the minutes - // if they're not both "00". - if (!(onTheHour(startCalendar) && onTheHour(endCalendar))) { - timePart = timePart + "m"; - } - } - - if (fallOnDifferentDates(startCalendar, endCalendar)) { - flags |= FORMAT_SHOW_DATE; - } - - if (fallInSameMonth(startCalendar, endCalendar) && (flags & FORMAT_NO_MONTH_DAY) != 0) { - flags &= (~FORMAT_SHOW_WEEKDAY); - flags &= (~FORMAT_SHOW_TIME); - } - - if ((flags & (FORMAT_SHOW_DATE | FORMAT_SHOW_TIME | FORMAT_SHOW_WEEKDAY)) == 0) { - flags |= FORMAT_SHOW_DATE; - } - - // If we've been asked to show the date, work out whether we think we should show the year. - if ((flags & FORMAT_SHOW_DATE) != 0) { - if ((flags & FORMAT_SHOW_YEAR) != 0) { - // The caller explicitly wants us to show the year. - } else if ((flags & FORMAT_NO_YEAR) != 0) { - // The caller explicitly doesn't want us to show the year, even if we otherwise would. - } else if (!fallInSameYear(startCalendar, endCalendar) || !isThisYear(startCalendar)) { - flags |= FORMAT_SHOW_YEAR; - } - } - - StringBuilder builder = new StringBuilder(); - if ((flags & (FORMAT_SHOW_DATE | FORMAT_NO_MONTH_DAY)) != 0) { - if ((flags & FORMAT_SHOW_YEAR) != 0) { - builder.append("y"); - } - builder.append(monthPart); - if ((flags & FORMAT_NO_MONTH_DAY) == 0) { - builder.append("d"); - } - } - if ((flags & FORMAT_SHOW_WEEKDAY) != 0) { - builder.append(weekPart); - } - if ((flags & FORMAT_SHOW_TIME) != 0) { - builder.append(timePart); - } - return builder.toString(); - } - private static boolean isMidnight(Calendar c) { return c.get(Calendar.HOUR_OF_DAY) == 0 && c.get(Calendar.MINUTE) == 0 && @@ -204,49 +107,4 @@ public final class DateIntervalFormat { c.get(Calendar.MILLISECOND) == 0; } - private static boolean onTheHour(Calendar c) { - return c.get(Calendar.MINUTE) == 0 && c.get(Calendar.SECOND) == 0; - } - - private static boolean fallOnDifferentDates(Calendar c1, Calendar c2) { - return c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR) || - c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH) || - c1.get(Calendar.DAY_OF_MONTH) != c2.get(Calendar.DAY_OF_MONTH); - } - - private static boolean fallInSameMonth(Calendar c1, Calendar c2) { - return c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH); - } - - private static boolean fallInSameYear(Calendar c1, Calendar c2) { - return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR); - } - - private static boolean isThisYear(Calendar c) { - Calendar now = (Calendar) c.clone(); - now.setTimeInMillis(System.currentTimeMillis()); - return c.get(Calendar.YEAR) == now.get(Calendar.YEAR); - } - - public static int dayDistance(Calendar c1, Calendar c2) { - return c2.get(Calendar.JULIAN_DAY) - c1.get(Calendar.JULIAN_DAY); - } - - /** - * Creates an immutable ICU timezone backed by the specified libcore timezone data. At the time of - * writing the libcore implementation is faster but restricted to 1902 - 2038. - * Callers must not modify the {@code tz} after calling this method. - */ - static com.ibm.icu.util.TimeZone icuTimeZone(TimeZone tz) { - JavaTimeZone javaTimeZone = new JavaTimeZone(tz, null); - javaTimeZone.freeze(); // Optimization - allows the timezone to be copied cheaply. - return javaTimeZone; - } - - static Calendar createIcuCalendar(com.ibm.icu.util.TimeZone icuTimeZone, ULocale icuLocale, - long timeInMillis) { - Calendar calendar = new GregorianCalendar(icuTimeZone, icuLocale); - calendar.setTimeInMillis(timeInMillis); - return calendar; - } } diff --git a/luni/src/main/java/libcore/icu/DateTimeFormat.java b/luni/src/main/java/libcore/icu/DateTimeFormat.java new file mode 100644 index 0000000..7323c26 --- /dev/null +++ b/luni/src/main/java/libcore/icu/DateTimeFormat.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package libcore.icu; + +import com.ibm.icu.text.DateFormat; +import com.ibm.icu.text.DateTimePatternGenerator; +import com.ibm.icu.text.DisplayContext; +import com.ibm.icu.text.SimpleDateFormat; +import com.ibm.icu.util.Calendar; +import com.ibm.icu.util.ULocale; + +import libcore.util.BasicLruCache; + +/** + * A formatter that outputs a single date/time. + */ +public class DateTimeFormat { + private static final FormatterCache CACHED_FORMATTERS = new FormatterCache(); + + static class FormatterCache extends BasicLruCache<String, DateFormat> { + FormatterCache() { + super(8); + } + } + + private DateTimeFormat() { + } + + public static String format(ULocale icuLocale, Calendar time, int flags, + DisplayContext displayContext) { + String skeleton = DateUtilsBridge.toSkeleton(time, flags); + String key = skeleton + "\t" + icuLocale + "\t" + time.getTimeZone(); + synchronized(CACHED_FORMATTERS) { + DateFormat formatter = CACHED_FORMATTERS.get(key); + if (formatter == null) { + DateTimePatternGenerator generator = DateTimePatternGenerator.getInstance(icuLocale); + formatter = new SimpleDateFormat(generator.getBestPattern(skeleton), icuLocale); + CACHED_FORMATTERS.put(key, formatter); + } + formatter.setContext(displayContext); + return formatter.format(time); + } + } +} diff --git a/luni/src/main/java/libcore/icu/DateUtilsBridge.java b/luni/src/main/java/libcore/icu/DateUtilsBridge.java new file mode 100644 index 0000000..88faa90 --- /dev/null +++ b/luni/src/main/java/libcore/icu/DateUtilsBridge.java @@ -0,0 +1,177 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package libcore.icu; + +import com.ibm.icu.impl.JavaTimeZone; +import com.ibm.icu.util.Calendar; +import com.ibm.icu.util.GregorianCalendar; +import com.ibm.icu.util.ULocale; + +/** + * Common methods and constants for the various ICU formatters used to support + * android.text.format.DateUtils. + */ +public final class DateUtilsBridge { + // These are all public API in DateUtils. There are others, but they're either for use with + // other methods (like FORMAT_ABBREV_RELATIVE), don't internationalize (like FORMAT_CAP_AMPM), + // or have never been implemented anyway. + public static final int FORMAT_SHOW_TIME = 0x00001; + public static final int FORMAT_SHOW_WEEKDAY = 0x00002; + public static final int FORMAT_SHOW_YEAR = 0x00004; + public static final int FORMAT_NO_YEAR = 0x00008; + public static final int FORMAT_SHOW_DATE = 0x00010; + public static final int FORMAT_NO_MONTH_DAY = 0x00020; + public static final int FORMAT_12HOUR = 0x00040; + public static final int FORMAT_24HOUR = 0x00080; + public static final int FORMAT_UTC = 0x02000; + public static final int FORMAT_ABBREV_TIME = 0x04000; + public static final int FORMAT_ABBREV_WEEKDAY = 0x08000; + public static final int FORMAT_ABBREV_MONTH = 0x10000; + public static final int FORMAT_NUMERIC_DATE = 0x20000; + public static final int FORMAT_ABBREV_RELATIVE = 0x40000; + public static final int FORMAT_ABBREV_ALL = 0x80000; + + /** + * Creates an immutable ICU timezone backed by the specified libcore timezone data. At the time of + * writing the libcore implementation is faster but restricted to 1902 - 2038. + * Callers must not modify the {@code tz} after calling this method. + */ + public static com.ibm.icu.util.TimeZone icuTimeZone(java.util.TimeZone tz) { + JavaTimeZone javaTimeZone = new JavaTimeZone(tz, null); + javaTimeZone.freeze(); // Optimization - allows the timezone to be copied cheaply. + return javaTimeZone; + } + + public static Calendar createIcuCalendar(com.ibm.icu.util.TimeZone icuTimeZone, ULocale icuLocale, + long timeInMillis) { + Calendar calendar = new GregorianCalendar(icuTimeZone, icuLocale); + calendar.setTimeInMillis(timeInMillis); + return calendar; + } + + public static String toSkeleton(Calendar calendar, int flags) { + return toSkeleton(calendar, calendar, flags); + } + + public static String toSkeleton(Calendar startCalendar, Calendar endCalendar, int flags) { + if ((flags & FORMAT_ABBREV_ALL) != 0) { + flags |= FORMAT_ABBREV_MONTH | FORMAT_ABBREV_TIME | FORMAT_ABBREV_WEEKDAY; + } + + String monthPart = "MMMM"; + if ((flags & FORMAT_NUMERIC_DATE) != 0) { + monthPart = "M"; + } else if ((flags & FORMAT_ABBREV_MONTH) != 0) { + monthPart = "MMM"; + } + + String weekPart = "EEEE"; + if ((flags & FORMAT_ABBREV_WEEKDAY) != 0) { + weekPart = "EEE"; + } + + String timePart = "j"; // "j" means choose 12 or 24 hour based on current locale. + if ((flags & FORMAT_24HOUR) != 0) { + timePart = "H"; + } else if ((flags & FORMAT_12HOUR) != 0) { + timePart = "h"; + } + + // If we've not been asked to abbreviate times, or we're using the 24-hour clock (where it + // never makes sense to leave out the minutes), include minutes. This gets us times like + // "4 PM" while avoiding times like "16" (for "16:00"). + if ((flags & FORMAT_ABBREV_TIME) == 0 || (flags & FORMAT_24HOUR) != 0) { + timePart += "m"; + } else { + // Otherwise, we're abbreviating a 12-hour time, and should only show the minutes + // if they're not both "00". + if (!(onTheHour(startCalendar) && onTheHour(endCalendar))) { + timePart = timePart + "m"; + } + } + + if (fallOnDifferentDates(startCalendar, endCalendar)) { + flags |= FORMAT_SHOW_DATE; + } + + if (fallInSameMonth(startCalendar, endCalendar) && (flags & FORMAT_NO_MONTH_DAY) != 0) { + flags &= (~FORMAT_SHOW_WEEKDAY); + flags &= (~FORMAT_SHOW_TIME); + } + + if ((flags & (FORMAT_SHOW_DATE | FORMAT_SHOW_TIME | FORMAT_SHOW_WEEKDAY)) == 0) { + flags |= FORMAT_SHOW_DATE; + } + + // If we've been asked to show the date, work out whether we think we should show the year. + if ((flags & FORMAT_SHOW_DATE) != 0) { + if ((flags & FORMAT_SHOW_YEAR) != 0) { + // The caller explicitly wants us to show the year. + } else if ((flags & FORMAT_NO_YEAR) != 0) { + // The caller explicitly doesn't want us to show the year, even if we otherwise would. + } else if (!fallInSameYear(startCalendar, endCalendar) || !isThisYear(startCalendar)) { + flags |= FORMAT_SHOW_YEAR; + } + } + + StringBuilder builder = new StringBuilder(); + if ((flags & (FORMAT_SHOW_DATE | FORMAT_NO_MONTH_DAY)) != 0) { + if ((flags & FORMAT_SHOW_YEAR) != 0) { + builder.append("y"); + } + builder.append(monthPart); + if ((flags & FORMAT_NO_MONTH_DAY) == 0) { + builder.append("d"); + } + } + if ((flags & FORMAT_SHOW_WEEKDAY) != 0) { + builder.append(weekPart); + } + if ((flags & FORMAT_SHOW_TIME) != 0) { + builder.append(timePart); + } + return builder.toString(); + } + + public static int dayDistance(Calendar c1, Calendar c2) { + return c2.get(Calendar.JULIAN_DAY) - c1.get(Calendar.JULIAN_DAY); + } + + private static boolean onTheHour(Calendar c) { + return c.get(Calendar.MINUTE) == 0 && c.get(Calendar.SECOND) == 0; + } + + private static boolean fallOnDifferentDates(Calendar c1, Calendar c2) { + return c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR) || + c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH) || + c1.get(Calendar.DAY_OF_MONTH) != c2.get(Calendar.DAY_OF_MONTH); + } + + private static boolean fallInSameMonth(Calendar c1, Calendar c2) { + return c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH); + } + + private static boolean fallInSameYear(Calendar c1, Calendar c2) { + return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR); + } + + private static boolean isThisYear(Calendar c) { + Calendar now = (Calendar) c.clone(); + now.setTimeInMillis(System.currentTimeMillis()); + return c.get(Calendar.YEAR) == now.get(Calendar.YEAR); + } +} diff --git a/luni/src/main/java/libcore/icu/RelativeDateTimeFormatter.java b/luni/src/main/java/libcore/icu/RelativeDateTimeFormatter.java index 8d62c78..e2afa61 100644 --- a/luni/src/main/java/libcore/icu/RelativeDateTimeFormatter.java +++ b/luni/src/main/java/libcore/icu/RelativeDateTimeFormatter.java @@ -23,22 +23,20 @@ import com.ibm.icu.text.DisplayContext; import com.ibm.icu.util.Calendar; import com.ibm.icu.util.ULocale; +import static libcore.icu.DateUtilsBridge.FORMAT_ABBREV_ALL; +import static libcore.icu.DateUtilsBridge.FORMAT_ABBREV_MONTH; +import static libcore.icu.DateUtilsBridge.FORMAT_ABBREV_RELATIVE; +import static libcore.icu.DateUtilsBridge.FORMAT_NO_YEAR; +import static libcore.icu.DateUtilsBridge.FORMAT_NUMERIC_DATE; +import static libcore.icu.DateUtilsBridge.FORMAT_SHOW_DATE; +import static libcore.icu.DateUtilsBridge.FORMAT_SHOW_TIME; +import static libcore.icu.DateUtilsBridge.FORMAT_SHOW_YEAR; + /** * Exposes icu4j's RelativeDateTimeFormatter. */ public final class RelativeDateTimeFormatter { - // Values from public API in DateUtils to be used in this class. They must - // match the ones in DateUtils.java. - public static final int FORMAT_SHOW_TIME = 0x00001; - public static final int FORMAT_SHOW_YEAR = 0x00004; - public static final int FORMAT_NO_YEAR = 0x00008; - public static final int FORMAT_SHOW_DATE = 0x00010; - public static final int FORMAT_ABBREV_MONTH = 0x10000; - public static final int FORMAT_NUMERIC_DATE = 0x20000; - public static final int FORMAT_ABBREV_RELATIVE = 0x40000; - public static final int FORMAT_ABBREV_ALL = 0x80000; - public static final long SECOND_IN_MILLIS = 1000; public static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60; public static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60; @@ -58,7 +56,7 @@ public final class RelativeDateTimeFormatter { FormatterCache() { super(8); } - }; + } private RelativeDateTimeFormatter() { } @@ -92,6 +90,14 @@ public final class RelativeDateTimeFormatter { */ public static String getRelativeTimeSpanString(Locale locale, java.util.TimeZone tz, long time, long now, long minResolution, int flags) { + // Android has been inconsistent about capitalization in the past. e.g. bug http://b/20247811. + // Now we capitalize everything consistently. + final DisplayContext displayContext = DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE; + return getRelativeTimeSpanString(locale, tz, time, now, minResolution, flags, displayContext); + } + + public static String getRelativeTimeSpanString(Locale locale, java.util.TimeZone tz, long time, + long now, long minResolution, int flags, DisplayContext displayContext) { if (locale == null) { throw new NullPointerException("locale == null"); } @@ -99,30 +105,30 @@ public final class RelativeDateTimeFormatter { throw new NullPointerException("tz == null"); } ULocale icuLocale = ULocale.forLocale(locale); - com.ibm.icu.util.TimeZone icuTimeZone = DateIntervalFormat.icuTimeZone(tz); + com.ibm.icu.util.TimeZone icuTimeZone = DateUtilsBridge.icuTimeZone(tz); + return getRelativeTimeSpanString(icuLocale, icuTimeZone, time, now, minResolution, flags, + displayContext); + } + + private static String getRelativeTimeSpanString(ULocale icuLocale, + com.ibm.icu.util.TimeZone icuTimeZone, long time, long now, long minResolution, int flags, + DisplayContext displayContext) { long duration = Math.abs(now - time); boolean past = (now >= time); com.ibm.icu.text.RelativeDateTimeFormatter.Style style; if ((flags & (FORMAT_ABBREV_RELATIVE | FORMAT_ABBREV_ALL)) != 0) { - style = com.ibm.icu.text.RelativeDateTimeFormatter.Style.SHORT; + style = com.ibm.icu.text.RelativeDateTimeFormatter.Style.SHORT; } else { - style = com.ibm.icu.text.RelativeDateTimeFormatter.Style.LONG; + style = com.ibm.icu.text.RelativeDateTimeFormatter.Style.LONG; } - // We are currently using the _NONE and _FOR_BEGINNING_OF_SENTENCE for the - // capitalization. We use _NONE for relative time strings, and the latter - // to capitalize the first letter of strings that don't contain - // quantities, such as "Yesterday", "Today" and etc. This is for backward - // compatibility (see b/14493853). - DisplayContext capitalizationContext = DisplayContext.CAPITALIZATION_NONE; - com.ibm.icu.text.RelativeDateTimeFormatter.Direction direction; if (past) { - direction = com.ibm.icu.text.RelativeDateTimeFormatter.Direction.LAST; + direction = com.ibm.icu.text.RelativeDateTimeFormatter.Direction.LAST; } else { - direction = com.ibm.icu.text.RelativeDateTimeFormatter.Direction.NEXT; + direction = com.ibm.icu.text.RelativeDateTimeFormatter.Direction.NEXT; } // 'relative' defaults to true as we are generating relative time span @@ -156,18 +162,17 @@ public final class RelativeDateTimeFormatter { // because for locales that don't have special terms for "2 days ago", // icu4j returns an empty string instead of falling back to strings // like "2 days ago". - capitalizationContext = DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE; String str; if (past) { synchronized (CACHED_FORMATTERS) { - str = getFormatter(icuLocale, style, capitalizationContext) + str = getFormatter(icuLocale, style, displayContext) .format( com.ibm.icu.text.RelativeDateTimeFormatter.Direction.LAST_2, com.ibm.icu.text.RelativeDateTimeFormatter.AbsoluteUnit.DAY); } } else { synchronized (CACHED_FORMATTERS) { - str = getFormatter(icuLocale, style, capitalizationContext) + str = getFormatter(icuLocale, style, displayContext) .format( com.ibm.icu.text.RelativeDateTimeFormatter.Direction.NEXT_2, com.ibm.icu.text.RelativeDateTimeFormatter.AbsoluteUnit.DAY); @@ -176,11 +181,9 @@ public final class RelativeDateTimeFormatter { if (str != null && !str.isEmpty()) { return str; } - // Fall back to show something like "2 days ago". Reset the - // capitalization setting. - capitalizationContext = DisplayContext.CAPITALIZATION_NONE; + // Fall back to show something like "2 days ago". } else if (count == 1) { - // Show "Yesterday / Tomorrow" instead of "1 day ago / in 1 day". + // Show "Yesterday / Tomorrow" instead of "1 day ago / In 1 day". aunit = com.ibm.icu.text.RelativeDateTimeFormatter.AbsoluteUnit.DAY; relative = false; } else if (count == 0) { @@ -193,6 +196,7 @@ public final class RelativeDateTimeFormatter { count = (int)(duration / WEEK_IN_MILLIS); unit = com.ibm.icu.text.RelativeDateTimeFormatter.RelativeUnit.WEEKS; } else { + Calendar timeCalendar = DateUtilsBridge.createIcuCalendar(icuTimeZone, icuLocale, time); // The duration is longer than a week and minResolution is not // WEEK_IN_MILLIS. Return the absolute date instead of relative time. @@ -202,8 +206,7 @@ public final class RelativeDateTimeFormatter { // formatDateRange() would determine that based on the current system // time and may give wrong results. if ((flags & (FORMAT_NO_YEAR | FORMAT_SHOW_YEAR)) == 0) { - Calendar timeCalendar = DateIntervalFormat.createIcuCalendar(icuTimeZone, icuLocale, time); - Calendar nowCalendar = DateIntervalFormat.createIcuCalendar(icuTimeZone, icuLocale, now); + Calendar nowCalendar = DateUtilsBridge.createIcuCalendar(icuTimeZone, icuLocale, now); if (timeCalendar.get(Calendar.YEAR) != nowCalendar.get(Calendar.YEAR)) { flags |= FORMAT_SHOW_YEAR; @@ -211,20 +214,16 @@ public final class RelativeDateTimeFormatter { flags |= FORMAT_NO_YEAR; } } - - return DateIntervalFormat.formatDateRange(icuLocale, icuTimeZone, time, time, flags); + return DateTimeFormat.format(icuLocale, timeCalendar, flags, displayContext); } - if (relative) { - synchronized (CACHED_FORMATTERS) { - return getFormatter(icuLocale, style, capitalizationContext) - .format(count, direction, unit); - } - } else { - capitalizationContext = DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE; - synchronized (CACHED_FORMATTERS) { - return getFormatter(icuLocale, style, capitalizationContext) - .format(direction, aunit); + synchronized (CACHED_FORMATTERS) { + com.ibm.icu.text.RelativeDateTimeFormatter formatter = + getFormatter(icuLocale, style, displayContext); + if (relative) { + return formatter.format(count, direction, unit); + } else { + return formatter.format(direction, aunit); } } } @@ -268,11 +267,7 @@ public final class RelativeDateTimeFormatter { throw new NullPointerException("tz == null"); } ULocale icuLocale = ULocale.forLocale(locale); - com.ibm.icu.util.TimeZone icuTimeZone = DateIntervalFormat.icuTimeZone(tz); - - // Get the time clause first. - String timeClause = DateIntervalFormat.formatDateRange(icuLocale, icuTimeZone, time, time, - FORMAT_SHOW_TIME); + com.ibm.icu.util.TimeZone icuTimeZone = DateUtilsBridge.icuTimeZone(tz); long duration = Math.abs(now - time); // It doesn't make much sense to have results like: "1 week ago, 10:50 AM". @@ -286,14 +281,10 @@ public final class RelativeDateTimeFormatter { style = com.ibm.icu.text.RelativeDateTimeFormatter.Style.LONG; } - // icu4j also has other options available to control the capitalization. We - // are currently using the _NONE option only. - DisplayContext capitalizationContext = DisplayContext.CAPITALIZATION_NONE; - - Calendar timeCalendar = DateIntervalFormat.createIcuCalendar(icuTimeZone, icuLocale, time); - Calendar nowCalendar = DateIntervalFormat.createIcuCalendar(icuTimeZone, icuLocale, now); + Calendar timeCalendar = DateUtilsBridge.createIcuCalendar(icuTimeZone, icuLocale, time); + Calendar nowCalendar = DateUtilsBridge.createIcuCalendar(icuTimeZone, icuLocale, now); - int days = Math.abs(DateIntervalFormat.dayDistance(timeCalendar, nowCalendar)); + int days = Math.abs(DateUtilsBridge.dayDistance(timeCalendar, nowCalendar)); // Now get the date clause, either in relative format or the actual date. String dateClause; @@ -304,7 +295,8 @@ public final class RelativeDateTimeFormatter { if (days > 0 && minResolution < DAY_IN_MILLIS) { minResolution = DAY_IN_MILLIS; } - dateClause = getRelativeTimeSpanString(locale, tz, time, now, minResolution, flags); + dateClause = getRelativeTimeSpanString(icuLocale, icuTimeZone, time, now, minResolution, + flags, DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE); } else { // We always use fixed flags to format the date clause. User-supplied // flags are ignored. @@ -316,9 +308,17 @@ public final class RelativeDateTimeFormatter { flags = FORMAT_SHOW_DATE | FORMAT_NO_YEAR | FORMAT_ABBREV_MONTH; } - dateClause = DateIntervalFormat.formatDateRange(icuLocale, icuTimeZone, time, time, flags); + dateClause = DateTimeFormat.format(icuLocale, timeCalendar, flags, + DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE); } + String timeClause = DateTimeFormat.format(icuLocale, timeCalendar, FORMAT_SHOW_TIME, + DisplayContext.CAPITALIZATION_NONE); + + // icu4j also has other options available to control the capitalization. We are currently using + // the _NONE option only. + DisplayContext capitalizationContext = DisplayContext.CAPITALIZATION_NONE; + // Combine the two clauses, such as '5 days ago, 10:50 AM'. synchronized (CACHED_FORMATTERS) { return getFormatter(icuLocale, style, capitalizationContext) @@ -336,12 +336,12 @@ public final class RelativeDateTimeFormatter { */ private static com.ibm.icu.text.RelativeDateTimeFormatter getFormatter( ULocale locale, com.ibm.icu.text.RelativeDateTimeFormatter.Style style, - DisplayContext capitalizationContext) { - String key = locale + "\t" + style + "\t" + capitalizationContext; + DisplayContext displayContext) { + String key = locale + "\t" + style + "\t" + displayContext; com.ibm.icu.text.RelativeDateTimeFormatter formatter = CACHED_FORMATTERS.get(key); if (formatter == null) { formatter = com.ibm.icu.text.RelativeDateTimeFormatter.getInstance( - locale, null, style, capitalizationContext); + locale, null, style, displayContext); CACHED_FORMATTERS.put(key, formatter); } return formatter; diff --git a/luni/src/test/java/libcore/icu/DateIntervalFormatTest.java b/luni/src/test/java/libcore/icu/DateIntervalFormatTest.java index d14710c..7adad72 100644 --- a/luni/src/test/java/libcore/icu/DateIntervalFormatTest.java +++ b/luni/src/test/java/libcore/icu/DateIntervalFormatTest.java @@ -20,7 +20,8 @@ import android.icu.util.Calendar; import android.icu.util.TimeZone; import android.icu.util.ULocale; -import static libcore.icu.DateIntervalFormat.*; +import static libcore.icu.DateIntervalFormat.formatDateRange; +import static libcore.icu.DateUtilsBridge.*; public class DateIntervalFormatTest extends junit.framework.TestCase { private static final long MINUTE = 60 * 1000; diff --git a/luni/src/test/java/libcore/icu/RelativeDateTimeFormatterTest.java b/luni/src/test/java/libcore/icu/RelativeDateTimeFormatterTest.java index 0b7f8c3..101896f 100644 --- a/luni/src/test/java/libcore/icu/RelativeDateTimeFormatterTest.java +++ b/luni/src/test/java/libcore/icu/RelativeDateTimeFormatterTest.java @@ -16,22 +16,25 @@ package libcore.icu; +import android.icu.util.ULocale; + import java.util.Calendar; import java.util.Locale; import java.util.TimeZone; -import static libcore.icu.RelativeDateTimeFormatter.getRelativeDateTimeString; -import static libcore.icu.RelativeDateTimeFormatter.getRelativeTimeSpanString; -import static libcore.icu.RelativeDateTimeFormatter.FORMAT_ABBREV_ALL; -import static libcore.icu.RelativeDateTimeFormatter.FORMAT_ABBREV_RELATIVE; -import static libcore.icu.RelativeDateTimeFormatter.FORMAT_NUMERIC_DATE; -import static libcore.icu.RelativeDateTimeFormatter.FORMAT_NO_YEAR; -import static libcore.icu.RelativeDateTimeFormatter.FORMAT_SHOW_YEAR; -import static libcore.icu.RelativeDateTimeFormatter.SECOND_IN_MILLIS; -import static libcore.icu.RelativeDateTimeFormatter.MINUTE_IN_MILLIS; -import static libcore.icu.RelativeDateTimeFormatter.HOUR_IN_MILLIS; + +import static libcore.icu.DateUtilsBridge.FORMAT_ABBREV_ALL; +import static libcore.icu.DateUtilsBridge.FORMAT_ABBREV_RELATIVE; +import static libcore.icu.DateUtilsBridge.FORMAT_NO_YEAR; +import static libcore.icu.DateUtilsBridge.FORMAT_NUMERIC_DATE; +import static libcore.icu.DateUtilsBridge.FORMAT_SHOW_YEAR; import static libcore.icu.RelativeDateTimeFormatter.DAY_IN_MILLIS; +import static libcore.icu.RelativeDateTimeFormatter.HOUR_IN_MILLIS; +import static libcore.icu.RelativeDateTimeFormatter.MINUTE_IN_MILLIS; +import static libcore.icu.RelativeDateTimeFormatter.SECOND_IN_MILLIS; import static libcore.icu.RelativeDateTimeFormatter.WEEK_IN_MILLIS; import static libcore.icu.RelativeDateTimeFormatter.YEAR_IN_MILLIS; +import static libcore.icu.RelativeDateTimeFormatter.getRelativeDateTimeString; +import static libcore.icu.RelativeDateTimeFormatter.getRelativeTimeSpanString; public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { @@ -47,19 +50,19 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { assertEquals("0 minutes ago", getRelativeTimeSpanString(en_US, tz, baseTime - SECOND_IN_MILLIS, baseTime, MINUTE_IN_MILLIS, 0)); - assertEquals("in 0 minutes", + assertEquals("In 0 minutes", getRelativeTimeSpanString(en_US, tz, baseTime + SECOND_IN_MILLIS, baseTime, MINUTE_IN_MILLIS, 0)); assertEquals("1 minute ago", getRelativeTimeSpanString(en_US, tz, 0, MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, 0)); - assertEquals("in 1 minute", + assertEquals("In 1 minute", getRelativeTimeSpanString(en_US, tz, MINUTE_IN_MILLIS, 0, MINUTE_IN_MILLIS, 0)); assertEquals("42 minutes ago", getRelativeTimeSpanString(en_US, tz, baseTime - 42 * MINUTE_IN_MILLIS, baseTime, MINUTE_IN_MILLIS, 0)); - assertEquals("in 42 minutes", + assertEquals("In 42 minutes", getRelativeTimeSpanString(en_US, tz, baseTime + 42 * MINUTE_IN_MILLIS, baseTime, MINUTE_IN_MILLIS, 0)); @@ -67,17 +70,17 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { assertEquals("2 hours ago", getRelativeTimeSpanString(en_US, tz, baseTime - TWO_HOURS_IN_MS, baseTime, MINUTE_IN_MILLIS, FORMAT_NUMERIC_DATE)); - assertEquals("in 2 hours", + assertEquals("In 2 hours", getRelativeTimeSpanString(en_US, tz, baseTime + TWO_HOURS_IN_MS, baseTime, MINUTE_IN_MILLIS, FORMAT_NUMERIC_DATE)); - assertEquals("in 42 min.", + assertEquals("In 42 min.", getRelativeTimeSpanString(en_US, tz, baseTime + (42 * MINUTE_IN_MILLIS), baseTime, MINUTE_IN_MILLIS, FORMAT_ABBREV_RELATIVE)); assertEquals("Tomorrow", getRelativeTimeSpanString(en_US, tz, DAY_IN_MILLIS, 0, DAY_IN_MILLIS, 0)); - assertEquals("in 2 days", + assertEquals("In 2 days", getRelativeTimeSpanString(en_US, tz, 2 * DAY_IN_MILLIS, 0, DAY_IN_MILLIS, 0)); assertEquals("Yesterday", getRelativeTimeSpanString(en_US, tz, 0, DAY_IN_MILLIS, DAY_IN_MILLIS, 0)); @@ -115,46 +118,46 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { public void test_getRelativeTimeSpanString() throws Exception { test_getRelativeTimeSpanString_helper(0 * SECOND_IN_MILLIS, 0, "0 seconds ago", "0 seconds ago"); - test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, 0, "1 minute ago", "in 1 minute"); - test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, 0, "1 minute ago", "in 1 minute"); - test_getRelativeTimeSpanString_helper(5 * DAY_IN_MILLIS, 0, "5 days ago", "in 5 days"); + test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, 0, "1 minute ago", "In 1 minute"); + test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, 0, "1 minute ago", "In 1 minute"); + test_getRelativeTimeSpanString_helper(5 * DAY_IN_MILLIS, 0, "5 days ago", "In 5 days"); test_getRelativeTimeSpanString_helper(0 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "0 seconds ago", "0 seconds ago"); test_getRelativeTimeSpanString_helper(1 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "1 second ago", - "in 1 second"); + "In 1 second"); test_getRelativeTimeSpanString_helper(2 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "2 seconds ago", - "in 2 seconds"); + "In 2 seconds"); test_getRelativeTimeSpanString_helper(25 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "25 seconds ago", - "in 25 seconds"); + "In 25 seconds"); test_getRelativeTimeSpanString_helper(75 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "1 minute ago", - "in 1 minute"); + "In 1 minute"); test_getRelativeTimeSpanString_helper(5000 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "1 hour ago", - "in 1 hour"); + "In 1 hour"); test_getRelativeTimeSpanString_helper(0 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "0 minutes ago", "0 minutes ago"); test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "1 minute ago", - "in 1 minute"); + "In 1 minute"); test_getRelativeTimeSpanString_helper(2 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "2 minutes ago", - "in 2 minutes"); + "In 2 minutes"); test_getRelativeTimeSpanString_helper(25 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "25 minutes ago", - "in 25 minutes"); + "In 25 minutes"); test_getRelativeTimeSpanString_helper(75 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "1 hour ago", - "in 1 hour"); + "In 1 hour"); test_getRelativeTimeSpanString_helper(720 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "12 hours ago", - "in 12 hours"); + "In 12 hours"); test_getRelativeTimeSpanString_helper(0 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, "0 hours ago", "0 hours ago"); test_getRelativeTimeSpanString_helper(1 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, "1 hour ago", - "in 1 hour"); + "In 1 hour"); test_getRelativeTimeSpanString_helper(2 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, "2 hours ago", - "in 2 hours"); + "In 2 hours"); test_getRelativeTimeSpanString_helper(5 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, "5 hours ago", - "in 5 hours"); + "In 5 hours"); test_getRelativeTimeSpanString_helper(20 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, "20 hours ago", - "in 20 hours"); + "In 20 hours"); test_getRelativeTimeSpanString_helper(0 * DAY_IN_MILLIS, DAY_IN_MILLIS, "Today", "Today"); test_getRelativeTimeSpanString_helper(20 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "Yesterday", @@ -162,68 +165,68 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { test_getRelativeTimeSpanString_helper(24 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "Yesterday", "Tomorrow"); test_getRelativeTimeSpanString_helper(2 * DAY_IN_MILLIS, DAY_IN_MILLIS, "2 days ago", - "in 2 days"); + "In 2 days"); test_getRelativeTimeSpanString_helper(25 * DAY_IN_MILLIS, DAY_IN_MILLIS, "January 11", "March 2"); test_getRelativeTimeSpanString_helper(0 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, "0 weeks ago", "0 weeks ago"); test_getRelativeTimeSpanString_helper(1 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, "1 week ago", - "in 1 week"); + "In 1 week"); test_getRelativeTimeSpanString_helper(2 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, "2 weeks ago", - "in 2 weeks"); + "In 2 weeks"); test_getRelativeTimeSpanString_helper(25 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, "25 weeks ago", - "in 25 weeks"); + "In 25 weeks"); // duration >= minResolution test_getRelativeTimeSpanString_helper(30 * SECOND_IN_MILLIS, 0, "30 seconds ago", - "in 30 seconds"); + "In 30 seconds"); test_getRelativeTimeSpanString_helper(30 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, - "30 minutes ago", "in 30 minutes"); + "30 minutes ago", "In 30 minutes"); test_getRelativeTimeSpanString_helper(30 * HOUR_IN_MILLIS, MINUTE_IN_MILLIS, "Yesterday", "Tomorrow"); test_getRelativeTimeSpanString_helper(5 * DAY_IN_MILLIS, MINUTE_IN_MILLIS, "5 days ago", - "in 5 days"); + "In 5 days"); test_getRelativeTimeSpanString_helper(30 * WEEK_IN_MILLIS, MINUTE_IN_MILLIS, "July 10, 2014", "September 3"); test_getRelativeTimeSpanString_helper(5 * 365 * DAY_IN_MILLIS, MINUTE_IN_MILLIS, "February 6, 2010", "February 4, 2020"); test_getRelativeTimeSpanString_helper(60 * SECOND_IN_MILLIS, MINUTE_IN_MILLIS, "1 minute ago", - "in 1 minute"); + "In 1 minute"); test_getRelativeTimeSpanString_helper(120 * SECOND_IN_MILLIS - 1, MINUTE_IN_MILLIS, - "1 minute ago", "in 1 minute"); + "1 minute ago", "In 1 minute"); test_getRelativeTimeSpanString_helper(60 * MINUTE_IN_MILLIS, HOUR_IN_MILLIS, "1 hour ago", - "in 1 hour"); + "In 1 hour"); test_getRelativeTimeSpanString_helper(120 * MINUTE_IN_MILLIS - 1, HOUR_IN_MILLIS, "1 hour ago", - "in 1 hour"); + "In 1 hour"); test_getRelativeTimeSpanString_helper(2 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "Today", "Today"); test_getRelativeTimeSpanString_helper(12 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "Yesterday", "Today"); test_getRelativeTimeSpanString_helper(24 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "Yesterday", "Tomorrow"); test_getRelativeTimeSpanString_helper(48 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "2 days ago", - "in 2 days"); + "In 2 days"); test_getRelativeTimeSpanString_helper(45 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "2 days ago", - "in 2 days"); + "In 2 days"); test_getRelativeTimeSpanString_helper(7 * DAY_IN_MILLIS, WEEK_IN_MILLIS, "1 week ago", - "in 1 week"); + "In 1 week"); test_getRelativeTimeSpanString_helper(14 * DAY_IN_MILLIS - 1, WEEK_IN_MILLIS, "1 week ago", - "in 1 week"); + "In 1 week"); // duration < minResolution test_getRelativeTimeSpanString_helper(59 * SECOND_IN_MILLIS, MINUTE_IN_MILLIS, "0 minutes ago", - "in 0 minutes"); + "In 0 minutes"); test_getRelativeTimeSpanString_helper(59 * MINUTE_IN_MILLIS, HOUR_IN_MILLIS, "0 hours ago", - "in 0 hours"); + "In 0 hours"); test_getRelativeTimeSpanString_helper(HOUR_IN_MILLIS - 1, HOUR_IN_MILLIS, "0 hours ago", - "in 0 hours"); + "In 0 hours"); test_getRelativeTimeSpanString_helper(DAY_IN_MILLIS - 1, DAY_IN_MILLIS, "Yesterday", "Tomorrow"); test_getRelativeTimeSpanString_helper(20 * SECOND_IN_MILLIS, WEEK_IN_MILLIS, "0 weeks ago", - "in 0 weeks"); + "In 0 weeks"); test_getRelativeTimeSpanString_helper(WEEK_IN_MILLIS - 1, WEEK_IN_MILLIS, "0 weeks ago", - "in 0 weeks"); + "In 0 weeks"); } public void test_getRelativeTimeSpanStringAbbrev() throws Exception { @@ -232,45 +235,45 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { test_getRelativeTimeSpanString_helper(0 * SECOND_IN_MILLIS, 0, flags, "0 sec. ago", "0 sec. ago"); test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, 0, flags, "1 min. ago", - "in 1 min."); - test_getRelativeTimeSpanString_helper(5 * DAY_IN_MILLIS, 0, flags, "5 days ago", "in 5 days"); + "In 1 min."); + test_getRelativeTimeSpanString_helper(5 * DAY_IN_MILLIS, 0, flags, "5 days ago", "In 5 days"); test_getRelativeTimeSpanString_helper(0 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags, "0 sec. ago", "0 sec. ago"); test_getRelativeTimeSpanString_helper(1 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags, - "1 sec. ago", "in 1 sec."); + "1 sec. ago", "In 1 sec."); test_getRelativeTimeSpanString_helper(2 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags, - "2 sec. ago", "in 2 sec."); + "2 sec. ago", "In 2 sec."); test_getRelativeTimeSpanString_helper(25 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags, - "25 sec. ago", "in 25 sec."); + "25 sec. ago", "In 25 sec."); test_getRelativeTimeSpanString_helper(75 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags, - "1 min. ago", "in 1 min."); + "1 min. ago", "In 1 min."); test_getRelativeTimeSpanString_helper(5000 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags, - "1 hr. ago", "in 1 hr."); + "1 hr. ago", "In 1 hr."); test_getRelativeTimeSpanString_helper(0 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags, "0 min. ago", "0 min. ago"); test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags, - "1 min. ago", "in 1 min."); + "1 min. ago", "In 1 min."); test_getRelativeTimeSpanString_helper(2 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags, - "2 min. ago", "in 2 min."); + "2 min. ago", "In 2 min."); test_getRelativeTimeSpanString_helper(25 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags, - "25 min. ago", "in 25 min."); + "25 min. ago", "In 25 min."); test_getRelativeTimeSpanString_helper(75 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags, - "1 hr. ago", "in 1 hr."); + "1 hr. ago", "In 1 hr."); test_getRelativeTimeSpanString_helper(720 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags, - "12 hr. ago", "in 12 hr."); + "12 hr. ago", "In 12 hr."); test_getRelativeTimeSpanString_helper(0 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, flags, "0 hr. ago", "0 hr. ago"); test_getRelativeTimeSpanString_helper(1 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, flags, - "1 hr. ago", "in 1 hr."); + "1 hr. ago", "In 1 hr."); test_getRelativeTimeSpanString_helper(2 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, flags, - "2 hr. ago", "in 2 hr."); + "2 hr. ago", "In 2 hr."); test_getRelativeTimeSpanString_helper(5 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, flags, - "5 hr. ago", "in 5 hr."); + "5 hr. ago", "In 5 hr."); test_getRelativeTimeSpanString_helper(20 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, flags, - "20 hr. ago", "in 20 hr."); + "20 hr. ago", "In 20 hr."); test_getRelativeTimeSpanString_helper(0 * DAY_IN_MILLIS, DAY_IN_MILLIS, flags, "Today", "Today"); @@ -279,41 +282,41 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { test_getRelativeTimeSpanString_helper(24 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags, "Yesterday", "Tomorrow"); test_getRelativeTimeSpanString_helper(2 * DAY_IN_MILLIS, DAY_IN_MILLIS, flags, - "2 days ago", "in 2 days"); + "2 days ago", "In 2 days"); test_getRelativeTimeSpanString_helper(25 * DAY_IN_MILLIS, DAY_IN_MILLIS, flags, "January 11", "March 2"); test_getRelativeTimeSpanString_helper(0 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, flags, "0 wk. ago", "0 wk. ago"); test_getRelativeTimeSpanString_helper(1 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, flags, - "1 wk. ago", "in 1 wk."); + "1 wk. ago", "In 1 wk."); test_getRelativeTimeSpanString_helper(2 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, flags, - "2 wk. ago", "in 2 wk."); + "2 wk. ago", "In 2 wk."); test_getRelativeTimeSpanString_helper(25 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, flags, - "25 wk. ago", "in 25 wk."); + "25 wk. ago", "In 25 wk."); // duration >= minResolution test_getRelativeTimeSpanString_helper(30 * SECOND_IN_MILLIS, 0, flags, "30 sec. ago", - "in 30 sec."); + "In 30 sec."); test_getRelativeTimeSpanString_helper(30 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags, - "30 min. ago", "in 30 min."); + "30 min. ago", "In 30 min."); test_getRelativeTimeSpanString_helper(30 * HOUR_IN_MILLIS, MINUTE_IN_MILLIS, flags, "Yesterday", "Tomorrow"); test_getRelativeTimeSpanString_helper(5 * DAY_IN_MILLIS, MINUTE_IN_MILLIS, flags, - "5 days ago", "in 5 days"); + "5 days ago", "In 5 days"); test_getRelativeTimeSpanString_helper(30 * WEEK_IN_MILLIS, MINUTE_IN_MILLIS, flags, "July 10, 2014", "September 3"); test_getRelativeTimeSpanString_helper(5 * 365 * DAY_IN_MILLIS, MINUTE_IN_MILLIS, flags, "February 6, 2010", "February 4, 2020"); test_getRelativeTimeSpanString_helper(60 * SECOND_IN_MILLIS, MINUTE_IN_MILLIS, flags, - "1 min. ago", "in 1 min."); + "1 min. ago", "In 1 min."); test_getRelativeTimeSpanString_helper(120 * SECOND_IN_MILLIS - 1, MINUTE_IN_MILLIS, flags, - "1 min. ago", "in 1 min."); + "1 min. ago", "In 1 min."); test_getRelativeTimeSpanString_helper(60 * MINUTE_IN_MILLIS, HOUR_IN_MILLIS, flags, - "1 hr. ago", "in 1 hr."); + "1 hr. ago", "In 1 hr."); test_getRelativeTimeSpanString_helper(120 * MINUTE_IN_MILLIS - 1, HOUR_IN_MILLIS, flags, - "1 hr. ago", "in 1 hr."); + "1 hr. ago", "In 1 hr."); test_getRelativeTimeSpanString_helper(2 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags, "Today", "Today"); test_getRelativeTimeSpanString_helper(12 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags, @@ -321,27 +324,27 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { test_getRelativeTimeSpanString_helper(24 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags, "Yesterday", "Tomorrow"); test_getRelativeTimeSpanString_helper(48 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags, - "2 days ago", "in 2 days"); + "2 days ago", "In 2 days"); test_getRelativeTimeSpanString_helper(45 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags, - "2 days ago", "in 2 days"); + "2 days ago", "In 2 days"); test_getRelativeTimeSpanString_helper(7 * DAY_IN_MILLIS, WEEK_IN_MILLIS, flags, - "1 wk. ago", "in 1 wk."); + "1 wk. ago", "In 1 wk."); test_getRelativeTimeSpanString_helper(14 * DAY_IN_MILLIS - 1, WEEK_IN_MILLIS, flags, - "1 wk. ago", "in 1 wk."); + "1 wk. ago", "In 1 wk."); // duration < minResolution test_getRelativeTimeSpanString_helper(59 * SECOND_IN_MILLIS, MINUTE_IN_MILLIS, flags, - "0 min. ago", "in 0 min."); + "0 min. ago", "In 0 min."); test_getRelativeTimeSpanString_helper(59 * MINUTE_IN_MILLIS, HOUR_IN_MILLIS, flags, - "0 hr. ago", "in 0 hr."); + "0 hr. ago", "In 0 hr."); test_getRelativeTimeSpanString_helper(HOUR_IN_MILLIS - 1, HOUR_IN_MILLIS, flags, - "0 hr. ago", "in 0 hr."); + "0 hr. ago", "In 0 hr."); test_getRelativeTimeSpanString_helper(DAY_IN_MILLIS - 1, DAY_IN_MILLIS, flags, "Yesterday", "Tomorrow"); test_getRelativeTimeSpanString_helper(20 * SECOND_IN_MILLIS, WEEK_IN_MILLIS, flags, - "0 wk. ago", "in 0 wk."); + "0 wk. ago", "In 0 wk."); test_getRelativeTimeSpanString_helper(WEEK_IN_MILLIS - 1, WEEK_IN_MILLIS, flags, - "0 wk. ago", "in 0 wk."); + "0 wk. ago", "In 0 wk."); } @@ -358,21 +361,21 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { final long now = cal.getTimeInMillis(); // 42 minutes ago - assertEquals("vor 42 Minuten", getRelativeTimeSpanString(de_DE, tz, + assertEquals("Vor 42 Minuten", getRelativeTimeSpanString(de_DE, tz, now - 42 * MINUTE_IN_MILLIS, now, MINUTE_IN_MILLIS, 0)); - // in 42 minutes - assertEquals("in 42 Minuten", getRelativeTimeSpanString(de_DE, tz, + // In 42 minutes + assertEquals("In 42 Minuten", getRelativeTimeSpanString(de_DE, tz, now + 42 * MINUTE_IN_MILLIS, now, MINUTE_IN_MILLIS, 0)); - // yesterday + // Yesterday assertEquals("Gestern", getRelativeTimeSpanString(de_DE, tz, now - DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0)); - // the day before yesterday + // The day before yesterday assertEquals("Vorgestern", getRelativeTimeSpanString(de_DE, tz, now - 2 * DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0)); - // tomorrow + // Tomorrow assertEquals("Morgen", getRelativeTimeSpanString(de_DE, tz, now + DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0)); - // the day after tomorrow + // The day after tomorrow assertEquals("Übermorgen", getRelativeTimeSpanString(de_DE, tz, now + 2 * DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0)); } @@ -386,21 +389,21 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { final long now = cal.getTimeInMillis(); // 42 minutes ago - assertEquals("il y a 42 minutes", getRelativeTimeSpanString(fr_FR, tz, + assertEquals("Il y a 42 minutes", getRelativeTimeSpanString(fr_FR, tz, now - (42 * MINUTE_IN_MILLIS), now, MINUTE_IN_MILLIS, 0)); - // in 42 minutes - assertEquals("dans 42 minutes", getRelativeTimeSpanString(fr_FR, tz, + // In 42 minutes + assertEquals("Dans 42 minutes", getRelativeTimeSpanString(fr_FR, tz, now + (42 * MINUTE_IN_MILLIS), now, MINUTE_IN_MILLIS, 0)); - // yesterday + // Yesterday assertEquals("Hier", getRelativeTimeSpanString(fr_FR, tz, now - DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0)); - // the day before yesterday + // The day before yesterday assertEquals("Avant-hier", getRelativeTimeSpanString(fr_FR, tz, now - 2 * DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0)); - // tomorrow + // Tomorrow assertEquals("Demain", getRelativeTimeSpanString(fr_FR, tz, now + DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0)); - // the day after tomorrow + // The day after tomorrow assertEquals("Après-demain", getRelativeTimeSpanString(fr_FR, tz, now + 2 * DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0)); } @@ -474,10 +477,10 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { getRelativeDateTimeString(en_US, tz, base - 5 * HOUR_IN_MILLIS, base, 0, WEEK_IN_MILLIS, 0)); - // 1 hour after 2:00 AM should be formatted as 'in 1 hour, 4:00 AM'. + // 1 hour after 2:00 AM should be formatted as 'In 1 hour, 4:00 AM'. cal.set(2014, Calendar.MARCH, 9, 2, 0, 0); base = cal.getTimeInMillis(); - assertEquals("in 1 hour, 4:00 AM", + assertEquals("In 1 hour, 4:00 AM", getRelativeDateTimeString(en_US, tz, base + 1 * HOUR_IN_MILLIS, base, 0, WEEK_IN_MILLIS, 0)); @@ -491,16 +494,16 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { cal.set(2014, Calendar.NOVEMBER, 2, 0, 45, 0); base = cal.getTimeInMillis(); - // 45 minutes after 0:45 AM should be 'in 45 minutes, 1:30 AM'. - assertEquals("in 45 minutes, 1:30 AM", + // 45 minutes after 0:45 AM should be 'In 45 minutes, 1:30 AM'. + assertEquals("In 45 minutes, 1:30 AM", getRelativeDateTimeString(en_US, tz, base + 45 * MINUTE_IN_MILLIS, base, 0, WEEK_IN_MILLIS, 0)); - // 45 minutes later, it should be 'in 45 minutes, 1:15 AM'. - assertEquals("in 45 minutes, 1:15 AM", + // 45 minutes later, it should be 'In 45 minutes, 1:15 AM'. + assertEquals("In 45 minutes, 1:15 AM", getRelativeDateTimeString(en_US, tz, base + 90 * MINUTE_IN_MILLIS, base + 45 * MINUTE_IN_MILLIS, 0, WEEK_IN_MILLIS, 0)); - // Another 45 minutes later, it should be 'in 45 minutes, 2:00 AM'. - assertEquals("in 45 minutes, 2:00 AM", + // Another 45 minutes later, it should be 'In 45 minutes, 2:00 AM'. + assertEquals("In 45 minutes, 2:00 AM", getRelativeDateTimeString(en_US, tz, base + 135 * MINUTE_IN_MILLIS, base + 90 * MINUTE_IN_MILLIS, 0, WEEK_IN_MILLIS, 0)); } @@ -596,7 +599,7 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { Calendar twoDaysLaterCalendar1 = Calendar.getInstance(tz, en_US); twoDaysLaterCalendar1.set(2011, Calendar.SEPTEMBER, 4, 10, 22, 0); long twoDaysLater1 = twoDaysLaterCalendar1.getTimeInMillis(); - assertEquals("in 2 days, 10:22 AM", + assertEquals("In 2 days, 10:22 AM", getRelativeDateTimeString(en_US, tz, twoDaysLater1, now, MINUTE_IN_MILLIS, WEEK_IN_MILLIS, 0)); @@ -604,7 +607,7 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { Calendar twoDaysLaterCalendar2 = Calendar.getInstance(tz, en_US); twoDaysLaterCalendar2.set(2011, Calendar.SEPTEMBER, 4, 10, 24, 0); long twoDaysLater2 = twoDaysLaterCalendar2.getTimeInMillis(); - assertEquals("in 2 days, 10:24 AM", + assertEquals("In 2 days, 10:24 AM", getRelativeDateTimeString(en_US, tz, twoDaysLater2, now, MINUTE_IN_MILLIS, WEEK_IN_MILLIS, 0)); } |