1 /* 2 * Copyright (C) 2009 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 package android.util.cts; 17 18 import android.util.TimeUtils; 19 20 import java.util.Calendar; 21 import java.util.TimeZone; 22 23 import junit.framework.TestCase; 24 25 public class TimeUtilsTest extends TestCase { testUnitedStates()26 public void testUnitedStates() throws Exception { 27 String[] mainstream = new String[] { 28 "America/New_York", // Eastern 29 "America/Chicago", // Central 30 "America/Denver", // Mountain 31 "America/Los_Angeles", // Pacific 32 "America/Anchorage", // Alaska 33 "Pacific/Honolulu", // Hawaii, no DST 34 }; 35 36 for (String name : mainstream) { 37 TimeZone tz = TimeZone.getTimeZone(name); 38 Calendar c = Calendar.getInstance(tz); 39 TimeZone guess; 40 41 c.set(2008, Calendar.OCTOBER, 20, 12, 00, 00); 42 guess = guessTimeZone(c, "us"); 43 assertEquals(name, guess.getID()); 44 45 c.set(2009, Calendar.JANUARY, 20, 12, 00, 00); 46 guess = guessTimeZone(c, "us"); 47 assertEquals(name, guess.getID()); 48 } 49 } 50 testWeirdUnitedStates()51 public void testWeirdUnitedStates() throws Exception { 52 String[] weird = new String[] { 53 "America/Phoenix", // Mountain, no DST 54 "America/Adak", // Same as Hawaii, but with DST 55 }; 56 57 for (String name : weird) { 58 TimeZone tz = TimeZone.getTimeZone(name); 59 Calendar c = Calendar.getInstance(tz); 60 TimeZone guess; 61 62 c.set(2008, Calendar.OCTOBER, 20, 12, 00, 00); 63 guess = guessTimeZone(c, "us"); 64 assertEquals(name, guess.getID()); 65 } 66 } 67 testOld()68 public void testOld() throws Exception { 69 String[] old = new String[] { 70 "America/Indiana/Indianapolis", // Eastern, formerly no DST 71 }; 72 73 for (String name : old) { 74 TimeZone tz = TimeZone.getTimeZone(name); 75 Calendar c = Calendar.getInstance(tz); 76 TimeZone guess; 77 78 c.set(2005, Calendar.OCTOBER, 20, 12, 00, 00); 79 guess = guessTimeZone(c, "us"); 80 assertEquals(name, guess.getID()); 81 } 82 } 83 testWorldWeird()84 public void testWorldWeird() throws Exception { 85 String[] world = new String[] { 86 // Distinguisable from Sydney only when DST not in effect 87 "au", "Australia/Lord_Howe", 88 }; 89 90 for (int i = 0; i < world.length; i += 2) { 91 String country = world[i]; 92 String name = world[i + 1]; 93 94 TimeZone tz = TimeZone.getTimeZone(name); 95 Calendar c = Calendar.getInstance(tz); 96 TimeZone guess; 97 98 c.set(2009, Calendar.JULY, 20, 12, 00, 00); 99 guess = guessTimeZone(c, country); 100 assertEquals(name, guess.getID()); 101 } 102 } 103 guessTimeZone(Calendar c, String country)104 private static TimeZone guessTimeZone(Calendar c, String country) { 105 return TimeUtils.getTimeZone(c.get(Calendar.ZONE_OFFSET) + c.get(Calendar.DST_OFFSET), 106 c.get(Calendar.DST_OFFSET) != 0, 107 c.getTimeInMillis(), 108 country); 109 } 110 } 111