diff options
author | Tao Bao <tbao@google.com> | 2015-03-23 23:32:31 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-03-23 23:32:31 +0000 |
commit | 61ac740852476171ce2b2023887fdf7759b09ef6 (patch) | |
tree | c229d6d1a9a68370d89789a9ec49e0aec5db545e | |
parent | 7cc1a6c4408549324ef9f35dcb57c947693987f0 (diff) | |
parent | af695ad46bc26b306dc1c1640663c358adeb430f (diff) | |
download | libcore-61ac740852476171ce2b2023887fdf7759b09ef6.zip libcore-61ac740852476171ce2b2023887fdf7759b09ef6.tar.gz libcore-61ac740852476171ce2b2023887fdf7759b09ef6.tar.bz2 |
Merge "Fix the year display when formatting absolute date string"
-rw-r--r-- | luni/src/main/java/libcore/icu/RelativeDateTimeFormatter.java | 29 | ||||
-rw-r--r-- | luni/src/test/java/libcore/icu/RelativeDateTimeFormatterTest.java | 55 |
2 files changed, 79 insertions, 5 deletions
diff --git a/luni/src/main/java/libcore/icu/RelativeDateTimeFormatter.java b/luni/src/main/java/libcore/icu/RelativeDateTimeFormatter.java index 0e715b6..e861571 100644 --- a/luni/src/main/java/libcore/icu/RelativeDateTimeFormatter.java +++ b/luni/src/main/java/libcore/icu/RelativeDateTimeFormatter.java @@ -32,6 +32,7 @@ public final class RelativeDateTimeFormatter { // 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; @@ -222,6 +223,27 @@ public final class RelativeDateTimeFormatter { } else { // The duration is longer than a week and minResolution is not // WEEK_IN_MILLIS. Return the absolute date instead of relative time. + + // Bug 19822016: + // If user doesn't supply the year display flag, we need to explicitly + // set that to show / hide the year based on time and now. Otherwise + // 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 = new GregorianCalendar(false); + timeCalendar.setTimeZone(tz); + timeCalendar.setTimeInMillis(time); + Calendar nowCalendar = new GregorianCalendar(false); + nowCalendar.setTimeZone(tz); + nowCalendar.setTimeInMillis(now); + + if (timeCalendar.get(Calendar.YEAR) != nowCalendar.get(Calendar.YEAR)) { + flags |= FORMAT_SHOW_YEAR; + } else { + flags |= FORMAT_NO_YEAR; + } + } + return DateIntervalFormat.formatDateRange(locale, tz, time, time, flags); } @@ -323,15 +345,12 @@ public final class RelativeDateTimeFormatter { } else { // We always use fixed flags to format the date clause. User-supplied // flags are ignored. - if (days == 0) { - // Same day - flags = FORMAT_SHOW_TIME; - } else if (timeCalendar.get(Calendar.YEAR) != nowCalendar.get(Calendar.YEAR)) { + if (timeCalendar.get(Calendar.YEAR) != nowCalendar.get(Calendar.YEAR)) { // Different years flags = FORMAT_SHOW_DATE | FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE; } else { // Default - flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH; + flags = FORMAT_SHOW_DATE | FORMAT_NO_YEAR | FORMAT_ABBREV_MONTH; } dateClause = DateIntervalFormat.formatDateRange(locale, tz, time, time, flags); diff --git a/luni/src/test/java/libcore/icu/RelativeDateTimeFormatterTest.java b/luni/src/test/java/libcore/icu/RelativeDateTimeFormatterTest.java index 9a79429..0b7f8c3 100644 --- a/luni/src/test/java/libcore/icu/RelativeDateTimeFormatterTest.java +++ b/luni/src/test/java/libcore/icu/RelativeDateTimeFormatterTest.java @@ -24,6 +24,8 @@ 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; @@ -606,4 +608,57 @@ public class RelativeDateTimeFormatterTest extends junit.framework.TestCase { getRelativeDateTimeString(en_US, tz, twoDaysLater2, now, MINUTE_IN_MILLIS, WEEK_IN_MILLIS, 0)); } + + // b/19822016: show / hide the year based on the dates in the arguments. + public void test_bug19822016() throws Exception { + Locale en_US = new Locale("en", "US"); + TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles"); + Calendar cal = Calendar.getInstance(tz, en_US); + // Feb 5, 2012 at 10:50 PST + cal.set(2012, Calendar.FEBRUARY, 5, 10, 50, 0); + long base = cal.getTimeInMillis(); + + assertEquals("Feb 5, 5:50 AM", getRelativeDateTimeString(en_US, tz, + base - 5 * HOUR_IN_MILLIS, base, 0, MINUTE_IN_MILLIS, 0)); + assertEquals("Jan 29, 10:50 AM", getRelativeDateTimeString(en_US, tz, + base - 7 * DAY_IN_MILLIS, base, 0, WEEK_IN_MILLIS, 0)); + assertEquals("11/27/2011, 10:50 AM", getRelativeDateTimeString(en_US, tz, + base - 10 * WEEK_IN_MILLIS, base, 0, WEEK_IN_MILLIS, 0)); + + assertEquals("January 6", getRelativeTimeSpanString(en_US, tz, + base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, 0)); + assertEquals("January 6", getRelativeTimeSpanString(en_US, tz, + base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_NO_YEAR)); + assertEquals("January 6, 2012", getRelativeTimeSpanString(en_US, tz, + base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_SHOW_YEAR)); + assertEquals("December 7, 2011", getRelativeTimeSpanString(en_US, tz, + base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, 0)); + assertEquals("December 7, 2011", getRelativeTimeSpanString(en_US, tz, + base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_SHOW_YEAR)); + assertEquals("December 7", getRelativeTimeSpanString(en_US, tz, + base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_NO_YEAR)); + + // Feb 5, 2018 at 10:50 PST + cal.set(2018, Calendar.FEBRUARY, 5, 10, 50, 0); + base = cal.getTimeInMillis(); + assertEquals("Feb 5, 5:50 AM", getRelativeDateTimeString(en_US, tz, + base - 5 * HOUR_IN_MILLIS, base, 0, MINUTE_IN_MILLIS, 0)); + assertEquals("Jan 29, 10:50 AM", getRelativeDateTimeString(en_US, tz, + base - 7 * DAY_IN_MILLIS, base, 0, WEEK_IN_MILLIS, 0)); + assertEquals("11/27/2017, 10:50 AM", getRelativeDateTimeString(en_US, tz, + base - 10 * WEEK_IN_MILLIS, base, 0, WEEK_IN_MILLIS, 0)); + + assertEquals("January 6", getRelativeTimeSpanString(en_US, tz, + base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, 0)); + assertEquals("January 6", getRelativeTimeSpanString(en_US, tz, + base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_NO_YEAR)); + assertEquals("January 6, 2018", getRelativeTimeSpanString(en_US, tz, + base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_SHOW_YEAR)); + assertEquals("December 7, 2017", getRelativeTimeSpanString(en_US, tz, + base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, 0)); + assertEquals("December 7, 2017", getRelativeTimeSpanString(en_US, tz, + base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_SHOW_YEAR)); + assertEquals("December 7", getRelativeTimeSpanString(en_US, tz, + base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_NO_YEAR)); + } } |