Search in sources :

Example 1 with TimeArrayTimeZoneRule

use of android.icu.util.TimeArrayTimeZoneRule in project j2objc by google.

the class TimeZoneRuleTest method TestTimeZoneRuleCoverage.

/*
     * API coverage tests for TimeZoneRule 
     */
@Test
public void TestTimeZoneRuleCoverage() {
    long time1 = getUTCMillis(2005, Calendar.JULY, 4);
    long time2 = getUTCMillis(2015, Calendar.JULY, 4);
    long time3 = getUTCMillis(1950, Calendar.JULY, 4);
    DateTimeRule dtr1 = new DateTimeRule(Calendar.FEBRUARY, 29, Calendar.SUNDAY, false, 3 * HOUR, // Last Sunday on or before Feb 29, at 3 AM, wall time
    DateTimeRule.WALL_TIME);
    DateTimeRule dtr2 = new DateTimeRule(Calendar.MARCH, 11, 2 * HOUR, // Mar 11, at 2 AM, standard time
    DateTimeRule.STANDARD_TIME);
    DateTimeRule dtr3 = new DateTimeRule(Calendar.OCTOBER, -1, Calendar.SATURDAY, 6 * HOUR, // Last Saturday in Oct, at 6 AM, UTC
    DateTimeRule.UTC_TIME);
    DateTimeRule dtr4 = new DateTimeRule(Calendar.MARCH, 8, Calendar.SUNDAY, true, 2 * HOUR, // First Sunday on or after Mar 8, at 2 AM, wall time
    DateTimeRule.WALL_TIME);
    AnnualTimeZoneRule a1 = new AnnualTimeZoneRule("a1", -3 * HOUR, 1 * HOUR, dtr1, 2000, AnnualTimeZoneRule.MAX_YEAR);
    AnnualTimeZoneRule a2 = new AnnualTimeZoneRule("a2", -3 * HOUR, 1 * HOUR, dtr1, 2000, AnnualTimeZoneRule.MAX_YEAR);
    AnnualTimeZoneRule a3 = new AnnualTimeZoneRule("a3", -3 * HOUR, 1 * HOUR, dtr1, 2000, 2010);
    InitialTimeZoneRule i1 = new InitialTimeZoneRule("i1", -3 * HOUR, 0);
    InitialTimeZoneRule i2 = new InitialTimeZoneRule("i2", -3 * HOUR, 0);
    InitialTimeZoneRule i3 = new InitialTimeZoneRule("i3", -3 * HOUR, 1 * HOUR);
    long[] emptytimes = {};
    long[] trtimes1 = { 0 };
    long[] trtimes2 = { 0, 10000000 };
    TimeArrayTimeZoneRule t0 = null;
    try {
        // Try to construct TimeArrayTimeZoneRule with null transition times
        t0 = new TimeArrayTimeZoneRule("nulltimes", -3 * HOUR, 0, null, DateTimeRule.UTC_TIME);
    } catch (IllegalArgumentException iae) {
        logln("TimeArrayTimeZoneRule constructor throws IllegalArgumentException as expected.");
        t0 = null;
    }
    if (t0 != null) {
        errln("FAIL: TimeArrayTimeZoneRule constructor did not throw IllegalArgumentException for null times");
    }
    try {
        // Try to construct TimeArrayTimeZoneRule with empty transition times
        t0 = new TimeArrayTimeZoneRule("nulltimes", -3 * HOUR, 0, emptytimes, DateTimeRule.UTC_TIME);
    } catch (IllegalArgumentException iae) {
        logln("TimeArrayTimeZoneRule constructor throws IllegalArgumentException as expected.");
        t0 = null;
    }
    if (t0 != null) {
        errln("FAIL: TimeArrayTimeZoneRule constructor did not throw IllegalArgumentException for empty times");
    }
    TimeArrayTimeZoneRule t1 = new TimeArrayTimeZoneRule("t1", -3 * HOUR, 0, trtimes1, DateTimeRule.UTC_TIME);
    TimeArrayTimeZoneRule t2 = new TimeArrayTimeZoneRule("t2", -3 * HOUR, 0, trtimes1, DateTimeRule.UTC_TIME);
    TimeArrayTimeZoneRule t3 = new TimeArrayTimeZoneRule("t3", -3 * HOUR, 0, trtimes2, DateTimeRule.UTC_TIME);
    TimeArrayTimeZoneRule t4 = new TimeArrayTimeZoneRule("t4", -3 * HOUR, 0, trtimes1, DateTimeRule.STANDARD_TIME);
    TimeArrayTimeZoneRule t5 = new TimeArrayTimeZoneRule("t5", -4 * HOUR, 1 * HOUR, trtimes1, DateTimeRule.WALL_TIME);
    // AnnualTimeZoneRule#getRule
    if (!a1.getRule().equals(a2.getRule())) {
        errln("FAIL: The same DateTimeRule must be returned from AnnualTimeZoneRule a1 and a2");
    }
    // AnnualTimeZoneRule#getStartYear
    int startYear = a1.getStartYear();
    if (startYear != 2000) {
        errln("FAIL: The start year of AnnualTimeZoneRule a1 must be 2000 - returned: " + startYear);
    }
    // AnnualTimeZoneRule#getEndYear
    int endYear = a1.getEndYear();
    if (endYear != AnnualTimeZoneRule.MAX_YEAR) {
        errln("FAIL: The start year of AnnualTimeZoneRule a1 must be MAX_YEAR - returned: " + endYear);
    }
    endYear = a3.getEndYear();
    if (endYear != 2010) {
        errln("FAIL: The start year of AnnualTimeZoneRule a3 must be 2010 - returned: " + endYear);
    }
    // AnnualTimeZone#getStartInYear
    Date d1 = a1.getStartInYear(2005, -3 * HOUR, 0);
    Date d2 = a3.getStartInYear(2005, -3 * HOUR, 0);
    if (d1 == null || d2 == null || !d1.equals(d2)) {
        errln("FAIL: AnnualTimeZoneRule#getStartInYear did not work as expected");
    }
    d2 = a3.getStartInYear(2015, -3 * HOUR, 0);
    if (d2 != null) {
        errln("FAIL: AnnualTimeZoneRule#getSTartInYear returned non-null date for 2015 which is out of rule range");
    }
    // AnnualTimeZone#getFirstStart
    d1 = a1.getFirstStart(-3 * HOUR, 0);
    d2 = a1.getFirstStart(-4 * HOUR, 1 * HOUR);
    if (d1 == null || d2 == null || !d1.equals(d2)) {
        errln("FAIL: The same start time should be returned by getFirstStart");
    }
    // AnnualTimeZone#getFinalStart
    d1 = a1.getFinalStart(-3 * HOUR, 0);
    if (d1 != null) {
        errln("FAIL: Non-null Date is returned by getFinalStart for a1");
    }
    d1 = a1.getStartInYear(2010, -3 * HOUR, 0);
    d2 = a3.getFinalStart(-3 * HOUR, 0);
    if (d1 == null || d2 == null || !d1.equals(d2)) {
        errln("FAIL: Bad date is returned by getFinalStart");
    }
    // AnnualTimeZone#getNextStart / getPreviousStart
    d1 = a1.getNextStart(time1, -3 * HOUR, 0, false);
    if (d1 == null) {
        errln("FAIL: Null Date is returned by getNextStart");
    } else {
        d2 = a1.getPreviousStart(d1.getTime(), -3 * HOUR, 0, true);
        if (d2 == null || !d1.equals(d2)) {
            errln("FAIL: Bad Date is returned by getPreviousStart");
        }
    }
    d1 = a3.getNextStart(time2, -3 * HOUR, 0, false);
    if (d1 != null) {
        errln("FAIL: getNextStart must return null when no start time is available after the base time");
    }
    d1 = a3.getFinalStart(-3 * HOUR, 0);
    d2 = a3.getPreviousStart(time2, -3 * HOUR, 0, false);
    if (d1 == null || d2 == null || !d1.equals(d2)) {
        errln("FAIL: getPreviousStart does not match with getFinalStart after the end year");
    }
    // AnnualTimeZone#isEquavalentTo
    if (!a1.isEquivalentTo(a2)) {
        errln("FAIL: AnnualTimeZoneRule a1 is equivalent to a2, but returned false");
    }
    if (a1.isEquivalentTo(a3)) {
        errln("FAIL: AnnualTimeZoneRule a1 is not equivalent to a3, but returned true");
    }
    if (!a1.isEquivalentTo(a1)) {
        errln("FAIL: AnnualTimeZoneRule a1 is equivalent to itself, but returned false");
    }
    if (a1.isEquivalentTo(t1)) {
        errln("FAIL: AnnualTimeZoneRule is not equivalent to TimeArrayTimeZoneRule, but returned true");
    }
    // AnnualTimeZone#isTransitionRule
    if (!a1.isTransitionRule()) {
        errln("FAIL: An AnnualTimeZoneRule is a transition rule, but returned false");
    }
    // AnnualTimeZone#toString
    String str = a1.toString();
    if (str == null || str.length() == 0) {
        errln("FAIL: AnnualTimeZoneRule#toString for a1 returns null or empty string");
    } else {
        logln("AnnualTimeZoneRule a1 : " + str);
    }
    str = a3.toString();
    if (str == null || str.length() == 0) {
        errln("FAIL: AnnualTimeZoneRule#toString for a3 returns null or empty string");
    } else {
        logln("AnnualTimeZoneRule a3 : " + str);
    }
    // InitialTimeZoneRule#isEquivalentRule
    if (!i1.isEquivalentTo(i2)) {
        errln("FAIL: InitialTimeZoneRule i1 is equivalent to i2, but returned false");
    }
    if (i1.isEquivalentTo(i3)) {
        errln("FAIL: InitialTimeZoneRule i1 is not equivalent to i3, but returned true");
    }
    if (i1.isEquivalentTo(a1)) {
        errln("FAIL: An InitialTimeZoneRule is not equivalent to an AnnualTimeZoneRule, but returned true");
    }
    // InitialTimeZoneRule#getFirstStart/getFinalStart/getNextStart/getPreviousStart
    d1 = i1.getFirstStart(0, 0);
    if (d1 != null) {
        errln("FAIL: Non-null Date is returned by InitialTimeZone#getFirstStart");
    }
    d1 = i1.getFinalStart(0, 0);
    if (d1 != null) {
        errln("FAIL: Non-null Date is returned by InitialTimeZone#getFinalStart");
    }
    d1 = i1.getNextStart(time1, 0, 0, false);
    if (d1 != null) {
        errln("FAIL: Non-null Date is returned by InitialTimeZone#getNextStart");
    }
    d1 = i1.getPreviousStart(time1, 0, 0, false);
    if (d1 != null) {
        errln("FAIL: Non-null Date is returned by InitialTimeZone#getPreviousStart");
    }
    // InitialTimeZoneRule#isTransitionRule
    if (i1.isTransitionRule()) {
        errln("FAIL: An InitialTimeZoneRule is not a transition rule, but returned true");
    }
    // InitialTimeZoneRule#toString
    str = i1.toString();
    if (str == null || str.length() == 0) {
        errln("FAIL: InitialTimeZoneRule#toString returns null or empty string");
    } else {
        logln("InitialTimeZoneRule i1 : " + str);
    }
    // TimeArrayTimeZoneRule#getStartTimes
    long[] times = t1.getStartTimes();
    if (times == null || times.length == 0 || times[0] != 0) {
        errln("FAIL: Bad start times are returned by TimeArrayTimeZoneRule#getStartTimes");
    }
    // TimeArrayTimeZoneRule#getTimeType
    if (t1.getTimeType() != DateTimeRule.UTC_TIME) {
        errln("FAIL: TimeArrayTimeZoneRule t1 uses UTC_TIME, but different type is returned");
    }
    if (t4.getTimeType() != DateTimeRule.STANDARD_TIME) {
        errln("FAIL: TimeArrayTimeZoneRule t4 uses STANDARD_TIME, but different type is returned");
    }
    if (t5.getTimeType() != DateTimeRule.WALL_TIME) {
        errln("FAIL: TimeArrayTimeZoneRule t5 uses WALL_TIME, but different type is returned");
    }
    // TimeArrayTimeZoneRule#getFirstStart/getFinalStart
    d1 = t1.getFirstStart(0, 0);
    if (d1 == null || d1.getTime() != trtimes1[0]) {
        errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t1");
    }
    d1 = t1.getFinalStart(0, 0);
    if (d1 == null || d1.getTime() != trtimes1[0]) {
        errln("FAIL: Bad final start time returned from TimeArrayTimeZoneRule t1");
    }
    d1 = t4.getFirstStart(-4 * HOUR, 1 * HOUR);
    if (d1 == null || (d1.getTime() != trtimes1[0] + 4 * HOUR)) {
        errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t4");
    }
    d1 = t5.getFirstStart(-4 * HOUR, 1 * HOUR);
    if (d1 == null || (d1.getTime() != trtimes1[0] + 3 * HOUR)) {
        errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t5");
    }
    // TimeArrayTimeZoneRule#getNextStart/getPreviousStart
    d1 = t3.getNextStart(time1, -3 * HOUR, 1 * HOUR, false);
    if (d1 != null) {
        errln("FAIL: Non-null Date is returned by getNextStart after the final transition for t3");
    }
    d1 = t3.getPreviousStart(time1, -3 * HOUR, 1 * HOUR, false);
    if (d1 == null || d1.getTime() != trtimes2[1]) {
        errln("FAIL: Bad start time returned by getPreviousStart for t3");
    } else {
        d2 = t3.getPreviousStart(d1.getTime(), -3 * HOUR, 1 * HOUR, false);
        if (d2 == null || d2.getTime() != trtimes2[0]) {
            errln("FAIL: Bad start time returned by getPreviousStart for t3");
        }
    }
    // time3 - year 1950, no result expected
    d1 = t3.getPreviousStart(time3, -3 * HOUR, 1 * HOUR, false);
    if (d1 != null) {
        errln("FAIL: Non-null Date is returned by getPrevoousStart for t3");
    }
    // TimeArrayTimeZoneRule#isEquivalentTo
    if (!t1.isEquivalentTo(t2)) {
        errln("FAIL: TimeArrayTimeZoneRule t1 is equivalent to t2, but returned false");
    }
    if (t1.isEquivalentTo(t3)) {
        errln("FAIL: TimeArrayTimeZoneRule t1 is not equivalent to t3, but returned true");
    }
    if (t1.isEquivalentTo(t4)) {
        errln("FAIL: TimeArrayTimeZoneRule t1 is not equivalent to t4, but returned true");
    }
    if (t1.isEquivalentTo(a1)) {
        errln("FAIL: TimeArrayTimeZoneRule is not equivalent to AnnualTimeZoneRule, but returned true");
    }
    // TimeArrayTimeZoneRule#isTransitionRule
    if (!t1.isTransitionRule()) {
        errln("FAIL: A TimeArrayTimeZoneRule is a transition rule, but returned false");
    }
    // TimeArrayTimeZoneRule#toString
    str = t3.toString();
    if (str == null || str.length() == 0) {
        errln("FAIL: TimeArrayTimeZoneRule#toString returns null or empty string");
    } else {
        logln("TimeArrayTimeZoneRule t3 : " + str);
    }
    // DateTimeRule#toString
    str = dtr1.toString();
    if (str == null || str.length() == 0) {
        errln("FAIL: DateTimeRule#toString for dtr1 returns null or empty string");
    } else {
        logln("DateTimeRule dtr1 : " + str);
    }
    str = dtr2.toString();
    if (str == null || str.length() == 0) {
        errln("FAIL: DateTimeRule#toString for dtr2 returns null or empty string");
    } else {
        logln("DateTimeRule dtr1 : " + str);
    }
    str = dtr3.toString();
    if (str == null || str.length() == 0) {
        errln("FAIL: DateTimeRule#toString for dtr3 returns null or empty string");
    } else {
        logln("DateTimeRule dtr1 : " + str);
    }
    str = dtr4.toString();
    if (str == null || str.length() == 0) {
        errln("FAIL: DateTimeRule#toString for dtr4 returns null or empty string");
    } else {
        logln("DateTimeRule dtr1 : " + str);
    }
}
Also used : DateTimeRule(android.icu.util.DateTimeRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) Date(java.util.Date) Test(org.junit.Test)

