Search in sources :

Example 21 with SimpleTimeZone

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

the class OlsonTimeZone method construct.

private void construct(UResourceBundle top, UResourceBundle res) {
    if ((top == null || res == null)) {
        throw new IllegalArgumentException();
    }
    if (DEBUG)
        System.out.println("OlsonTimeZone(" + res.getKey() + ")");
    UResourceBundle r;
    int[] transPre32, trans32, transPost32;
    transPre32 = trans32 = transPost32 = null;
    transitionCount = 0;
    // Pre-32bit second transitions
    try {
        r = res.get("transPre32");
        transPre32 = r.getIntVector();
        if (transPre32.length % 2 != 0) {
            // elements in the pre-32bit must be an even number
            throw new IllegalArgumentException("Invalid Format");
        }
        transitionCount += transPre32.length / 2;
    } catch (MissingResourceException e) {
    // Pre-32bit transition data is optional
    }
    // 32bit second transitions
    try {
        r = res.get("trans");
        trans32 = r.getIntVector();
        transitionCount += trans32.length;
    } catch (MissingResourceException e) {
    // 32bit transition data is optional
    }
    // Post-32bit second transitions
    try {
        r = res.get("transPost32");
        transPost32 = r.getIntVector();
        if (transPost32.length % 2 != 0) {
            // elements in the post-32bit must be an even number
            throw new IllegalArgumentException("Invalid Format");
        }
        transitionCount += transPost32.length / 2;
    } catch (MissingResourceException e) {
    // Post-32bit transition data is optional
    }
    if (transitionCount > 0) {
        transitionTimes64 = new long[transitionCount];
        int idx = 0;
        if (transPre32 != null) {
            for (int i = 0; i < transPre32.length / 2; i++, idx++) {
                transitionTimes64[idx] = ((transPre32[i * 2]) & 0x00000000FFFFFFFFL) << 32 | ((transPre32[i * 2 + 1]) & 0x00000000FFFFFFFFL);
            }
        }
        if (trans32 != null) {
            for (int i = 0; i < trans32.length; i++, idx++) {
                transitionTimes64[idx] = trans32[i];
            }
        }
        if (transPost32 != null) {
            for (int i = 0; i < transPost32.length / 2; i++, idx++) {
                transitionTimes64[idx] = ((transPost32[i * 2]) & 0x00000000FFFFFFFFL) << 32 | ((transPost32[i * 2 + 1]) & 0x00000000FFFFFFFFL);
            }
        }
    } else {
        transitionTimes64 = null;
    }
    // Type offsets list must be of even size, with size >= 2
    r = res.get("typeOffsets");
    typeOffsets = r.getIntVector();
    if ((typeOffsets.length < 2 || typeOffsets.length > 0x7FFE || typeOffsets.length % 2 != 0)) {
        throw new IllegalArgumentException("Invalid Format");
    }
    typeCount = typeOffsets.length / 2;
    // Type map data must be of the same size as the transition count
    if (transitionCount > 0) {
        r = res.get("typeMap");
        typeMapData = r.getBinary(null);
        if (typeMapData == null || typeMapData.length != transitionCount) {
            throw new IllegalArgumentException("Invalid Format");
        }
    } else {
        typeMapData = null;
    }
    // Process final rule and data, if any
    finalZone = null;
    finalStartYear = Integer.MAX_VALUE;
    finalStartMillis = Double.MAX_VALUE;
    String ruleID = null;
    try {
        ruleID = res.getString("finalRule");
        r = res.get("finalRaw");
        int ruleRaw = r.getInt() * Grego.MILLIS_PER_SECOND;
        r = loadRule(top, ruleID);
        int[] ruleData = r.getIntVector();
        if (ruleData == null || ruleData.length != 11) {
            throw new IllegalArgumentException("Invalid Format");
        }
        finalZone = new SimpleTimeZone(ruleRaw, "", ruleData[0], ruleData[1], ruleData[2], ruleData[3] * Grego.MILLIS_PER_SECOND, ruleData[4], ruleData[5], ruleData[6], ruleData[7], ruleData[8] * Grego.MILLIS_PER_SECOND, ruleData[9], ruleData[10] * Grego.MILLIS_PER_SECOND);
        r = res.get("finalYear");
        finalStartYear = r.getInt();
        // Note: Setting finalStartYear to the finalZone is problematic.  When a date is around
        // year boundary, SimpleTimeZone may return false result when DST is observed at the
        // beginning of year.  We could apply safe margin (day or two), but when one of recurrent
        // rules falls around year boundary, it could return false result.  Without setting the
        // start year, finalZone works fine around the year boundary of the start year.
        // finalZone.setStartYear(finalStartYear);
        // Compute the millis for Jan 1, 0:00 GMT of the finalYear
        // Note: finalStartMillis is used for detecting either if
        // historic transition data or finalZone to be used.  In an
        // extreme edge case - for example, two transitions fall into
        // small windows of time around the year boundary, this may
        // result incorrect offset computation.  But I think it will
        // never happen practically.  Yoshito - Feb 20, 2010
        finalStartMillis = Grego.fieldsToDay(finalStartYear, 0, 1) * Grego.MILLIS_PER_DAY;
    } catch (MissingResourceException e) {
        if (ruleID != null) {
            // creating finalZone
            throw new IllegalArgumentException("Invalid Format");
        }
    }
}
Also used : UResourceBundle(android.icu.util.UResourceBundle) SimpleTimeZone(android.icu.util.SimpleTimeZone) MissingResourceException(java.util.MissingResourceException)

