Search in sources :

Example 36 with SimpleTimeZone

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

the class CompatibilityTest method TestGenericAPI.

@Test
public void TestGenericAPI() {
    // not used String str;
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(1990, Calendar.APRIL, 15);
    Date when = tempcal.getTime();
    String tzid = "TestZone";
    int tzoffset = 123400;
    SimpleTimeZone zone = new SimpleTimeZone(tzoffset, tzid);
    Calendar cal = (Calendar) Calendar.getInstance((SimpleTimeZone) zone.clone());
    if (!zone.equals(cal.getTimeZone()))
        errln("FAIL: Calendar.getTimeZone failed");
    Calendar cal2 = Calendar.getInstance(cal.getTimeZone());
    cal.setTime(when);
    cal2.setTime(when);
    if (!(cal.equals(cal2)))
        errln("FAIL: Calendar.operator== failed");
    // if ((*cal != *cal2))  errln("FAIL: Calendar.operator!= failed");
    if (!cal.equals(cal2) || cal.before(cal2) || cal.after(cal2))
        errln("FAIL: equals/before/after failed");
    cal2.setTime(new Date(when.getTime() + 1000));
    if (cal.equals(cal2) || cal2.before(cal) || cal.after(cal2))
        errln("FAIL: equals/before/after failed");
    cal.roll(Calendar.SECOND, true);
    if (!cal.equals(cal2) || cal.before(cal2) || cal.after(cal2))
        errln("FAIL: equals/before/after failed");
    // Roll back to January
    cal.roll(Calendar.MONTH, (int) (1 + Calendar.DECEMBER - cal.get(Calendar.MONTH)));
    if (cal.equals(cal2) || cal2.before(cal) || cal.after(cal2))
        errln("FAIL: equals/before/after failed");
    for (int i = 0; i < 2; ++i) {
        boolean lenient = (i > 0);
        cal.setLenient(lenient);
        if (lenient != cal.isLenient())
            errln("FAIL: setLenient/isLenient failed");
    // Later: Check for lenient behavior
    }
    int i;
    for (i = Calendar.SUNDAY; i <= Calendar.SATURDAY; ++i) {
        cal.setFirstDayOfWeek(i);
        if (cal.getFirstDayOfWeek() != i)
            errln("FAIL: set/getFirstDayOfWeek failed");
    }
    for (i = 1; i <= 7; ++i) {
        cal.setMinimalDaysInFirstWeek(i);
        if (cal.getMinimalDaysInFirstWeek() != i)
            errln("FAIL: set/getFirstDayOfWeek failed");
    }
    for (i = 0; i < cal.getFieldCount(); ++i) {
        if (cal.getMinimum(i) > cal.getGreatestMinimum(i))
            errln("FAIL: getMinimum larger than getGreatestMinimum for field " + i);
        if (cal.getLeastMaximum(i) > cal.getMaximum(i))
            errln("FAIL: getLeastMaximum larger than getMaximum for field " + i);
        if (cal.getMinimum(i) >= cal.getMaximum(i))
            errln("FAIL: getMinimum not less than getMaximum for field " + i);
    }
    cal.setTimeZone(TimeZone.getDefault());
    cal.clear();
    cal.set(1984, 5, 24);
    tempcal.clear();
    tempcal.set(1984, 5, 24);
    if (cal.getTime().getTime() != tempcal.getTime().getTime()) {
        errln("FAIL: Calendar.set(3 args) failed");
        logln(" Got: " + cal.getTime() + "  Expected: " + tempcal.getTime());
    }
    cal.clear();
    cal.set(1985, 2, 2, 11, 49);
    tempcal.clear();
    tempcal.set(1985, 2, 2, 11, 49);
    if (cal.getTime().getTime() != tempcal.getTime().getTime()) {
        errln("FAIL: Calendar.set(5 args) failed");
        logln(" Got: " + cal.getTime() + "  Expected: " + tempcal.getTime());
    }
    cal.clear();
    cal.set(1995, 9, 12, 1, 39, 55);
    tempcal.clear();
    tempcal.set(1995, 9, 12, 1, 39, 55);
    if (cal.getTime().getTime() != tempcal.getTime().getTime()) {
        errln("FAIL: Calendar.set(6 args) failed");
        logln(" Got: " + cal.getTime() + "  Expected: " + tempcal.getTime());
    }
    cal.getTime();
    // others not to be?  Revisit the appropriateness of this.  - Alan NEWCAL
    for (i = 0; i < cal.getFieldCount(); ++i) {
        switch(i) {
            case Calendar.YEAR:
            case Calendar.MONTH:
            case Calendar.DATE:
            case Calendar.HOUR_OF_DAY:
            case Calendar.MINUTE:
            case Calendar.SECOND:
            case Calendar.EXTENDED_YEAR:
                if (!cal.isSet(i))
                    errln("FAIL: " + FIELD_NAME[i] + " is not set");
                break;
            default:
                if (cal.isSet(i))
                    errln("FAIL: " + FIELD_NAME[i] + " is set");
        }
        cal.clear(i);
        if (cal.isSet(i))
            errln("FAIL: Calendar.clear/isSet failed");
    }
    // delete cal;
    // delete cal2;
    Locale[] loc = Calendar.getAvailableLocales();
    long count = loc.length;
    if (count < 1 || loc == null) {
        errln("FAIL: getAvailableLocales failed");
    } else {
        for (i = 0; i < count; ++i) {
            cal = Calendar.getInstance(loc[i]);
        // delete cal;
        }
    }
    cal = Calendar.getInstance(TimeZone.getDefault(), Locale.ENGLISH);
    // delete cal;
    cal = Calendar.getInstance(zone, Locale.ENGLISH);
    // delete cal;
    GregorianCalendar gc = new GregorianCalendar(zone);
    // delete gc;
    gc = new GregorianCalendar(Locale.ENGLISH);
    // delete gc;
    gc = new GregorianCalendar(Locale.ENGLISH);
    // delete gc;
    gc = new GregorianCalendar(zone, Locale.ENGLISH);
    // delete gc;
    gc = new GregorianCalendar(zone);
    // delete gc;
    gc = new GregorianCalendar(1998, 10, 14, 21, 43);
    tempcal.clear();
    tempcal.set(1998, 10, 14, 21, 43);
    if (gc.getTime().getTime() != tempcal.getTime().getTime())
        errln("FAIL: new GregorianCalendar(ymdhm) failed");
    // delete gc;
    gc = new GregorianCalendar(1998, 10, 14, 21, 43, 55);
    tempcal.clear();
    tempcal.set(1998, 10, 14, 21, 43, 55);
    if (gc.getTime().getTime() != tempcal.getTime().getTime())
        errln("FAIL: new GregorianCalendar(ymdhms) failed");
// C++ only:
// GregorianCalendar gc2 = new GregorianCalendar(Locale.ENGLISH);
// gc2 = gc;
// if (gc2 != gc || !(gc2 == gc)) errln("FAIL: GregorianCalendar assignment/operator==/operator!= failed");
// delete gc;
// delete z;
}
Also used : Locale(java.util.Locale) SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 37 with SimpleTimeZone

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