Example 2 with TimeArrayTimeZoneRule

use of android.icu.util.TimeArrayTimeZoneRule in project j2objc by google.

the class OlsonTimeZone method initTransitionRules.

private synchronized void initTransitionRules() {
    if (transitionRulesInitialized) {
        return;
    }
    initialRule = null;
    firstTZTransition = null;
    firstFinalTZTransition = null;
    historicRules = null;
    firstTZTransitionIdx = 0;
    finalZoneWithStartYear = null;
    String stdName = getID() + "(STD)";
    String dstName = getID() + "(DST)";
    int raw, dst;
    // Create initial rule
    raw = initialRawOffset() * Grego.MILLIS_PER_SECOND;
    dst = initialDstOffset() * Grego.MILLIS_PER_SECOND;
    initialRule = new InitialTimeZoneRule((dst == 0 ? stdName : dstName), raw, dst);
    if (transitionCount > 0) {
        int transitionIdx, typeIdx;
        // For now, keeping this code for just in case. Feb 19, 2010 Yoshito
        for (transitionIdx = 0; transitionIdx < transitionCount; transitionIdx++) {
            if (getInt(typeMapData[transitionIdx]) != 0) {
                // type 0 is the initial type
                break;
            }
            firstTZTransitionIdx++;
        }
        if (transitionIdx == transitionCount) {
        // Actually no transitions...
        } else {
            // Build historic rule array
            long[] times = new long[transitionCount];
            for (typeIdx = 0; typeIdx < typeCount; typeIdx++) {
                // Gather all start times for each pair of offsets
                int nTimes = 0;
                for (transitionIdx = firstTZTransitionIdx; transitionIdx < transitionCount; transitionIdx++) {
                    if (typeIdx == getInt(typeMapData[transitionIdx])) {
                        long tt = transitionTimes64[transitionIdx] * Grego.MILLIS_PER_SECOND;
                        if (tt < finalStartMillis) {
                            // Exclude transitions after finalMillis
                            times[nTimes++] = tt;
                        }
                    }
                }
                if (nTimes > 0) {
                    long[] startTimes = new long[nTimes];
                    System.arraycopy(times, 0, startTimes, 0, nTimes);
                    // Create a TimeArrayTimeZoneRule
                    raw = typeOffsets[typeIdx * 2] * Grego.MILLIS_PER_SECOND;
                    dst = typeOffsets[typeIdx * 2 + 1] * Grego.MILLIS_PER_SECOND;
                    if (historicRules == null) {
                        historicRules = new TimeArrayTimeZoneRule[typeCount];
                    }
                    historicRules[typeIdx] = new TimeArrayTimeZoneRule((dst == 0 ? stdName : dstName), raw, dst, startTimes, DateTimeRule.UTC_TIME);
                }
            }
            // Create initial transition
            typeIdx = getInt(typeMapData[firstTZTransitionIdx]);
            firstTZTransition = new TimeZoneTransition(transitionTimes64[firstTZTransitionIdx] * Grego.MILLIS_PER_SECOND, initialRule, historicRules[typeIdx]);
        }
    }
    if (finalZone != null) {
        // Get the first occurrence of final rule starts
        long startTime = (long) finalStartMillis;
        TimeZoneRule firstFinalRule;
        if (finalZone.useDaylightTime()) {
            /*
                 * Note: When an OlsonTimeZone is constructed, we should set the final year
                 * as the start year of finalZone.  However, the boundary condition used for
                 * getting offset from finalZone has some problems.  So setting the start year
                 * in the finalZone will cause a problem.  For now, we do not set the valid
                 * start year when the construction time and create a clone and set the
                 * start year when extracting rules.
                 */
            finalZoneWithStartYear = (SimpleTimeZone) finalZone.clone();
            finalZoneWithStartYear.setStartYear(finalStartYear);
            TimeZoneTransition tzt = finalZoneWithStartYear.getNextTransition(startTime, false);
            firstFinalRule = tzt.getTo();
            startTime = tzt.getTime();
        } else {
            finalZoneWithStartYear = finalZone;
            firstFinalRule = new TimeArrayTimeZoneRule(finalZone.getID(), finalZone.getRawOffset(), 0, new long[] { startTime }, DateTimeRule.UTC_TIME);
        }
        TimeZoneRule prevRule = null;
        if (transitionCount > 0) {
            prevRule = historicRules[getInt(typeMapData[transitionCount - 1])];
        }
        if (prevRule == null) {
            // No historic transitions, but only finalZone available
            prevRule = initialRule;
        }
        firstFinalTZTransition = new TimeZoneTransition(startTime, prevRule, firstFinalRule);
    }
    transitionRulesInitialized = true;
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) TimeZoneRule(android.icu.util.TimeZoneRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) TimeZoneTransition(android.icu.util.TimeZoneTransition) InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule)

