1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.icu; 18 19 import android.icu.util.Calendar; 20 import android.icu.util.TimeZone; 21 import android.icu.util.ULocale; 22 23 import static libcore.icu.DateIntervalFormat.formatDateRange; 24 import static libcore.icu.DateUtilsBridge.*; 25 26 public class DateIntervalFormatTest extends junit.framework.TestCase { 27 private static final long MINUTE = 60 * 1000; 28 private static final long HOUR = 60 * MINUTE; 29 private static final long DAY = 24 * HOUR; 30 private static final long MONTH = 31 * DAY; 31 private static final long YEAR = 12 * MONTH; 32 33 // These are the old CTS tests for DateIntervalFormat.formatDateRange. test_formatDateInterval()34 public void test_formatDateInterval() throws Exception { 35 TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles"); 36 37 Calendar c = Calendar.getInstance(tz, ULocale.US); 38 c.set(Calendar.MONTH, Calendar.JANUARY); 39 c.set(Calendar.DAY_OF_MONTH, 19); 40 c.set(Calendar.HOUR_OF_DAY, 3); 41 c.set(Calendar.MINUTE, 30); 42 c.set(Calendar.SECOND, 15); 43 c.set(Calendar.MILLISECOND, 0); 44 long timeWithCurrentYear = c.getTimeInMillis(); 45 46 c.set(Calendar.YEAR, 2009); 47 long fixedTime = c.getTimeInMillis(); 48 49 c.set(Calendar.MINUTE, 0); 50 c.set(Calendar.SECOND, 0); 51 long onTheHour = c.getTimeInMillis(); 52 53 long noonDuration = (8 * 60 + 30) * 60 * 1000 - 15 * 1000; 54 long midnightDuration = (3 * 60 + 30) * 60 * 1000 + 15 * 1000; 55 56 ULocale de_DE = new ULocale("de", "DE"); 57 ULocale en_US = new ULocale("en", "US"); 58 ULocale es_ES = new ULocale("es", "ES"); 59 ULocale es_US = new ULocale("es", "US"); 60 61 assertEquals("Monday", formatDateRange(en_US, tz, fixedTime, fixedTime + HOUR, FORMAT_SHOW_WEEKDAY)); 62 assertEquals("January 19", formatDateRange(en_US, tz, timeWithCurrentYear, timeWithCurrentYear + HOUR, FORMAT_SHOW_DATE)); 63 assertEquals("3:30 AM", formatDateRange(en_US, tz, fixedTime, fixedTime, FORMAT_SHOW_TIME)); 64 assertEquals("January 19, 2009", formatDateRange(en_US, tz, fixedTime, fixedTime + HOUR, FORMAT_SHOW_YEAR)); 65 assertEquals("January 19", formatDateRange(en_US, tz, fixedTime, fixedTime + HOUR, FORMAT_NO_YEAR)); 66 assertEquals("January", formatDateRange(en_US, tz, timeWithCurrentYear, timeWithCurrentYear + HOUR, FORMAT_NO_MONTH_DAY)); 67 assertEquals("3:30 AM", formatDateRange(en_US, tz, fixedTime, fixedTime, FORMAT_12HOUR | FORMAT_SHOW_TIME)); 68 assertEquals("03:30", formatDateRange(en_US, tz, fixedTime, fixedTime, FORMAT_24HOUR | FORMAT_SHOW_TIME)); 69 assertEquals("3:30 AM", formatDateRange(en_US, tz, fixedTime, fixedTime, FORMAT_12HOUR /*| FORMAT_CAP_AMPM*/ | FORMAT_SHOW_TIME)); 70 assertEquals("12:00 PM", formatDateRange(en_US, tz, fixedTime + noonDuration, fixedTime + noonDuration, FORMAT_12HOUR | FORMAT_SHOW_TIME)); 71 assertEquals("12:00 PM", formatDateRange(en_US, tz, fixedTime + noonDuration, fixedTime + noonDuration, FORMAT_12HOUR | FORMAT_SHOW_TIME /*| FORMAT_CAP_NOON*/)); 72 assertEquals("12:00 PM", formatDateRange(en_US, tz, fixedTime + noonDuration, fixedTime + noonDuration, FORMAT_12HOUR /*| FORMAT_NO_NOON*/ | FORMAT_SHOW_TIME)); 73 assertEquals("12:00 AM", formatDateRange(en_US, tz, fixedTime - midnightDuration, fixedTime - midnightDuration, FORMAT_12HOUR | FORMAT_SHOW_TIME /*| FORMAT_NO_MIDNIGHT*/)); 74 assertEquals("3:30 AM", formatDateRange(en_US, tz, fixedTime, fixedTime, FORMAT_SHOW_TIME | FORMAT_UTC)); 75 assertEquals("3 AM", formatDateRange(en_US, tz, onTheHour, onTheHour, FORMAT_SHOW_TIME | FORMAT_ABBREV_TIME)); 76 assertEquals("Mon", formatDateRange(en_US, tz, fixedTime, fixedTime + HOUR, FORMAT_SHOW_WEEKDAY | FORMAT_ABBREV_WEEKDAY)); 77 assertEquals("Jan 19", formatDateRange(en_US, tz, timeWithCurrentYear, timeWithCurrentYear + HOUR, FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH)); 78 assertEquals("Jan 19", formatDateRange(en_US, tz, timeWithCurrentYear, timeWithCurrentYear + HOUR, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 79 80 assertEquals("1/19/2009", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * HOUR, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 81 assertEquals("1/19/2009 – 1/22/2009", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 82 assertEquals("1/19/2009 – 4/22/2009", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 83 assertEquals("1/19/2009 – 2/9/2012", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 84 85 assertEquals("19.1.2009", formatDateRange(de_DE, tz, fixedTime, fixedTime + HOUR, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 86 assertEquals("19.01.2009 – 22.01.2009", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 87 assertEquals("19.01.2009 – 22.04.2009", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 88 assertEquals("19.01.2009 – 09.02.2012", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 89 90 assertEquals("19/1/2009", formatDateRange(es_US, tz, fixedTime, fixedTime + HOUR, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 91 assertEquals("19/1/2009–22/1/2009", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 92 assertEquals("19/1/2009–22/4/2009", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 93 assertEquals("19/1/2009–9/2/2012", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 94 95 assertEquals("19/1/2009", formatDateRange(es_ES, tz, fixedTime, fixedTime + HOUR, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 96 assertEquals("19/1/2009–22/1/2009", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 97 assertEquals("19/1/2009–22/4/2009", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 98 assertEquals("19/1/2009–9/2/2012", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE)); 99 100 // These are some random other test cases I came up with. 101 102 assertEquals("January 19 – 22, 2009", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * DAY, 0)); 103 assertEquals("Jan 19 – 22, 2009", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 104 assertEquals("Mon, Jan 19 – Thu, Jan 22, 2009", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_WEEKDAY | FORMAT_ABBREV_ALL)); 105 assertEquals("Monday, January 19 – Thursday, January 22, 2009", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_WEEKDAY)); 106 107 assertEquals("January 19 – April 22, 2009", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * MONTH, 0)); 108 assertEquals("Jan 19 – Apr 22, 2009", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 109 assertEquals("Mon, Jan 19 – Wed, Apr 22, 2009", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_WEEKDAY | FORMAT_ABBREV_ALL)); 110 assertEquals("January – April 2009", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_NO_MONTH_DAY)); 111 112 assertEquals("Jan 19, 2009 – Feb 9, 2012", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 113 assertEquals("Jan 2009 – Feb 2012", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_NO_MONTH_DAY | FORMAT_ABBREV_ALL)); 114 assertEquals("January 19, 2009 – February 9, 2012", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * YEAR, 0)); 115 assertEquals("Monday, January 19, 2009 – Thursday, February 9, 2012", formatDateRange(en_US, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_WEEKDAY)); 116 117 // The same tests but for de_DE. 118 119 assertEquals("19.–22. Januar 2009", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * DAY, 0)); 120 assertEquals("19.–22. Jan. 2009", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 121 assertEquals("Mo., 19. – Do., 22. Jan. 2009", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_WEEKDAY | FORMAT_ABBREV_ALL)); 122 assertEquals("Montag, 19. – Donnerstag, 22. Januar 2009", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_WEEKDAY)); 123 124 assertEquals("19. Januar – 22. April 2009", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * MONTH, 0)); 125 assertEquals("19. Jan. – 22. Apr. 2009", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 126 assertEquals("Mo., 19. Jan. – Mi., 22. Apr. 2009", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_WEEKDAY | FORMAT_ABBREV_ALL)); 127 assertEquals("Januar–April 2009", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_NO_MONTH_DAY)); 128 129 assertEquals("19. Jan. 2009 – 9. Feb. 2012", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 130 assertEquals("Jan. 2009 – Feb. 2012", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_NO_MONTH_DAY | FORMAT_ABBREV_ALL)); 131 assertEquals("19. Januar 2009 – 9. Februar 2012", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * YEAR, 0)); 132 assertEquals("Montag, 19. Januar 2009 – Donnerstag, 9. Februar 2012", formatDateRange(de_DE, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_WEEKDAY)); 133 134 // The same tests but for es_US. 135 136 assertEquals("19–22 de enero de 2009", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * DAY, 0)); 137 assertEquals("19 – 22 de ene. de 2009", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 138 assertEquals("lun., 19 de ene. – jue., 22 de ene. de 2009", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_WEEKDAY | FORMAT_ABBREV_ALL)); 139 assertEquals("lunes, 19 de enero–jueves, 22 de enero de 2009", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_WEEKDAY)); 140 141 assertEquals("19 de enero–22 de abril de 2009", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * MONTH, 0)); 142 assertEquals("19 de ene. – 22 de abr. de 2009", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 143 assertEquals("lun., 19 de ene. – mié., 22 de abr. de 2009", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_WEEKDAY | FORMAT_ABBREV_ALL)); 144 assertEquals("enero–abril de 2009", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_NO_MONTH_DAY)); 145 146 assertEquals("19 de ene. de 2009 – 9 de feb. de 2012", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 147 assertEquals("ene. de 2009 – feb. de 2012", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_NO_MONTH_DAY | FORMAT_ABBREV_ALL)); 148 assertEquals("19 de enero de 2009–9 de febrero de 2012", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * YEAR, 0)); 149 assertEquals("lunes, 19 de enero de 2009–jueves, 9 de febrero de 2012", formatDateRange(es_US, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_WEEKDAY)); 150 151 // The same tests but for es_ES. 152 153 assertEquals("19–22 de enero de 2009", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * DAY, 0)); 154 assertEquals("19–22 ene. 2009", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 155 assertEquals("lun., 19 ene. – jue., 22 ene. 2009", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_WEEKDAY | FORMAT_ABBREV_ALL)); 156 assertEquals("lunes, 19 de enero–jueves, 22 de enero de 2009", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * DAY, FORMAT_SHOW_WEEKDAY)); 157 158 assertEquals("19 de enero–22 de abril de 2009", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * MONTH, 0)); 159 assertEquals("19 ene. – 22 abr. 2009", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 160 assertEquals("lun., 19 ene. – mié., 22 abr. 2009", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_SHOW_WEEKDAY | FORMAT_ABBREV_ALL)); 161 assertEquals("enero–abril de 2009", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * MONTH, FORMAT_NO_MONTH_DAY)); 162 163 assertEquals("19 ene. 2009 – 9 feb. 2012", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL)); 164 assertEquals("ene. 2009 – feb. 2012", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_NO_MONTH_DAY | FORMAT_ABBREV_ALL)); 165 assertEquals("19 de enero de 2009–9 de febrero de 2012", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * YEAR, 0)); 166 assertEquals("lunes, 19 de enero de 2009–jueves, 9 de febrero de 2012", formatDateRange(es_ES, tz, fixedTime, fixedTime + 3 * YEAR, FORMAT_SHOW_WEEKDAY)); 167 } 168 169 // http://b/8862241 - we should be able to format dates past 2038. 170 // See also http://code.google.com/p/android/issues/detail?id=13050. test8862241()171 public void test8862241() throws Exception { 172 ULocale l = ULocale.US; 173 TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles"); 174 Calendar c = Calendar.getInstance(tz, l); 175 c.clear(); 176 c.set(2042, Calendar.JANUARY, 19, 3, 30); 177 long jan_19_2042 = c.getTimeInMillis(); 178 c.set(2046, Calendar.OCTOBER, 4, 3, 30); 179 long oct_4_2046 = c.getTimeInMillis(); 180 int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL; 181 assertEquals("Jan 19, 2042 – Oct 4, 2046", formatDateRange(l, tz, jan_19_2042, oct_4_2046, flags)); 182 } 183 184 // http://b/10089890 - we should take the given time zone into account. test10089890()185 public void test10089890() throws Exception { 186 ULocale l = ULocale.US; 187 TimeZone utc = TimeZone.getTimeZone("UTC"); 188 TimeZone pacific = TimeZone.getTimeZone("America/Los_Angeles"); 189 int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL | FORMAT_SHOW_TIME | FORMAT_24HOUR; 190 191 // The Unix epoch is UTC, so 0 is 1970-01-01T00:00Z... 192 assertEquals("Jan 1, 1970, 00:00 – Jan 2, 1970, 00:00", formatDateRange(l, utc, 0, DAY + 1, flags)); 193 // But MTV is hours behind, so 0 was still the afternoon of the previous day... 194 assertEquals("Dec 31, 1969, 16:00 – Jan 1, 1970, 16:00", formatDateRange(l, pacific, 0, DAY, flags)); 195 } 196 197 // http://b/10318326 - we can drop the minutes in a 12-hour time if they're zero, 198 // but not if we're using the 24-hour clock. That is: "4 PM" is reasonable, "16" is not. test10318326()199 public void test10318326() throws Exception { 200 long midnight = 0; 201 long teaTime = 16 * HOUR; 202 203 int time12 = FORMAT_12HOUR | FORMAT_SHOW_TIME; 204 int time24 = FORMAT_24HOUR | FORMAT_SHOW_TIME; 205 int abbr12 = time12 | FORMAT_ABBREV_ALL; 206 int abbr24 = time24 | FORMAT_ABBREV_ALL; 207 208 ULocale l = ULocale.US; 209 TimeZone utc = TimeZone.getTimeZone("UTC"); 210 211 // Full length on-the-hour times. 212 assertEquals("00:00", formatDateRange(l, utc, midnight, midnight, time24)); 213 assertEquals("12:00 AM", formatDateRange(l, utc, midnight, midnight, time12)); 214 assertEquals("16:00", formatDateRange(l, utc, teaTime, teaTime, time24)); 215 assertEquals("4:00 PM", formatDateRange(l, utc, teaTime, teaTime, time12)); 216 217 // Abbreviated on-the-hour times. 218 assertEquals("00:00", formatDateRange(l, utc, midnight, midnight, abbr24)); 219 assertEquals("12 AM", formatDateRange(l, utc, midnight, midnight, abbr12)); 220 assertEquals("16:00", formatDateRange(l, utc, teaTime, teaTime, abbr24)); 221 assertEquals("4 PM", formatDateRange(l, utc, teaTime, teaTime, abbr12)); 222 223 // Abbreviated on-the-hour ranges. 224 assertEquals("00:00 – 16:00", formatDateRange(l, utc, midnight, teaTime, abbr24)); 225 assertEquals("12 AM – 4 PM", formatDateRange(l, utc, midnight, teaTime, abbr12)); 226 227 // Abbreviated mixed ranges. 228 assertEquals("00:00 – 16:01", formatDateRange(l, utc, midnight, teaTime + MINUTE, abbr24)); 229 assertEquals("12:00 AM – 4:01 PM", formatDateRange(l, utc, midnight, teaTime + MINUTE, abbr12)); 230 } 231 232 // http://b/10560853 - when the time is not displayed, an end time 0 ms into the next day is 233 // considered to belong to the previous day. test10560853_when_time_not_displayed()234 public void test10560853_when_time_not_displayed() throws Exception { 235 ULocale l = ULocale.US; 236 TimeZone utc = TimeZone.getTimeZone("UTC"); 237 238 long midnight = 0; 239 long midnightNext = 1 * DAY; 240 241 int flags = FORMAT_SHOW_DATE | FORMAT_SHOW_WEEKDAY; 242 243 // An all-day event runs until 0 milliseconds into the next day, but is formatted as if it's 244 // just the first day. 245 assertEquals("Thursday, January 1, 1970", formatDateRange(l, utc, midnight, midnightNext, flags)); 246 247 // Run one millisecond over, though, and you're into the next day. 248 long nextMorning = 1 * DAY + 1; 249 assertEquals("Thursday, January 1 – Friday, January 2, 1970", formatDateRange(l, utc, midnight, nextMorning, flags)); 250 251 // But the same reasoning applies for that day. 252 long nextMidnight = 2 * DAY; 253 assertEquals("Thursday, January 1 – Friday, January 2, 1970", formatDateRange(l, utc, midnight, nextMidnight, flags)); 254 } 255 256 // http://b/10560853 - when the start and end times are otherwise on the same day, 257 // an end time 0 ms into the next day is considered to belong to the previous day. test10560853_for_single_day_events()258 public void test10560853_for_single_day_events() throws Exception { 259 ULocale l = ULocale.US; 260 TimeZone utc = TimeZone.getTimeZone("UTC"); 261 262 int flags = FORMAT_SHOW_TIME | FORMAT_24HOUR | FORMAT_SHOW_DATE; 263 264 assertEquals("January 1, 1970, 22:00 – 00:00", formatDateRange(l, utc, 22 * HOUR, 24 * HOUR, flags)); 265 assertEquals("January 1, 1970, 22:00 – January 2, 1970, 00:30", formatDateRange(l, utc, 22 * HOUR, 24 * HOUR + 30 * MINUTE, flags)); 266 } 267 268 // The fix for http://b/10560853 didn't work except for the day around the epoch, which was 269 // all the unit test checked! test_single_day_events_later_than_epoch()270 public void test_single_day_events_later_than_epoch() throws Exception { 271 ULocale l = ULocale.US; 272 TimeZone utc = TimeZone.getTimeZone("UTC"); 273 274 int flags = FORMAT_SHOW_TIME | FORMAT_24HOUR | FORMAT_SHOW_DATE; 275 276 Calendar c = Calendar.getInstance(utc, l); 277 c.clear(); 278 c.set(1980, Calendar.JANUARY, 1, 0, 0); 279 long jan_1_1980 = c.getTimeInMillis(); 280 assertEquals("January 1, 1980, 22:00 – 00:00", 281 formatDateRange(l, utc, jan_1_1980 + 22 * HOUR, jan_1_1980 + 24 * HOUR, flags)); 282 assertEquals("January 1, 1980, 22:00 – January 2, 1980, 00:30", 283 formatDateRange(l, utc, jan_1_1980 + 22 * HOUR, jan_1_1980 + 24 * HOUR + 30 * MINUTE, flags)); 284 } 285 286 // The fix for http://b/10560853 didn't work except for UTC, which was 287 // all the unit test checked! test_single_day_events_not_in_UTC()288 public void test_single_day_events_not_in_UTC() throws Exception { 289 ULocale l = ULocale.US; 290 TimeZone pacific = TimeZone.getTimeZone("America/Los_Angeles"); 291 292 int flags = FORMAT_SHOW_TIME | FORMAT_24HOUR | FORMAT_SHOW_DATE; 293 294 Calendar c = Calendar.getInstance(pacific, l); 295 c.clear(); 296 c.set(1980, Calendar.JANUARY, 1, 0, 0); 297 long jan_1_1980 = c.getTimeInMillis(); 298 assertEquals("January 1, 1980, 22:00 – 00:00", 299 formatDateRange(l, pacific, jan_1_1980 + 22 * HOUR, jan_1_1980 + 24 * HOUR, flags)); 300 301 c.set(1980, Calendar.JULY, 1, 0, 0); 302 long jul_1_1980 = c.getTimeInMillis(); 303 assertEquals("July 1, 1980, 22:00 – 00:00", 304 formatDateRange(l, pacific, jul_1_1980 + 22 * HOUR, jul_1_1980 + 24 * HOUR, flags)); 305 } 306 307 // http://b/10209343 - even if the caller didn't explicitly ask us to include the year, 308 // we should do so for years other than the current year. test10209343_when_not_this_year()309 public void test10209343_when_not_this_year() { 310 ULocale l = ULocale.US; 311 TimeZone utc = TimeZone.getTimeZone("UTC"); 312 313 int flags = FORMAT_SHOW_DATE | FORMAT_SHOW_WEEKDAY | FORMAT_SHOW_TIME | FORMAT_24HOUR; 314 315 assertEquals("Thursday, January 1, 1970, 00:00", formatDateRange(l, utc, 0L, 0L, flags)); 316 317 long t1833 = ((long) Integer.MIN_VALUE + Integer.MIN_VALUE) * 1000L; 318 assertEquals("Sunday, November 24, 1833, 17:31", formatDateRange(l, utc, t1833, t1833, flags)); 319 320 long t1901 = Integer.MIN_VALUE * 1000L; 321 assertEquals("Friday, December 13, 1901, 20:45", formatDateRange(l, utc, t1901, t1901, flags)); 322 323 long t2038 = Integer.MAX_VALUE * 1000L; 324 assertEquals("Tuesday, January 19, 2038, 03:14", formatDateRange(l, utc, t2038, t2038, flags)); 325 326 long t2106 = (2L + Integer.MAX_VALUE + Integer.MAX_VALUE) * 1000L; 327 assertEquals("Sunday, February 7, 2106, 06:28", formatDateRange(l, utc, t2106, t2106, flags)); 328 } 329 330 // http://b/10209343 - for the current year, we should honor the FORMAT_SHOW_YEAR flags. test10209343_when_this_year()331 public void test10209343_when_this_year() { 332 // Construct a date in the current year (whenever the test happens to be run). 333 ULocale l = ULocale.US; 334 TimeZone utc = TimeZone.getTimeZone("UTC"); 335 Calendar c = Calendar.getInstance(utc, l); 336 c.set(Calendar.MONTH, Calendar.FEBRUARY); 337 c.set(Calendar.DAY_OF_MONTH, 10); 338 c.set(Calendar.HOUR_OF_DAY, 0); 339 long thisYear = c.getTimeInMillis(); 340 341 // You don't get the year if it's this year... 342 assertEquals("February 10", formatDateRange(l, utc, thisYear, thisYear, FORMAT_SHOW_DATE)); 343 344 // ...unless you explicitly ask for it. 345 assertEquals(String.format("February 10, %d", c.get(Calendar.YEAR)), 346 formatDateRange(l, utc, thisYear, thisYear, FORMAT_SHOW_DATE | FORMAT_SHOW_YEAR)); 347 348 // ...or it's not actually this year... 349 Calendar c2 = (Calendar) c.clone(); 350 c2.set(Calendar.YEAR, 1980); 351 long oldYear = c2.getTimeInMillis(); 352 assertEquals("February 10, 1980", formatDateRange(l, utc, oldYear, oldYear, FORMAT_SHOW_DATE)); 353 354 // (But you can disable that!) 355 assertEquals("February 10", formatDateRange(l, utc, oldYear, oldYear, FORMAT_SHOW_DATE | FORMAT_NO_YEAR)); 356 357 // ...or the start and end years aren't the same... 358 assertEquals(String.format("February 10, 1980 – February 10, %d", c.get(Calendar.YEAR)), 359 formatDateRange(l, utc, oldYear, thisYear, FORMAT_SHOW_DATE)); 360 361 // (And you can't avoid that --- icu4c steps in and overrides you.) 362 assertEquals(String.format("February 10, 1980 – February 10, %d", c.get(Calendar.YEAR)), 363 formatDateRange(l, utc, oldYear, thisYear, FORMAT_SHOW_DATE | FORMAT_NO_YEAR)); 364 } 365 366 // http://b/8467515 - yet another y2k38 bug report. test8467515()367 public void test8467515() throws Exception { 368 ULocale l = ULocale.US; 369 TimeZone utc = TimeZone.getTimeZone("UTC"); 370 int flags = FORMAT_SHOW_DATE | FORMAT_SHOW_WEEKDAY | FORMAT_SHOW_YEAR | FORMAT_ABBREV_MONTH | FORMAT_ABBREV_WEEKDAY; 371 long t; 372 373 Calendar calendar = Calendar.getInstance(utc, l); 374 calendar.clear(); 375 376 calendar.set(2038, Calendar.JANUARY, 19, 12, 0, 0); 377 t = calendar.getTimeInMillis(); 378 assertEquals("Tue, Jan 19, 2038", formatDateRange(l, utc, t, t, flags)); 379 380 calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0); 381 t = calendar.getTimeInMillis(); 382 assertEquals("Mon, Jan 1, 1900", formatDateRange(l, utc, t, t, flags)); 383 } 384 385 // http://b/12004664 test12004664()386 public void test12004664() throws Exception { 387 TimeZone utc = TimeZone.getTimeZone("UTC"); 388 Calendar c = Calendar.getInstance(utc, ULocale.US); 389 c.clear(); 390 c.set(Calendar.YEAR, 1980); 391 c.set(Calendar.MONTH, Calendar.FEBRUARY); 392 c.set(Calendar.DAY_OF_MONTH, 10); 393 c.set(Calendar.HOUR_OF_DAY, 0); 394 long thisYear = c.getTimeInMillis(); 395 396 int flags = FORMAT_SHOW_DATE | FORMAT_SHOW_WEEKDAY | FORMAT_SHOW_YEAR; 397 assertEquals("Sunday, February 10, 1980", formatDateRange(new ULocale("en", "US"), utc, thisYear, thisYear, flags)); 398 399 // If we supported non-Gregorian calendars, this is what that we'd expect for these ULocales. 400 // This is really the correct behavior, but since java.util.Calendar currently only supports 401 // the Gregorian calendar, we want to deliberately force icu4c to agree, otherwise we'd have 402 // a mix of calendars throughout an app's UI depending on whether Java or native code formatted 403 // the date. 404 // assertEquals("یکشنبه ۲۱ بهمن ۱۳۵۸ ه.ش.", formatDateRange(new ULocale("fa"), utc, thisYear, thisYear, flags)); 405 // assertEquals("AP ۱۳۵۸ سلواغه ۲۱, یکشنبه", formatDateRange(new ULocale("ps"), utc, thisYear, thisYear, flags)); 406 // assertEquals("วันอาทิตย์ 10 กุมภาพันธ์ 2523", formatDateRange(new ULocale("th"), utc, thisYear, thisYear, flags)); 407 408 // For now, here are the localized Gregorian strings instead... 409 assertEquals("یکشنبه ۱۰ فوریهٔ ۱۹۸۰", formatDateRange(new ULocale("fa"), utc, thisYear, thisYear, flags)); 410 assertEquals("یکشنبه د ۱۹۸۰ د فبروري ۱۰", formatDateRange(new ULocale("ps"), utc, thisYear, thisYear, flags)); 411 assertEquals("วันอาทิตย์ที่ 10 กุมภาพันธ์ ค.ศ. 1980", formatDateRange(new ULocale("th"), utc, thisYear, thisYear, flags)); 412 } 413 414 // http://b/13234532 test13234532()415 public void test13234532() throws Exception { 416 ULocale l = ULocale.US; 417 TimeZone utc = TimeZone.getTimeZone("UTC"); 418 419 int flags = FORMAT_SHOW_TIME | FORMAT_ABBREV_ALL | FORMAT_12HOUR; 420 421 assertEquals("10 – 11 AM", formatDateRange(l, utc, 10*HOUR, 11*HOUR, flags)); 422 assertEquals("11 AM – 1 PM", formatDateRange(l, utc, 11*HOUR, 13*HOUR, flags)); 423 assertEquals("2 – 3 PM", formatDateRange(l, utc, 14*HOUR, 15*HOUR, flags)); 424 } 425 426 // http://b/20708022 testEndOfDayOnLastDayOfMonth()427 public void testEndOfDayOnLastDayOfMonth() throws Exception { 428 final ULocale locale = new ULocale("en"); 429 final TimeZone timeZone = TimeZone.getTimeZone("UTC"); 430 431 assertEquals("11:00 PM – 12:00 AM", formatDateRange(locale, timeZone, 432 1430434800000L, 1430438400000L, FORMAT_SHOW_TIME)); 433 } 434 } 435