Search in sources :

Example 56 with Calendar

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

the class DataDrivenCalendarTest method testOps.

private void testOps(TestDataModule.TestData testData, DataMap settings) {
    // Get 'from' time
    CalendarFieldsSet fromSet = new CalendarFieldsSet(), toSet = new CalendarFieldsSet(), paramsSet = new CalendarFieldsSet(), diffSet = new CalendarFieldsSet();
    // DateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
    // Start the processing
    int n = 0;
    long fromDate = 0;
    long toDate = 0;
    boolean useDate = false;
    for (Iterator iter = testData.getDataIterator(); iter.hasNext(); ) {
        ++n;
        DataMap currentCase = (DataMap) iter.next();
        String caseString = "[case " + n + "]";
        // build to calendar
        // Headers { "locale","from","operation","params","to" }
        // #1 locale
        String param = "locale";
        String locale;
        String testSetting = currentCase.getString(param);
        locale = testSetting;
        ULocale loc = new ULocale(locale);
        Calendar fromCalendar = Calendar.getInstance(loc);
        fromSet.clear();
        // #2 'from' info
        param = "from";
        String from = testSetting = currentCase.getString(param);
        if (from.startsWith(kMILLIS)) {
            useDate = true;
            fromDate = Long.parseLong(from.substring(kMILLIS.length()));
        } else {
            fromSet.parseFrom(testSetting);
        }
        // System.err.println("fromset: ["+testSetting+"] >> " + fromSet);
        // #4 'operation' info
        param = "operation";
        String operation = testSetting = currentCase.getString(param);
        paramsSet.clear();
        // #3 'params' info
        param = "params";
        String paramsData = testSetting = currentCase.getString(param);
        // parse with inheritance.
        paramsSet.parseFrom(paramsData);
        // System.err.println("paramsSet: ["+testSetting+"] >> " + paramsSet);
        toSet.clear();
        // #4 'to' info
        param = "to";
        String to = testSetting = currentCase.getString(param);
        if (to.startsWith(kMILLIS)) {
            useDate = true;
            toDate = Long.parseLong(to.substring(kMILLIS.length()));
        } else {
            toSet.parseFrom(testSetting, fromSet);
        }
        // toSet.parseFrom(testSetting, fromSet); // parse with inheritance.
        // System.err.println("toSet: ["+testSetting+"] >> " + toSet);
        String caseContentsString = locale + ":  from " + from + ": " + operation + " [[[ " + paramsSet + " ]]]   >>> " + to;
        logln(caseString + ": " + caseContentsString);
        // / prepare calendar
        if (useDate) {
            fromCalendar.setTimeInMillis(fromDate);
        } else {
            fromSet.setOnCalendar(fromCalendar);
        }
        // from calendar:  'starting date'
        diffSet.clear();
        // Is the calendar sane after being set?
        if (!fromSet.matches(fromCalendar, diffSet)) {
            String diffs = diffSet.diffFrom(fromSet);
            errln((String) "FAIL: " + caseString + ", SET SOURCE calendar was not set: Differences: " + diffs);
        } else {
            // verifies that the requested fields were set.
            logln(" " + caseString + " SET SOURCE calendar match.");
        }
        // to calendar - copy of from calendar
        Calendar toCalendar = (Calendar) fromCalendar.clone();
        // / perform op on 'to calendar'
        for (int q = 0; q < paramsSet.fieldCount(); q++) {
            if (paramsSet.isSet(q)) {
                if (operation.equals(kROLL)) {
                    toCalendar.roll(q, paramsSet.get(q));
                } else if (operation.equals(kADD)) {
                    toCalendar.add(q, paramsSet.get(q));
                } else {
                    errln(caseString + " FAIL: unknown operation " + operation);
                }
                logln(operation + " of " + paramsSet.get(q));
            }
        }
        // now - what's the result?
        diffSet.clear();
        if (useDate) {
            if (toCalendar.getTimeInMillis() == toDate) {
                logln(caseString + " SUCCESS: got=expected=" + toDate);
                logln("PASS: " + caseString + " matched! ");
            } else {
                // failing because of this.
                if ((caseString.equals("[case 31]") || caseString.equals("[case 36]")) && TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) {
                    logln(caseString + " FAIL(expected): got " + toCalendar.getTimeInMillis() + "  expected " + toDate);
                } else {
                    errln(caseString + " FAIL: got " + toCalendar.getTimeInMillis() + "  expected " + toDate);
                }
            }
        } else if (!toSet.matches(toCalendar, diffSet)) {
            String diffs = diffSet.diffFrom(toSet);
            errln((String) "FAIL: " + caseString + " - , " + caseContentsString + " Differences: " + diffs);
        } else {
            logln("PASS: " + caseString + " matched! ");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) Iterator(java.util.Iterator) CalendarFieldsSet(android.icu.dev.test.util.CalendarFieldsSet) DataMap(android.icu.dev.test.TestDataModule.DataMap)

Example 57 with Calendar

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

the class ChineseTest method TestMapping.

/**
 * Test basic mapping to and from Gregorian.
 */
@Test
public void TestMapping() {
    final int[] DATA = { // Gregorian    Chinese
    1964, 9, 4, 4601, 7, 0, 28, 1964, 9, 5, 4601, 7, 0, 29, 1964, 9, 6, 4601, 8, 0, 1, 1964, 9, 7, 4601, 8, 0, 2, 1961, 12, 25, 4598, 11, 0, 18, 1999, 6, 4, 4636, 4, 0, 21, 1990, 5, 23, 4627, 4, 0, 29, 1990, 5, 24, 4627, 5, 0, 1, 1990, 6, 22, 4627, 5, 0, 30, 1990, 6, 23, 4627, 5, 1, 1, 1990, 7, 20, 4627, 5, 1, 28, 1990, 7, 21, 4627, 5, 1, 29, 1990, 7, 22, 4627, 6, 0, 1 };
    ChineseCalendar cal = new ChineseCalendar();
    StringBuffer buf = new StringBuffer();
    logln("Gregorian -> Chinese");
    // java.util.Calendar grego = java.util.Calendar.getInstance();
    Calendar grego = Calendar.getInstance();
    grego.clear();
    for (int i = 0; i < DATA.length; ) {
        grego.set(DATA[i++], DATA[i++] - 1, DATA[i++]);
        Date date = grego.getTime();
        cal.setTime(date);
        int y = cal.get(Calendar.EXTENDED_YEAR);
        // 0-based -> 1-based
        int m = cal.get(Calendar.MONTH) + 1;
        int L = cal.get(Calendar.IS_LEAP_MONTH);
        int d = cal.get(Calendar.DAY_OF_MONTH);
        // Expected y, m, isLeapMonth, d
        int yE = DATA[i++];
        // 1-based
        int mE = DATA[i++];
        int LE = DATA[i++];
        int dE = DATA[i++];
        buf.setLength(0);
        buf.append(date + " -> ");
        buf.append(y + "/" + m + (L == 1 ? "(leap)" : "") + "/" + d);
        if (y == yE && m == mE && L == LE && d == dE) {
            logln("OK: " + buf.toString());
        } else {
            errln("Fail: " + buf.toString() + ", expected " + yE + "/" + mE + (LE == 1 ? "(leap)" : "") + "/" + dE);
        }
    }
    logln("Chinese -> Gregorian");
    for (int i = 0; i < DATA.length; ) {
        grego.set(DATA[i++], DATA[i++] - 1, DATA[i++]);
        Date dexp = grego.getTime();
        int cyear = DATA[i++];
        int cmonth = DATA[i++];
        int cisleapmonth = DATA[i++];
        int cdayofmonth = DATA[i++];
        cal.clear();
        cal.set(Calendar.EXTENDED_YEAR, cyear);
        cal.set(Calendar.MONTH, cmonth - 1);
        cal.set(Calendar.IS_LEAP_MONTH, cisleapmonth);
        cal.set(Calendar.DAY_OF_MONTH, cdayofmonth);
        Date date = cal.getTime();
        buf.setLength(0);
        buf.append(cyear + "/" + cmonth + (cisleapmonth == 1 ? "(leap)" : "") + "/" + cdayofmonth);
        buf.append(" -> " + date);
        if (date.equals(dexp)) {
            logln("OK: " + buf.toString());
        } else {
            errln("Fail: " + buf.toString() + ", expected " + dexp);
        }
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Date(java.util.Date) Test(org.junit.Test)

Example 58 with Calendar

use of android.icu.util.Calendar 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 59 with Calendar

use of android.icu.util.Calendar 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 60 with Calendar

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

the class HolidayTest method TestIsOn.

@Test
public void TestIsOn() {
    // jb 1901
    SimpleHoliday sh = new SimpleHoliday(Calendar.AUGUST, 15, "Doug's Day", 1958, 2058);
    Calendar gcal = new GregorianCalendar();
    gcal.clear();
    gcal.set(Calendar.YEAR, 2000);
    gcal.set(Calendar.MONTH, Calendar.AUGUST);
    gcal.set(Calendar.DAY_OF_MONTH, 15);
    Date d0 = gcal.getTime();
    gcal.add(Calendar.SECOND, 1);
    Date d1 = gcal.getTime();
    gcal.add(Calendar.SECOND, -2);
    Date d2 = gcal.getTime();
    gcal.add(Calendar.DAY_OF_MONTH, 1);
    Date d3 = gcal.getTime();
    gcal.add(Calendar.SECOND, 1);
    Date d4 = gcal.getTime();
    gcal.add(Calendar.SECOND, -2);
    gcal.set(Calendar.YEAR, 1957);
    Date d5 = gcal.getTime();
    gcal.set(Calendar.YEAR, 1958);
    Date d6 = gcal.getTime();
    gcal.set(Calendar.YEAR, 2058);
    Date d7 = gcal.getTime();
    gcal.set(Calendar.YEAR, 2059);
    Date d8 = gcal.getTime();
    Date[] dates = { d0, d1, d2, d3, d4, d5, d6, d7, d8 };
    boolean[] isOns = { true, true, false, true, false, false, true, true, false };
    for (int i = 0; i < dates.length; ++i) {
        Date d = dates[i];
        logln("\ndate: " + d);
        boolean isOn = sh.isOn(d);
        logln("isOnDate: " + isOn);
        if (isOn != isOns[i]) {
            errln("date: " + d + " should be on Doug's Day!");
        }
        Date h = sh.firstAfter(d);
        logln("firstAfter: " + h);
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleHoliday(android.icu.util.SimpleHoliday) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Calendar (android.icu.util.Calendar)233 Test (org.junit.Test)155 GregorianCalendar (android.icu.util.GregorianCalendar)135 Date (java.util.Date)96 JapaneseCalendar (android.icu.util.JapaneseCalendar)72 SimpleDateFormat (android.icu.text.SimpleDateFormat)69 ULocale (android.icu.util.ULocale)60 BuddhistCalendar (android.icu.util.BuddhistCalendar)53 ChineseCalendar (android.icu.util.ChineseCalendar)52 IslamicCalendar (android.icu.util.IslamicCalendar)48 TimeZone (android.icu.util.TimeZone)41 DateFormat (android.icu.text.DateFormat)37 HebrewCalendar (android.icu.util.HebrewCalendar)32 FieldPosition (java.text.FieldPosition)29 SimpleTimeZone (android.icu.util.SimpleTimeZone)26 ParsePosition (java.text.ParsePosition)23 TaiwanCalendar (android.icu.util.TaiwanCalendar)22 ParseException (java.text.ParseException)21 ChineseDateFormat (android.icu.text.ChineseDateFormat)17 IOException (java.io.IOException)16