Package com.ibm.icu.util

Examples of com.ibm.icu.util.TimeZone


     *
     * @param args the command line arguments.
     */
    public static void main(String[] args)
    {
        TimeZone utc = new SimpleTimeZone(0, "UTC");
        Calendar cal = Calendar.getInstance(utc, Locale.ENGLISH);
        MessageFormat fmt = new MessageFormat("{1} = {0, date, full} {0, time, full}");
        Object arguments[] = {cal, null};
       
        arguments[0] = cal;
View Full Code Here


    /*
     * Test case for checking if a TimeZone is properly set in the result calendar
     * and if the result TimeZone has the expected behavior.
     */
    public void TestTimeZoneRoundTrip() {
        TimeZone unknownZone = new SimpleTimeZone(-31415, "Etc/Unknown");
        int badDstOffset = -1234;
        int badZoneOffset = -2345;

        int[][] testDateData = {
            {2007, 1, 15},
            {2007, 6, 15},
            {1990, 1, 15},
            {1990, 6, 15},
            {1960, 1, 15},
            {1960, 6, 15},
        };

        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        cal.clear();

        // Set up rule equivalency test range
        long low, high;
        cal.set(1900, 0, 1);
        low = cal.getTimeInMillis();
        cal.set(2040, 0, 1);
        high = cal.getTimeInMillis();

        // Set up test dates
        Date[] DATES = new Date[testDateData.length];
        cal.clear();
        for (int i = 0; i < DATES.length; i++) {
            cal.set(testDateData[i][0], testDateData[i][1], testDateData[i][2]);
            DATES[i] = cal.getTime();
        }

        // Set up test locales
        ULocale[] LOCALES = null;
        if (getInclusion() > 5) {
            LOCALES = ULocale.getAvailableLocales();
        } else {
            LOCALES = new ULocale[] {new ULocale("en"), new ULocale("en_CA"), new ULocale("fr"), new ULocale("zh_Hant")};
        }

        String[] tzids = TimeZone.getAvailableIDs();
        int[] inOffsets = new int[2];
        int[] outOffsets = new int[2];

        // Run the roundtrip test
        for (int locidx = 0; locidx < LOCALES.length; locidx++) {
            for (int patidx = 0; patidx < PATTERNS.length; patidx++) {
                SimpleDateFormat sdf = new SimpleDateFormat(PATTERNS[patidx], LOCALES[locidx]);

                for (int tzidx = 0; tzidx < tzids.length; tzidx++) {
                    TimeZone tz = TimeZone.getTimeZone(tzids[tzidx]);

                    for (int datidx = 0; datidx < DATES.length; datidx++) {
                        // Format
                        sdf.setTimeZone(tz);
                        String tzstr = sdf.format(DATES[datidx]);

                        // Before parse, set unknown zone to SimpleDateFormat instance
                        // just for making sure that it does not depends on the time zone
                        // originally set.
                        sdf.setTimeZone(unknownZone);

                        // Parse
                        ParsePosition pos = new ParsePosition(0);
                        Calendar outcal = Calendar.getInstance(unknownZone);
                        outcal.set(Calendar.DST_OFFSET, badDstOffset);
                        outcal.set(Calendar.ZONE_OFFSET, badZoneOffset);

                        sdf.parse(tzstr, outcal, pos);

                        // Check the result
                        TimeZone outtz = outcal.getTimeZone();

                        tz.getOffset(DATES[datidx].getTime(), false, inOffsets);
                        outtz.getOffset(DATES[datidx].getTime(), false, outOffsets);

                        if (PATTERNS[patidx].equals("VVVV")) {
                            // Location: time zone rule must be preserved except
                            // zones not actually associated with a specific location.
                            // Time zones in this category do not have "/" in its ID.
                            String canonicalID = TimeZone.getCanonicalID(tzids[tzidx]);
                            if (canonicalID != null && !outtz.getID().equals(canonicalID)) {
                                // Canonical ID did not match - check the rules
                                boolean bFailure = false;
                                if ((tz instanceof BasicTimeZone) && (outtz instanceof BasicTimeZone)) {
                                    bFailure = !(canonicalID.indexOf('/') == -1)
                                                && !((BasicTimeZone)outtz).hasEquivalentTransitions(tz, low, high);
                                }
                                if (bFailure) {
                                    errln("Canonical round trip failed; tz=" + tzids[tzidx]
                                            + ", locale=" + LOCALES[locidx] + ", pattern=" + PATTERNS[patidx]
                                            + ", time=" + DATES[datidx].getTime() + ", str=" + tzstr
                                            + ", outtz=" + outtz.getID());
                                } else {
                                    logln("Canonical round trip failed (as expected); tz=" + tzids[tzidx]
                                            + ", locale=" + LOCALES[locidx] + ", pattern=" + PATTERNS[patidx]
                                            + ", time=" + DATES[datidx].getTime() + ", str=" + tzstr
                                            + ", outtz=" + outtz.getID());
                                }
                            }
                        } else {
                            // Check if localized GMT format or RFC format is used.
                            int numDigits = 0;
View Full Code Here

                    if (id == null || !id.equals(ids[zidx])) {
                        // Skip aliases
                        continue;
                    }
                    BasicTimeZone btz = (BasicTimeZone)TimeZone.getTimeZone(ids[zidx], TimeZone.TIMEZONE_ICU);
                    TimeZone tz = TimeZone.getTimeZone(ids[zidx]);
                    sdf.setTimeZone(tz);

                    long t = START_TIME;
                    TimeZoneTransition tzt = null;
                    boolean middle = true;
View Full Code Here

        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());
        }       
    }
View Full Code Here

            errln("FAIL: getDateFormat() must throw IllegalArgumentException for dateStyle:DF_NONE/timeStyle:DF_NONE");
        }

        // Set explicit time zone
        logln("Set timezone - America/Sao_Paulo");
        TimeZone tz = TimeZone.getTimeZone("America/Sao_Paulo");
        gp.setTimeZone(tz);
        df = gp.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_MEDIUM);
        String tzid = df.getTimeZone().getID();
        if (!tzid.equals("America/Sao_Paulo")) {
            errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
View Full Code Here

     * 4. If a generic non-location string is not available, use generic location
     *    string.
     */
    private String getGenericString(Calendar cal, boolean isShort, boolean commonlyUsedOnly) {
        String result = null;
        TimeZone tz = cal.getTimeZone();
        String tzid = tz.getID();

        if (!isFullyLoaded) {
            // Lazy loading
            loadZone(tzid);
        }

        ZoneStrings zstrings = (ZoneStrings)tzidToStrings.get(tzid);
        if (zstrings == null) {
            // ICU's own array does not have entries for aliases
            String canonicalID = ZoneMeta.getCanonicalSystemID(tzid);
            if (canonicalID != null && !canonicalID.equals(tzid)) {
                // Canonicalize tzid here.  The rest of operations
                // require tzid to be canonicalized.
                tzid = canonicalID;
                zstrings = (ZoneStrings)tzidToStrings.get(tzid);
            }
        }
        if (zstrings != null) {
            if (isShort) {
                if (!commonlyUsedOnly || zstrings.isShortFormatCommonlyUsed()) {
                    result = zstrings.getString(ZSIDX_SHORT_GENERIC);
                }
            } else {
                result = zstrings.getString(ZSIDX_LONG_GENERIC);
            }
        }
        if (result == null && mzidToStrings != null) {
            // try metazone
            long time = cal.getTimeInMillis();
            String mzid = ZoneMeta.getMetazoneID(tzid, time);
            if (mzid != null) {
                boolean useStandard = false;
                if (cal.get(Calendar.DST_OFFSET) == 0) {
                    useStandard = true;
                    // Check if the zone actually uses daylight saving time around the time
                    if (tz instanceof BasicTimeZone) {
                        BasicTimeZone btz = (BasicTimeZone)tz;
                        TimeZoneTransition before = btz.getPreviousTransition(time, true);
                        if (before != null
                                && (time - before.getTime() < DST_CHECK_RANGE)
                                && before.getFrom().getDSTSavings() != 0) {
                            useStandard = false;
                        } else {
                            TimeZoneTransition after = btz.getNextTransition(time, false);
                            if (after != null
                                    && (after.getTime() - time < DST_CHECK_RANGE)
                                    && after.getTo().getDSTSavings() != 0) {
                                useStandard = false;
                            }
                        }
                    } else {
                        // If not BasicTimeZone... only if the instance is not an ICU's implementation.
                        // We may get a wrong answer in edge case, but it should practically work OK.
                        int[] offsets = new int[2];
                        tz.getOffset(time - DST_CHECK_RANGE, false, offsets);
                        if (offsets[1] != 0) {
                            useStandard = false;
                        } else {
                            tz.getOffset(time + DST_CHECK_RANGE, false, offsets);
                            if (offsets[1] != 0){
                                useStandard = false;
                            }
                        }
                    }
                }
                if (useStandard) {
                    result = getString(tzid, (isShort ? ZSIDX_SHORT_STANDARD : ZSIDX_LONG_STANDARD),
                            time, commonlyUsedOnly);

                    // Note:
                    // In CLDR 1.5.1, a same localization is used for both generic and standard
                    // for some metazones in some locales.  This is actually data bugs and should
                    // be resolved in later versions of CLDR.  For now, we check if the standard
                    // name is different from its generic name below.
                    if (result != null) {
                        String genericNonLocation = getString(tzid, (isShort ? ZSIDX_SHORT_GENERIC : ZSIDX_LONG_GENERIC),
                                time, commonlyUsedOnly);
                        if (genericNonLocation != null && result.equalsIgnoreCase(genericNonLocation)) {
                            result = null;
                        }
                    }
                }
                if (result == null){
                    ZoneStrings mzstrings = (ZoneStrings)mzidToStrings.get(mzid);
                    if (mzstrings != null) {
                        if (isShort) {
                            if (!commonlyUsedOnly || mzstrings.isShortFormatCommonlyUsed()) {
                                result = mzstrings.getString(ZSIDX_SHORT_GENERIC);
                            }
                        } else {
                            result = mzstrings.getString(ZSIDX_LONG_GENERIC);
                        }
                    }
                    if (result != null) {
                        // Check if the offsets at the given time matches the preferred zone's offsets
                        String preferredId = ZoneMeta.getZoneIdByMetazone(mzid, getRegion());
                        if (!tzid.equals(preferredId)) {
                            // Check if the offsets at the given time are identical with the preferred zone
                            int raw = cal.get(Calendar.ZONE_OFFSET);
                            int sav = cal.get(Calendar.DST_OFFSET);
                            TimeZone preferredZone = TimeZone.getTimeZone(preferredId);
                            int[] preferredOffsets = new int[2];
                            // Check offset in preferred time zone with wall time.
                            // With getOffset(time, false, preferredOffsets),
                            // you may get incorrect results because of time overlap at DST->STD
                            // transition.
                            preferredZone.getOffset(time + raw + sav, true, preferredOffsets);
                            if (raw != preferredOffsets[0] || sav != preferredOffsets[1]) {
                                // Use generic partial location string as fallback
                                result = zstrings.getGenericPartialLocationString(mzid, isShort, commonlyUsedOnly);
                            }
                        }
View Full Code Here

        }
        return val;
    }

    protected TimeZone safeGetTimeZone(String id) {
        TimeZone tz = TimeZone.getTimeZone(id);
        if (tz == null) {
            // should never happen
            errln("FAIL: TimeZone.getTimeZone(" + id + ") => null");
        }
        if (!tz.getID().equals(id)) {
            warnln("FAIL: TimeZone.getTimeZone(" + id + ") => " + tz.getID());
        }
        return tz;
    }
View Full Code Here

    /*
     * Test method for 'com.ibm.icu.util.TimeZone.hashCode()'
     */
    public void testHashCode() {
        TimeZone tz1 = TimeZone.getTimeZone("PST");
        TimeZone tz2 = TimeZone.getTimeZone("PST");
        TimeZone tzn = TimeZone.getTimeZone("CST");
        testEHCS(tz1, tz2, tzn);
    }
View Full Code Here

    /*
     * Test method for 'com.ibm.icu.util.TimeZone.getOffset(int, int, int, int, int, int)'
     */
    public void testGetOffset() {
        TimeZone tz = TimeZone.getTimeZone("PST");
        int offset = tz.getOffset(1, 2004, 0, 01, 1, 0);
        assertEquals(-28800000, offset);
    }
View Full Code Here

    /*
     * Test method for 'com.ibm.icu.util.TimeZone.setRawOffset(int)'
     */
    public void testSetRawOffset() {
        TimeZone tz = TimeZone.getTimeZone("PST");
        int value = tz.getRawOffset();
        int value1 = value + 100000;
        tz.setRawOffset(value1);
        int result = tz.getRawOffset();
        assertNotEqual(value, result);
        assertEquals(value1, result);
    }
View Full Code Here

TOP

Related Classes of com.ibm.icu.util.TimeZone

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.