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

The following examples show how to use android.icu.util.TimeZone#getID() . 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: TimeZoneRuleTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestRBTZTransition() {
    int[] STARTYEARS = {
        1950,
        1975,
        2000,
        2010
    };

    String[] zids = getTestZIDs();
    for (int i = 0; i < zids.length; i++) {
        TimeZone tz = TimeZone.getTimeZone(zids[i], TimeZone.TIMEZONE_ICU);
        if (tz == null) {
            break;
        }
        for (int j = 0; j < STARTYEARS.length; j++) {
            long startTime = getUTCMillis(STARTYEARS[j], Calendar.JANUARY, 1);
            TimeZoneRule[] rules = ((BasicTimeZone)tz).getTimeZoneRules(startTime);
            RuleBasedTimeZone rbtz = new RuleBasedTimeZone(tz.getID() + "(RBTZ)",
                    (InitialTimeZoneRule)rules[0]);
            for (int k = 1; k < rules.length; k++) {
                rbtz.addTransitionRule(rules[k]);
            }

            // Compare the original OlsonTimeZone with the RBTZ starting the startTime for 20 years
            long until = getUTCMillis(STARTYEARS[j] + 20, Calendar.JANUARY, 1);

            // Ascending
            compareTransitionsAscending(tz, rbtz, startTime, until, false);
            // Ascending/inclusive
            compareTransitionsAscending(tz, rbtz, startTime + 1, until, true);
            // Descending
            compareTransitionsDescending(tz, rbtz, startTime, until, false);
            // Descending/inclusive
            compareTransitionsDescending(tz, rbtz, startTime + 1, until, true);
        }
        
    }
}
 
Example 2
Source File: TimeZoneAdapter.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs an adapter for a android.icu.util.TimeZone object.
 */
public TimeZoneAdapter(TimeZone zone) {
    this.zone = zone;
    super.setID(zone.getID());
}
 
Example 3
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
ZoneDescriptor(TimeZone zone) {
    this.id = zone.getID();
    this.offset = zone.getRawOffset() / 60000;
    this.daylight = zone.useDaylightTime();
}
 
Example 4
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * As part of the VM fix (see CCC approved RFE 4028006, bug
 * 4044013), TimeZone.getTimeZone() has been modified to recognize
 * generic IDs of the form GMT[+-]hh:mm, GMT[+-]hhmm, and
 * GMT[+-]hh.  Test this behavior here.
 *
 * Bug 4044013
 */
@Test
public void TestCustomParse() {
    String[] DATA = {
        // ID               offset(sec)     output ID
        "GMT",              "0",            "GMT",      // system ID
        "GMT-YOUR.AD.HERE", "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT0",             "0",            "GMT0",     // system ID
        "GMT+0",            "0",            "GMT+0",    // system ID
        "GMT+1",            "3600",         "GMT+01:00",
        "GMT-0030",         "-1800",        "GMT-00:30",
        "GMT+15:99",        "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+",             "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-",             "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+0:",           "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-:",            "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+0010",         "600",          "GMT+00:10",
        "GMT-10",           "-36000",       "GMT-10:00",
        "GMT+30",           "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-3:30",         "-12600",       "GMT-03:30",
        "GMT-230",          "-9000",        "GMT-02:30",
        "GMT+05:13:05",     "18785",        "GMT+05:13:05",
        "GMT-71023",        "-25823",       "GMT-07:10:23",
        "GMT+01:23:45:67",  "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+01:234",       "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-2:31:123",     "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+3:75",         "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-01010101",     "0",            TimeZone.UNKNOWN_ZONE_ID,
    };
    for (int i = 0; i < DATA.length; i += 3) {
        String id = DATA[i];
        int offset = Integer.parseInt(DATA[i+1]);
        String expId = DATA[i+2];

        TimeZone zone = TimeZone.getTimeZone(id);
        String gotID = zone.getID();
        int gotOffset = zone.getRawOffset()/1000;

        logln(id + " -> " + gotID + " " + gotOffset);

        if (offset != gotOffset) {
            errln("FAIL: Unexpected offset for " + id + " - returned:" + gotOffset + " expected:" + offset);
        }
        if (!expId.equals(gotID)) {
            if (TimeZone.getDefaultTimeZoneType() != TimeZone.TIMEZONE_ICU) {
                logln("ID for " + id + " - returned:" + gotID + " expected:" + expId);
            } else {
                errln("FAIL: Unexpected ID for " + id + " - returned:" + gotID + " expected:" + expId);
            }
        }
    }
}
 