Example 22 with SimpleTimeZone

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

the class OlsonTimeZone method setRawOffset.

/* (non-Javadoc)
     * @see android.icu.util.TimeZone#setRawOffset(int)
     */
@Override
public void setRawOffset(int offsetMillis) {
    if (isFrozen()) {
        throw new UnsupportedOperationException("Attempt to modify a frozen OlsonTimeZone instance.");
    }
    if (getRawOffset() == offsetMillis) {
        return;
    }
    long current = System.currentTimeMillis();
    if (current < finalStartMillis) {
        SimpleTimeZone stz = new SimpleTimeZone(offsetMillis, getID());
        boolean bDst = useDaylightTime();
        if (bDst) {
            TimeZoneRule[] currentRules = getSimpleTimeZoneRulesNear(current);
            if (currentRules.length != 3) {
                // DST was observed at the beginning of this year, so useDaylightTime
                // returned true.  getSimpleTimeZoneRulesNear requires at least one
                // future transition for making a pair of rules.  This implementation
                // rolls back the time before the latest offset transition.
                TimeZoneTransition tzt = getPreviousTransition(current, false);
                if (tzt != null) {
                    currentRules = getSimpleTimeZoneRulesNear(tzt.getTime() - 1);
                }
            }
            if (currentRules.length == 3 && (currentRules[1] instanceof AnnualTimeZoneRule) && (currentRules[2] instanceof AnnualTimeZoneRule)) {
                // A pair of AnnualTimeZoneRule
                AnnualTimeZoneRule r1 = (AnnualTimeZoneRule) currentRules[1];
                AnnualTimeZoneRule r2 = (AnnualTimeZoneRule) currentRules[2];
                DateTimeRule start, end;
                int offset1 = r1.getRawOffset() + r1.getDSTSavings();
                int offset2 = r2.getRawOffset() + r2.getDSTSavings();
                int sav;
                if (offset1 > offset2) {
                    start = r1.getRule();
                    end = r2.getRule();
                    sav = offset1 - offset2;
                } else {
                    start = r2.getRule();
                    end = r1.getRule();
                    sav = offset2 - offset1;
                }
                // getSimpleTimeZoneRulesNear always return rules using DOW / WALL_TIME
                stz.setStartRule(start.getRuleMonth(), start.getRuleWeekInMonth(), start.getRuleDayOfWeek(), start.getRuleMillisInDay());
                stz.setEndRule(end.getRuleMonth(), end.getRuleWeekInMonth(), end.getRuleDayOfWeek(), end.getRuleMillisInDay());
                // set DST saving amount and start year
                stz.setDSTSavings(sav);
            } else {
                // This could only happen if last rule is DST
                // and the rule used forever.  For example, Asia/Dhaka
                // in tzdata2009i stays in DST forever.
                // Hack - set DST starting at midnight on Jan 1st,
                // ending 23:59:59.999 on Dec 31st
                stz.setStartRule(0, 1, 0);
                stz.setEndRule(11, 31, Grego.MILLIS_PER_DAY - 1);
            }
        }
        int[] fields = Grego.timeToFields(current, null);
        finalStartYear = fields[0];
        finalStartMillis = Grego.fieldsToDay(fields[0], 0, 1);
        if (bDst) {
            // we probably do not need to set start year of final rule
            // to finalzone itself, but we always do this for now.
            stz.setStartYear(finalStartYear);
        }
        finalZone = stz;
    } else {
        finalZone.setRawOffset(offsetMillis);
    }
    transitionRulesInitialized = false;
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) TimeZoneRule(android.icu.util.TimeZoneRule) DateTimeRule(android.icu.util.DateTimeRule) TimeZoneTransition(android.icu.util.TimeZoneTransition) SimpleTimeZone(android.icu.util.SimpleTimeZone) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule)

