Java Code Examples for android.icu.util.TimeZone#getFrozenTimeZone()

The following examples show how to use android.icu.util.TimeZone#getFrozenTimeZone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestFreezable() {
    // Test zones - initially thawed
    TimeZone[] ZA1 = {
        TimeZone.getDefault(),
        TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU),
        TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_JDK),
        new SimpleTimeZone(0, "stz"),
        new RuleBasedTimeZone("rbtz", new InitialTimeZoneRule("rbtz0", 0, 0)),
        VTimeZone.create("America/New_York"),
    };

    checkThawed(ZA1, "ZA1");
    // freeze
    for (int i = 0; i < ZA1.length; i++) {
        ZA1[i].freeze();
    }
    checkFrozen(ZA1, "ZA1(frozen)");

    // Test zones - initially frozen
    final TimeZone[] ZA2 = {
        TimeZone.GMT_ZONE,
        TimeZone.UNKNOWN_ZONE,
        TimeZone.getFrozenTimeZone("America/Los_Angeles"),
        new SimpleTimeZone(3600000, "frz_stz").freeze(),
        new RuleBasedTimeZone("frz_rbtz", new InitialTimeZoneRule("frz_rbtz0", 3600000, 0)).freeze(),
        VTimeZone.create("Asia/Tokyo").freeze(),
    };

    checkFrozen(ZA2, "ZA2");
    TimeZone[] ZA2_thawed = new TimeZone[ZA2.length];
    // create thawed clone
    for (int i = 0; i < ZA2_thawed.length; i++) {
        ZA2_thawed[i] = ZA2[i].cloneAsThawed();
    }
    checkThawed(ZA2_thawed, "ZA2(thawed)");

}
 
Example 2
Source File: DateIntervalFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestGetSetTimeZone(){
    DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("MMMdHHmm", Locale.ENGLISH);
    long date1 = 1299090600000L; // 2011-Mar-02 1030 in US/Pacific, 2011-Mar-03 0330 in Asia/Tokyo
    long date2 = 1299115800000L; // 2011-Mar-02 1730 in US/Pacific, 2011-Mar-03 1030 in Asia/Tokyo
    DateInterval dtitv = new DateInterval(date1, date2);
    TimeZone tzCalif = TimeZone.getFrozenTimeZone("US/Pacific");
    TimeZone tzTokyo = TimeZone.getFrozenTimeZone("Asia/Tokyo");
    String fmtCalif = "Mar 2, 10:30 \u2013 17:30"; // ICU4C result is "Mar 2, 10:30 \u2013 17:30" (does not duplicate day)
    String fmtTokyo = "Mar 3, 03:30 \u2013 10:30"; // ICU4C result is "Mar 3, 03:30 \u2013 10:30" (does not duplicate day)

    StringBuffer buf = new StringBuffer();
    FieldPosition pos = new FieldPosition(0);
    dtitvfmt.setTimeZone(tzCalif);
    dtitvfmt.format(dtitv, buf, pos);
    if (!buf.toString().equals(fmtCalif)) {
        errln("DateIntervalFormat for tzCalif, expect \"" + fmtCalif + "\", get \"" + buf + "\"");
    }

    buf.setLength(0);
    pos.setBeginIndex(0);
    dtitvfmt.setTimeZone(tzTokyo);
    dtitvfmt.format(dtitv, buf, pos);
    if (!buf.toString().equals(fmtTokyo)) {
        errln("DateIntervalFormat for tzTokyo, expect \"" + fmtTokyo + "\", get \"" + buf + "\"");
    }

    if (!dtitvfmt.getTimeZone().equals(tzTokyo)) {
        errln("DateIntervalFormat.getTimeZone() returns mismatch");
    }
}
 