Example 5
Source File: TimeZoneRuleTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private void compareTransitionsAscending(TimeZone tz1, TimeZone tz2, long start, long end, boolean inclusive) {
    BasicTimeZone z1 = (BasicTimeZone)tz1;
    BasicTimeZone z2 = (BasicTimeZone)tz2;
    String zid1 = tz1.getID();
    String zid2 = tz2.getID();

    long time = start;
    while(true) {
        TimeZoneTransition tzt1 = z1.getNextTransition(time, inclusive);
        TimeZoneTransition tzt2 = z2.getNextTransition(time, inclusive);
        boolean inRange1 = false;
        boolean inRange2 = false;
        if (tzt1 != null) {
            if (tzt1.getTime() < end || (inclusive && tzt1.getTime() == end)) {
                inRange1 = true;
            }
        }
        if (tzt2 != null) {
            if (tzt2.getTime() < end || (inclusive && tzt2.getTime() == end)) {
                inRange2 = true;
            }
        }
        if (!inRange1 && !inRange2) {
            // No more transition in the range
            break;
        }
        if (!inRange1) {
            errln("FAIL: " + zid1 + " does not have any transitions after " + time + " before " + end);
            break;
        }
        if (!inRange2) {
            errln("FAIL: " + zid2 + " does not have any transitions after " + time + " before " + end);
            break;
        }
        if (tzt1.getTime() != tzt2.getTime()) {
            errln("FAIL: First transition after " + time + " "
                    + zid1 + "[" + tzt1.getTime() + "] "
                    + zid2 + "[" + tzt2.getTime() + "]");
            break;
        }
        time = tzt1.getTime();
        if (inclusive) {
            time++;
        }
    }
}
 
Example 6
Source File: TimeZoneRuleTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private void compareTransitionsDescending(TimeZone tz1, TimeZone tz2, long start, long end, boolean inclusive) {
    BasicTimeZone z1 = (BasicTimeZone)tz1;
    BasicTimeZone z2 = (BasicTimeZone)tz2;
    String zid1 = tz1.getID();
    String zid2 = tz2.getID();
    long time = end;
    while(true) {
        TimeZoneTransition tzt1 = z1.getPreviousTransition(time, inclusive);
        TimeZoneTransition tzt2 = z2.getPreviousTransition(time, inclusive);
        boolean inRange1 = false;
        boolean inRange2 = false;
        if (tzt1 != null) {
            if (tzt1.getTime() > start || (inclusive && tzt1.getTime() == start)) {
                inRange1 = true;
            }
        }
        if (tzt2 != null) {
            if (tzt2.getTime() > start || (inclusive && tzt2.getTime() == start)) {
                inRange2 = true;
            }
        }
        if (!inRange1 && !inRange2) {
            // No more transition in the range
            break;
        }
        if (!inRange1) {
            errln("FAIL: " + zid1 + " does not have any transitions before " + time + " after " + start);
            break;
        }
        if (!inRange2) {
            errln("FAIL: " + zid2 + " does not have any transitions before " + time + " after " + start);
            break;
        }
        if (tzt1.getTime() != tzt2.getTime()) {
            errln("FAIL: Last transition before " + time + " "
                    + zid1 + "[" + tzt1.getTime() + "] "
                    + zid2 + "[" + tzt2.getTime() + "]");
            break;
        }
        time = tzt1.getTime();
        if (inclusive) {
            time--;
        }
    }
}
 
