FileDocCategorySizeDatePackage
TimeTest.javaAPI DocAndroid 1.5 API21447Wed May 06 22:42:02 BST 2009com.android.unit_tests

TimeTest

public class TimeTest extends TestCase

Fields Summary
private DateTest[]
dayTests
private DateTest[]
minuteTests
private static final String[]
mTimeZones
Constructors Summary
Methods Summary
public voiddisableTestGetJulianDay()

    
    
         
        Time time = new Time();

        // For each day of the year, and for each timezone, get the Julian
        // day for 12am and then check that if we change the time we get the
        // same Julian day.
        for (int monthDay = 1; monthDay <= 366; monthDay++) {
            for (int zoneIndex = 0; zoneIndex < mTimeZones.length; zoneIndex++) {
                // We 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, 2008);
                time.timezone = mTimeZones[zoneIndex];
                long millis = time.normalize(true);
                if (zoneIndex == 0) {
                    Log.i("TimeTest", time.format("%B %d, %Y"));
                }
                
                // This is the Julian day for 12am for this day of the year
                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, 2008);
                        millis = time.normalize(true);
                        int day = Time.getJulianDay(millis, time.gmtoff);
                        if (day != julianDay) {
                            Log.e("TimeTest", "Julian day: " + day + " at time "
                                    + time.hour + ":" + time.minute
                                    + " != today's Julian day: " + julianDay
                                    + " timezone: " + time.timezone);
                        }
                        assertEquals(day, julianDay);
                    }
                }
            }
        }
    
public voiddisableTestSetJulianDay()

        Time time = new Time();
        
        // For each day of the year in 2008, and for each timezone,
        // test that we can set the Julian day correctly.
        for (int monthDay = 1; monthDay <= 366; monthDay++) {
            for (int zoneIndex = 0; zoneIndex < mTimeZones.length; zoneIndex++) {
                // We 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, 2008);
                time.timezone = mTimeZones[zoneIndex];
                long millis = time.normalize(true);
                if (zoneIndex == 0) {
                    Log.i("TimeTest", time.format("%B %d, %Y"));
                }
                int julianDay = Time.getJulianDay(millis, time.gmtoff);
                
                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.
                // Examples: Africa/Cairo on April 25, 2008
                //  America/Sao_Paulo on October 12, 2008
                //  Atlantic/Azores on March 30, 2008
                assertTrue(time.hour == 0 || time.hour == 1);
                assertEquals(0, time.minute);
                assertEquals(0, time.second);

                millis = time.toMillis(false);
                int day = Time.getJulianDay(millis, time.gmtoff);
                if (day != julianDay) {
                    Log.i("TimeTest", "Error: gmtoff " + (time.gmtoff / 3600.0)
                            + " day " + julianDay
                            + " millis " + millis
                            + " " + time.format("%B %d, %Y") + " " + time.timezone);
                }
                assertEquals(day, julianDay);
            }
        }
    
public voidtestClear0()

        Time t = new Time(Time.TIMEZONE_UTC);
        t.clear(Time.TIMEZONE_UTC);
    
public voidtestCompare0()

        Time a = new Time(Time.TIMEZONE_UTC);
        Time b = new Time("America/Los_Angeles");
        int r = Time.compare(a, b);
        // System.out.println("r=" + r);
    
public voidtestCtor0()

        Time t = new Time(Time.TIMEZONE_UTC);
        assertEquals(Time.TIMEZONE_UTC, t.timezone);
    
public voidtestFormat0()

        Time t = new Time(Time.TIMEZONE_UTC);
        String r = t.format("%Y%m%dT%H%M%S");
        // System.out.println("r='" + r + "'");
    
public voidtestGetActualMaximum0()

        Time t = new Time(Time.TIMEZONE_UTC);
        int r = t.getActualMaximum(Time.SECOND);
        // System.out.println("r=" + r);
    
public voidtestGetCurrentTimezone0()

        String r = Time.getCurrentTimezone();
        // System.out.println("r='" + r + "'");
    
public voidtestMillis0()

        Time t = new Time(Time.TIMEZONE_UTC);
        t.set(0, 0, 0, 1, 1, 2006);
        long r = t.toMillis(true /* ignore isDst */);
        // System.out.println("r=" + r);
        t.set(1, 0, 0, 1, 1, 2006);
        r = t.toMillis(true /* ignore isDst */);
        // System.out.println("r=" + r);
    