Example 23 with SimpleTimeZone

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

the class CalendarRegressionTest method Test4040996.

@Test
public void Test4040996() {
    try {
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
        Calendar calendar = new GregorianCalendar(pdt);
        calendar.set(Calendar.MONTH, 3);
        calendar.set(Calendar.DAY_OF_MONTH, 18);
        calendar.set(Calendar.SECOND, 30);
        logln("MONTH: " + calendar.get(Calendar.MONTH));
        logln("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
        logln("MINUTE: " + calendar.get(Calendar.MINUTE));
        logln("SECOND: " + calendar.get(Calendar.SECOND));
        calendar.add(Calendar.SECOND, 6);
        // This will print out todays date for MONTH and DAY_OF_MONTH
        // instead of the date it was set to.
        // This happens when adding MILLISECOND or MINUTE also
        logln("MONTH: " + calendar.get(Calendar.MONTH));
        logln("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
        logln("MINUTE: " + calendar.get(Calendar.MINUTE));
        logln("SECOND: " + calendar.get(Calendar.SECOND));
        if (calendar.get(Calendar.MONTH) != 3 || calendar.get(Calendar.DAY_OF_MONTH) != 18 || calendar.get(Calendar.SECOND) != 36)
            errln("Fail: Calendar.add misbehaves");
    } catch (Exception e) {
        warnln("Could not load data. " + e.getMessage());
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) Test(org.junit.Test)

Example 24 with SimpleTimeZone

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

the class TimeZoneRegressionTest method Test4176686.

/**
 * getDisplayName doesn't work with unusual savings/offsets.
 */
@Test
public void Test4176686() {
    // Construct a zone that does not observe DST but
    // that does have a DST savings (which should be ignored).
    // 1:30
    int offset = 90 * 60000;
    SimpleTimeZone z1 = new SimpleTimeZone(offset, "_std_zone_");
    // 0:45
    z1.setDSTSavings(45 * 60000);
    // Construct a zone that observes DST for the first 6 months.
    SimpleTimeZone z2 = new SimpleTimeZone(offset, "_dst_zone_");
    // 0:45
    z2.setDSTSavings(45 * 60000);
    z2.setStartRule(Calendar.JANUARY, 1, 0);
    z2.setEndRule(Calendar.JULY, 1, 0);
    // Also check DateFormat
    DateFormat fmt1 = new SimpleDateFormat("z");
    // Format uses standard zone
    fmt1.setTimeZone(z1);
    DateFormat fmt2 = new SimpleDateFormat("z");
    // Format uses DST zone
    fmt2.setTimeZone(z2);
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(1970, Calendar.FEBRUARY, 1);
    // Time in DST
    Date dst = tempcal.getTime();
    tempcal.set(1970, Calendar.AUGUST, 1);
    // Time in standard
    Date std = tempcal.getTime();
    // Description, Result, Expected Result
    String[] DATA = { "getDisplayName(false, SHORT)/std zone", z1.getDisplayName(false, TimeZone.SHORT), "GMT+1:30", "getDisplayName(false, LONG)/std zone", z1.getDisplayName(false, TimeZone.LONG), "GMT+01:30", "getDisplayName(true, SHORT)/std zone", z1.getDisplayName(true, TimeZone.SHORT), "GMT+1:30", "getDisplayName(true, LONG)/std zone", z1.getDisplayName(true, TimeZone.LONG), "GMT+01:30", "getDisplayName(false, SHORT)/dst zone", z2.getDisplayName(false, TimeZone.SHORT), "GMT+1:30", "getDisplayName(false, LONG)/dst zone", z2.getDisplayName(false, TimeZone.LONG), "GMT+01:30", "getDisplayName(true, SHORT)/dst zone", z2.getDisplayName(true, TimeZone.SHORT), "GMT+2:15", "getDisplayName(true, LONG)/dst zone", z2.getDisplayName(true, TimeZone.LONG), "GMT+02:15", "DateFormat.format(std)/std zone", fmt1.format(std), "GMT+1:30", "DateFormat.format(dst)/std zone", fmt1.format(dst), "GMT+1:30", "DateFormat.format(std)/dst zone", fmt2.format(std), "GMT+1:30", "DateFormat.format(dst)/dst zone", fmt2.format(dst), "GMT+2:15" };
    for (int i = 0; i < DATA.length; i += 3) {
        if (!DATA[i + 1].equals(DATA[i + 2])) {
            errln("FAIL: " + DATA[i] + " -> " + DATA[i + 1] + ", exp " + DATA[i + 2]);
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 25 with SimpleTimeZone

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

the class TimeZoneRegressionTest method Test4073215.

@Test
public void Test4073215() {
    SimpleTimeZone z = new SimpleTimeZone(0, "GMT");
    if (z.useDaylightTime())
        errln("Fail: Fix test to start with non-DST zone");
    z.setStartRule(Calendar.FEBRUARY, 1, Calendar.SUNDAY, 0);
    z.setEndRule(Calendar.MARCH, -1, Calendar.SUNDAY, 0);
    if (!z.useDaylightTime())
        errln("Fail: DST not active");
    Calendar tempcal = Calendar.getInstance();
    tempcal.clear();
    tempcal.setTimeZone(z);
    tempcal.set(1997, Calendar.JANUARY, 31);
    Date d1 = tempcal.getTime();
    if (z.inDaylightTime(d1)) {
        errln("Fail: DST not working as expected");
    }
    tempcal.set(1997, Calendar.MARCH, 1);
    Date d2 = tempcal.getTime();
    if (!z.inDaylightTime(d2)) {
        errln("Fail: DST not working as expected");
    }
    tempcal.clear();
    tempcal.set(1997, Calendar.MARCH, 31);
    Date d3 = tempcal.getTime();
    if (z.inDaylightTime(d3)) {
        errln("Fail: DST not working as expected");
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Date(java.util.Date) Test(org.junit.Test)

Aggregations

SimpleTimeZone (android.icu.util.SimpleTimeZone)39 Test (org.junit.Test)35 TimeZone (android.icu.util.TimeZone)17 Date (java.util.Date)15 GregorianCalendar (android.icu.util.GregorianCalendar)14 BasicTimeZone (android.icu.util.BasicTimeZone)10 Calendar (android.icu.util.Calendar)9 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)9 JavaTimeZone (android.icu.impl.JavaTimeZone)7 VTimeZone (android.icu.util.VTimeZone)7 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)7 SimpleDateFormat (android.icu.text.SimpleDateFormat)6 InitialTimeZoneRule (android.icu.util.InitialTimeZoneRule)5 CalendarAstronomer (android.icu.impl.CalendarAstronomer)4 AnnualTimeZoneRule (android.icu.util.AnnualTimeZoneRule)4 TimeArrayTimeZoneRule (android.icu.util.TimeArrayTimeZoneRule)4 DateFormat (android.icu.text.DateFormat)3 DateTimeRule (android.icu.util.DateTimeRule)3 TimeZoneTransition (android.icu.util.TimeZoneTransition)3 ULocale (android.icu.util.ULocale)3