Search in sources :

Example 1 with RelativeDateTimeUnit

use of android.icu.text.RelativeDateTimeFormatter.RelativeDateTimeUnit in project j2objc by google.

the class RelativeDateTimeFormatterTest method TestRelativeDateTimeUnitFormatters.

@Test
public void TestRelativeDateTimeUnitFormatters() {
    double[] offsets = { -5.0, -2.2, -2.0, -1.0, -0.7, 0.0, 0.7, 1.0, 2.0, 5.0 };
    String[] en_decDef_long_midSent_week = { /*  text                    numeric */
    "5 weeks ago", "5 weeks ago", /* -5   */
    "2.2 weeks ago", "2.2 weeks ago", /* -2.2 */
    "2 weeks ago", "2 weeks ago", /* -2   */
    "last week", "1 week ago", /* -1   */
    "0.7 weeks ago", "0.7 weeks ago", /* -0.7 */
    "this week", "in 0 weeks", /*  0   */
    "in 0.7 weeks", "in 0.7 weeks", /*  0.7 */
    "next week", "in 1 week", /*  1   */
    "in 2 weeks", "in 2 weeks", /*  2   */
    "in 5 weeks", "in 5 weeks" /*  5   */
    };
    String[] en_dec0_long_midSent_week = { /*  text                    numeric */
    "5 weeks ago", "5 weeks ago", /* -5   */
    "2 weeks ago", "2 weeks ago", /* -2.2 */
    "2 weeks ago", "2 weeks ago", /* -2   */
    "last week", "1 week ago", /* -1   */
    "0 weeks ago", "0 weeks ago", /* -0.7 */
    "this week", "in 0 weeks", /*  0   */
    "in 0 weeks", "in 0 weeks", /*  0.7 */
    "next week", "in 1 week", /*  1   */
    "in 2 weeks", "in 2 weeks", /*  2   */
    "in 5 weeks", "in 5 weeks" /*  5   */
    };
    String[] en_decDef_short_midSent_week = { /*  text                    numeric */
    "5 wk. ago", "5 wk. ago", /* -5   */
    "2.2 wk. ago", "2.2 wk. ago", /* -2.2 */
    "2 wk. ago", "2 wk. ago", /* -2   */
    "last wk.", "1 wk. ago", /* -1   */
    "0.7 wk. ago", "0.7 wk. ago", /* -0.7 */
    "this wk.", "in 0 wk.", /*  0   */
    "in 0.7 wk.", "in 0.7 wk.", /*  0.7 */
    "next wk.", "in 1 wk.", /*  1   */
    "in 2 wk.", "in 2 wk.", /*  2   */
    "in 5 wk.", "in 5 wk." /*  5   */
    };
    String[] en_decDef_long_midSent_min = { /*  text                    numeric */
    "5 minutes ago", "5 minutes ago", /* -5   */
    "2.2 minutes ago", "2.2 minutes ago", /* -2.2 */
    "2 minutes ago", "2 minutes ago", /* -2   */
    "1 minute ago", "1 minute ago", /* -1   */
    "0.7 minutes ago", "0.7 minutes ago", /* -0.7 */
    "in 0 minutes", "in 0 minutes", /*  0   */
    "in 0.7 minutes", "in 0.7 minutes", /*  0.7 */
    "in 1 minute", "in 1 minute", /*  1   */
    "in 2 minutes", "in 2 minutes", /*  2   */
    "in 5 minutes", "in 5 minutes" /*  5   */
    };
    String[] en_dec0_long_midSent_tues = { /*  text                    numeric */
    "", /*no data */
    "", /* -5   */
    "", /*no data */
    "", /* -2.2 */
    "", /*no data */
    "", /* -2   */
    "last Tuesday", "", /* -1   */
    "", /*no data */
    "", /* -0.7 */
    "this Tuesday", "", /*  0   */
    "", /*no data */
    "", /*  0.7 */
    "next Tuesday", "", /*  1   */
    "", /*no data */
    "", /*  2   */
    "", /*no data */
    "" /*no data */
    /*  5   */
    };
    String[] fr_decDef_long_midSent_day = { /*  text                    numeric */
    "il y a 5 jours", "il y a 5 jours", /* -5   */
    "il y a 2,2 jours", "il y a 2,2 jours", /* -2.2 */
    "avant-hier", "il y a 2 jours", /* -2   */
    "hier", "il y a 1 jour", /* -1   */
    "il y a 0,7 jour", "il y a 0,7 jour", /* -0.7 */
    "aujourd’hui", "dans 0 jour", /*  0   */
    "dans 0,7 jour", "dans 0,7 jour", /*  0.7 */
    "demain", "dans 1 jour", /*  1   */
    "après-demain", "dans 2 jours", /*  2   */
    "dans 5 jours", "dans 5 jours" /*  5   */
    };
    class TestRelativeDateTimeUnitItem {

        public String localeID;

        public int decPlaces;

        /* fixed decimal places; -1 to use default num formatter */
        public Style width;

        public DisplayContext capContext;

        public RelativeDateTimeUnit unit;

        public String[] expectedResults;

        /* for the various offsets */
        public TestRelativeDateTimeUnitItem(String locID, int decP, RelativeDateTimeFormatter.Style wid, DisplayContext capC, RelativeDateTimeUnit ut, String[] expR) {
            localeID = locID;
            decPlaces = decP;
            width = wid;
            capContext = capC;
            unit = ut;
            expectedResults = expR;
        }
    }
    ;
    final TestRelativeDateTimeUnitItem[] items = { new TestRelativeDateTimeUnitItem("en", -1, Style.LONG, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.WEEK, en_decDef_long_midSent_week), new TestRelativeDateTimeUnitItem("en", 0, Style.LONG, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.WEEK, en_dec0_long_midSent_week), new TestRelativeDateTimeUnitItem("en", -1, Style.SHORT, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.WEEK, en_decDef_short_midSent_week), new TestRelativeDateTimeUnitItem("en", -1, Style.LONG, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.MINUTE, en_decDef_long_midSent_min), new TestRelativeDateTimeUnitItem("en", -1, Style.LONG, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.TUESDAY, en_dec0_long_midSent_tues), new TestRelativeDateTimeUnitItem("fr", -1, Style.LONG, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.DAY, fr_decDef_long_midSent_day) };
    for (TestRelativeDateTimeUnitItem item : items) {
        ULocale uloc = new ULocale(item.localeID);
        NumberFormat nf = null;
        if (item.decPlaces >= 0) {
            nf = NumberFormat.getInstance(uloc, NumberFormat.NUMBERSTYLE);
            nf.setMinimumFractionDigits(item.decPlaces);
            nf.setMaximumFractionDigits(item.decPlaces);
            nf.setRoundingMode(BigDecimal.ROUND_DOWN);
        }
        RelativeDateTimeFormatter reldatefmt = RelativeDateTimeFormatter.getInstance(uloc, nf, item.width, item.capContext);
        for (int iOffset = 0; iOffset < offsets.length; iOffset++) {
            double offset = offsets[iOffset];
            if (item.unit == RelativeDateTimeUnit.TUESDAY && offset != -1.0 && offset != 0.0 && offset != 1.0) {
                continue;
            /* we do not currently have data for this */
            }
            String result = reldatefmt.format(offset, item.unit);
            assertEquals("RelativeDateTimeUnit format locale " + item.localeID + ", dec " + item.decPlaces + ", width " + item.width + ", unit " + item.unit, item.expectedResults[iOffset * 2], result);
            if (item.unit == RelativeDateTimeUnit.TUESDAY) {
                continue;
            /* we do not currently have numeric-style data for this */
            }
            result = reldatefmt.formatNumeric(offset, item.unit);
            assertEquals("RelativeDateTimeUnit formatNum locale " + item.localeID + ", dec " + item.decPlaces + ", width " + item.width + ", unit " + item.unit, item.expectedResults[iOffset * 2 + 1], result);
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DisplayContext(android.icu.text.DisplayContext) RelativeDateTimeUnit(android.icu.text.RelativeDateTimeFormatter.RelativeDateTimeUnit) RelativeDateTimeFormatter(android.icu.text.RelativeDateTimeFormatter) Style(android.icu.text.RelativeDateTimeFormatter.Style) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Aggregations

DisplayContext (android.icu.text.DisplayContext)1 NumberFormat (android.icu.text.NumberFormat)1 RelativeDateTimeFormatter (android.icu.text.RelativeDateTimeFormatter)1 RelativeDateTimeUnit (android.icu.text.RelativeDateTimeFormatter.RelativeDateTimeUnit)1 Style (android.icu.text.RelativeDateTimeFormatter.Style)1 ULocale (android.icu.util.ULocale)1 Test (org.junit.Test)1