Example 3
Source File: IcuZoneRulesProvider.java    From j2objc with Apache License 2.0 4 votes vote down vote up
static ZoneRules generateZoneRules(String zoneId) {
    TimeZone timeZone = TimeZone.getFrozenTimeZone(zoneId);
    // Assumption #0
    verify(timeZone instanceof OlsonTimeZone, zoneId,
            "Unexpected time zone class " + timeZone.getClass());
    OlsonTimeZone tz = (OlsonTimeZone) timeZone;
    TimeZoneRule[] rules = tz.getTimeZoneRules();
    // Assumption #1
    InitialTimeZoneRule initial = (InitialTimeZoneRule) rules[0];

    ZoneOffset baseStandardOffset = millisToOffset(initial.getRawOffset());
    ZoneOffset baseWallOffset =
            millisToOffset((initial.getRawOffset() + initial.getDSTSavings()));

    List<ZoneOffsetTransition> standardOffsetTransitionList = new ArrayList<>();
    List<ZoneOffsetTransition> transitionList = new ArrayList<>();
    List<ZoneOffsetTransitionRule> lastRules = new ArrayList<>();

    int preLastDstSavings = 0;
    AnnualTimeZoneRule last1 = null;
    AnnualTimeZoneRule last2 = null;

    TimeZoneTransition transition = tz.getNextTransition(Long.MIN_VALUE, false);
    int transitionCount = 1;
    // This loop has two possible exit conditions (in normal operation):
    // 1. for zones that end with a static value and have no ongoing DST changes, it will exit
    //    via the normal condition (transition != null)
    // 2. for zones with ongoing DST changes (represented by a "final zone" in ICU4J, and by
    //    "last rules" in java.time) the "break transitionLoop" will be used to exit the loop.
    transitionLoop:
    while (transition != null) {
        TimeZoneRule from = transition.getFrom();
        TimeZoneRule to = transition.getTo();
        boolean hadEffect = false;
        if (from.getRawOffset() != to.getRawOffset()) {
            standardOffsetTransitionList.add(new ZoneOffsetTransition(
                    TimeUnit.MILLISECONDS.toSeconds(transition.getTime()),
                    millisToOffset(from.getRawOffset()),
                    millisToOffset(to.getRawOffset())));
            hadEffect = true;
        }
        int fromTotalOffset = from.getRawOffset() + from.getDSTSavings();
        int toTotalOffset = to.getRawOffset() + to.getDSTSavings();
        if (fromTotalOffset != toTotalOffset) {
            transitionList.add(new ZoneOffsetTransition(
                    TimeUnit.MILLISECONDS.toSeconds(transition.getTime()),
                    millisToOffset(fromTotalOffset),
                    millisToOffset(toTotalOffset)));
            hadEffect = true;
        }
        // Assumption #5
        verify(hadEffect, zoneId, "Transition changed neither total nor raw offset.");
        if (to instanceof AnnualTimeZoneRule) {
            // The presence of an AnnualTimeZoneRule is taken as an indication of a final rule.
            if (last1 == null) {
                preLastDstSavings = from.getDSTSavings();
                last1 = (AnnualTimeZoneRule) to;
                // Assumption #4
                verify(last1.getEndYear() == AnnualTimeZoneRule.MAX_YEAR, zoneId,
                        "AnnualTimeZoneRule is not permanent.");
            } else {
                last2 = (AnnualTimeZoneRule) to;
                // Assumption #4
                verify(last2.getEndYear() == AnnualTimeZoneRule.MAX_YEAR, zoneId,
                        "AnnualTimeZoneRule is not permanent.");

                // Assumption #3
                transition = tz.getNextTransition(transition.getTime(), false);
                verify(transition.getTo() == last1, zoneId,
                        "Unexpected rule after 2 AnnualTimeZoneRules.");
                break transitionLoop;
            }
        } else {
            // Assumption #2
            verify(last1 == null, zoneId, "Unexpected rule after AnnualTimeZoneRule.");
        }
        verify(transitionCount <= MAX_TRANSITIONS, zoneId,
                "More than " + MAX_TRANSITIONS + " transitions.");
        transition = tz.getNextTransition(transition.getTime(), false);
        transitionCount++;
    }
    if (last1 != null) {
        // Assumption #3
        verify(last2 != null, zoneId, "Only one AnnualTimeZoneRule.");
        lastRules.add(toZoneOffsetTransitionRule(last1, preLastDstSavings));
        lastRules.add(toZoneOffsetTransitionRule(last2, last1.getDSTSavings()));
    }

    return ZoneRules.of(baseStandardOffset, baseWallOffset, standardOffsetTransitionList,
            transitionList, lastRules);
}