Example 7
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
boolean checkCalendar314(GregorianCalendar testCal, TimeZone testTZ) {
    // GregorianCalendar testCal = (GregorianCalendar)aCal.clone();

    final int ONE_DAY = 24*60*60*1000;

    int tzOffset, tzRawOffset;
    Float tzOffsetFloat,tzRawOffsetFloat;
    // Here is where the user made an error.  They were passing in the value of
    // the MILLSECOND field; you need to pass in the millis in the day in STANDARD
    // time.
    int millis = testCal.get(Calendar.MILLISECOND) +
        1000 * (testCal.get(Calendar.SECOND) +
                60 * (testCal.get(Calendar.MINUTE) +
                      60 * (testCal.get(Calendar.HOUR_OF_DAY)))) -
        testCal.get(Calendar.DST_OFFSET);

    /* Fix up millis to be in range.  ASSUME THAT WE ARE NOT AT THE
     * BEGINNING OR END OF A MONTH.  We must add this code because
     * getOffset() has been changed to be more strict about the parameters
     * it receives -- it turns out that this test was passing in illegal
     * values. */
    int date = testCal.get(Calendar.DATE);
    int dow  = testCal.get(Calendar.DAY_OF_WEEK);
    while (millis < 0) {
        millis += ONE_DAY;
        --date;
        dow = Calendar.SUNDAY + ((dow - Calendar.SUNDAY + 6) % 7);
    }
    while (millis >= ONE_DAY) {
        millis -= ONE_DAY;
        ++date;
        dow = Calendar.SUNDAY + ((dow - Calendar.SUNDAY + 1) % 7);
    }

    tzOffset = testTZ.getOffset(testCal.get(Calendar.ERA),
                                testCal.get(Calendar.YEAR),
                                testCal.get(Calendar.MONTH),
                                date,
                                dow,
                                millis);
    tzRawOffset = testTZ.getRawOffset();
    tzOffsetFloat = new Float((float)tzOffset/(float)3600000);
    tzRawOffsetFloat = new Float((float)tzRawOffset/(float)3600000);

    Date testDate = testCal.getTime();

    boolean inDaylightTime = testTZ.inDaylightTime(testDate);
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
    sdf.setCalendar(testCal);
    String inDaylightTimeString;

    boolean passed;

    if (inDaylightTime)
    {
        inDaylightTimeString = " DST ";
        passed = (tzOffset == (tzRawOffset + 3600000));
    }
    else
    {
        inDaylightTimeString = "     ";
        passed = (tzOffset == tzRawOffset);
    }

    String output = testTZ.getID() + " " + sdf.format(testDate) +
        " Offset(" + tzOffsetFloat + ")" +
        " RawOffset(" + tzRawOffsetFloat + ")" +
        " " + millis/(float)3600000 + " " +
        inDaylightTimeString;

    if (passed)
        output += "     ";
    else
        output += "ERROR";

    if (passed) logln(output); else errln(output);
    return passed;
}
 
Example 8
Source File: GlobalizationPreferencesTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestTimeZone() {
    GlobalizationPreferences gp = new GlobalizationPreferences();

    // Set locale - zh_CN 
    logln("Set locale - zh_CN");
    gp.setLocale(new ULocale("zh_CN"));
    TimeZone tz = gp.getTimeZone();
    String tzid = tz.getID();
    if (!tzid.equals("Asia/Shanghai")) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: Asia/Shanghai");
    }

    // Set locale - en
    logln("Set locale - en");
    gp.setLocale(new ULocale("en"));
    tz = gp.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals("America/New_York")) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: America/New_York");
    }

    // Set territory - GB
    logln("Set territory - GB");
    gp.setTerritory("GB");
    tz = gp.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals("Europe/London")) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: Europe/London");
    }

    // Check if getTimeZone returns a safe clone
    tz.setID("Bad_ID");
    tz = gp.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals("Europe/London")) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: Europe/London");
    }

    // Set explicit time zone
    TimeZone jst = TimeZone.getTimeZone("Asia/Tokyo");
    String customJstId = "Japan_Standard_Time";
    jst.setID(customJstId);
    gp.setTimeZone(jst);
    tz = gp.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals(customJstId)) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: " + customJstId);
    }

    // Freeze
    logln("Freeze this object");
    TimeZone cst = TimeZone.getTimeZone("Europe/Paris");
    boolean bFrozen = false;
    gp.freeze();
    try {
        gp.setTimeZone(cst);
    } catch (UnsupportedOperationException uoe) {
        logln("setTimeZone is blocked");
        bFrozen = true;
    }
    if (!bFrozen) {
        errln("FAIL: setTimeZone must be blocked");
    }

    // Modifiable clone
    logln("cloneAsThawed");
    GlobalizationPreferences gp1 = (GlobalizationPreferences)gp.cloneAsThawed();
    tz = gp1.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals(customJstId)) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: " + customJstId);
    }

    // Set explicit time zone
    gp1.setTimeZone(cst);
    tz = gp1.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals(cst.getID())) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: " + cst.getID());
    }        
}