• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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 com.android.unit_tests;
18 
19 import android.test.suitebuilder.annotation.SmallTest;
20 import android.test.suitebuilder.annotation.Suppress;
21 import android.text.format.Time;
22 import android.util.Log;
23 
24 import junit.framework.TestCase;
25 
26 public class TimeTest extends TestCase {
27 
28     @SmallTest
testNormalize0()29     public void testNormalize0() throws Exception {
30         Time t = new Time(Time.TIMEZONE_UTC);
31         t.parse("20060432T010203");
32         t.normalize(false /* use isDst */);
33 //        System.out.println("got: " + t.year + '-'
34 //                + t.month + '-' + t.monthDay
35 //                + ' ' + t.hour + ':' + t.minute
36 //                + ':' + t.second
37 //                + "( " + t.isDst + ',' + t.gmtoff
38 //                + ',' + t.weekDay
39 //                + ',' + t.yearDay + ')');
40     }
41 
42     private static class DateTest {
43         public int year1;
44         public int month1;
45         public int day1;
46         public int hour1;
47         public int minute1;
48         public int dst1;
49 
50         public int offset;
51 
52         public int year2;
53         public int month2;
54         public int day2;
55         public int hour2;
56         public int minute2;
57         public int dst2;
58 
DateTest(int year1, int month1, int day1, int hour1, int minute1, int dst1, int offset, int year2, int month2, int day2, int hour2, int minute2, int dst2)59         public DateTest(int year1, int month1, int day1, int hour1, int minute1, int dst1,
60                 int offset, int year2, int month2, int day2, int hour2, int minute2,
61                 int dst2) {
62             this.year1 = year1;
63             this.month1 = month1;
64             this.day1 = day1;
65             this.hour1 = hour1;
66             this.minute1 = minute1;
67             this.dst1 = dst1;
68             this.offset = offset;
69             this.year2 = year2;
70             this.month2 = month2;
71             this.day2 = day2;
72             this.hour2 = hour2;
73             this.minute2 = minute2;
74             this.dst2 = dst2;
75         }
76 
DateTest(int year1, int month1, int day1, int hour1, int minute1, int offset, int year2, int month2, int day2, int hour2, int minute2)77         public DateTest(int year1, int month1, int day1, int hour1, int minute1,
78                 int offset, int year2, int month2, int day2, int hour2, int minute2) {
79             this.year1 = year1;
80             this.month1 = month1;
81             this.day1 = day1;
82             this.hour1 = hour1;
83             this.minute1 = minute1;
84             this.dst1 = -1;
85             this.offset = offset;
86             this.year2 = year2;
87             this.month2 = month2;
88             this.day2 = day2;
89             this.hour2 = hour2;
90             this.minute2 = minute2;
91             this.dst2 = -1;
92         }
93     }
94 
95     // These tests assume that DST changes on Nov 4, 2007 at 2am (to 1am).
96 
97     // The "offset" field in "dayTests" represents days.
98     // Use normalize(true) with these tests to change the date by 1 day.
99     private DateTest[] dayTests = {
100             // The month numbers are 0-relative, so Jan=0, Feb=1,...Dec=11
101 
102             // Nov 4, 12am + 0 day = Nov 4, 12am
103             // Nov 5, 12am + 0 day = Nov 5, 12am
104             new DateTest(2007, 10, 4, 0, 0, 0, 2007, 10, 4, 0, 0),
105             new DateTest(2007, 10, 5, 0, 0, 0, 2007, 10, 5, 0, 0),
106 
107             // Nov 3, 12am + 1 day = Nov 4, 12am
108             // Nov 4, 12am + 1 day = Nov 5, 12am
109             // Nov 5, 12am + 1 day = Nov 6, 12am
110             new DateTest(2007, 10, 3, 0, 0, 1, 2007, 10, 4, 0, 0),
111             new DateTest(2007, 10, 4, 0, 0, 1, 2007, 10, 5, 0, 0),
112             new DateTest(2007, 10, 5, 0, 0, 1, 2007, 10, 6, 0, 0),
113 
114             // Nov 3, 1am + 1 day = Nov 4, 1am
115             // Nov 4, 1am + 1 day = Nov 5, 1am
116             // Nov 5, 1am + 1 day = Nov 6, 1am
117             new DateTest(2007, 10, 3, 1, 0, 1, 2007, 10, 4, 1, 0),
118             new DateTest(2007, 10, 4, 1, 0, 1, 2007, 10, 5, 1, 0),
119             new DateTest(2007, 10, 5, 1, 0, 1, 2007, 10, 6, 1, 0),
120 
121             // Nov 3, 2am + 1 day = Nov 4, 2am
122             // Nov 4, 2am + 1 day = Nov 5, 2am
123             // Nov 5, 2am + 1 day = Nov 6, 2am
124             new DateTest(2007, 10, 3, 2, 0, 1, 2007, 10, 4, 2, 0),
125             new DateTest(2007, 10, 4, 2, 0, 1, 2007, 10, 5, 2, 0),
126             new DateTest(2007, 10, 5, 2, 0, 1, 2007, 10, 6, 2, 0),
127     };
128 
129     // The "offset" field in "minuteTests" represents minutes.
130     // Use normalize(false) with these tests.
131     private DateTest[] minuteTests = {
132             // The month numbers are 0-relative, so Jan=0, Feb=1,...Dec=11
133 
134             // Nov 4, 12am + 0 minutes = Nov 4, 12am
135             // Nov 5, 12am + 0 minutes = Nov 5, 12am
136             new DateTest(2007, 10, 4, 0, 0, 0, 2007, 10, 4, 0, 0),
137             new DateTest(2007, 10, 5, 0, 0, 0, 2007, 10, 5, 0, 0),
138 
139             // Nov 3, 12am + 60 minutes = Nov 3, 1am
140             // Nov 4, 12am + 60 minutes = Nov 4, 1am
141             // Nov 5, 12am + 60 minutes = Nov 5, 1am
142             new DateTest(2007, 10, 3, 0, 0, 60, 2007, 10, 3, 1, 0),
143             new DateTest(2007, 10, 4, 0, 0, 60, 2007, 10, 4, 1, 0),
144             new DateTest(2007, 10, 5, 0, 0, 60, 2007, 10, 5, 1, 0),
145 
146             // Nov 3, 1am + 60 minutes = Nov 3, 2am
147             // Nov 4, 1am (PDT) + 30 minutes = Nov 4, 1:30am (PDT)
148             // Nov 4, 1am (PDT) + 60 minutes = Nov 4, 1am (PST)
149             new DateTest(2007, 10, 3, 1, 0, 60, 2007, 10, 3, 2, 0),
150             new DateTest(2007, 10, 4, 1, 0, 1, 30, 2007, 10, 4, 1, 30, 1),
151             new DateTest(2007, 10, 4, 1, 0, 1, 60, 2007, 10, 4, 1, 0, 0),
152 
153             // Nov 4, 1:30am (PDT) + 15 minutes = Nov 4, 1:45am (PDT)
154             // Nov 4, 1:30am (PDT) + 30 minutes = Nov 4, 1:00am (PST)
155             // Nov 4, 1:30am (PDT) + 60 minutes = Nov 4, 1:30am (PST)
156             new DateTest(2007, 10, 4, 1, 30, 1, 15, 2007, 10, 4, 1, 45, 1),
157             new DateTest(2007, 10, 4, 1, 30, 1, 30, 2007, 10, 4, 1, 0, 0),
158             new DateTest(2007, 10, 4, 1, 30, 1, 60, 2007, 10, 4, 1, 30, 0),
159 
160             // Nov 4, 1:30am (PST) + 15 minutes = Nov 4, 1:45am (PST)
161             // Nov 4, 1:30am (PST) + 30 minutes = Nov 4, 2:00am (PST)
162             // Nov 5, 1am + 60 minutes = Nov 5, 2am
163             new DateTest(2007, 10, 4, 1, 30, 0, 15, 2007, 10, 4, 1, 45, 0),
164             new DateTest(2007, 10, 4, 1, 30, 0, 30, 2007, 10, 4, 2, 0, 0),
165             new DateTest(2007, 10, 5, 1, 0, 60, 2007, 10, 5, 2, 0),
166 
167             // Nov 3, 2am + 60 minutes = Nov 3, 3am
168             // Nov 4, 2am + 30 minutes = Nov 4, 2:30am
169             // Nov 4, 2am + 60 minutes = Nov 4, 3am
170             // Nov 5, 2am + 60 minutes = Nov 5, 3am
171             new DateTest(2007, 10, 3, 2, 0, 60, 2007, 10, 3, 3, 0),
172             new DateTest(2007, 10, 4, 2, 0, 30, 2007, 10, 4, 2, 30),
173             new DateTest(2007, 10, 4, 2, 0, 60, 2007, 10, 4, 3, 0),
174             new DateTest(2007, 10, 5, 2, 0, 60, 2007, 10, 5, 3, 0),
175     };
176 
177     @SmallTest
testNormalize1()178     public void testNormalize1() throws Exception {
179         Time local = new Time("America/Los_Angeles");
180 
181         int len = dayTests.length;
182         for (int index = 0; index < len; index++) {
183             DateTest test = dayTests[index];
184             local.set(0, test.minute1, test.hour1, test.day1, test.month1, test.year1);
185             // call normalize() to make sure that isDst is set
186             local.normalize(false /* use isDst */);
187             local.monthDay += test.offset;
188             local.normalize(true /* ignore isDst */);
189             if (local.year != test.year2 || local.month != test.month2
190                     || local.monthDay != test.day2 || local.hour != test.hour2
191                     || local.minute != test.minute2) {
192                 String expectedTime = String.format("%d-%02d-%02d %02d:%02d",
193                         test.year2, test.month2, test.day2, test.hour2, test.minute2);
194                 String actualTime = String.format("%d-%02d-%02d %02d:%02d",
195                         local.year, local.month, local.monthDay, local.hour, local.minute);
196                 throw new RuntimeException(
197                         "day test index " + index + ", normalize(): expected local " + expectedTime
198                                 + " got: " + actualTime);
199             }
200 
201             local.set(0, test.minute1, test.hour1, test.day1, test.month1, test.year1);
202             // call normalize() to make sure that isDst is set
203             local.normalize(false /* use isDst */);
204             local.monthDay += test.offset;
205             long millis = local.toMillis(true /* ignore isDst */);
206             local.set(millis);
207             if (local.year != test.year2 || local.month != test.month2
208                     || local.monthDay != test.day2 || local.hour != test.hour2
209                     || local.minute != test.minute2) {
210                 String expectedTime = String.format("%d-%02d-%02d %02d:%02d",
211                         test.year2, test.month2, test.day2, test.hour2, test.minute2);
212                 String actualTime = String.format("%d-%02d-%02d %02d:%02d",
213                         local.year, local.month, local.monthDay, local.hour, local.minute);
214                 throw new RuntimeException(
215                         "day test index " + index + ", toMillis(): expected local " + expectedTime
216                                 + " got: " + actualTime);
217             }
218         }
219 
220         len = minuteTests.length;
221         for (int index = 0; index < len; index++) {
222             DateTest test = minuteTests[index];
223             local.set(0, test.minute1, test.hour1, test.day1, test.month1, test.year1);
224             local.isDst = test.dst1;
225             // call normalize() to make sure that isDst is set
226             local.normalize(false /* use isDst */);
227             if (test.dst2 == -1) test.dst2 = local.isDst;
228             local.minute += test.offset;
229             local.normalize(false /* use isDst */);
230             if (local.year != test.year2 || local.month != test.month2
231                     || local.monthDay != test.day2 || local.hour != test.hour2
232                     || local.minute != test.minute2 || local.isDst != test.dst2) {
233                 String expectedTime = String.format("%d-%02d-%02d %02d:%02d isDst: %d",
234                         test.year2, test.month2, test.day2, test.hour2, test.minute2,
235                         test.dst2);
236                 String actualTime = String.format("%d-%02d-%02d %02d:%02d isDst: %d",
237                         local.year, local.month, local.monthDay, local.hour, local.minute,
238                         local.isDst);
239                 throw new RuntimeException(
240                         "minute test index " + index + ", normalize(): expected local " + expectedTime
241                                 + " got: " + actualTime);
242             }
243 
244             local.set(0, test.minute1, test.hour1, test.day1, test.month1, test.year1);
245             local.isDst = test.dst1;
246             // call normalize() to make sure that isDst is set
247             local.normalize(false /* use isDst */);
248             if (test.dst2 == -1) test.dst2 = local.isDst;
249             local.minute += test.offset;
250             long millis = local.toMillis(false /* use isDst */);
251             local.set(millis);
252             if (local.year != test.year2 || local.month != test.month2
253                     || local.monthDay != test.day2 || local.hour != test.hour2
254                     || local.minute != test.minute2 || local.isDst != test.dst2) {
255                 String expectedTime = String.format("%d-%02d-%02d %02d:%02d isDst: %d",
256                         test.year2, test.month2, test.day2, test.hour2, test.minute2,
257                         test.dst2);
258                 String actualTime = String.format("%d-%02d-%02d %02d:%02d isDst: %d",
259                         local.year, local.month, local.monthDay, local.hour, local.minute,
260                         local.isDst);
261                 throw new RuntimeException(
262                         "minute test index " + index + ", toMillis(): expected local " + expectedTime
263                                 + " got: " + actualTime);
264             }
265         }
266     }
267 
268     @SmallTest
testSwitchTimezone0()269     public void testSwitchTimezone0() throws Exception {
270         Time t = new Time(Time.TIMEZONE_UTC);
271         t.parse("20061005T120000");
272         t.switchTimezone("America/Los_Angeles");
273         // System.out.println("got: " + t);
274     }
275 
276     @SmallTest
testCtor0()277     public void testCtor0() throws Exception {
278         Time t = new Time(Time.TIMEZONE_UTC);
279         assertEquals(Time.TIMEZONE_UTC, t.timezone);
280     }
281 
282     @SmallTest
testGetActualMaximum0()283     public void testGetActualMaximum0() throws Exception {
284         Time t = new Time(Time.TIMEZONE_UTC);
285         int r = t.getActualMaximum(Time.SECOND);
286         // System.out.println("r=" + r);
287     }
288 
289     @SmallTest
testClear0()290     public void testClear0() throws Exception {
291         Time t = new Time(Time.TIMEZONE_UTC);
292         t.clear(Time.TIMEZONE_UTC);
293     }
294 
295     @SmallTest
testCompare0()296     public void testCompare0() throws Exception {
297         Time a = new Time(Time.TIMEZONE_UTC);
298         Time b = new Time("America/Los_Angeles");
299         int r = Time.compare(a, b);
300         // System.out.println("r=" + r);
301     }
302 
303     @SmallTest
testFormat0()304     public void testFormat0() throws Exception {
305         Time t = new Time(Time.TIMEZONE_UTC);
306         String r = t.format("%Y%m%dT%H%M%S");
307         // System.out.println("r='" + r + "'");
308     }
309 
310     @SmallTest
testToString0()311     public void testToString0() throws Exception {
312         Time t = new Time(Time.TIMEZONE_UTC);
313         String r = t.toString();
314         // System.out.println("r='" + r + "'");
315     }
316 
317     @SmallTest
testGetCurrentTimezone0()318     public void testGetCurrentTimezone0() throws Exception {
319         String r = Time.getCurrentTimezone();
320         // System.out.println("r='" + r + "'");
321     }
322 
323     @SmallTest
testSetToNow0()324     public void testSetToNow0() throws Exception {
325         Time t = new Time(Time.TIMEZONE_UTC);
326         t.setToNow();
327         // System.out.println("t=" + t);
328     }
329 
330     @SmallTest
testMillis0()331     public void testMillis0() throws Exception {
332         Time t = new Time(Time.TIMEZONE_UTC);
333         t.set(0, 0, 0, 1, 1, 2006);
334         long r = t.toMillis(true /* ignore isDst */);
335         // System.out.println("r=" + r);
336         t.set(1, 0, 0, 1, 1, 2006);
337         r = t.toMillis(true /* ignore isDst */);
338         // System.out.println("r=" + r);
339     }
340 
341     @SmallTest
testMillis1()342     public void testMillis1() throws Exception {
343         Time t = new Time(Time.TIMEZONE_UTC);
344         t.set(1, 0, 0, 1, 0, 1970);
345         long r = t.toMillis(true /* ignore isDst */);
346         // System.out.println("r=" + r);
347     }
348 
349     @SmallTest
testParse0()350     public void testParse0() throws Exception {
351         Time t = new Time(Time.TIMEZONE_UTC);
352         t.parse("12345678T901234");
353         // System.out.println("t=" + t);
354     }
355 
356     @SmallTest
testSet0()357     public void testSet0() throws Exception {
358         Time t = new Time(Time.TIMEZONE_UTC);
359         t.set(1000L);
360         // System.out.println("t.year=" + t.year);
361         // System.out.println("t=" + t);
362         t.set(2000L);
363         // System.out.println("t=" + t);
364         t.set(1000L * 60);
365         // System.out.println("t=" + t);
366         t.set((1000L * 60 * 60 * 24) + 1000L);
367         // System.out.println("t=" + t);
368     }
369 
370     @SmallTest
testSet1()371     public void testSet1() throws Exception {
372         Time t = new Time(Time.TIMEZONE_UTC);
373         t.set(1, 2, 3, 4, 5, 6);
374         // System.out.println("t=" + t);
375     }
376 
377     // Timezones that cover the world.  Some GMT offsets occur more than
378     // once in case some cities decide to change their GMT offset.
379     private static final String[] mTimeZones = {
380         "Pacific/Kiritimati",
381         "Pacific/Enderbury",
382         "Pacific/Fiji",
383         "Antarctica/South_Pole",
384         "Pacific/Norfolk",
385         "Pacific/Ponape",
386         "Asia/Magadan",
387         "Australia/Lord_Howe",
388         "Australia/Sydney",
389         "Australia/Adelaide",
390         "Asia/Tokyo",
391         "Asia/Seoul",
392         "Asia/Taipei",
393         "Asia/Singapore",
394         "Asia/Hong_Kong",
395         "Asia/Saigon",
396         "Asia/Bangkok",
397         "Indian/Cocos",
398         "Asia/Rangoon",
399         "Asia/Omsk",
400         "Antarctica/Mawson",
401         "Asia/Colombo",
402         "Asia/Calcutta",
403         "Asia/Oral",
404         "Asia/Kabul",
405         "Asia/Dubai",
406         "Asia/Tehran",
407         "Europe/Moscow",
408         "Asia/Baghdad",
409         "Africa/Mogadishu",
410         "Europe/Athens",
411         "Africa/Cairo",
412         "Europe/Rome",
413         "Europe/Berlin",
414         "Europe/Amsterdam",
415         "Africa/Tunis",
416         "Europe/London",
417         "Europe/Dublin",
418         "Atlantic/St_Helena",
419         "Africa/Monrovia",
420         "Africa/Accra",
421         "Atlantic/Azores",
422         "Atlantic/South_Georgia",
423         "America/Noronha",
424         "America/Sao_Paulo",
425         "America/Cayenne",
426         "America/St_Johns",
427         "America/Puerto_Rico",
428         "America/Aruba",
429         "America/New_York",
430         "America/Chicago",
431         "America/Denver",
432         "America/Los_Angeles",
433         "America/Anchorage",
434         "Pacific/Marquesas",
435         "America/Adak",
436         "Pacific/Honolulu",
437         "Pacific/Midway",
438     };
439 
440     @Suppress
disableTestGetJulianDay()441     public void disableTestGetJulianDay() throws Exception {
442         Time time = new Time();
443 
444         // For each day of the year, and for each timezone, get the Julian
445         // day for 12am and then check that if we change the time we get the
446         // same Julian day.
447         for (int monthDay = 1; monthDay <= 366; monthDay++) {
448             for (int zoneIndex = 0; zoneIndex < mTimeZones.length; zoneIndex++) {
449                 // We leave the "month" as zero because we are changing the
450                 // "monthDay" from 1 to 366.  The call to normalize() will
451                 // then change the "month" (but we don't really care).
452                 time.set(0, 0, 0, monthDay, 0, 2008);
453                 time.timezone = mTimeZones[zoneIndex];
454                 long millis = time.normalize(true);
455                 if (zoneIndex == 0) {
456                     Log.i("TimeTest", time.format("%B %d, %Y"));
457                 }
458 
459                 // This is the Julian day for 12am for this day of the year
460                 int julianDay = Time.getJulianDay(millis, time.gmtoff);
461 
462                 // Change the time during the day and check that we get the same
463                 // Julian day.
464                 for (int hour = 0; hour < 24; hour++) {
465                     for (int minute = 0; minute < 60; minute += 15) {
466                         time.set(0, minute, hour, monthDay, 0, 2008);
467                         millis = time.normalize(true);
468                         int day = Time.getJulianDay(millis, time.gmtoff);
469                         if (day != julianDay) {
470                             Log.e("TimeTest", "Julian day: " + day + " at time "
471                                     + time.hour + ":" + time.minute
472                                     + " != today's Julian day: " + julianDay
473                                     + " timezone: " + time.timezone);
474                         }
475                         assertEquals(day, julianDay);
476                     }
477                 }
478             }
479         }
480     }
481 
482     @Suppress
disableTestSetJulianDay()483     public void disableTestSetJulianDay() throws Exception {
484         Time time = new Time();
485 
486         // For each day of the year in 2008, and for each timezone,
487         // test that we can set the Julian day correctly.
488         for (int monthDay = 1; monthDay <= 366; monthDay++) {
489             for (int zoneIndex = 0; zoneIndex < mTimeZones.length; zoneIndex++) {
490                 // We leave the "month" as zero because we are changing the
491                 // "monthDay" from 1 to 366.  The call to normalize() will
492                 // then change the "month" (but we don't really care).
493                 time.set(0, 0, 0, monthDay, 0, 2008);
494                 time.timezone = mTimeZones[zoneIndex];
495                 long millis = time.normalize(true);
496                 if (zoneIndex == 0) {
497                     Log.i("TimeTest", time.format("%B %d, %Y"));
498                 }
499                 int julianDay = Time.getJulianDay(millis, time.gmtoff);
500 
501                 time.setJulianDay(julianDay);
502 
503                 // Some places change daylight saving time at 12am and so there
504                 // is no 12am on some days in some timezones.  In those cases,
505                 // the time is set to 1am.
506                 // Examples: Africa/Cairo on April 25, 2008
507                 //  America/Sao_Paulo on October 12, 2008
508                 //  Atlantic/Azores on March 30, 2008
509                 assertTrue(time.hour == 0 || time.hour == 1);
510                 assertEquals(0, time.minute);
511                 assertEquals(0, time.second);
512 
513                 millis = time.toMillis(false);
514                 int day = Time.getJulianDay(millis, time.gmtoff);
515                 if (day != julianDay) {
516                     Log.i("TimeTest", "Error: gmtoff " + (time.gmtoff / 3600.0)
517                             + " day " + julianDay
518                             + " millis " + millis
519                             + " " + time.format("%B %d, %Y") + " " + time.timezone);
520                 }
521                 assertEquals(day, julianDay);
522             }
523         }
524     }
525 }
526