diff options
Diffstat (limited to 'tests/src')
3 files changed, 359 insertions, 217 deletions
diff --git a/tests/src/com/android/calendarcommon2/RRuleTest.java b/tests/src/com/android/calendarcommon2/RRuleTest.java index a473263..18217a3 100644 --- a/tests/src/com/android/calendarcommon2/RRuleTest.java +++ b/tests/src/com/android/calendarcommon2/RRuleTest.java @@ -114,8 +114,7 @@ public class RRuleTest extends TestCase { RecurrenceProcessor rp = new RecurrenceProcessor(); RecurrenceSet recur = new RecurrenceSet(rrule, rdate, exrule, exdate); - long[] out = rp.expand(dtstart, recur, rangeStart.toMillis(false /* use isDst */), - rangeEnd.toMillis(false /* use isDst */)); + long[] out = rp.expand(dtstart, recur, rangeStart.toMillis(), rangeEnd.toMillis()); if (METHOD_TRACE) { Debug.stopMethodTracing(); diff --git a/tests/src/com/android/calendarcommon2/RecurrenceProcessorTest.java b/tests/src/com/android/calendarcommon2/RecurrenceProcessorTest.java index 0687ec4..3cd9177 100644 --- a/tests/src/com/android/calendarcommon2/RecurrenceProcessorTest.java +++ b/tests/src/com/android/calendarcommon2/RecurrenceProcessorTest.java @@ -23,7 +23,6 @@ import android.test.suitebuilder.annotation.MediumTest; import android.test.suitebuilder.annotation.SmallTest; import android.text.TextUtils; import android.util.Log; -import android.util.TimeFormatException; import junit.framework.TestCase; import java.util.TreeSet; @@ -105,8 +104,7 @@ public class RecurrenceProcessorTest extends TestCase { RecurrenceProcessor rp = new RecurrenceProcessor(); RecurrenceSet recur = new RecurrenceSet(rrule, rdate, exrule, exdate); - long[] out = rp.expand(dtstart, recur, rangeStart.toMillis(false /* use isDst */), - rangeEnd.toMillis(false /* use isDst */)); + long[] out = rp.expand(dtstart, recur, rangeStart.toMillis(), rangeEnd.toMillis()); if (METHOD_TRACE) { Debug.stopMethodTracing(); @@ -149,12 +147,12 @@ public class RecurrenceProcessorTest extends TestCase { if (lastOccur != -1) { outCal.set(lastOccur); lastStr = outCal.format2445(); - lastMillis = outCal.toMillis(true /* ignore isDst */); + lastMillis = outCal.toMillis(); } if (last != null && last.length() > 0) { Time expectedLast = new Time(tz); expectedLast.parse(last); - expectedMillis = expectedLast.toMillis(true /* ignore isDst */); + expectedMillis = expectedLast.toMillis(); } if (lastMillis != expectedMillis) { if (SPEW) { @@ -597,7 +595,7 @@ public class RecurrenceProcessorTest extends TestCase { "20060219T100000" }, "20060220T020001"); fail("Bad UNTIL string failed to throw exception"); - } catch (TimeFormatException e) { + } catch (IllegalArgumentException e) { // expected } } @@ -2459,8 +2457,8 @@ public class RecurrenceProcessorTest extends TestCase { dtstart.parse("20010101T000000"); rangeStart.parse("20010101T000000"); rangeEnd.parse("20090101T000000"); - long rangeStartMillis = rangeStart.toMillis(false /* use isDst */); - long rangeEndMillis = rangeEnd.toMillis(false /* use isDst */); + long rangeStartMillis = rangeStart.toMillis(); + long rangeEndMillis = rangeEnd.toMillis(); long startTime = System.currentTimeMillis(); for (int iterations = 0; iterations < 5; iterations++) { @@ -2503,12 +2501,12 @@ public class RecurrenceProcessorTest extends TestCase { long startTime = System.currentTimeMillis(); for (int i = 0; i < ITERATIONS; i++) { - date.month += 1; - date.monthDay += 100; - date.normalize(true); - date.month -= 1; - date.monthDay -= 100; - date.normalize(true); + date.add(Time.MONTH, 1); + date.add(Time.MONTH_DAY, 100); + date.normalize(); + date.add(Time.MONTH, -1); + date.add(Time.MONTH_DAY, -100); + date.normalize(); } long endTime = System.currentTimeMillis(); @@ -2520,11 +2518,11 @@ public class RecurrenceProcessorTest extends TestCase { date.parse("20090404T100000"); startTime = System.currentTimeMillis(); for (int i = 0; i < ITERATIONS; i++) { - date.month += 1; - date.monthDay += 100; + date.add(Time.MONTH, 1); + date.add(Time.MONTH_DAY, 100); RecurrenceProcessor.unsafeNormalize(date); - date.month -= 1; - date.monthDay -= 100; + date.add(Time.MONTH, -1); + date.add(Time.MONTH_DAY, -100); RecurrenceProcessor.unsafeNormalize(date); } diff --git a/tests/src/com/android/calendarcommon2/TimeTest.java b/tests/src/com/android/calendarcommon2/TimeTest.java index 600506a..df27c4f 100644 --- a/tests/src/com/android/calendarcommon2/TimeTest.java +++ b/tests/src/com/android/calendarcommon2/TimeTest.java @@ -29,9 +29,19 @@ import junit.framework.TestCase; public class TimeTest extends TestCase { @SmallTest + public void testNullTimezone() { + try { + Time t = new Time(null); + fail("expected a null timezone to throw an exception."); + } catch (NullPointerException npe) { + // expected. + } + } + + @SmallTest public void testTimezone() { Time t = new Time(Time.TIMEZONE_UTC); - assertEquals(Time.TIMEZONE_UTC, t.timezone); + assertEquals(Time.TIMEZONE_UTC, t.getTimezone()); } @SmallTest @@ -39,23 +49,40 @@ public class TimeTest extends TestCase { Time t = new Time(Time.TIMEZONE_UTC); String newTimezone = "America/Los_Angeles"; t.switchTimezone(newTimezone); - assertEquals(newTimezone, t.timezone); + assertEquals(newTimezone, t.getTimezone()); } @SmallTest public void testGetActualMaximum() { Time t = new Time(Time.TIMEZONE_UTC); - t.set(0, 0, 2020); + t.set(1, 0, 2020); assertEquals(59, t.getActualMaximum(Time.SECOND)); assertEquals(59, t.getActualMaximum(Time.MINUTE)); assertEquals(23, t.getActualMaximum(Time.HOUR)); assertEquals(31, t.getActualMaximum(Time.MONTH_DAY)); assertEquals(11, t.getActualMaximum(Time.MONTH)); - assertEquals(2037, t.getActualMaximum(Time.YEAR)); - assertEquals(6, t.getActualMaximum(Time.WEEK_DAY)); - assertEquals(365, t.getActualMaximum(Time.YEAR_DAY)); // 2020 is a leap year - t.set(0, 0, 2019); - assertEquals(364, t.getActualMaximum(Time.YEAR_DAY)); + assertEquals(7, t.getActualMaximum(Time.WEEK_DAY)); + assertEquals(366, t.getActualMaximum(Time.YEAR_DAY)); // 2020 is a leap year + t.set(1, 0, 2019); + assertEquals(365, t.getActualMaximum(Time.YEAR_DAY)); + } + + @SmallTest + public void testAdd() { + Time t = new Time(Time.TIMEZONE_UTC); + t.set(0, 0, 0, 1, 0, 2020); + t.add(Time.SECOND, 1); + assertEquals(1, t.getSecond()); + t.add(Time.MINUTE, 1); + assertEquals(1, t.getMinute()); + t.add(Time.HOUR, 1); + assertEquals(1, t.getHour()); + t.add(Time.MONTH_DAY, 1); + assertEquals(2, t.getDay()); + t.add(Time.MONTH, 1); + assertEquals(1, t.getMonth()); + t.add(Time.YEAR, 1); + assertEquals(2021, t.getYear()); } @SmallTest @@ -63,37 +90,40 @@ public class TimeTest extends TestCase { Time t = new Time(Time.TIMEZONE_UTC); t.clear(Time.TIMEZONE_UTC); - assertEquals(Time.TIMEZONE_UTC, t.timezone); - assertFalse(t.allDay); - assertEquals(0, t.second); - assertEquals(0, t.minute); - assertEquals(0, t.hour); - assertEquals(0, t.monthDay); - assertEquals(0, t.month); - assertEquals(0, t.year); - assertEquals(0, t.weekDay); - assertEquals(0, t.yearDay); - assertEquals(0, t.gmtoff); - assertEquals(-1, t.isDst); + assertEquals(Time.TIMEZONE_UTC, t.getTimezone()); + assertFalse(t.isAllDay()); + assertEquals(0, t.getSecond()); + assertEquals(0, t.getMinute()); + assertEquals(0, t.getHour()); + assertEquals(1, t.getDay()); // default for Calendar is 1 + assertEquals(0, t.getMonth()); + assertEquals(1970, t.getYear()); + assertEquals(4, t.getWeekDay()); // 1970 Jan 1 --> Thursday + assertEquals(0, t.getYearDay()); + assertEquals(0, t.getGmtOffset()); } @SmallTest public void testCompare() { Time a = new Time(Time.TIMEZONE_UTC); Time b = new Time("America/Los_Angeles"); - assertTrue(Time.compare(a, b) < 0); - } + assertTrue(a.compareTo(b) < 0); - @SmallTest - public void testFormat() { - Time t = new Time(Time.TIMEZONE_UTC); - assertEquals("19700101T000000", t.format("%Y%m%dT%H%M%S")); + Time c = new Time("Asia/Calcutta"); + assertTrue(a.compareTo(c) > 0); + + Time d = new Time(Time.TIMEZONE_UTC); + assertEquals(0, a.compareTo(d)); } @SmallTest public void testFormat2445() { - Time t = new Time(Time.TIMEZONE_UTC); + Time t = new Time(); + assertEquals("19700101T000000", t.format2445()); + t.setTimezone(Time.TIMEZONE_UTC); assertEquals("19700101T000000Z", t.format2445()); + t.setAllDay(true); + assertEquals("19700101", t.format2445()); } @SmallTest @@ -105,26 +135,63 @@ public class TimeTest extends TestCase { } @SmallTest - public void testMillis0() { + public void testToMillis() { Time t = new Time(Time.TIMEZONE_UTC); + t.set(1, 0, 0, 1, 0, 1970); + assertEquals(1000L, t.toMillis()); + t.set(0, 0, 0, 1, 1, 2020); - assertEquals(1580515200000L, t.toMillis(true)); + assertEquals(1580515200000L, t.toMillis()); t.set(1, 0, 0, 1, 1, 2020); - assertEquals(1580515201000L, t.toMillis(true)); + assertEquals(1580515201000L, t.toMillis()); + + t.set(1, 0, 2020); + assertEquals(1577836800000L, t.toMillis()); + t.set(1, 1, 2020); + assertEquals(1580515200000L, t.toMillis()); } @SmallTest - public void testMillis1() { + public void testToMillis_overflow() { Time t = new Time(Time.TIMEZONE_UTC); - t.set(1, 0, 0, 1, 0, 1970); - assertEquals(1000L, t.toMillis(true)); + t.set(32, 0, 2020); + assertEquals(1580515200000L, t.toMillis()); + assertEquals(1, t.getDay()); + assertEquals(1, t.getMonth()); } @SmallTest public void testParse() { Time t = new Time(Time.TIMEZONE_UTC); - assertTrue(t.parse("20201010T160000Z")); - assertFalse(t.parse("12345678T901234")); + t.parse("20201010T160000Z"); + assertEquals(2020, t.getYear()); + assertEquals(9, t.getMonth()); + assertEquals(10, t.getDay()); + assertEquals(16, t.getHour()); + assertEquals(0, t.getMinute()); + assertEquals(0, t.getSecond()); + + t.parse("20200220"); + assertEquals(2020, t.getYear()); + assertEquals(1, t.getMonth()); + assertEquals(20, t.getDay()); + assertEquals(0, t.getHour()); + assertEquals(0, t.getMinute()); + assertEquals(0, t.getSecond()); + + try { + t.parse("invalid"); + fail(); + } catch (IllegalArgumentException e) { + // expected + } + + try { + t.parse("20201010Z160000"); + fail(); + } catch (IllegalArgumentException e) { + // expected + } } @SmallTest @@ -132,51 +199,58 @@ public class TimeTest extends TestCase { Time t = new Time(Time.TIMEZONE_UTC); t.parse3339("1980-05-23"); - if (!t.allDay || t.year != 1980 || t.month != 4 || t.monthDay != 23) { + if (!t.isAllDay() || t.getYear() != 1980 || t.getMonth() != 4 || t.getDay() != 23) { fail("Did not parse all-day date correctly"); } t.parse3339("1980-05-23T09:50:50"); - if (t.allDay || t.year != 1980 || t.month != 4 || t.monthDay != 23 || t.hour != 9 - || t.minute != 50 || t.second != 50 || t.gmtoff != 0) { + if (t.isAllDay() || t.getYear() != 1980 || t.getMonth() != 4 || t.getDay() != 23 + || t.getHour() != 9 || t.getMinute() != 50 || t.getSecond() != 50 + || t.getGmtOffset() != 0) { fail("Did not parse timezone-offset-less date correctly"); } t.parse3339("1980-05-23T09:50:50Z"); - if (t.allDay || t.year != 1980 || t.month != 4 || t.monthDay != 23 || t.hour != 9 - || t.minute != 50 || t.second != 50 || t.gmtoff != 0) { + if (t.isAllDay() || t.getYear() != 1980 || t.getMonth() != 4 || t.getDay() != 23 + || t.getHour() != 9 || t.getMinute() != 50 || t.getSecond() != 50 + || t.getGmtOffset() != 0) { fail("Did not parse UTC date correctly"); } t.parse3339("1980-05-23T09:50:50.0Z"); - if (t.allDay || t.year != 1980 || t.month != 4 || t.monthDay != 23 || t.hour != 9 - || t.minute != 50 || t.second != 50 || t.gmtoff != 0) { + if (t.isAllDay() || t.getYear() != 1980 || t.getMonth() != 4 || t.getDay() != 23 + || t.getHour() != 9 || t.getMinute() != 50 || t.getSecond() != 50 + || t.getGmtOffset() != 0) { fail("Did not parse UTC date correctly"); } t.parse3339("1980-05-23T09:50:50.12Z"); - if (t.allDay || t.year != 1980 || t.month != 4 || t.monthDay != 23 || t.hour != 9 - || t.minute != 50 || t.second != 50 || t.gmtoff != 0) { + if (t.isAllDay() || t.getYear() != 1980 || t.getMonth() != 4 || t.getDay() != 23 + || t.getHour() != 9 || t.getMinute() != 50 || t.getSecond() != 50 + || t.getGmtOffset() != 0) { fail("Did not parse UTC date correctly"); } t.parse3339("1980-05-23T09:50:50.123Z"); - if (t.allDay || t.year != 1980 || t.month != 4 || t.monthDay != 23 || t.hour != 9 - || t.minute != 50 || t.second != 50 || t.gmtoff != 0) { + if (t.isAllDay() || t.getYear() != 1980 || t.getMonth() != 4 || t.getDay() != 23 + || t.getHour() != 9 || t.getMinute() != 50 || t.getSecond() != 50 + || t.getGmtOffset() != 0) { fail("Did not parse UTC date correctly"); } // the time should be normalized to UTC t.parse3339("1980-05-23T09:50:50-01:05"); - if (t.allDay || t.year != 1980 || t.month != 4 || t.monthDay != 23 || t.hour != 10 - || t.minute != 55 || t.second != 50 || t.gmtoff != 0) { + if (t.isAllDay() || t.getYear() != 1980 || t.getMonth() != 4 || t.getDay() != 23 + || t.getHour() != 10 || t.getMinute() != 55 || t.getSecond() != 50 + || t.getGmtOffset() != 0) { fail("Did not parse timezone-offset date correctly"); } // the time should be normalized to UTC t.parse3339("1980-05-23T09:50:50.123-01:05"); - if (t.allDay || t.year != 1980 || t.month != 4 || t.monthDay != 23 || t.hour != 10 - || t.minute != 55 || t.second != 50 || t.gmtoff != 0) { + if (t.isAllDay() || t.getYear() != 1980 || t.getMonth() != 4 || t.getDay() != 23 + || t.getHour() != 10 || t.getMinute() != 55 || t.getSecond() != 50 + || t.getGmtOffset() != 0) { fail("Did not parse timezone-offset date correctly"); } @@ -203,74 +277,93 @@ public class TimeTest extends TestCase { } @SmallTest - public void testSet0() { + public void testSet_millis() { Time t = new Time(Time.TIMEZONE_UTC); t.set(1000L); - assertEquals(1970, t.year); - assertEquals(1, t.second); + assertEquals(1970, t.getYear()); + assertEquals(1, t.getSecond()); t.set(2000L); - assertEquals(2, t.second); - assertEquals(0, t.minute); + assertEquals(2, t.getSecond()); + assertEquals(0, t.getMinute()); t.set(1000L * 60); - assertEquals(1, t.minute); - assertEquals(0, t.hour); + assertEquals(1, t.getMinute()); + assertEquals(0, t.getHour()); t.set(1000L * 60 * 60); - assertEquals(1, t.hour); - assertEquals(1, t.monthDay); + assertEquals(1, t.getHour()); + assertEquals(1, t.getDay()); t.set((1000L * 60 * 60 * 24) + 1000L); - assertEquals(2, t.monthDay); - assertEquals(1970, t.year); + assertEquals(2, t.getDay()); + assertEquals(1970, t.getYear()); } @SmallTest - public void testSet1() { + public void testSet_dayMonthYear() { Time t = new Time(Time.TIMEZONE_UTC); t.set(1, 2, 2021); - assertEquals(1, t.monthDay); - assertEquals(2, t.month); - assertEquals(2021, t.year); + assertEquals(1, t.getDay()); + assertEquals(2, t.getMonth()); + assertEquals(2021, t.getYear()); } @SmallTest - public void testSet2() { + public void testSet_secondMinuteHour() { Time t = new Time(Time.TIMEZONE_UTC); t.set(1, 2, 3, 4, 5, 2021); - assertEquals(1, t.second); - assertEquals(2, t.minute); - assertEquals(3, t.hour); - assertEquals(4, t.monthDay); - assertEquals(5, t.month); - assertEquals(2021, t.year); + assertEquals(1, t.getSecond()); + assertEquals(2, t.getMinute()); + assertEquals(3, t.getHour()); + assertEquals(4, t.getDay()); + assertEquals(5, t.getMonth()); + assertEquals(2021, t.getYear()); + } + + @SmallTest + public void testSet_overflow() { + // Jan 32nd --> Feb 1st + Time t = new Time(Time.TIMEZONE_UTC); + t.set(32, 0, 2020); + assertEquals(1, t.getDay()); + assertEquals(1, t.getMonth()); + assertEquals(2020, t.getYear()); + + t = new Time(Time.TIMEZONE_UTC); + t.set(5, 10, 15, 32, 0, 2020); + assertEquals(5, t.getSecond()); + assertEquals(10, t.getMinute()); + assertEquals(15, t.getHour()); + assertEquals(1, t.getDay()); + assertEquals(1, t.getMonth()); + assertEquals(2020, t.getYear()); } @SmallTest - public void testSet3() { + public void testSet_other() { Time t = new Time(Time.TIMEZONE_UTC); t.set(1, 2, 3, 4, 5, 2021); Time t2 = new Time(); t2.set(t); - assertEquals(Time.TIMEZONE_UTC, t2.timezone); - assertEquals(1, t2.second); - assertEquals(2, t2.minute); - assertEquals(3, t2.hour); - assertEquals(4, t2.monthDay); - assertEquals(5, t2.month); - assertEquals(2021, t2.year); + assertEquals(Time.TIMEZONE_UTC, t2.getTimezone()); + assertEquals(1, t2.getSecond()); + assertEquals(2, t2.getMinute()); + assertEquals(3, t2.getHour()); + assertEquals(4, t2.getDay()); + assertEquals(5, t2.getMonth()); + assertEquals(2021, t2.getYear()); } @SmallTest public void testSetToNow() { - Time t = new Time(Time.TIMEZONE_UTC); - t.setToNow(); - long ms = t.toMillis(true); long now = System.currentTimeMillis(); - // ensure millis returned are within 1 second of when they were set - assertTrue(ms < now && ms > (now - 1000)); + Time t = new Time(Time.TIMEZONE_UTC); + t.set(now); + long ms = t.toMillis(); + // ensure time is within 1 second because of rounding errors + assertTrue("now: " + now + "; actual: " + ms, Math.abs(ms - now) < 1000); } @SmallTest @@ -280,6 +373,14 @@ public class TimeTest extends TestCase { assertEquals(1, t.getWeekNumber()); t.set(1, 1, 2020); assertEquals(5, t.getWeekNumber()); + + // ensure ISO 8601 standards are met: weeks start on Monday and the first week has at least + // 4 days in it (the year's first Thursday or Jan 4th) + for (int i = 1; i <= 8; i++) { + t.set(i, 0, 2020); + // Jan 6th is the first Monday in 2020 so that would be week 2 + assertEquals(i < 6 ? 1 : 2, t.getWeekNumber()); + } } private static class DateTest { @@ -288,7 +389,6 @@ public class TimeTest extends TestCase { public int day1; public int hour1; public int minute1; - public int dst1; public int offset; @@ -297,7 +397,6 @@ public class TimeTest extends TestCase { public int day2; public int hour2; public int minute2; - public int dst2; public DateTest(int year1, int month1, int day1, int hour1, int minute1, int offset, int year2, int month2, int day2, int hour2, int minute2) { @@ -306,49 +405,30 @@ public class TimeTest extends TestCase { this.day1 = day1; this.hour1 = hour1; this.minute1 = minute1; - this.dst1 = -1; this.offset = offset; this.year2 = year2; this.month2 = month2; this.day2 = day2; this.hour2 = hour2; this.minute2 = minute2; - this.dst2 = -1; - } - - public 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) { - this.year1 = year1; - this.month1 = month1; - this.day1 = day1; - this.hour1 = hour1; - this.minute1 = minute1; - this.dst1 = dst1; - this.offset = offset; - this.year2 = year2; - this.month2 = month2; - this.day2 = day2; - this.hour2 = hour2; - this.minute2 = minute2; - this.dst2 = dst2; } public boolean equals(Time time) { - return time.year == year2 && time.month == month2 && time.monthDay == day2 - && time.hour == hour2 && time.minute == minute2; + return time.getYear() == year2 && time.getMonth() == month2 && time.getDay() == day2 + && time.getHour() == hour2 && time.getMinute() == minute2; } + } - public boolean equalsWithDst(Time time) { - return time.year == year2 && time.month == month2 && time.monthDay == day2 - && time.hour == hour2 && time.minute == minute2 && time.isDst == dst2; - } + @SmallTest + public void testNormalize() { + Time t = new Time(Time.TIMEZONE_UTC); + t.parse("20060432T010203"); + assertEquals(1146531723000L, t.normalize()); } /* These tests assume that DST changes on Nov 4, 2007 at 2am (to 1am). */ // The "offset" field in "dayTests" represents days. - // Use normalize(true) with these tests to change the date by 1 day. // Note: the month numbers are 0-relative, so Jan=0, Feb=1,...Dec=11 private DateTest[] dayTests = { // Nov 4, 12am + 0 day = Nov 4, 12am @@ -376,11 +456,12 @@ public class TimeTest extends TestCase { }; // The "offset" field in "minuteTests" represents minutes. - // Use normalize(false) with these tests. // Note: the month numbers are 0-relative, so Jan=0, Feb=1,...Dec=11 private DateTest[] minuteTests = { // Nov 4, 12am + 0 minutes = Nov 4, 12am new DateTest(2007, 10, 4, 0, 0, 0, 2007, 10, 4, 0, 0), + // Nov 4, 12am + 60 minutes = Nov 4, 1am + new DateTest(2007, 10, 4, 0, 0, 60, 2007, 10, 4, 1, 0), // Nov 5, 12am + 0 minutes = Nov 5, 12am new DateTest(2007, 10, 5, 0, 0, 0, 2007, 10, 5, 0, 0), // Nov 3, 12am + 60 minutes = Nov 3, 1am @@ -391,20 +472,24 @@ public class TimeTest extends TestCase { new DateTest(2007, 10, 5, 0, 0, 60, 2007, 10, 5, 1, 0), // Nov 3, 1am + 60 minutes = Nov 3, 2am new DateTest(2007, 10, 3, 1, 0, 60, 2007, 10, 3, 2, 0), + // Nov 4, 12:59am (PDT) + 2 minutes = Nov 4, 1:01am (PDT) + new DateTest(2007, 10, 4, 0, 59, 2, 2007, 10, 4, 1, 1), + // Nov 4, 12:59am (PDT) + 62 minutes = Nov 4, 1:01am (PST) + new DateTest(2007, 10, 4, 0, 59, 62, 2007, 10, 4, 1, 1), + // Nov 4, 12:30am (PDT) + 120 minutes = Nov 4, 1:30am (PST) + new DateTest(2007, 10, 4, 0, 30, 120, 2007, 10, 4, 1, 30), + // Nov 4, 12:30am (PDT) + 90 minutes = Nov 4, 1:00am (PST) + new DateTest(2007, 10, 4, 0, 30, 90, 2007, 10, 4, 1, 0), // Nov 4, 1am (PDT) + 30 minutes = Nov 4, 1:30am (PDT) - new DateTest(2007, 10, 4, 1, 0, 1, 30, 2007, 10, 4, 1, 30, 1), - // Nov 4, 1am (PDT) + 60 minutes = Nov 4, 1am (PST) - new DateTest(2007, 10, 4, 1, 0, 1, 60, 2007, 10, 4, 1, 0, 0), + new DateTest(2007, 10, 4, 1, 0, 30, 2007, 10, 4, 1, 30), // Nov 4, 1:30am (PDT) + 15 minutes = Nov 4, 1:45am (PDT) - new DateTest(2007, 10, 4, 1, 30, 1, 15, 2007, 10, 4, 1, 45, 1), - // Nov 4, 1:30am (PDT) + 30 minutes = Nov 4, 1:00am (PST) - new DateTest(2007, 10, 4, 1, 30, 1, 30, 2007, 10, 4, 1, 0, 0), - // Nov 4, 1:30am (PDT) + 60 minutes = Nov 4, 1:30am (PST) - new DateTest(2007, 10, 4, 1, 30, 1, 60, 2007, 10, 4, 1, 30, 0), + new DateTest(2007, 10, 4, 1, 30, 15, 2007, 10, 4, 1, 45), + // Mar 11, 1:30am (PST) + 30 minutes = Mar 11, 3am (PDT) + new DateTest(2007, 2, 11, 1, 30, 30, 2007, 2, 11, 3, 0), // Nov 4, 1:30am (PST) + 15 minutes = Nov 4, 1:45am (PST) - new DateTest(2007, 10, 4, 1, 30, 0, 15, 2007, 10, 4, 1, 45, 0), + new DateTest(2007, 10, 4, 1, 30, 15, 2007, 10, 4, 1, 45), // Nov 4, 1:30am (PST) + 30 minutes = Nov 4, 2:00am (PST) - new DateTest(2007, 10, 4, 1, 30, 0, 30, 2007, 10, 4, 2, 0, 0), + new DateTest(2007, 10, 4, 1, 30, 30, 2007, 10, 4, 2, 0), // Nov 5, 1am + 60 minutes = Nov 5, 2am new DateTest(2007, 10, 5, 1, 0, 60, 2007, 10, 5, 2, 0), // Nov 3, 2am + 60 minutes = Nov 3, 3am @@ -415,46 +500,35 @@ public class TimeTest extends TestCase { new DateTest(2007, 10, 4, 2, 0, 60, 2007, 10, 4, 3, 0), // Nov 5, 2am + 60 minutes = Nov 5, 3am new DateTest(2007, 10, 5, 2, 0, 60, 2007, 10, 5, 3, 0), + // NOTE: Calendar assumes 1am PDT == 1am PST, the two are not distinct, hence why the transition boundary itself has no tests }; - @SmallTest - public void testNormalize0() { - Time t = new Time(Time.TIMEZONE_UTC); - t.parse("20060432T010203"); - assertEquals(1146531723000L, t.normalize(false)); - } - @MediumTest - public void testNormalize1() { + public void testNormalize_dst() { Time local = new Time("America/Los_Angeles"); int len = dayTests.length; for (int index = 0; index < len; index++) { DateTest test = dayTests[index]; local.set(0, test.minute1, test.hour1, test.day1, test.month1, test.year1); - // call normalize() to make sure that isDst is set - local.normalize(false); - local.monthDay += test.offset; - local.normalize(true); + local.add(Time.MONTH_DAY, test.offset); if (!test.equals(local)) { String expectedTime = String.format("%d-%02d-%02d %02d:%02d", test.year2, test.month2, test.day2, test.hour2, test.minute2); String actualTime = String.format("%d-%02d-%02d %02d:%02d", - local.year, local.month, local.monthDay, local.hour, local.minute); + local.getYear(), local.getMonth(), local.getDay(), local.getHour(), + local.getMinute()); fail("Expected: " + expectedTime + "; Actual: " + actualTime); } local.set(0, test.minute1, test.hour1, test.day1, test.month1, test.year1); - // call normalize() to make sure that isDst is set - local.normalize(false); - local.monthDay += test.offset; - long millis = local.toMillis(true); - local.set(millis); + local.add(Time.MONTH_DAY, test.offset); if (!test.equals(local)) { String expectedTime = String.format("%d-%02d-%02d %02d:%02d", test.year2, test.month2, test.day2, test.hour2, test.minute2); String actualTime = String.format("%d-%02d-%02d %02d:%02d", - local.year, local.month, local.monthDay, local.hour, local.minute); + local.getYear(), local.getMonth(), local.getDay(), local.getHour(), + local.getMinute()); fail("Expected: " + expectedTime + "; Actual: " + actualTime); } } @@ -463,40 +537,109 @@ public class TimeTest extends TestCase { for (int index = 0; index < len; index++) { DateTest test = minuteTests[index]; local.set(0, test.minute1, test.hour1, test.day1, test.month1, test.year1); - local.isDst = test.dst1; - // call normalize() to make sure that isDst is set - local.normalize(false); - if (test.dst2 == -1) test.dst2 = local.isDst; - local.minute += test.offset; - local.normalize(false); - if (!test.equalsWithDst(local)) { - String expectedTime = String.format("%d-%02d-%02d %02d:%02d isDst: %d", - test.year2, test.month2, test.day2, test.hour2, test.minute2, test.dst2); - String actualTime = String.format("%d-%02d-%02d %02d:%02d isDst: %d", - local.year, local.month, local.monthDay, local.hour, local.minute, - local.isDst); + local.add(Time.MINUTE, test.offset); + if (!test.equals(local)) { + String expectedTime = String.format("%d-%02d-%02d %02d:%02d", + test.year2, test.month2, test.day2, test.hour2, test.minute2); + String actualTime = String.format("%d-%02d-%02d %02d:%02d", + local.getYear(), local.getMonth(), local.getDay(), local.getHour(), + local.getMinute()); fail("Expected: " + expectedTime + "; Actual: " + actualTime); } local.set(0, test.minute1, test.hour1, test.day1, test.month1, test.year1); - local.isDst = test.dst1; - // call normalize() to make sure that isDst is set - local.normalize(false); - if (test.dst2 == -1) test.dst2 = local.isDst; - local.minute += test.offset; - long millis = local.toMillis(false); - local.set(millis); - if (!test.equalsWithDst(local)) { - String expectedTime = String.format("%d-%02d-%02d %02d:%02d isDst: %d", - test.year2, test.month2, test.day2, test.hour2, test.minute2, test.dst2); - String actualTime = String.format("%d-%02d-%02d %02d:%02d isDst: %d", - local.year, local.month, local.monthDay, local.hour, local.minute, - local.isDst); + local.add(Time.MINUTE, test.offset); + if (!test.equals(local)) { + String expectedTime = String.format("%d-%02d-%02d %02d:%02d", + test.year2, test.month2, test.day2, test.hour2, test.minute2); + String actualTime = String.format("%d-%02d-%02d %02d:%02d", + local.getYear(), local.getMonth(), local.getDay(), local.getHour(), + local.getMinute()); fail("Expected: " + expectedTime + "; Actual: " + actualTime); } } } + @SmallTest + public void testNormalize_overflow() { + Time t = new Time(Time.TIMEZONE_UTC); + t.set(32, 0, 2020); + t.normalize(); + assertEquals(1, t.getDay()); + assertEquals(1, t.getMonth()); + } + + @SmallTest + public void testDstBehavior_addDays_ignoreDst() { + Time time = new Time("America/Los_Angeles"); + time.set(4, 10, 2007); // set to Nov 4, 2007, 12am + assertEquals(1194159600000L, time.normalize()); + time.add(Time.MONTH_DAY, 1); // changes to Nov 5, 2007, 12am + assertEquals(1194249600000L, time.toMillis()); + + time = new Time("America/Los_Angeles"); + time.set(11, 2, 2007); // set to Mar 11, 2007, 12am + assertEquals(1173600000000L, time.normalize()); + time.add(Time.MONTH_DAY, 1); // changes to Mar 12, 2007, 12am + assertEquals(1173682800000L, time.toMillis()); + } + + @SmallTest + public void testDstBehavior_addDays_applyDst() { + if (!Time.APPLY_DST_CHANGE_LOGIC) { + return; + } + Time time = new Time("America/Los_Angeles"); + time.set(4, 10, 2007); // set to Nov 4, 2007, 12am + assertEquals(1194159600000L, time.normalizeApplyDst()); + time.add(Time.MONTH_DAY, 1); // changes to Nov 4, 2007, 11pm (fall back) + assertEquals(1194246000000L, time.toMillisApplyDst()); + + time = new Time("America/Los_Angeles"); + time.set(11, 2, 2007); // set to Mar 11, 2007, 12am + assertEquals(1173600000000L, time.normalizeApplyDst()); + time.add(Time.MONTH_DAY, 1); // changes to Mar 12, 2007, 1am (roll forward) + assertEquals(1173686400000L, time.toMillisApplyDst()); + } + + @SmallTest + public void testDstBehavior_addHours_ignoreDst() { + // Note: by default, Calendar applies DST logic if adding hours or minutes but not if adding + // days, hence in this test, only if the APPLY_DST_CHANGE_LOGIC flag is false, then the time + // is adjusted with DST + Time time = new Time("America/Los_Angeles"); + time.set(4, 10, 2007); // set to Nov 4, 2007, 12am + assertEquals(1194159600000L, time.normalize()); + time.add(Time.HOUR, 24); // changes to Nov 5, 2007, 12am + assertEquals(Time.APPLY_DST_CHANGE_LOGIC ? 1194249600000L : 1194246000000L, + time.toMillis()); + + time = new Time("America/Los_Angeles"); + time.set(11, 2, 2007); // set to Mar 11, 2007, 12am + assertEquals(1173600000000L, time.normalize()); + time.add(Time.HOUR, 24); // changes to Mar 12, 2007, 12am + assertEquals(Time.APPLY_DST_CHANGE_LOGIC ? 1173682800000L : 1173686400000L, + time.toMillis()); + } + + @SmallTest + public void testDstBehavior_addHours_applyDst() { + if (!Time.APPLY_DST_CHANGE_LOGIC) { + return; + } + Time time = new Time("America/Los_Angeles"); + time.set(4, 10, 2007); // set to Nov 4, 2007, 12am + assertEquals(1194159600000L, time.normalizeApplyDst()); + time.add(Time.HOUR, 24); // changes to Nov 4, 2007, 11pm (fall back) + assertEquals(1194246000000L, time.toMillisApplyDst()); + + time = new Time("America/Los_Angeles"); + time.set(11, 2, 2007); // set to Mar 11, 2007, 12am + assertEquals(1173600000000L, time.normalizeApplyDst()); + time.add(Time.HOUR, 24); // changes to Mar 12, 2007, 1am (roll forward) + assertEquals(1173686400000L, time.toMillisApplyDst()); + } + // Timezones that cover the world. // Some GMT offsets occur more than once in case some cities decide to change their GMT offset. private static final String[] mTimeZones = { @@ -562,32 +705,33 @@ public class TimeTest extends TestCase { @MediumTest public void testGetJulianDay() { - Time time = new Time(); - - // for a random day in the year 2020 and for a random timezone, get the Julian day for 12am - // and then check that if we change the time we get the same Julian day. - int monthDay = (int) (Math.random() * 365) + 1; - int zoneIndex = (int) (Math.random() * mTimeZones.length); - time.set(0, 0, 0, monthDay, 0, 2020); - time.timezone = mTimeZones[zoneIndex]; - long millis = time.normalize(true); - - int julianDay = Time.getJulianDay(millis, time.gmtoff); - - // change the time during the day and check that we get the same Julian day. - for (int hour = 0; hour < 24; hour++) { - for (int minute = 0; minute < 60; minute += 15) { - time.set(0, minute, hour, monthDay, 0, 2020); - millis = time.normalize(true); - int day = Time.getJulianDay(millis, time.gmtoff); - assertEquals(day, julianDay); + Time time = new Time(Time.TIMEZONE_UTC); + + // for 30 random days in the year 2020 and for a random timezone, get the Julian day for + // 12am and then check that if we change the time we get the same Julian day. + for (int i = 0; i < 30; i++) { + int monthDay = (int) (Math.random() * 365) + 1; + int zoneIndex = (int) (Math.random() * mTimeZones.length); + time.setTimezone(mTimeZones[zoneIndex]); + time.set(0, 0, 0, monthDay, 0, 2020); + + int julianDay = Time.getJulianDay(time.normalize(), time.getGmtOffset()); + + // change the time during the day and check that we get the same Julian day. + for (int hour = 0; hour < 24; hour++) { + for (int minute = 0; minute < 60; minute += 15) { + time.set(0, minute, hour, monthDay, 0, 2020); + int day = Time.getJulianDay(time.normalize(), time.getGmtOffset()); + assertEquals(day, julianDay); + time.clear(Time.TIMEZONE_UTC); + } } } } @MediumTest public void testSetJulianDay() { - Time time = new Time(); + Time time = new Time(Time.TIMEZONE_UTC); // for each day in the year 2020, pick a random timezone, and verify that we can // set the Julian day correctly. @@ -596,22 +740,23 @@ public class TimeTest extends TestCase { // leave the "month" as zero because we are changing the "monthDay" from 1 to 366. // the call to normalize() will then change the "month" (but we don't really care). time.set(0, 0, 0, monthDay, 0, 2020); - time.timezone = mTimeZones[zoneIndex]; - long millis = time.normalize(true); - int julianDay = Time.getJulianDay(millis, time.gmtoff); + time.setTimezone(mTimeZones[zoneIndex]); + long millis = time.normalize(); + int julianDay = Time.getJulianDay(millis, time.getGmtOffset()); time.setJulianDay(julianDay); // some places change daylight saving time at 12am and so there is no 12am on some days // in some timezones - in those cases, the time is set to 1am. // some examples: Africa/Cairo, America/Sao_Paulo, Atlantic/Azores - assertTrue(time.hour == 0 || time.hour == 1); - assertEquals(0, time.minute); - assertEquals(0, time.second); + assertTrue(time.getHour() == 0 || time.getHour() == 1); + assertEquals(0, time.getMinute()); + assertEquals(0, time.getSecond()); - millis = time.toMillis(false); - int day = Time.getJulianDay(millis, time.gmtoff); + millis = time.toMillis(); + int day = Time.getJulianDay(millis, time.getGmtOffset()); assertEquals(day, julianDay); + time.clear(Time.TIMEZONE_UTC); } } } |