Search in sources :

Example 31 with SimpleTimeZone

use of android.icu.util.SimpleTimeZone 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)

Example 32 with SimpleTimeZone

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

the class TimeZoneRegressionTest method Test4154525.

/**
 * SimpleTimeZone accepts illegal DST savings values.  These values
 * must be non-zero.  There is no upper limit at this time.
 */
@Test
public void Test4154525() {
    final int GOOD = 1, BAD = 0;
    int[] DATA = { 1, GOOD, 0, BAD, -1, BAD, 60 * 60 * 1000, GOOD, Integer.MIN_VALUE, BAD // Integer.MAX_VALUE, ?, // no upper limit on DST savings at this time
    };
    for (int i = 0; i < DATA.length; i += 2) {
        int savings = DATA[i];
        boolean valid = DATA[i + 1] == GOOD;
        String method = null;
        for (int j = 0; j < 2; ++j) {
            try {
                switch(j) {
                    case 0:
                        method = "constructor";
                        SimpleTimeZone z = new SimpleTimeZone(0, "id", Calendar.JANUARY, 1, 0, 0, Calendar.MARCH, 1, 0, 0, // <- what we're interested in
                        savings);
                        break;
                    case 1:
                        method = "setDSTSavings()";
                        z = new SimpleTimeZone(0, "GMT");
                        z.setDSTSavings(savings);
                        break;
                }
                if (valid) {
                    logln("Pass: DST savings of " + savings + " accepted by " + method);
                } else {
                    errln("Fail: DST savings of " + savings + " accepted by " + method);
                }
            } catch (IllegalArgumentException e) {
                if (valid) {
                    errln("Fail: DST savings of " + savings + " to " + method + " gave " + e);
                } else {
                    logln("Pass: DST savings of " + savings + " to " + method + " gave " + e);
                }
            }
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) Test(org.junit.Test)

Example 33 with SimpleTimeZone

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

the class TimeZoneRegressionTest method Test4162593.

/**
 * TimeZone broken at midnight.  The TimeZone code fails to handle
 * transitions at midnight correctly.
 */
@Test
public void Test4162593() {
    SimpleDateFormat fmt = new SimpleDateFormat("z", Locale.US);
    final int ONE_HOUR = 60 * 60 * 1000;
    final float H = (float) ONE_HOUR;
    TimeZone initialZone = TimeZone.getDefault();
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm z");
    SimpleTimeZone asuncion = new SimpleTimeZone(-4 * ONE_HOUR, "America/Asuncion", /*PY%sT*/
    Calendar.OCTOBER, 1, 0, /*DOM*/
    0 * ONE_HOUR, Calendar.MARCH, 1, 0, /*DOM*/
    0 * ONE_HOUR, 1 * ONE_HOUR);
    /* Zone
         * Starting time
         * Transition expected between start+1H and start+2H
         */
    Object[] DATA = { new SimpleTimeZone(2 * ONE_HOUR, "Asia/Damascus", /*EE%sT*/
    Calendar.APRIL, 1, 0, /*DOM*/
    0 * ONE_HOUR, Calendar.OCTOBER, 1, 0, /*DOM*/
    0 * ONE_HOUR, 1 * ONE_HOUR), new int[] { 1998, Calendar.SEPTEMBER, 30, 22, 0 }, Boolean.TRUE, asuncion, new int[] { 2000, Calendar.FEBRUARY, 28, 22, 0 }, Boolean.FALSE, asuncion, new int[] { 2000, Calendar.FEBRUARY, 29, 22, 0 }, Boolean.TRUE };
    String[] zone = new String[4];
    for (int j = 0; j < DATA.length; j += 3) {
        TimeZone tz = (TimeZone) DATA[j];
        TimeZone.setDefault(tz);
        fmt.setTimeZone(tz);
        sdf.setTimeZone(tz);
        // Must construct the Date object AFTER setting the default zone
        int[] p = (int[]) DATA[j + 1];
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(p[0], p[1], p[2], p[3], p[4]);
        long start = cal.getTime().getTime();
        boolean transitionExpected = ((Boolean) DATA[j + 2]).booleanValue();
        logln(tz.getID() + ":");
        for (int i = 0; i < 4; ++i) {
            Date d = new Date(start + i * ONE_HOUR);
            zone[i] = fmt.format(d);
            logln("" + i + ": " + sdf.format(d) + " => " + zone[i] + " (" + d.getTime() / H + ")");
        }
        cal.set(p[0], p[1], p[2], 0, 0);
        for (int i = 0; i < 4; ++i) {
            int h = 22 + i;
            int dom = p[2] + (h >= 24 ? 1 : 0);
            h %= 24;
            int ms = h * ONE_HOUR;
            cal.clear();
            cal.set(p[0], p[1], dom, 0, 0);
            int off = tz.getOffset(GregorianCalendar.AD, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), cal.get(Calendar.DAY_OF_WEEK), ms);
            cal.add(Calendar.HOUR, h);
            int dstOffset = cal.get(Calendar.DST_OFFSET);
            logln("h=" + h + "; dom=" + dom + "; ZONE_OFFSET=" + cal.get(Calendar.ZONE_OFFSET) / H + "; DST_OFFSET=" + dstOffset / H + "; getOffset()=" + off / H + " (" + cal.getTime().getTime() / H + ")");
        }
        if (zone[0].equals(zone[1]) && (zone[1].equals(zone[2]) != transitionExpected) && zone[2].equals(zone[3])) {
            logln("Ok: transition " + transitionExpected);
        } else {
            errln("FAIL: expected " + (transitionExpected ? "transition" : "no transition"));
        }
    }
    // restore the initial time zone so that this test case
    // doesn't affect the others.
    TimeZone.setDefault(initialZone);
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Date(java.util.Date) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 34 with SimpleTimeZone

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

the class TimeScaleDataTest method TestDotNet.

/*
     * ICU's Universal Time Scale is designed to be tick-for-tick compatible with
     * .Net System.DateTime. Verify that this is so for the
     * .Net-supported date range (years 1-9999 AD).
     * This requires a proleptic Gregorian calendar because that's what .Net uses.
     * Proleptic: No Julian/Gregorian switchover, or a switchover before
     * any date that we test, that is, before 0001 AD.
     */
@Test
public void TestDotNet() {
    TimeZone utc;
    final long dayMillis = 86400 * 1000L;
    /* 1 day = 86400 seconds */
    final long dayTicks = 86400 * 10000000L;
    // offset for dotNetDateTimeTicks[] field
    final int kYear = 0;
    final int kMonth = 1;
    final int kDay = 2;
    final int kTicks = 3;
    final int kIncrement = 4;
    GregorianCalendar cal;
    long icuDate;
    long ticks, millis;
    int i;
    /* Open a proleptic Gregorian calendar. */
    long before0001AD = -1000000 * dayMillis;
    utc = new SimpleTimeZone(0, "UTC");
    cal = new GregorianCalendar(utc, Locale.ENGLISH);
    cal.setGregorianChange(new Date(before0001AD));
    for (i = 0; i < dotNetDateTimeTicks.length; i += kIncrement) {
        /* Test conversion from .Net/Universal time to ICU time. */
        millis = UniversalTimeScale.toLong(dotNetDateTimeTicks[i + kTicks], UniversalTimeScale.ICU4C_TIME);
        cal.clear();
        cal.set((int) dotNetDateTimeTicks[i + kYear], (int) dotNetDateTimeTicks[i + kMonth] - 1, /* Java & ICU use January = month 0. */
        (int) dotNetDateTimeTicks[i + kDay]);
        icuDate = cal.getTimeInMillis();
        if (millis != icuDate) {
            /* Print days not millis. */
            errln("UniversalTimeScale.toLong(ticks[" + i + "], ICU4C)=" + (millis / dayMillis) + " != " + (icuDate / dayMillis) + "=ucal_getMillis(" + dotNetDateTimeTicks[i + kYear] + "-" + dotNetDateTimeTicks[i + kMonth] + "-" + dotNetDateTimeTicks[i + kDay] + ")");
        }
        /* Test conversion from ICU time to .Net/Universal time. */
        ticks = UniversalTimeScale.from(icuDate, UniversalTimeScale.ICU4C_TIME);
        if (ticks != dotNetDateTimeTicks[i + kTicks]) {
            /* Print days not ticks. */
            errln("UniversalTimeScale.from(date[" + i + "], ICU4C)=" + (ticks / dayTicks) + " != " + dotNetDateTimeTicks[i + kTicks] / dayTicks + "=.Net System.DateTime(" + dotNetDateTimeTicks[i + kYear] + "-" + dotNetDateTimeTicks[i + kMonth] + "-" + dotNetDateTimeTicks[i + kDay] + ").Ticks");
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 35 with SimpleTimeZone

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

the class TimeZoneRuleTest method TestSimpleTimeZoneCoverage.

/*
     * API coverage test for BasicTimeZone APIs in SimpleTimeZone
     */
@Test
public void TestSimpleTimeZoneCoverage() {
    long time1 = getUTCMillis(1990, Calendar.JUNE, 1);
    long time2 = getUTCMillis(2000, Calendar.JUNE, 1);
    TimeZoneTransition tzt1, tzt2;
    // BasicTimeZone API implementation in SimpleTimeZone
    SimpleTimeZone stz1 = new SimpleTimeZone(-5 * HOUR, "GMT-5");
    tzt1 = stz1.getNextTransition(time1, false);
    if (tzt1 != null) {
        errln("FAIL: No transition must be returned by getNextTranstion for SimpleTimeZone with no DST rule");
    }
    tzt1 = stz1.getPreviousTransition(time1, false);
    if (tzt1 != null) {
        errln("FAIL: No transition must be returned by getPreviousTransition  for SimpleTimeZone with no DST rule");
    }
    TimeZoneRule[] tzrules = stz1.getTimeZoneRules();
    if (tzrules.length != 1 || !(tzrules[0] instanceof InitialTimeZoneRule)) {
        errln("FAIL: Invalid results returned by SimpleTimeZone#getTimeZoneRules");
    }
    // Set DST rule
    // March 11
    stz1.setStartRule(Calendar.MARCH, 11, 2 * HOUR);
    // First Sunday in November
    stz1.setEndRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY, 2 * HOUR);
    tzt1 = stz1.getNextTransition(time1, false);
    if (tzt1 == null) {
        errln("FAIL: Non-null transition must be returned by getNextTranstion for SimpleTimeZone with a DST rule");
    } else {
        String str = tzt1.toString();
        if (str == null || str.length() == 0) {
            errln("FAIL: TimeZoneTransition#toString returns null or empty string");
        } else {
            logln(str);
        }
    }
    tzt1 = stz1.getPreviousTransition(time1, false);
    if (tzt1 == null) {
        errln("FAIL: Non-null transition must be returned by getPreviousTransition  for SimpleTimeZone with a DST rule");
    }
    tzrules = stz1.getTimeZoneRules();
    if (tzrules.length != 3 || !(tzrules[0] instanceof InitialTimeZoneRule) || !(tzrules[1] instanceof AnnualTimeZoneRule) || !(tzrules[2] instanceof AnnualTimeZoneRule)) {
        errln("FAIL: Invalid results returned by SimpleTimeZone#getTimeZoneRules for a SimpleTimeZone with DST");
    }
    // Set DST start year
    stz1.setStartYear(2007);
    tzt1 = stz1.getPreviousTransition(time1, false);
    if (tzt1 != null) {
        errln("FAIL: No transition must be returned before 1990");
    }
    // transition after 1990-06-01
    tzt1 = stz1.getNextTransition(time1, false);
    // transition after 2000-06-01
    tzt2 = stz1.getNextTransition(time2, false);
    if (tzt1 == null || tzt2 == null || !tzt1.equals(tzt2)) {
        errln("FAIL: Bad transition returned by SimpleTimeZone#getNextTransition");
    }
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) TimeArrayTimeZoneRule(android.icu.util.TimeArrayTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) TimeZoneRule(android.icu.util.TimeZoneRule) TimeZoneTransition(android.icu.util.TimeZoneTransition) InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) 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