Search in sources :

Example 36 with GregorianCalendar

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

the class CalendarTestFmwk method doLimitsTest.

/**
 * Test the functions getXxxMinimum() and getXxxMaximum() by marching a
 * test calendar 'cal' through 'numberOfDays' sequential days starting
 * with 'startDate'.  For each date, read a field value along with its
 * reported actual minimum and actual maximum.  These values are
 * checked against one another as well as against getMinimum(),
 * getGreatestMinimum(), getLeastMaximum(), and getMaximum().  We
 * expect to see:
 *
 * 1. minimum <= actualMinimum <= greatestMinimum <=
 *    leastMaximum <= actualMaximum <= maximum
 *
 * 2. actualMinimum <= value <= actualMaximum
 *
 * Note: In addition to outright failures, this test reports some
 * results as warnings.  These are not generally of concern, but they
 * should be evaluated by a human.  To see these, run this test in
 * verbose mode.
 * @param cal the calendar to be tested
 * @param fieldsToTest an array of field values to be tested, e.g., new
 * int[] { Calendar.MONTH, Calendar.DAY_OF_MONTH }.  It only makes
 * sense to test the day fields; the time fields are not tested by this
 * method.  If null, then test all standard fields.
 * @param startDate the first date to test
 * @param testDuration if positive, the number of days to be tested.
 * If negative, the number of seconds to run the test.
 */
protected void doLimitsTest(Calendar cal, int[] fieldsToTest, Date startDate, int testDuration) {
    GregorianCalendar greg = new GregorianCalendar();
    greg.setTime(startDate);
    logln("Start: " + startDate);
    if (fieldsToTest == null) {
        fieldsToTest = new int[] { Calendar.ERA, Calendar.YEAR, Calendar.MONTH, Calendar.WEEK_OF_YEAR, Calendar.WEEK_OF_MONTH, Calendar.DAY_OF_MONTH, Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK_IN_MONTH, Calendar.YEAR_WOY, Calendar.EXTENDED_YEAR };
    }
    // Keep a record of minima and maxima that we actually see.
    // These are kept in an array of arrays of hashes.
    Map[][] limits = new Map[fieldsToTest.length][2];
    // Meaningless placeholder
    Object nub = new Object();
    // This test can run for a long time; show progress.
    long millis = System.currentTimeMillis();
    // 5 sec
    long mark = millis + 5000;
    // stop time if testDuration<0
    millis -= testDuration * 1000;
    for (int i = 0; testDuration > 0 ? i < testDuration : System.currentTimeMillis() < millis; ++i) {
        if (System.currentTimeMillis() >= mark) {
            logln("(" + i + " days)");
            // 5 sec
            mark += 5000;
        }
        cal.setTimeInMillis(greg.getTimeInMillis());
        for (int j = 0; j < fieldsToTest.length; ++j) {
            int f = fieldsToTest[j];
            int v = cal.get(f);
            int minActual = cal.getActualMinimum(f);
            int maxActual = cal.getActualMaximum(f);
            int minLow = cal.getMinimum(f);
            int minHigh = cal.getGreatestMinimum(f);
            int maxLow = cal.getLeastMaximum(f);
            int maxHigh = cal.getMaximum(f);
            // Fetch the hash for this field and keep track of the
            // minima and maxima.
            Map[] h = limits[j];
            if (h[0] == null) {
                h[0] = new HashMap();
                h[1] = new HashMap();
            }
            h[0].put(new Integer(minActual), nub);
            h[1].put(new Integer(maxActual), nub);
            if (minActual < minLow || minActual > minHigh) {
                errln("Fail: " + ymdToString(cal) + " Range for min of " + FIELD_NAME[f] + "(" + f + ")=" + minLow + ".." + minHigh + ", actual_min=" + minActual);
            }
            if (maxActual < maxLow || maxActual > maxHigh) {
                errln("Fail: " + ymdToString(cal) + " Range for max of " + FIELD_NAME[f] + "(" + f + ")=" + maxLow + ".." + maxHigh + ", actual_max=" + maxActual);
            }
            if (v < minActual || v > maxActual) {
                errln("Fail: " + ymdToString(cal) + " " + FIELD_NAME[f] + "(" + f + ")=" + v + ", actual range=" + minActual + ".." + maxActual + ", allowed=(" + minLow + ".." + minHigh + ")..(" + maxLow + ".." + maxHigh + ")");
            }
        }
        greg.add(Calendar.DAY_OF_YEAR, 1);
    }
    // Check actual maxima and minima seen against ranges returned
    // by API.
    StringBuffer buf = new StringBuffer();
    for (int j = 0; j < fieldsToTest.length; ++j) {
        int f = fieldsToTest[j];
        buf.setLength(0);
        buf.append(FIELD_NAME[f]);
        Map[] h = limits[j];
        boolean fullRangeSeen = true;
        for (int k = 0; k < 2; ++k) {
            int rangeLow = (k == 0) ? cal.getMinimum(f) : cal.getLeastMaximum(f);
            int rangeHigh = (k == 0) ? cal.getGreatestMinimum(f) : cal.getMaximum(f);
            // seen, then there may be a problem.
            if (h[k].get(new Integer(rangeLow)) == null || h[k].get(new Integer(rangeHigh)) == null) {
                fullRangeSeen = false;
            }
            buf.append(k == 0 ? " minima seen=(" : "; maxima seen=(");
            for (Object v : h[k].keySet()) {
                buf.append(" " + v);
            }
            buf.append(") range=" + rangeLow + ".." + rangeHigh);
        }
        if (fullRangeSeen) {
            logln("OK: " + buf.toString());
        } else {
            // This may or may not be an error -- if the range of dates
            // we scan over doesn't happen to contain a minimum or
            // maximum, it doesn't mean some other range won't.
            logln("Warning: " + buf.toString());
        }
    }
    logln("End: " + greg.getTime());
}
Also used : HashMap(java.util.HashMap) GregorianCalendar(android.icu.util.GregorianCalendar) Map(java.util.Map) HashMap(java.util.HashMap)

