Search in sources :

Example 31 with GregorianCalendar

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

the class CalendarRegressionTest method Test4209071.

/**
 * Calendar DAY_OF_WEEK_IN_MONTH fields->time broken. The problem is in the
 * field disambiguation code in GregorianCalendar. This code is supposed to
 * choose the most recent set of fields among the following:
 *
 * MONTH + DAY_OF_MONTH MONTH + WEEK_OF_MONTH + DAY_OF_WEEK MONTH +
 * DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK DAY_OF_YEAR WEEK_OF_YEAR + DAY_OF_WEEK
 */
@Test
public void Test4209071() {
    Calendar cal = Calendar.getInstance(Locale.US);
    // General field setting test
    int Y = 1995;
    Date[] d = new Date[13];
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(Y, Calendar.JANUARY, 1);
    d[0] = tempcal.getTime();
    tempcal.set(Y, Calendar.MARCH, 1);
    d[1] = tempcal.getTime();
    tempcal.set(Y, Calendar.JANUARY, 4);
    d[2] = tempcal.getTime();
    tempcal.set(Y, Calendar.JANUARY, 18);
    d[3] = tempcal.getTime();
    tempcal.set(Y, Calendar.JANUARY, 18);
    d[4] = tempcal.getTime();
    tempcal.set(Y - 1, Calendar.DECEMBER, 22);
    d[5] = tempcal.getTime();
    tempcal.set(Y, Calendar.JANUARY, 26);
    d[6] = tempcal.getTime();
    tempcal.set(Y, Calendar.JANUARY, 26);
    d[7] = tempcal.getTime();
    tempcal.set(Y, Calendar.MARCH, 1);
    d[8] = tempcal.getTime();
    tempcal.set(Y, Calendar.OCTOBER, 6);
    d[9] = tempcal.getTime();
    tempcal.set(Y, Calendar.OCTOBER, 13);
    d[10] = tempcal.getTime();
    tempcal.set(Y, Calendar.AUGUST, 10);
    d[11] = tempcal.getTime();
    tempcal.set(Y, Calendar.DECEMBER, 7);
    d[12] = tempcal.getTime();
    Object[] FIELD_DATA = { // 0
    new int[] {}, d[0], // 1
    new int[] { Calendar.MONTH, Calendar.MARCH }, d[1], // 2
    new int[] { Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY }, d[2], // 3
    new int[] { Calendar.DAY_OF_WEEK, Calendar.THURSDAY, Calendar.DAY_OF_MONTH, 18 }, d[3], // 4
    new int[] { Calendar.DAY_OF_MONTH, 18, Calendar.DAY_OF_WEEK, Calendar.THURSDAY }, d[4], // 5 (WOM -1 is in previous month)
    new int[] { Calendar.DAY_OF_MONTH, 18, Calendar.WEEK_OF_MONTH, -1, Calendar.DAY_OF_WEEK, Calendar.THURSDAY }, d[5], // 6
    new int[] { Calendar.DAY_OF_MONTH, 18, Calendar.WEEK_OF_MONTH, 4, Calendar.DAY_OF_WEEK, Calendar.THURSDAY }, d[6], // 7 (DIM -1 is in same month)
    new int[] { Calendar.DAY_OF_MONTH, 18, Calendar.DAY_OF_WEEK_IN_MONTH, -1, Calendar.DAY_OF_WEEK, Calendar.THURSDAY }, d[7], // 8
    new int[] { Calendar.WEEK_OF_YEAR, 9, Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY }, d[8], // 9
    new int[] { Calendar.MONTH, Calendar.OCTOBER, Calendar.DAY_OF_WEEK_IN_MONTH, 1, Calendar.DAY_OF_WEEK, Calendar.FRIDAY }, d[9], // 10
    new int[] { Calendar.MONTH, Calendar.OCTOBER, Calendar.WEEK_OF_MONTH, 2, Calendar.DAY_OF_WEEK, Calendar.FRIDAY }, d[10], // 11
    new int[] { Calendar.MONTH, Calendar.OCTOBER, Calendar.DAY_OF_MONTH, 15, Calendar.DAY_OF_YEAR, 222 }, d[11], // 12
    new int[] { Calendar.DAY_OF_WEEK, Calendar.THURSDAY, Calendar.MONTH, Calendar.DECEMBER }, d[12] };
    for (int i = 0; i < FIELD_DATA.length; i += 2) {
        int[] fields = (int[]) FIELD_DATA[i];
        Date exp = (Date) FIELD_DATA[i + 1];
        cal.clear();
        cal.set(Calendar.YEAR, Y);
        for (int j = 0; j < fields.length; j += 2) {
            cal.set(fields[j], fields[j + 1]);
        }
        Date act = cal.getTime();
        if (!act.equals(exp)) {
            errln("FAIL: Test " + (i / 2) + " got " + act + ", want " + exp + " (see test/java/util/Calendar/CalendarRegressionTest.java");
        }
    }
    tempcal.set(1997, Calendar.JANUARY, 5);
    d[0] = tempcal.getTime();
    tempcal.set(1997, Calendar.JANUARY, 26);
    d[1] = tempcal.getTime();
    tempcal.set(1997, Calendar.FEBRUARY, 23);
    d[2] = tempcal.getTime();
    tempcal.set(1997, Calendar.JANUARY, 26);
    d[3] = tempcal.getTime();
    tempcal.set(1997, Calendar.JANUARY, 5);
    d[4] = tempcal.getTime();
    tempcal.set(1996, Calendar.DECEMBER, 8);
    d[5] = tempcal.getTime();
    // Test specific failure reported in bug
    Object[] DATA = { new Integer(1), d[0], new Integer(4), d[1], new Integer(8), d[2], new Integer(-1), d[3], new Integer(-4), d[4], new Integer(-8), d[5] };
    for (int i = 0; i < DATA.length; i += 2) {
        cal.clear();
        cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, ((Number) DATA[i]).intValue());
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.YEAR, 1997);
        Date actual = cal.getTime();
        if (!actual.equals(DATA[i + 1])) {
            errln("FAIL: Sunday " + DATA[i] + " of Jan 1997 -> " + actual + ", want " + DATA[i + 1]);
        }
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Date(java.util.Date) Test(org.junit.Test)