the class AstroTest method TestLunarPosition.

@Test
public void TestLunarPosition() {
    GregorianCalendar gc = new GregorianCalendar(new SimpleTimeZone(0, "UTC"));
    CalendarAstronomer astro = new CalendarAstronomer();
    // year, month, day, hour, minute, ascension(radians), declination(radians)
    final double[][] tests = { { 1979, 2, 26, 16, 00, -0.3778379118188744, -0.1399698825594198 } };
    logln("");
    for (int i = 0; i < tests.length; i++) {
        gc.clear();
        gc.set((int) tests[i][0], (int) tests[i][1] - 1, (int) tests[i][2], (int) tests[i][3], (int) tests[i][4]);
        astro.setDate(gc.getTime());
        Equatorial result = astro.getMoonPosition();
        if (result.ascension != tests[i][5]) {
            if ((float) result.ascension == (float) tests[i][5]) {
                logln("result.ascension(" + result.ascension + ") !=  tests[i][5](" + tests[i][5] + ") in double for test " + i);
            } else {
                errln("FAIL: result.ascension(" + result.ascension + ") !=  tests[i][5](" + tests[i][5] + ") for test " + i);
            }
        }
        if (result.declination != tests[i][6]) {
            if ((float) result.declination == (float) tests[i][6]) {
                logln("result.declination(" + result.declination + ") !=  tests[i][6](" + tests[i][6] + ") in double for test " + i);
            } else {
                errln("FAIL: result.declination(" + result.declination + ") !=  tests[i][6](" + tests[i][6] + ") for test " + i);
            }
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) CalendarAstronomer(android.icu.impl.CalendarAstronomer) Equatorial(android.icu.impl.CalendarAstronomer.Equatorial) Test(org.junit.Test)

Example 38 with SimpleTimeZone

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

the class AstroTest method TestMoonAge.

@Test
public void TestMoonAge() {
    GregorianCalendar gc = new GregorianCalendar(new SimpleTimeZone(0, "GMT"));
    CalendarAstronomer calastro = new CalendarAstronomer();
    // more testcases are around the date 05/20/2012
    // ticket#3785  UDate ud0 = 1337557623000.0;
    double[][] testcase = { { 2012, 5, 20, 16, 48, 59 }, { 2012, 5, 20, 16, 47, 34 }, { 2012, 5, 21, 00, 00, 00 }, { 2012, 5, 20, 14, 55, 59 }, { 2012, 5, 21, 7, 40, 40 }, { 2023, 9, 25, 10, 00, 00 }, { 2008, 7, 7, 15, 00, 33 }, { 1832, 9, 24, 2, 33, 41 }, { 2016, 1, 31, 23, 59, 59 }, { 2099, 5, 20, 14, 55, 59 } };
    // Moon phase angle - Got from http://www.moonsystem.to/checkupe.htm
    double[] angle = { 356.8493418421329, 356.8386760059673, 0.09625415252237701, 355.9986960782416, 3.5714026601303317, 124.26906744384183, 59.80247650195558, 357.54163205513123, 268.41779281511094, 4.82340276581624 };
    double precision = PI / 32;
    for (int i = 0; i < testcase.length; i++) {
        gc.clear();
        String testString = "CASE[" + i + "]: Year " + (int) testcase[i][0] + " Month " + (int) testcase[i][1] + " Day " + (int) testcase[i][2] + " Hour " + (int) testcase[i][3] + " Minutes " + (int) testcase[i][4] + " Seconds " + (int) testcase[i][5];
        gc.set((int) testcase[i][0], (int) testcase[i][1] - 1, (int) testcase[i][2], (int) testcase[i][3], (int) testcase[i][4], (int) testcase[i][5]);
        calastro.setDate(gc.getTime());
        double expectedAge = (angle[i] * PI) / 180;
        double got = calastro.getMoonAge();
        logln(testString);
        if (!(got > expectedAge - precision && got < expectedAge + precision)) {
            errln("FAIL: expected " + expectedAge + " got " + got);
        } else {
            logln("PASS: expected " + expectedAge + " got " + got);
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) CalendarAstronomer(android.icu.impl.CalendarAstronomer) Test(org.junit.Test)

Example 39 with SimpleTimeZone

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

the class TimeZoneFormatTest method TestISOFormat.

@Test
public void TestISOFormat() {
    final int[] OFFSET = { // 0
    0, // 0.999s
    999, // -59.999s
    -59999, // 1m
    60000, // -1m 17.777s
    -77777, // 30m
    1800000, // -1h
    -3600000, // 10h
    36000000, // -10h 30m
    -37800000, // -10h 30m 45s
    -37845000, // 30h
    108000000 };
    final String[][] ISO_STR = { // 0
    { "Z", "Z", "Z", "Z", "Z", "+00", "+0000", "+00:00", "+0000", "+00:00", "+0000" }, // 999
    { "Z", "Z", "Z", "Z", "Z", "+00", "+0000", "+00:00", "+0000", "+00:00", "+0000" }, // -59999
    { "Z", "Z", "Z", "-000059", "-00:00:59", "+00", "+0000", "+00:00", "-000059", "-00:00:59", "-000059" }, // 60000
    { "+0001", "+0001", "+00:01", "+0001", "+00:01", "+0001", "+0001", "+00:01", "+0001", "+00:01", "+0001" }, // -77777
    { "-0001", "-0001", "-00:01", "-000117", "-00:01:17", "-0001", "-0001", "-00:01", "-000117", "-00:01:17", "-000117" }, // 1800000
    { "+0030", "+0030", "+00:30", "+0030", "+00:30", "+0030", "+0030", "+00:30", "+0030", "+00:30", "+0030" }, // -3600000
    { "-01", "-0100", "-01:00", "-0100", "-01:00", "-01", "-0100", "-01:00", "-0100", "-01:00", "-0100" }, // 36000000
    { "+10", "+1000", "+10:00", "+1000", "+10:00", "+10", "+1000", "+10:00", "+1000", "+10:00", "+1000" }, // -37800000
    { "-1030", "-1030", "-10:30", "-1030", "-10:30", "-1030", "-1030", "-10:30", "-1030", "-10:30", "-1030" }, // -37845000
    { "-1030", "-1030", "-10:30", "-103045", "-10:30:45", "-1030", "-1030", "-10:30", "-103045", "-10:30:45", "-103045" }, // 108000000
    { null, null, null, null, null, null, null, null, null, null, null } };
    final String[] PATTERN = { "X", "XX", "XXX", "XXXX", "XXXXX", "x", "xx", "xxx", "xxxx", "xxxxx", // equivalent to "xxxx"
    "Z" };
    final int[] MIN_OFFSET_UNIT = { 60000, 60000, 60000, 1000, 1000, 60000, 60000, 60000, 1000, 1000, 1000 };
    // Formatting
    SimpleDateFormat sdf = new SimpleDateFormat();
    Date d = new Date();
    for (int i = 0; i < OFFSET.length; i++) {
        SimpleTimeZone tz = new SimpleTimeZone(OFFSET[i], "Zone Offset:" + String.valueOf(OFFSET[i]) + "ms");
        sdf.setTimeZone(tz);
        for (int j = 0; j < PATTERN.length; j++) {
            sdf.applyPattern(PATTERN[j]);
            try {
                String result = sdf.format(d);
                if (!result.equals(ISO_STR[i][j])) {
                    errln("FAIL: pattern=" + PATTERN[j] + ", offset=" + OFFSET[i] + " -> " + result + " (expected: " + ISO_STR[i][j] + ")");
                }
            } catch (IllegalArgumentException e) {
                if (ISO_STR[i][j] != null) {
                    errln("FAIL: IAE thrown for pattern=" + PATTERN[j] + ", offset=" + OFFSET[i] + " (expected: " + ISO_STR[i][j] + ")");
                }
            }
        }
    }
    // Parsing
    SimpleTimeZone bogusTZ = new SimpleTimeZone(-1, "Zone Offset: -1ms");
    for (int i = 0; i < ISO_STR.length; i++) {
        for (int j = 0; j < ISO_STR[i].length; j++) {
            if (ISO_STR[i][j] == null) {
                continue;
            }
            ParsePosition pos = new ParsePosition(0);
            Calendar outcal = Calendar.getInstance(bogusTZ);
            sdf.applyPattern(PATTERN[j]);
            sdf.parse(ISO_STR[i][j], outcal, pos);
            if (pos.getIndex() != ISO_STR[i][j].length()) {
                errln("FAIL: Failed to parse the entire input string: " + ISO_STR[i][j]);
                continue;
            }
            TimeZone outtz = outcal.getTimeZone();
            int outOffset = outtz.getRawOffset();
            int adjustedOffset = OFFSET[i] / MIN_OFFSET_UNIT[j] * MIN_OFFSET_UNIT[j];
            if (outOffset != adjustedOffset) {
                errln("FAIL: Incorrect offset:" + outOffset + "ms for input string: " + ISO_STR[i][j] + " (expected:" + adjustedOffset + "ms)");
            }
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) 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