Example 37 with GregorianCalendar

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

the class ChineseTest method Test6510.

@Test
public void Test6510() {
    Calendar gregorianCalendar;
    ChineseCalendar chineseCalendar, chineseCalendar2;
    ChineseDateFormat dateFormat;
    SimpleDateFormat simpleDateFormat;
    simpleDateFormat = new android.icu.text.SimpleDateFormat("MM/dd/yyyy G 'at' HH:mm:ss vvvv", Locale.US);
    dateFormat = new android.icu.text.ChineseDateFormat("MM/dd/yyyy(G) HH:mm:ss", Locale.CHINA);
    // lunar to gregorian
    chineseCalendar = new ChineseCalendar(77, 26, Calendar.JANUARY, 0, 6, 0, 0, 0);
    // coverage
    assertEquals("equivalent ChineseCalendar() constructors", chineseCalendar, new ChineseCalendar(77, 26, Calendar.JANUARY, 0, 6));
    gregorianCalendar = Calendar.getInstance(Locale.US);
    gregorianCalendar.setTime(chineseCalendar.getTime());
    // gregorian to lunar
    chineseCalendar2 = new ChineseCalendar();
    chineseCalendar2.setTimeInMillis(gregorianCalendar.getTimeInMillis());
    // validate roundtrip
    if (chineseCalendar.getTimeInMillis() != chineseCalendar2.getTimeInMillis()) {
        errln("time1: " + chineseCalendar.getTimeInMillis());
        errln("time2: " + chineseCalendar2.getTimeInMillis());
        errln("Lunar [MM/dd/y(G) HH:mm:ss] " + dateFormat.format(chineseCalendar));
        errln("**PROBLEM Grego [MM/dd/y(G) HH:mm:ss] " + simpleDateFormat.format(gregorianCalendar));
        errln("Grego [MM/dd/y(G) HH:mm:ss] " + simpleDateFormat.format(gregorianCalendar));
        errln("Lunar [MM/dd/y(G) HH:mm:ss] " + dateFormat.format(chineseCalendar2));
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ChineseDateFormat(android.icu.text.ChineseDateFormat) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 38 with GregorianCalendar

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

the class CopticTest method TestYear.

@Test
public void TestYear() {
    // Gregorian Calendar
    Calendar gCal = new GregorianCalendar();
    Date gToday = gCal.getTime();
    gCal.add(GregorianCalendar.MONTH, 2);
    Date gFuture = gCal.getTime();
    DateFormat gDF = DateFormat.getDateInstance(gCal, DateFormat.FULL);
    logln("gregorian calendar: " + gDF.format(gToday) + " + 2 months = " + gDF.format(gFuture));
    // Coptic Calendar
    CopticCalendar cCal = new CopticCalendar();
    Date cToday = cCal.getTime();
    cCal.add(CopticCalendar.MONTH, 2);
    Date cFuture = cCal.getTime();
    DateFormat cDF = DateFormat.getDateInstance(cCal, DateFormat.FULL);
    logln("coptic calendar: " + cDF.format(cToday) + " + 2 months = " + cDF.format(cFuture));
    // EthiopicCalendar
    EthiopicCalendar eCal = new EthiopicCalendar();
    Date eToday = eCal.getTime();
    // add 2 months
    eCal.add(EthiopicCalendar.MONTH, 2);
    eCal.setAmeteAlemEra(false);
    Date eFuture = eCal.getTime();
    DateFormat eDF = DateFormat.getDateInstance(eCal, DateFormat.FULL);
    logln("ethiopic calendar: " + eDF.format(eToday) + " + 2 months = " + eDF.format(eFuture));
}
Also used : EthiopicCalendar(android.icu.util.EthiopicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) CopticCalendar(android.icu.util.CopticCalendar) EthiopicCalendar(android.icu.util.EthiopicCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) CopticCalendar(android.icu.util.CopticCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 39 with GregorianCalendar

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

the class CompatibilityTest method TestSecondsZero121.

// Try to zero out the seconds field
@Test
public void TestSecondsZero121() {
    Calendar cal = new GregorianCalendar();
    // Initialize with current date/time
    cal.setTime(new Date());
    // Round down to minute
    cal.set(Calendar.SECOND, 0);
    Date d = cal.getTime();
    String s = d.toString();
    if (s.indexOf(":00 ") < 0)
        errln("Expected to see :00 in " + s);
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 40 with GregorianCalendar

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

the class CompatibilityTest method TestFieldSet4781.

// Verify that setting fields works.  This test fails when an exception is thrown.
@Test
public void TestFieldSet4781() {
    try {
        GregorianCalendar g = new GregorianCalendar();
        GregorianCalendar g2 = new GregorianCalendar();
        // At this point UTC value is set, various fields are not.
        // Now set to noon.
        g2.set(Calendar.HOUR, 12);
        g2.set(Calendar.MINUTE, 0);
        g2.set(Calendar.SECOND, 0);
        // it thinks the YEAR is set and it is NOT.
        if (g2.equals(g))
            logln("Same");
        else
            logln("Different");
    } catch (IllegalArgumentException e) {
        errln("Unexpected exception seen: " + e);
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) 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