public voidtestMillis1()

        Time t = new Time(Time.TIMEZONE_UTC);
        t.set(1, 0, 0, 1, 0, 1970);
        long r = t.toMillis(true /* ignore isDst */);
        // System.out.println("r=" + r);
    
public voidtestNormalize0()

        Time t = new Time(Time.TIMEZONE_UTC);
        t.parse("20060432T010203");
        t.normalize(false /* use isDst */);
//        System.out.println("got: " + t.year + '-'
//                + t.month + '-' + t.monthDay
//                + ' ' + t.hour + ':' + t.minute
//                + ':' + t.second
//                + "( " + t.isDst + ',' + t.gmtoff
//                + ',' + t.weekDay
//                + ',' + t.yearDay + ')');
    
public voidtestNormalize1()


    
         
        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 /* use isDst */);
            local.monthDay += test.offset;
            local.normalize(true /* ignore isDst */);
            if (local.year != test.year2 || local.month != test.month2
                    || local.monthDay != test.day2 || local.hour != test.hour2
                    || local.minute != test.minute2) {
                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);
                throw new RuntimeException(
                        "day test index " + index + ", normalize(): expected local " + expectedTime
                                + " got: " + 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 /* use isDst */);
            local.monthDay += test.offset;
            long millis = local.toMillis(true /* ignore isDst */);
            local.set(millis);
            if (local.year != test.year2 || local.month != test.month2
                    || local.monthDay != test.day2 || local.hour != test.hour2
                    || local.minute != test.minute2) {
                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);
                throw new RuntimeException(
                        "day test index " + index + ", toMillis(): expected local " + expectedTime
                                + " got: " + actualTime);
            }
        }

        len = minuteTests.length;
        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 /* use isDst */);
            if (test.dst2 == -1) test.dst2 = local.isDst;
            local.minute += test.offset;
            local.normalize(false /* use isDst */);
            if (local.year != test.year2 || local.month != test.month2
                    || local.monthDay != test.day2 || local.hour != test.hour2
                    || local.minute != test.minute2 || local.isDst != test.dst2) {
                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);
                throw new RuntimeException(
                        "minute test index " + index + ", normalize(): expected local " + expectedTime
                                + " got: " + 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 /* use isDst */);
            if (test.dst2 == -1) test.dst2 = local.isDst;
            local.minute += test.offset;
            long millis = local.toMillis(false /* use isDst */);
            local.set(millis);
            if (local.year != test.year2 || local.month != test.month2
                    || local.monthDay != test.day2 || local.hour != test.hour2
                    || local.minute != test.minute2 || local.isDst != test.dst2) {
                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);
                throw new RuntimeException(
                        "minute test index " + index + ", toMillis(): expected local " + expectedTime
                                + " got: " + actualTime);
            }
        }
    
public voidtestParse0()

        Time t = new Time(Time.TIMEZONE_UTC);
        t.parse("12345678T901234");
        // System.out.println("t=" + t);
    
public voidtestSet0()

        Time t = new Time(Time.TIMEZONE_UTC);
        t.set(1000L);
        // System.out.println("t.year=" + t.year);
        // System.out.println("t=" + t);
        t.set(2000L);
        // System.out.println("t=" + t);
        t.set(1000L * 60);
        // System.out.println("t=" + t);
        t.set((1000L * 60 * 60 * 24) + 1000L);
        // System.out.println("t=" + t);
    
public voidtestSet1()

        Time t = new Time(Time.TIMEZONE_UTC);
        t.set(1, 2, 3, 4, 5, 6);
        // System.out.println("t=" + t);
    
public voidtestSetToNow0()

        Time t = new Time(Time.TIMEZONE_UTC);
        t.setToNow();
        // System.out.println("t=" + t);
    
public voidtestSwitchTimezone0()

        Time t = new Time(Time.TIMEZONE_UTC);
        t.parse("20061005T120000");
        t.switchTimezone("America/Los_Angeles");
        // System.out.println("got: " + t);
    
public voidtestToString0()

        Time t = new Time(Time.TIMEZONE_UTC);
        String r = t.toString();
        // System.out.println("r='" + r + "'");