Example 3 with TimeArrayTimeZoneRule

use of android.icu.util.TimeArrayTimeZoneRule in project j2objc by google.

the class OlsonTimeZone method getTimeZoneRules.

/* (non-Javadoc)
     * @see android.icu.util.BasicTimeZone#getTimeZoneRules()
     */
@Override
public TimeZoneRule[] getTimeZoneRules() {
    initTransitionRules();
    int size = 1;
    if (historicRules != null) {
        // includes non transition data.
        for (int i = 0; i < historicRules.length; i++) {
            if (historicRules[i] != null) {
                size++;
            }
        }
    }
    if (finalZone != null) {
        if (finalZone.useDaylightTime()) {
            size += 2;
        } else {
            size++;
        }
    }
    TimeZoneRule[] rules = new TimeZoneRule[size];
    int idx = 0;
    rules[idx++] = initialRule;
    if (historicRules != null) {
        for (int i = 0; i < historicRules.length; i++) {
            if (historicRules[i] != null) {
                rules[idx++] = historicRules[i];
            }
        }
    }
    if (finalZone != null) {
        if (finalZone.useDaylightTime()) {
            TimeZoneRule[] stzr = finalZoneWithStartYear.getTimeZoneRules();
            // Adding only transition rules
            rules[idx++] = stzr[1];
            rules[idx++] = stzr[2];
        } else {
            // Create a TimeArrayTimeZoneRule at finalMillis
            rules[idx++] = new TimeArrayTimeZoneRule(getID() + "(STD)", finalZone.getRawOffset(), 0, new long[] { (long) finalStartMillis }, DateTimeRule.UTC_TIME);
        }
    }
    return rules;
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) TimeZoneRule(android.icu.util.TimeZoneRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule)