Example 32 with GregorianCalendar

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

the class CalendarRegressionTest method TestYearJump3279.

@Test
public void TestYearJump3279() {
    final long time = 1041148800000L;
    Calendar c = new GregorianCalendar();
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);
    c.setTimeInMillis(time);
    int year1 = c.get(Calendar.YEAR);
    logln("time: " + fmt.format(new Date(c.getTimeInMillis())));
    logln("setting DOW to " + c.getFirstDayOfWeek());
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    logln("week: " + c.getTime());
    logln("week adjust: " + fmt.format(new Date(c.getTimeInMillis())));
    int year2 = c.get(Calendar.YEAR);
    if (year1 != year2) {
        errln("Error: adjusted day of week, and year jumped from " + year1 + " to " + year2);
    } else {
        logln("Year remained " + year2 + " - PASS.");
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 33 with GregorianCalendar

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

the class AstroTest method TestCoverage.

@Test
public void TestCoverage() {
    GregorianCalendar cal = new GregorianCalendar(1958, Calendar.AUGUST, 15);
    Date then = cal.getTime();
    CalendarAstronomer myastro = new CalendarAstronomer(then);
    // Latitude:  34 degrees 05' North
    // Longitude:  118 degrees 22' West
    double laLat = 34 + 5d / 60, laLong = 360 - (118 + 22d / 60);
    CalendarAstronomer myastro2 = new CalendarAstronomer(laLong, laLat);
    double eclLat = laLat * Math.PI / 360;
    double eclLong = laLong * Math.PI / 360;
    Ecliptic ecl = new Ecliptic(eclLat, eclLong);
    logln("ecliptic: " + ecl);
    CalendarAstronomer myastro3 = new CalendarAstronomer();
    myastro3.setJulianDay((4713 + 2000) * 365.25);
    CalendarAstronomer[] astronomers = { // check cache
    myastro, // check cache
    myastro2, // check cache
    myastro3, // check cache
    myastro2 };
    for (int i = 0; i < astronomers.length; ++i) {
        CalendarAstronomer astro = astronomers[i];
        logln("astro: " + astro);
        logln("   time: " + astro.getTime());
        logln("   date: " + astro.getDate());
        logln("   cent: " + astro.getJulianCentury());
        logln("   gw sidereal: " + astro.getGreenwichSidereal());
        logln("   loc sidereal: " + astro.getLocalSidereal());
        logln("   equ ecl: " + astro.eclipticToEquatorial(ecl));
        logln("   equ long: " + astro.eclipticToEquatorial(eclLong));
        logln("   horiz: " + astro.eclipticToHorizon(eclLong));
        logln("   sunrise: " + new Date(astro.getSunRiseSet(true)));
        logln("   sunset: " + new Date(astro.getSunRiseSet(false)));
        logln("   moon phase: " + astro.getMoonPhase());
        logln("   moonrise: " + new Date(astro.getMoonRiseSet(true)));
        logln("   moonset: " + new Date(astro.getMoonRiseSet(false)));
        logln("   prev summer solstice: " + new Date(astro.getSunTime(CalendarAstronomer.SUMMER_SOLSTICE, false)));
        logln("   next summer solstice: " + new Date(astro.getSunTime(CalendarAstronomer.SUMMER_SOLSTICE, true)));
        logln("   prev full moon: " + new Date(astro.getMoonTime(CalendarAstronomer.FULL_MOON, false)));
        logln("   next full moon: " + new Date(astro.getMoonTime(CalendarAstronomer.FULL_MOON, true)));
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Ecliptic(android.icu.impl.CalendarAstronomer.Ecliptic) Date(java.util.Date) CalendarAstronomer(android.icu.impl.CalendarAstronomer) Test(org.junit.Test)

Example 34 with GregorianCalendar

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

the class AstroTest method TestSolarLongitude.

@Test
public void TestSolarLongitude() {
    GregorianCalendar gc = new GregorianCalendar(new SimpleTimeZone(0, "UTC"));
    CalendarAstronomer astro = new CalendarAstronomer();
    // year, month, day, hour, minute, longitude (radians), ascension(radians), declination(radians)
    final double[][] tests = { { 1980, 7, 27, 00, 00, 2.166442986535465, 2.2070499713207730, 0.3355704075759270 }, { 1988, 7, 27, 00, 00, 2.167484927693959, 2.2081183335606176, 0.3353093444275315 } };
    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());
        double longitude = astro.getSunLongitude();
        if (longitude != tests[i][5]) {
            if ((float) longitude == (float) tests[i][5]) {
                logln("longitude(" + longitude + ") !=  tests[i][5](" + tests[i][5] + ") in double for test " + i);
            } else {
                errln("FAIL: longitude(" + longitude + ") !=  tests[i][5](" + tests[i][5] + ") for test " + i);
            }
        }
        Equatorial result = astro.getSunPosition();
        if (result.ascension != tests[i][6]) {
            if ((float) result.ascension == (float) tests[i][6]) {
                logln("result.ascension(" + result.ascension + ") !=  tests[i][6](" + tests[i][6] + ") in double for test " + i);
            } else {
                errln("FAIL: result.ascension(" + result.ascension + ") !=  tests[i][6](" + tests[i][6] + ") for test " + i);
            }
        }
        if (result.declination != tests[i][7]) {
            if ((float) result.declination == (float) tests[i][7]) {
                logln("result.declination(" + result.declination + ") !=  tests[i][7](" + tests[i][7] + ") in double for test " + i);
            } else {
                errln("FAIL: result.declination(" + result.declination + ") !=  tests[i][7](" + tests[i][7] + ") 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 35 with GregorianCalendar

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

the class AstroTest method TestSunriseTimes.

@Test
public void TestSunriseTimes() {
    // logln("Sunrise/Sunset times for San Jose, California, USA");
    // CalendarAstronomer astro = new CalendarAstronomer(-121.55, 37.20);
    // TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    // We'll use a table generated by the UNSO website as our reference
    // From: http://aa.usno.navy.mil/
    // -Location: W079 25, N43 40
    // -Rise and Set for the Sun for 2001
    // -Zone:  4h West of Greenwich
    int[] USNO = { 6, 59, 19, 45, 6, 57, 19, 46, 6, 56, 19, 47, 6, 54, 19, 48, 6, 52, 19, 49, 6, 50, 19, 51, 6, 48, 19, 52, 6, 47, 19, 53, 6, 45, 19, 54, 6, 43, 19, 55, 6, 42, 19, 57, 6, 40, 19, 58, 6, 38, 19, 59, 6, 36, 20, 0, 6, 35, 20, 1, 6, 33, 20, 3, 6, 31, 20, 4, 6, 30, 20, 5, 6, 28, 20, 6, 6, 27, 20, 7, 6, 25, 20, 8, 6, 23, 20, 10, 6, 22, 20, 11, 6, 20, 20, 12, 6, 19, 20, 13, 6, 17, 20, 14, 6, 16, 20, 16, 6, 14, 20, 17, 6, 13, 20, 18, 6, 11, 20, 19 };
    logln("Sunrise/Sunset times for Toronto, Canada");
    CalendarAstronomer astro = new CalendarAstronomer(-(79 + 25 / 60), 43 + 40 / 60);
    // As of ICU4J 2.8 the ICU4J time zones implement pass-through
    // to the underlying JDK.  Because of variation in the
    // underlying JDKs, we have to use a fixed-offset
    // SimpleTimeZone to get consistent behavior between JDKs.
    // The offset we want is [-18000000, 3600000] (raw, dst).
    // [aliu 10/15/03]
    // TimeZone tz = TimeZone.getTimeZone("America/Montreal");
    TimeZone tz = new SimpleTimeZone(-18000000 + 3600000, "Montreal(FIXED)");
    GregorianCalendar cal = new GregorianCalendar(tz, Locale.US);
    GregorianCalendar cal2 = new GregorianCalendar(tz, Locale.US);
    cal.clear();
    cal.set(Calendar.YEAR, 2001);
    cal.set(Calendar.MONTH, Calendar.APRIL);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    // must be near local noon for getSunRiseSet to work
    cal.set(Calendar.HOUR_OF_DAY, 12);
    DateFormat df = DateFormat.getTimeInstance(cal, DateFormat.MEDIUM, Locale.US);
    DateFormat df2 = DateFormat.getDateTimeInstance(cal, DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US);
    DateFormat day = DateFormat.getDateInstance(cal, DateFormat.MEDIUM, Locale.US);
    for (int i = 0; i < 30; i++) {
        astro.setDate(cal.getTime());
        Date sunrise = new Date(astro.getSunRiseSet(true));
        Date sunset = new Date(astro.getSunRiseSet(false));
        cal2.setTime(cal.getTime());
        cal2.set(Calendar.SECOND, 0);
        cal2.set(Calendar.MILLISECOND, 0);
        cal2.set(Calendar.HOUR_OF_DAY, USNO[4 * i + 0]);
        cal2.set(Calendar.MINUTE, USNO[4 * i + 1]);
        Date exprise = cal2.getTime();
        cal2.set(Calendar.HOUR_OF_DAY, USNO[4 * i + 2]);
        cal2.set(Calendar.MINUTE, USNO[4 * i + 3]);
        Date expset = cal2.getTime();
        // Compute delta of what we got to the USNO data, in seconds
        int deltarise = Math.abs((int) (sunrise.getTime() - exprise.getTime()) / 1000);
        int deltaset = Math.abs((int) (sunset.getTime() - expset.getTime()) / 1000);
        // Allow a deviation of 0..MAX_DEV seconds
        // It would be nice to get down to 60 seconds, but at this
        // point that appears to be impossible without a redo of the
        // algorithm using something more advanced than Duffett-Smith.
        final int MAX_DEV = 180;
        if (deltarise > MAX_DEV || deltaset > MAX_DEV) {
            if (deltarise > MAX_DEV) {
                errln("FAIL: " + day.format(cal.getTime()) + ", Sunrise: " + df2.format(sunrise) + " (USNO " + df.format(exprise) + " d=" + deltarise + "s)");
            } else {
                logln(day.format(cal.getTime()) + ", Sunrise: " + df.format(sunrise) + " (USNO " + df.format(exprise) + ")");
            }
            if (deltaset > MAX_DEV) {
                errln("FAIL: " + day.format(cal.getTime()) + ", Sunset: " + df2.format(sunset) + " (USNO " + df.format(expset) + " d=" + deltaset + "s)");
            } else {
                logln(day.format(cal.getTime()) + ", Sunset: " + df.format(sunset) + " (USNO " + df.format(expset) + ")");
            }
        } else {
            logln(day.format(cal.getTime()) + ", Sunrise: " + df.format(sunrise) + " (USNO " + df.format(exprise) + ")" + ", Sunset: " + df.format(sunset) + " (USNO " + df.format(expset) + ")");
        }
        cal.add(Calendar.DATE, 1);
    }
// CalendarAstronomer a = new CalendarAstronomer(-(71+5/60), 42+37/60);
// cal.clear();
// cal.set(cal.YEAR, 1986);
// cal.set(cal.MONTH, cal.MARCH);
// cal.set(cal.DATE, 10);
// cal.set(cal.YEAR, 1988);
// cal.set(cal.MONTH, cal.JULY);
// cal.set(cal.DATE, 27);
// a.setDate(cal.getTime());
// long r = a.getSunRiseSet2(true);
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) DateFormat(android.icu.text.DateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) CalendarAstronomer(android.icu.impl.CalendarAstronomer) Date(java.util.Date) Test(org.junit.Test)

Aggregations

GregorianCalendar (android.icu.util.GregorianCalendar)111 Test (org.junit.Test)102 Date (java.util.Date)63 Calendar (android.icu.util.Calendar)40 SimpleDateFormat (android.icu.text.SimpleDateFormat)34 SimpleTimeZone (android.icu.util.SimpleTimeZone)28 TimeZone (android.icu.util.TimeZone)28 DateFormat (android.icu.text.DateFormat)24 JapaneseCalendar (android.icu.util.JapaneseCalendar)17 ULocale (android.icu.util.ULocale)16 ChineseCalendar (android.icu.util.ChineseCalendar)15 IslamicCalendar (android.icu.util.IslamicCalendar)14 BuddhistCalendar (android.icu.util.BuddhistCalendar)12 ChineseDateFormat (android.icu.text.ChineseDateFormat)8 IOException (java.io.IOException)8 ParsePosition (java.text.ParsePosition)8 CalendarAstronomer (android.icu.impl.CalendarAstronomer)7 HebrewCalendar (android.icu.util.HebrewCalendar)7 FieldPosition (java.text.FieldPosition)7 ParseException (java.text.ParseException)6