1 /* 2 * Copyright (C) 2008 Google Inc. 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 com.android.unit_tests; 18 19 import junit.framework.TestCase; 20 21 import android.util.TimeUtils; 22 23 import java.util.Calendar; 24 import java.util.TimeZone; 25 26 /** 27 * TimeUtilsTest tests the time zone guesser. 28 */ 29 public class TimeUtilsTest extends TestCase { testMainstream()30 public void testMainstream() throws Exception { 31 String[] mainstream = new String[] { 32 "America/New_York", // Eastern 33 "America/Chicago", // Central 34 "America/Denver", // Mountain 35 "America/Los_Angeles", // Pacific 36 "America/Anchorage", // Alaska 37 "Pacific/Honolulu", // Hawaii, no DST 38 }; 39 40 for (String name : mainstream) { 41 TimeZone tz = TimeZone.getTimeZone(name); 42 Calendar c = Calendar.getInstance(tz); 43 TimeZone guess; 44 45 c.set(2008, Calendar.OCTOBER, 20, 12, 00, 00); 46 guess = guess(c, "us"); 47 assertEquals(name, guess.getID()); 48 49 c.set(2009, Calendar.JANUARY, 20, 12, 00, 00); 50 guess = guess(c, "us"); 51 assertEquals(name, guess.getID()); 52 } 53 } 54 testWeird()55 public void testWeird() throws Exception { 56 String[] weird = new String[] { 57 "America/Phoenix", // Mountain, no DST 58 "America/Adak", // Same as Hawaii, but with DST 59 }; 60 61 for (String name : weird) { 62 TimeZone tz = TimeZone.getTimeZone(name); 63 Calendar c = Calendar.getInstance(tz); 64 TimeZone guess; 65 66 c.set(2008, Calendar.OCTOBER, 20, 12, 00, 00); 67 guess = guess(c, "us"); 68 assertEquals(name, guess.getID()); 69 } 70 } 71 testOld()72 public void testOld() throws Exception { 73 String[] old = new String[] { 74 "America/Indiana/Indianapolis", // Eastern, formerly no DST 75 }; 76 77 for (String name : old) { 78 TimeZone tz = TimeZone.getTimeZone(name); 79 Calendar c = Calendar.getInstance(tz); 80 TimeZone guess; 81 82 c.set(2005, Calendar.OCTOBER, 20, 12, 00, 00); 83 guess = guess(c, "us"); 84 assertEquals(name, guess.getID()); 85 } 86 } 87 testWorld()88 public void testWorld() throws Exception { 89 String[] world = new String[] { 90 "ad", "Europe/Andorra", 91 "ae", "Asia/Dubai", 92 "af", "Asia/Kabul", 93 "ag", "America/Antigua", 94 "ai", "America/Anguilla", 95 "al", "Europe/Tirane", 96 "am", "Asia/Yerevan", 97 "an", "America/Curacao", 98 "ao", "Africa/Luanda", 99 "aq", "Antarctica/McMurdo", 100 "aq", "Antarctica/DumontDUrville", 101 "aq", "Antarctica/Casey", 102 "aq", "Antarctica/Davis", 103 "aq", "Antarctica/Mawson", 104 "aq", "Antarctica/Syowa", 105 "aq", "Antarctica/Rothera", 106 "aq", "Antarctica/Palmer", 107 "ar", "America/Argentina/Buenos_Aires", 108 "as", "Pacific/Pago_Pago", 109 "at", "Europe/Vienna", 110 "au", "Australia/Sydney", 111 "au", "Australia/Adelaide", 112 "au", "Australia/Perth", 113 "au", "Australia/Eucla", 114 "aw", "America/Aruba", 115 "ax", "Europe/Mariehamn", 116 "az", "Asia/Baku", 117 "ba", "Europe/Sarajevo", 118 "bb", "America/Barbados", 119 "bd", "Asia/Dhaka", 120 "be", "Europe/Brussels", 121 "bf", "Africa/Ouagadougou", 122 "bg", "Europe/Sofia", 123 "bh", "Asia/Bahrain", 124 "bi", "Africa/Bujumbura", 125 "bj", "Africa/Porto-Novo", 126 "bm", "Atlantic/Bermuda", 127 "bn", "Asia/Brunei", 128 "bo", "America/La_Paz", 129 "br", "America/Noronha", 130 "br", "America/Sao_Paulo", 131 "br", "America/Manaus", 132 "bs", "America/Nassau", 133 "bt", "Asia/Thimphu", 134 "bw", "Africa/Gaborone", 135 "by", "Europe/Minsk", 136 "bz", "America/Belize", 137 "ca", "America/St_Johns", 138 "ca", "America/Halifax", 139 "ca", "America/Toronto", 140 "ca", "America/Winnipeg", 141 "ca", "America/Edmonton", 142 "ca", "America/Vancouver", 143 "cc", "Indian/Cocos", 144 "cd", "Africa/Lubumbashi", 145 "cd", "Africa/Kinshasa", 146 "cf", "Africa/Bangui", 147 "cg", "Africa/Brazzaville", 148 "ch", "Europe/Zurich", 149 "ci", "Africa/Abidjan", 150 "ck", "Pacific/Rarotonga", 151 "cl", "America/Santiago", 152 "cl", "Pacific/Easter", 153 "cm", "Africa/Douala", 154 "cn", "Asia/Shanghai", 155 "co", "America/Bogota", 156 "cr", "America/Costa_Rica", 157 "cu", "America/Havana", 158 "cv", "Atlantic/Cape_Verde", 159 "cx", "Indian/Christmas", 160 "cy", "Asia/Nicosia", 161 "cz", "Europe/Prague", 162 "de", "Europe/Berlin", 163 "dj", "Africa/Djibouti", 164 "dk", "Europe/Copenhagen", 165 "dm", "America/Dominica", 166 "do", "America/Santo_Domingo", 167 "dz", "Africa/Algiers", 168 "ec", "America/Guayaquil", 169 "ec", "Pacific/Galapagos", 170 "ee", "Europe/Tallinn", 171 "eg", "Africa/Cairo", 172 "eh", "Africa/El_Aaiun", 173 "er", "Africa/Asmara", 174 "es", "Europe/Madrid", 175 "es", "Atlantic/Canary", 176 "et", "Africa/Addis_Ababa", 177 "fi", "Europe/Helsinki", 178 "fj", "Pacific/Fiji", 179 "fk", "Atlantic/Stanley", 180 "fm", "Pacific/Ponape", 181 "fm", "Pacific/Truk", 182 "fo", "Atlantic/Faroe", 183 "fr", "Europe/Paris", 184 "ga", "Africa/Libreville", 185 "gb", "Europe/London", 186 "gd", "America/Grenada", 187 "ge", "Asia/Tbilisi", 188 "gf", "America/Cayenne", 189 "gg", "Europe/Guernsey", 190 "gh", "Africa/Accra", 191 "gi", "Europe/Gibraltar", 192 "gl", "America/Danmarkshavn", 193 "gl", "America/Scoresbysund", 194 "gl", "America/Godthab", 195 "gl", "America/Thule", 196 "gm", "Africa/Banjul", 197 "gn", "Africa/Conakry", 198 "gp", "America/Guadeloupe", 199 "gq", "Africa/Malabo", 200 "gr", "Europe/Athens", 201 "gs", "Atlantic/South_Georgia", 202 "gt", "America/Guatemala", 203 "gu", "Pacific/Guam", 204 "gw", "Africa/Bissau", 205 "gy", "America/Guyana", 206 "hk", "Asia/Hong_Kong", 207 "hn", "America/Tegucigalpa", 208 "hr", "Europe/Zagreb", 209 "ht", "America/Port-au-Prince", 210 "hu", "Europe/Budapest", 211 "id", "Asia/Jayapura", 212 "id", "Asia/Makassar", 213 "id", "Asia/Jakarta", 214 "ie", "Europe/Dublin", 215 "il", "Asia/Jerusalem", 216 "im", "Europe/Isle_of_Man", 217 "in", "Asia/Calcutta", 218 "io", "Indian/Chagos", 219 "iq", "Asia/Baghdad", 220 "ir", "Asia/Tehran", 221 "is", "Atlantic/Reykjavik", 222 "it", "Europe/Rome", 223 "je", "Europe/Jersey", 224 "jm", "America/Jamaica", 225 "jo", "Asia/Amman", 226 "jp", "Asia/Tokyo", 227 "ke", "Africa/Nairobi", 228 "kg", "Asia/Bishkek", 229 "kh", "Asia/Phnom_Penh", 230 "ki", "Pacific/Kiritimati", 231 "ki", "Pacific/Enderbury", 232 "ki", "Pacific/Tarawa", 233 "km", "Indian/Comoro", 234 "kn", "America/St_Kitts", 235 "kp", "Asia/Pyongyang", 236 "kr", "Asia/Seoul", 237 "kw", "Asia/Kuwait", 238 "ky", "America/Cayman", 239 "kz", "Asia/Almaty", 240 "kz", "Asia/Aqtau", 241 "la", "Asia/Vientiane", 242 "lb", "Asia/Beirut", 243 "lc", "America/St_Lucia", 244 "li", "Europe/Vaduz", 245 "lk", "Asia/Colombo", 246 "lr", "Africa/Monrovia", 247 "ls", "Africa/Maseru", 248 "lt", "Europe/Vilnius", 249 "lu", "Europe/Luxembourg", 250 "lv", "Europe/Riga", 251 "ly", "Africa/Tripoli", 252 "ma", "Africa/Casablanca", 253 "mc", "Europe/Monaco", 254 "md", "Europe/Chisinau", 255 "me", "Europe/Podgorica", 256 "mg", "Indian/Antananarivo", 257 "mh", "Pacific/Majuro", 258 "mk", "Europe/Skopje", 259 "ml", "Africa/Bamako", 260 "mm", "Asia/Rangoon", 261 "mn", "Asia/Choibalsan", 262 "mn", "Asia/Hovd", 263 "mo", "Asia/Macau", 264 "mp", "Pacific/Saipan", 265 "mq", "America/Martinique", 266 "mr", "Africa/Nouakchott", 267 "ms", "America/Montserrat", 268 "mt", "Europe/Malta", 269 "mu", "Indian/Mauritius", 270 "mv", "Indian/Maldives", 271 "mw", "Africa/Blantyre", 272 "mx", "America/Mexico_City", 273 "mx", "America/Chihuahua", 274 "mx", "America/Tijuana", 275 "my", "Asia/Kuala_Lumpur", 276 "mz", "Africa/Maputo", 277 "na", "Africa/Windhoek", 278 "nc", "Pacific/Noumea", 279 "ne", "Africa/Niamey", 280 "nf", "Pacific/Norfolk", 281 "ng", "Africa/Lagos", 282 "ni", "America/Managua", 283 "nl", "Europe/Amsterdam", 284 "no", "Europe/Oslo", 285 "np", "Asia/Katmandu", 286 "nr", "Pacific/Nauru", 287 "nu", "Pacific/Niue", 288 "nz", "Pacific/Auckland", 289 "nz", "Pacific/Chatham", 290 "om", "Asia/Muscat", 291 "pa", "America/Panama", 292 "pe", "America/Lima", 293 "pf", "Pacific/Gambier", 294 "pf", "Pacific/Marquesas", 295 "pf", "Pacific/Tahiti", 296 "pg", "Pacific/Port_Moresby", 297 "ph", "Asia/Manila", 298 "pk", "Asia/Karachi", 299 "pl", "Europe/Warsaw", 300 "pm", "America/Miquelon", 301 "pn", "Pacific/Pitcairn", 302 "pr", "America/Puerto_Rico", 303 "ps", "Asia/Gaza", 304 "pt", "Europe/Lisbon", 305 "pt", "Atlantic/Azores", 306 "pw", "Pacific/Palau", 307 "py", "America/Asuncion", 308 "qa", "Asia/Qatar", 309 "re", "Indian/Reunion", 310 "ro", "Europe/Bucharest", 311 "rs", "Europe/Belgrade", 312 "ru", "Asia/Kamchatka", 313 "ru", "Asia/Magadan", 314 "ru", "Asia/Vladivostok", 315 "ru", "Asia/Yakutsk", 316 "ru", "Asia/Irkutsk", 317 "ru", "Asia/Krasnoyarsk", 318 "ru", "Asia/Novosibirsk", 319 "ru", "Asia/Yekaterinburg", 320 "ru", "Europe/Samara", 321 "ru", "Europe/Moscow", 322 "ru", "Europe/Kaliningrad", 323 "rw", "Africa/Kigali", 324 "sa", "Asia/Riyadh", 325 "sb", "Pacific/Guadalcanal", 326 "sc", "Indian/Mahe", 327 "sd", "Africa/Khartoum", 328 "se", "Europe/Stockholm", 329 "sg", "Asia/Singapore", 330 "sh", "Atlantic/St_Helena", 331 "si", "Europe/Ljubljana", 332 "sj", "Arctic/Longyearbyen", 333 "sk", "Europe/Bratislava", 334 "sl", "Africa/Freetown", 335 "sm", "Europe/San_Marino", 336 "sn", "Africa/Dakar", 337 "so", "Africa/Mogadishu", 338 "sr", "America/Paramaribo", 339 "st", "Africa/Sao_Tome", 340 "sv", "America/El_Salvador", 341 "sy", "Asia/Damascus", 342 "sz", "Africa/Mbabane", 343 "tc", "America/Grand_Turk", 344 "td", "Africa/Ndjamena", 345 "tf", "Indian/Kerguelen", 346 "tg", "Africa/Lome", 347 "th", "Asia/Bangkok", 348 "tj", "Asia/Dushanbe", 349 "tk", "Pacific/Fakaofo", 350 "tl", "Asia/Dili", 351 "tm", "Asia/Ashgabat", 352 "tn", "Africa/Tunis", 353 "to", "Pacific/Tongatapu", 354 "tr", "Europe/Istanbul", 355 "tt", "America/Port_of_Spain", 356 "tv", "Pacific/Funafuti", 357 "tw", "Asia/Taipei", 358 "tz", "Africa/Dar_es_Salaam", 359 "ua", "Europe/Kiev", 360 "ug", "Africa/Kampala", 361 "um", "Pacific/Wake", 362 "um", "Pacific/Johnston", 363 "um", "Pacific/Midway", 364 "us", "America/New_York", 365 "us", "America/Chicago", 366 "us", "America/Denver", 367 "us", "America/Los_Angeles", 368 "us", "America/Anchorage", 369 "us", "Pacific/Honolulu", 370 "uy", "America/Montevideo", 371 "uz", "Asia/Tashkent", 372 "va", "Europe/Vatican", 373 "vc", "America/St_Vincent", 374 "ve", "America/Caracas", 375 "vg", "America/Tortola", 376 "vi", "America/St_Thomas", 377 "vn", "Asia/Saigon", 378 "vu", "Pacific/Efate", 379 "wf", "Pacific/Wallis", 380 "ws", "Pacific/Apia", 381 "ye", "Asia/Aden", 382 "yt", "Indian/Mayotte", 383 "za", "Africa/Johannesburg", 384 "zm", "Africa/Lusaka", 385 "zw", "Africa/Harare", 386 }; 387 388 for (int i = 0; i < world.length; i += 2) { 389 String country = world[i]; 390 String name = world[i + 1]; 391 392 TimeZone tz = TimeZone.getTimeZone(name); 393 Calendar c = Calendar.getInstance(tz); 394 TimeZone guess; 395 396 c.set(2009, Calendar.JULY, 20, 12, 00, 00); 397 guess = guess(c, country); 398 assertEquals(name, guess.getID()); 399 400 c.set(2009, Calendar.JANUARY, 20, 12, 00, 00); 401 guess = guess(c, country); 402 assertEquals(name, guess.getID()); 403 } 404 } 405 testWorldWeird()406 public void testWorldWeird() throws Exception { 407 String[] world = new String[] { 408 // Distinguisable from Sydney only when DST not in effect 409 "au", "Australia/Lord_Howe", 410 }; 411 412 for (int i = 0; i < world.length; i += 2) { 413 String country = world[i]; 414 String name = world[i + 1]; 415 416 TimeZone tz = TimeZone.getTimeZone(name); 417 Calendar c = Calendar.getInstance(tz); 418 TimeZone guess; 419 420 c.set(2009, Calendar.JULY, 20, 12, 00, 00); 421 guess = guess(c, country); 422 assertEquals(name, guess.getID()); 423 } 424 } 425 guess(Calendar c, String country)426 private static TimeZone guess(Calendar c, String country) { 427 return TimeUtils.getTimeZone(c.get(c.ZONE_OFFSET) + c.get(c.DST_OFFSET), 428 c.get(c.DST_OFFSET) != 0, 429 c.getTimeInMillis(), 430 country); 431 } 432 } 433