Example 4 with TimeArrayTimeZoneRule

use of android.icu.util.TimeArrayTimeZoneRule in project j2objc by google.

the class TimeZoneTest method checkFrozen.

private void checkFrozen(TimeZone[] frozenZones, String zaName) {
    for (int i = 0; i < frozenZones.length; i++) {
        if (!frozenZones[i].isFrozen()) {
            errln("Fail: " + zaName + "[" + i + "] is not frozen.");
        }
        // clone
        TimeZone copy = (TimeZone) frozenZones[i].clone();
        if (frozenZones[i] != copy) {
            errln("Fail: " + zaName + "[" + i + "] - clone does not return the object itself.");
        }
        // cloneAsThawed
        TimeZone thawed = (TimeZone) frozenZones[i].cloneAsThawed();
        if (thawed.isFrozen() || !frozenZones[i].equals(thawed)) {
            errln("Fail: " + zaName + "[" + i + "] - cloneAsThawed does not work.");
        }
        // setID
        try {
            String newID = "foo";
            frozenZones[i].setID(newID);
            errln("Fail: " + zaName + "[" + i + "] - setID must throw UnsupportedOperationException.");
        } catch (UnsupportedOperationException e) {
        // OK
        }
        // setRawOffset
        if (!(frozenZones[i] instanceof RuleBasedTimeZone)) {
            // RuleBasedTimeZone does not supprot setRawOffset
            try {
                int newOffset = -3600000;
                frozenZones[i].setRawOffset(newOffset);
                errln("Fail: " + zaName + "[" + i + "] - setRawOffset must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        }
        if (frozenZones[i] instanceof SimpleTimeZone) {
            SimpleTimeZone stz = (SimpleTimeZone) frozenZones[i];
            // setDSTSavings
            try {
                int newDSTSavings = 1800000;
                stz.setDSTSavings(newDSTSavings);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setDSTSavings must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setStartRule
            try {
                stz.setStartRule(Calendar.JANUARY, -1, Calendar.SUNDAY, 0);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartRule must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setEndRule
            try {
                stz.setEndRule(Calendar.DECEMBER, 1, Calendar.SUNDAY, 0);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setEndRule must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setStartYear
            try {
                stz.setStartYear(2000);
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartYear must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        } else if (frozenZones[i] instanceof RuleBasedTimeZone) {
            RuleBasedTimeZone rbtz = (RuleBasedTimeZone) frozenZones[i];
            // addTransitionRule
            try {
                TimeArrayTimeZoneRule tr1 = new TimeArrayTimeZoneRule("tr1", 7200000, 0, new long[] { 0 }, DateTimeRule.UTC_TIME);
                rbtz.addTransitionRule(tr1);
                errln("Fail: (RuleBasedTimeZone)" + zaName + "[" + i + "] - addTransitionRule must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        } else if (frozenZones[i] instanceof VTimeZone) {
            VTimeZone vtz = (VTimeZone) frozenZones[i];
            // setTZURL
            try {
                String tzUrl = "http://icu-project.org/timezone";
                vtz.setTZURL(tzUrl);
                errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setTZURL must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
            // setLastModified
            try {
                Date d = new Date();
                vtz.setLastModified(d);
                errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setLastModified must throw UnsupportedOperationException.");
            } catch (UnsupportedOperationException e) {
            // OK
            }
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) NativeTimeZone(com.google.j2objc.util.NativeTimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) JavaTimeZone(android.icu.impl.JavaTimeZone) VTimeZone(android.icu.util.VTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) VTimeZone(android.icu.util.VTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Date(java.util.Date)

Example 5 with TimeArrayTimeZoneRule

use of android.icu.util.TimeArrayTimeZoneRule in project j2objc by google.

the class TimeZoneTest method checkThawed.

private void checkThawed(TimeZone[] thawedZones, String zaName) {
    for (int i = 0; i < thawedZones.length; i++) {
        if (thawedZones[i].isFrozen()) {
            errln("Fail: " + zaName + "[" + i + "] is frozen.");
        }
        // clone
        TimeZone copy = (TimeZone) thawedZones[i].clone();
        if (thawedZones[i] == copy || !thawedZones[i].equals(copy)) {
            errln("Fail: " + zaName + "[" + i + "] - clone does not work.");
        }
        // cloneAsThawed
        TimeZone thawed = (TimeZone) thawedZones[i].cloneAsThawed();
        if (thawed.isFrozen() || !thawedZones[i].equals(thawed)) {
            errln("Fail: " + zaName + "[" + i + "] - cloneAsThawed does not work.");
        }
        // setID
        try {
            String newID = "foo";
            thawedZones[i].setID(newID);
            if (!thawedZones[i].getID().equals(newID)) {
                errln("Fail: " + zaName + "[" + i + "] - setID(\"" + newID + "\") does not work.");
            }
        } catch (UnsupportedOperationException e) {
            errln("Fail: " + zaName + "[" + i + "] - setID throws UnsupportedOperationException.");
        }
        // setRawOffset
        // J2ObjC: RuleBasedTimeZone and NativeTimeZone do not support setRawOffset.
        boolean isNativeTimeZone = Optional.of(thawedZones[i]).filter(JavaTimeZone.class::isInstance).map(JavaTimeZone.class::cast).map(JavaTimeZone::unwrap).filter(NativeTimeZone.class::isInstance).isPresent();
        if (!(thawedZones[i] instanceof RuleBasedTimeZone) && !isNativeTimeZone) {
            try {
                int newOffset = -3600000;
                thawedZones[i].setRawOffset(newOffset);
                if (thawedZones[i].getRawOffset() != newOffset) {
                    errln("Fail: " + zaName + "[" + i + "] - setRawOffset(" + newOffset + ") does not work.");
                }
            } catch (UnsupportedOperationException e) {
                errln("Fail: " + zaName + "[" + i + "] - setRawOffset throws UnsupportedOperationException.");
            }
        }
        if (thawedZones[i] instanceof SimpleTimeZone) {
            SimpleTimeZone stz = (SimpleTimeZone) thawedZones[i];
            // setDSTSavings
            try {
                int newDSTSavings = 1800000;
                stz.setDSTSavings(newDSTSavings);
                if (stz.getDSTSavings() != newDSTSavings) {
                    errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setDSTSavings(" + newDSTSavings + ") does not work.");
                }
            } catch (UnsupportedOperationException e) {
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setDSTSavings throws UnsupportedOperationException.");
            }
            // setStartRule
            try {
                stz.setStartRule(Calendar.JANUARY, -1, Calendar.SUNDAY, 0);
            } catch (UnsupportedOperationException e) {
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartRule throws UnsupportedOperationException.");
            }
            // setEndRule
            try {
                stz.setEndRule(Calendar.DECEMBER, 1, Calendar.SUNDAY, 0);
            } catch (UnsupportedOperationException e) {
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setEndRule throws UnsupportedOperationException.");
            }
            // setStartYear
            try {
                stz.setStartYear(2000);
            } catch (UnsupportedOperationException e) {
                errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartYear throws UnsupportedOperationException.");
            }
        } else if (thawedZones[i] instanceof RuleBasedTimeZone) {
            RuleBasedTimeZone rbtz = (RuleBasedTimeZone) thawedZones[i];
            // addTransitionRule
            try {
                TimeArrayTimeZoneRule tr1 = new TimeArrayTimeZoneRule("tr1", 7200000, 0, new long[] { 0 }, DateTimeRule.UTC_TIME);
                rbtz.addTransitionRule(tr1);
            } catch (UnsupportedOperationException e) {
                errln("Fail: (RuleBasedTimeZone)" + zaName + "[" + i + "] - addTransitionRule throws UnsupportedOperationException.");
            }
        } else if (thawedZones[i] instanceof VTimeZone) {
            VTimeZone vtz = (VTimeZone) thawedZones[i];
            // setTZURL
            try {
                String tzUrl = "http://icu-project.org/timezone";
                vtz.setTZURL(tzUrl);
                if (!vtz.getTZURL().equals(tzUrl)) {
                    errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setTZURL does not work.");
                }
            } catch (UnsupportedOperationException e) {
                errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setTZURL throws UnsupportedOperationException.");
            }
            // setLastModified
            try {
                Date d = new Date();
                vtz.setLastModified(d);
                if (!vtz.getLastModified().equals(d)) {
                    errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setLastModified does not work.");
                }
            } catch (UnsupportedOperationException e) {
                errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setLastModified throws UnsupportedOperationException.");
            }
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) NativeTimeZone(com.google.j2objc.util.NativeTimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) JavaTimeZone(android.icu.impl.JavaTimeZone) VTimeZone(android.icu.util.VTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) VTimeZone(android.icu.util.VTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Date(java.util.Date) JavaTimeZone(android.icu.impl.JavaTimeZone)

Aggregations

TimeArrayTimeZoneRule (android.icu.util.TimeArrayTimeZoneRule)5 AnnualTimeZoneRule (android.icu.util.AnnualTimeZoneRule)3 InitialTimeZoneRule (android.icu.util.InitialTimeZoneRule)3 Date (java.util.Date)3 JavaTimeZone (android.icu.impl.JavaTimeZone)2 BasicTimeZone (android.icu.util.BasicTimeZone)2 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)2 SimpleTimeZone (android.icu.util.SimpleTimeZone)2 TimeZone (android.icu.util.TimeZone)2 TimeZoneRule (android.icu.util.TimeZoneRule)2 VTimeZone (android.icu.util.VTimeZone)2 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)2 DateTimeRule (android.icu.util.DateTimeRule)1 TimeZoneTransition (android.icu.util.TimeZoneTransition)1 Test (org.junit.Test)1