Search in sources :

Example 6 with DateInterval

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

the class DateIntervalFormatTest method TestTicket11583.

@Test
public void TestTicket11583() {
    ULocale[] locales = { ULocale.ENGLISH, SPANISH, LA_SPANISH };
    String[] skeletons = { "yMMMMd", "yMMMM", "MMMM", "yMMMd", "yMMM", "MMM", "yMMd", "yMMdd", "yMM", "MM", "yMdd", "yMd", "yM", "M" };
    final long startDate = 1232364615000L;
    final long endDate = 1240399815000L;
    // "yMMM";
    String filterPattern = null;
    for (ULocale locale : locales) {
        for (String skeleton : skeletons) {
            if (filterPattern != null && !skeleton.equals(filterPattern)) {
                continue;
            }
            DateFormat dateFormat = DateFormat.getPatternInstance(skeleton, locale);
            String dateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern();
            DateIntervalFormat intervalFormat = DateIntervalFormat.getInstance(skeleton, locale);
            DateIntervalInfo intervalInfo = intervalFormat.getDateIntervalInfo();
            if (skeleton.equals(filterPattern)) {
                logln(filterPattern + " => " + intervalInfo.getRawPatterns().get(filterPattern));
            }
            DateInterval date_interval = new DateInterval(startDate, endDate);
            String interval = intervalFormat.format(date_interval);
            String formattedStart = dateFormat.format(startDate);
            String formattedEnd = dateFormat.format(endDate);
            PatternInfo patternInfo = intervalFormat.getRawPatterns().get("M");
            String firstPart = patternInfo.getFirstPart();
            String secondPart = patternInfo.getSecondPart();
            if (!matches(dateFormatPattern, firstPart, secondPart)) {
                if (logKnownIssue("11585", "incompatible pattern between date format and date interval format")) {
                    logln("For skeleton " + skeleton + "/locale " + locale + ": mismatch between date format «" + dateFormatPattern + "» and date interval format «" + firstPart + secondPart + "».");
                } else {
                    errln("For skeleton " + skeleton + "/locale " + locale + ": mismatch between date format «" + dateFormatPattern + "» and date interval format «" + firstPart + secondPart + "».");
                }
            }
            logln(locale + "\tskeleton: «" + skeleton + "»\tpattern: «" + dateFormatPattern + "»\tintervalPattern1: «" + firstPart + "»\tintervalPattern2: «" + secondPart + "»\tstartDate: «" + formattedStart + "»\tendDate: «" + formattedEnd + "»\tinterval: «" + interval + "»");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateIntervalInfo(android.icu.text.DateIntervalInfo) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) PatternInfo(android.icu.text.DateIntervalInfo.PatternInfo) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 7 with DateInterval

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

the class DateIntervalFormatTest method TestFPos_SkelWithSeconds.

@Test
public void TestFPos_SkelWithSeconds() {
    final long[] deltas = { // none
    0L, // 200 millisec
    200L, // 20 sec
    20000L, // 20 min
    1200000L, // 2 hrs
    7200000L, // 12 hrs
    43200000L, // 8 days
    691200000L, // 16 days,
    1382400000L, // 100 days
    8640000000L };
    class ExpectPosAndFormat {

        public int posBegin;

        public int posEnd;

        public String format;

        // Simple constructor
        public ExpectPosAndFormat(int pBegin, int pEnd, String fmt) {
            posBegin = pBegin;
            posEnd = pEnd;
            format = fmt;
        }
    }
    ;
    final ExpectPosAndFormat[] exp_en_HHmm = { new ExpectPosAndFormat(3, 5, "09:00"), new ExpectPosAndFormat(3, 5, "09:00"), new ExpectPosAndFormat(3, 5, "09:00"), new ExpectPosAndFormat(3, 5, "09:00 \u2013 09:20"), new ExpectPosAndFormat(3, 5, "09:00 \u2013 11:00"), new ExpectPosAndFormat(3, 5, "09:00 \u2013 21:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00 \u2013 11/28/2014, 09:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00 \u2013 12/6/2014, 09:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00 \u2013 2/28/2015, 09:00") };
    final ExpectPosAndFormat[] exp_en_HHmmss = { new ExpectPosAndFormat(3, 5, "09:00:00"), new ExpectPosAndFormat(3, 5, "09:00:00"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 09:00:20"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 09:20:00"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 11:00:00"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 21:00:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00:00 \u2013 11/28/2014, 09:00:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00:00 \u2013 12/6/2014, 09:00:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00:00 \u2013 2/28/2015, 09:00:00") };
    final ExpectPosAndFormat[] exp_en_yyMMdd = { new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14 \u2013 11/28/14"), new ExpectPosAndFormat(0, 0, "11/20/14 \u2013 12/6/14"), new ExpectPosAndFormat(0, 0, "11/20/14 \u2013 2/28/15") };
    final ExpectPosAndFormat[] exp_en_yyMMddHHmm = { new ExpectPosAndFormat(13, 15, "11/20/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 09:20"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 11:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 21:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 11/28/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 12/06/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 02/28/15, 09:00") };
    final ExpectPosAndFormat[] exp_en_yyMMddHHmmss = { new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 09:00:20"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 09:20:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 11:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 21:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 11/28/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 12/06/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 02/28/15, 09:00:00") };
    @SuppressWarnings("unused") final ExpectPosAndFormat[] exp_en_yMMMdhmmssz = { new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 9:00:20 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 9:20:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 11:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 9:00:00 PM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 Nov 28, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 Dec 6, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 Feb 28, 2015, 9:00:00 AM GMT") };
    final ExpectPosAndFormat[] exp_ja_yyMMddHHmm = { new ExpectPosAndFormat(11, 13, "14/11/20 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9\u664200\u5206\uFF5E9\u664220\u5206"), new ExpectPosAndFormat(11, 13, "14/11/20 9\u664200\u5206\uFF5E11\u664200\u5206"), new ExpectPosAndFormat(11, 13, "14/11/20 9\u664200\u5206\uFF5E21\u664200\u5206"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00\uFF5E14/11/28 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00\uFF5E14/12/06 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00\uFF5E15/02/28 9:00") };
    final ExpectPosAndFormat[] exp_ja_yyMMddHHmmss = { new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E9:00:20"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E9:20:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E11:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E21:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E14/11/28 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E14/12/06 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E15/02/28 9:00:00") };
    final ExpectPosAndFormat[] exp_ja_yMMMdHHmmss = { new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E9:00:20"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E9:20:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E11:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E21:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E2014\u5E7411\u670828\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E2014\u5E7412\u67086\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E2015\u5E742\u670828\u65E5 9:00:00") };
    class LocaleAndSkeletonItem {

        public String locale;

        public String skeleton;

        public int fieldToCheck;

        public ExpectPosAndFormat[] expected;

        // Simple constructor
        public LocaleAndSkeletonItem(String loc, String skel, int field, ExpectPosAndFormat[] exp) {
            locale = loc;
            skeleton = skel;
            fieldToCheck = field;
            expected = exp;
        }
    }
    ;
    final LocaleAndSkeletonItem[] locSkelItems = { new LocaleAndSkeletonItem("en", "HHmm", DateFormat.MINUTE_FIELD, exp_en_HHmm), new LocaleAndSkeletonItem("en", "HHmmss", DateFormat.MINUTE_FIELD, exp_en_HHmmss), new LocaleAndSkeletonItem("en", "yyMMdd", DateFormat.MINUTE_FIELD, exp_en_yyMMdd), new LocaleAndSkeletonItem("en", "yyMMddHHmm", DateFormat.MINUTE_FIELD, exp_en_yyMMddHHmm), new LocaleAndSkeletonItem("en", "yyMMddHHmmss", DateFormat.MINUTE_FIELD, exp_en_yyMMddHHmmss), // new LocaleAndSkeletonItem( "en",		"yMMMdhmmssz",  DateFormat.MINUTE_FIELD, exp_en_yMMMdhmmssz ),
    new LocaleAndSkeletonItem("ja", "yyMMddHHmm", DateFormat.MINUTE_FIELD, exp_ja_yyMMddHHmm), new LocaleAndSkeletonItem("ja", "yyMMddHHmmss", DateFormat.MINUTE_FIELD, exp_ja_yyMMddHHmmss), new LocaleAndSkeletonItem("ja", "yMMMdHHmmss", DateFormat.MINUTE_FIELD, exp_ja_yMMMdHHmmss) };
    // final String zoneGMT = "GMT";
    // 2014 Nov 20 09:00 GMT
    final long startTimeGMT = 1416474000000L;
    TimeZone localZone = TimeZone.getDefault();
    long startTime = startTimeGMT - localZone.getOffset(startTimeGMT);
    for (LocaleAndSkeletonItem item : locSkelItems) {
        DateIntervalFormat difmt = DateIntervalFormat.getInstance(item.skeleton, new ULocale(item.locale));
        int dIdx, dCount = deltas.length;
        for (dIdx = 0; dIdx < dCount; dIdx++) {
            DateInterval di = new DateInterval(startTime, startTime + deltas[dIdx]);
            StringBuffer actual = new StringBuffer(64);
            FieldPosition pos = new FieldPosition(item.fieldToCheck);
            String actualString = difmt.format(di, actual, pos).toString();
            ExpectPosAndFormat expectPosFmt = item.expected[dIdx];
            if (!actualString.equals(expectPosFmt.format) || pos.getBeginIndex() != expectPosFmt.posBegin || pos.getEndIndex() != expectPosFmt.posEnd) {
                errln("For locale " + item.locale + ", skeleton " + item.skeleton + ", delta " + deltas[dIdx] + ": expect " + expectPosFmt.posBegin + "-" + expectPosFmt.posEnd + " \"" + expectPosFmt.format + "\"; get " + pos.getBeginIndex() + "-" + pos.getEndIndex() + " \"" + actualString + "\"");
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) FieldPosition(java.text.FieldPosition) TimeZone(android.icu.util.TimeZone) Test(org.junit.Test)

Example 8 with DateInterval

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

the class DateIntervalFormatTest method TestDateIntervalFormatCoverage.

@Test
public void TestDateIntervalFormatCoverage() throws Exception {
    long date1 = 1299090600000L;
    long date2 = 1299115800000L;
    DateInterval dtitv = new DateInterval(date1, date2);
    DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("MMMd", Locale.ENGLISH);
    DateIntervalInfo dtintinf = new DateIntervalInfo(ULocale.ENGLISH);
    // Check the default private constructor
    checkDefaultPrivateConstructor(DateIntervalFormat.class);
    // Check clone
    DateIntervalFormat dtitvfmtClone = (DateIntervalFormat) dtitvfmt.clone();
    assertEquals("DateIntervalFormat.clone() failed", dtitvfmt.format(dtitv), dtitvfmtClone.format(dtitv));
    // Coverage for getInstance
    assertNotNull("Expected DateIntervalFormat object", DateIntervalFormat.getInstance("MMMd", dtintinf));
    assertNotNull("Expected DateIntervalFormat object", DateIntervalFormat.getInstance("MMMdHHmm", Locale.ENGLISH, dtintinf));
    // Coverage for parseObject. Exception expected.
    try {
        dtitvfmt.parseObject("", new ParsePosition(0));
        errln("Exception was expected when calling DateIntervalFormat.parseObject()");
    } catch (Exception e) {
    /* No op */
    }
    // Check getPatterns()
    Output<String> secondPart = new Output<String>();
    Calendar fromCalendar = Calendar.getInstance(Locale.ENGLISH);
    fromCalendar.set(2016, 5, 22);
    Calendar toCalendar = Calendar.getInstance(Locale.ENGLISH);
    toCalendar.set(2016, 5, 23);
    assertEquals("Date interval pattern mismatch.", dtitvfmt.getPatterns(fromCalendar, toCalendar, secondPart), "MMM d – ");
    assertEquals("Date interval pattern mismatch.", secondPart.value, "d");
}
Also used : DateIntervalInfo(android.icu.text.DateIntervalInfo) Output(android.icu.util.Output) Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) ParseException(java.text.ParseException) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 9 with DateInterval

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

the class DateIntervalFormatTest method stress.

private void stress(String[] data, int data_length, Locale loc, String locName) {
    String[] skeleton = { "EEEEdMMMMy", "dMMMMy", "dMMMM", "MMMMy", "EEEEdMMMM", "EEEdMMMy", "dMMMy", "dMMM", "MMMy", "EEEdMMM", "EEEdMy", "dMy", "dM", "My", "EEEdM", "d", "EEEd", "y", "M", "MMM", "MMMM", "hm", "hmv", "hmz", "h", "hv", "hz", // following could be normalized
    "EEddMMyyyy", "EddMMy", "hhmm", "hhmmzz", // following could not be normalized
    "hms", "dMMMMMy", "EEEEEdM" };
    int i = 0;
    SimpleDateFormat ref = new SimpleDateFormat(data[i++], loc);
    while (i < data_length) {
        // 'f'
        String datestr = data[i++];
        String datestr_2 = data[i++];
        Date date;
        Date date_2;
        try {
            date = ref.parse(datestr);
            date_2 = ref.parse(datestr_2);
        } catch (ParseException e) {
            errln("parse exception" + e);
            continue;
        }
        DateInterval dtitv = new DateInterval(date.getTime(), date_2.getTime());
        for (int skeletonIndex = 0; skeletonIndex < skeleton.length; ++skeletonIndex) {
            String oneSkeleton = skeleton[skeletonIndex];
            // need special handle of "Thai" since the default calendar
            // of "Thai" is "Budd", not "Gregorian".
            DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance(oneSkeleton, loc);
            /*
                if ( locName.equals("th") ) {
                    // reset calendar to be Gregorian
                    GregorianCalendar gregCal = new GregorianCalendar(loc);
                    DateFormat dformat = dtitvfmt.getDateFormat();
                    DateFormat newOne = (DateFormat)dformat.clone();
                    newOne.setCalendar(gregCal);
                    dtitvfmt.setDateFormat(newOne);
                }
                 */
            dtitvfmt.format(dtitv);
        }
        // test interval format by algorithm
        for (int style = DateFormat.FULL; style < 4; ++style) {
            SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateInstance(style, loc);
            FieldPosition pos = new FieldPosition(0);
            StringBuffer str = new StringBuffer("");
            Calendar fromCalendar = (Calendar) dtfmt.getCalendar().clone();
            Calendar toCalendar = (Calendar) dtfmt.getCalendar().clone();
            fromCalendar.setTimeInMillis(dtitv.getFromDate());
            toCalendar.setTimeInMillis(dtitv.getToDate());
            dtfmt.intervalFormatByAlgorithm(fromCalendar, toCalendar, str, pos);
        }
    }
}
Also used : Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) ParseException(java.text.ParseException) DateInterval(android.icu.util.DateInterval) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date)

Example 10 with DateInterval

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

the class DateIntervalFormatTest method testGetInstance_String_Locale_DateIntervalInfo.

@Test
public void testGetInstance_String_Locale_DateIntervalInfo() {
    DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(new ULocale("ca"));
    DateIntervalFormat dateIntervalFormat = DateIntervalFormat.getInstance(DateFormat.YEAR_MONTH, Locale.GERMAN, dateIntervalInfo);
    Calendar from = Calendar.getInstance();
    from.set(2000, Calendar.JANUARY, 1, 12, 0);
    Calendar to = Calendar.getInstance();
    to.set(2001, Calendar.FEBRUARY, 1, 12, 0);
    DateInterval interval = new DateInterval(from.getTimeInMillis(), to.getTimeInMillis());
    dateIntervalFormat.setTimeZone(from.getTimeZone());
    // Month names are German, format is Catalan
    assertEquals("Wrong date interval", "Januar de 2000 – Februar de 2001", dateIntervalFormat.format(interval));
}
Also used : ULocale(android.icu.util.ULocale) DateIntervalInfo(android.icu.text.DateIntervalInfo) Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) Test(org.junit.Test)

Aggregations

DateIntervalFormat (android.icu.text.DateIntervalFormat)11 DateInterval (android.icu.util.DateInterval)11 ULocale (android.icu.util.ULocale)7 Test (org.junit.Test)7 DateIntervalInfo (android.icu.text.DateIntervalInfo)6 Calendar (android.icu.util.Calendar)6 SimpleDateFormat (android.icu.text.SimpleDateFormat)5 FieldPosition (java.text.FieldPosition)5 ParseException (java.text.ParseException)5 Date (java.util.Date)4 TimeZone (android.icu.util.TimeZone)3 DateFormat (android.icu.text.DateFormat)2 PatternInfo (android.icu.text.DateIntervalInfo.PatternInfo)1 Output (android.icu.util.Output)1 ParsePosition (java.text.ParsePosition)1 ArrayList (java.util.ArrayList)1