Search in sources :

Example 11 with RelativeDateTimeFormatter

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

the class RelativeDateTimeFormatterTest method TestJavaLocale.

@Test
public void TestJavaLocale() {
    Locale loc = Locale.US;
    double amount = 12.3456d;
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(loc);
    String s = fmt.format(amount, Direction.LAST, RelativeUnit.SECONDS);
    assertEquals("Java Locale.US", "12.346 seconds ago", s);
    // Modified instance
    NumberFormat nf = fmt.getNumberFormat();
    nf.setMaximumFractionDigits(1);
    fmt = RelativeDateTimeFormatter.getInstance(loc, nf);
    s = fmt.format(amount, Direction.LAST, RelativeUnit.SECONDS);
    assertEquals("Java Locale.US", "12.3 seconds ago", s);
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RelativeDateTimeFormatter(android.icu.text.RelativeDateTimeFormatter) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 12 with RelativeDateTimeFormatter

use of android.icu.text.RelativeDateTimeFormatter 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)

Example 13 with RelativeDateTimeFormatter

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

the class RelativeDateTimeFormatterTest method TestRelativeDateWithoutQuantityShort.

@Test
public void TestRelativeDateWithoutQuantityShort() {
    Object[][] data = { { Direction.NEXT_2, AbsoluteUnit.DAY, null }, { Direction.NEXT, AbsoluteUnit.DAY, "tomorrow" }, { Direction.NEXT, AbsoluteUnit.WEEK, "next wk." }, { Direction.NEXT, AbsoluteUnit.MONTH, "next mo." }, { Direction.NEXT, AbsoluteUnit.YEAR, "next yr." }, { Direction.NEXT, AbsoluteUnit.MONDAY, "next Mon." }, { Direction.NEXT, AbsoluteUnit.TUESDAY, "next Tue." }, { Direction.NEXT, AbsoluteUnit.WEDNESDAY, "next Wed." }, { Direction.NEXT, AbsoluteUnit.THURSDAY, "next Thu." }, { Direction.NEXT, AbsoluteUnit.FRIDAY, "next Fri." }, { Direction.NEXT, AbsoluteUnit.SATURDAY, "next Sat." }, { Direction.NEXT, AbsoluteUnit.SUNDAY, "next Sun." }, { Direction.LAST_2, AbsoluteUnit.DAY, null }, { Direction.LAST, AbsoluteUnit.DAY, "yesterday" }, { Direction.LAST, AbsoluteUnit.WEEK, "last wk." }, { Direction.LAST, AbsoluteUnit.MONTH, "last mo." }, { Direction.LAST, AbsoluteUnit.YEAR, "last yr." }, { Direction.LAST, AbsoluteUnit.MONDAY, "last Mon." }, { Direction.LAST, AbsoluteUnit.TUESDAY, "last Tue." }, { Direction.LAST, AbsoluteUnit.WEDNESDAY, "last Wed." }, { Direction.LAST, AbsoluteUnit.THURSDAY, "last Thu." }, { Direction.LAST, AbsoluteUnit.FRIDAY, "last Fri." }, { Direction.LAST, AbsoluteUnit.SATURDAY, "last Sat." }, { Direction.LAST, AbsoluteUnit.SUNDAY, "last Sun." }, { Direction.THIS, AbsoluteUnit.DAY, "today" }, { Direction.THIS, AbsoluteUnit.WEEK, "this wk." }, { Direction.THIS, AbsoluteUnit.MONTH, "this mo." }, { Direction.THIS, AbsoluteUnit.YEAR, "this yr." }, { Direction.THIS, AbsoluteUnit.MONDAY, "this Mon." }, { Direction.THIS, AbsoluteUnit.TUESDAY, "this Tue." }, { Direction.THIS, AbsoluteUnit.WEDNESDAY, "this Wed." }, { Direction.THIS, AbsoluteUnit.THURSDAY, "this Thu." }, { Direction.THIS, AbsoluteUnit.FRIDAY, "this Fri." }, { Direction.THIS, AbsoluteUnit.SATURDAY, "this Sat." }, { Direction.THIS, AbsoluteUnit.SUNDAY, "this Sun." }, { Direction.PLAIN, AbsoluteUnit.DAY, "day" }, { Direction.PLAIN, AbsoluteUnit.WEEK, "wk." }, { Direction.PLAIN, AbsoluteUnit.MONTH, "mo." }, { Direction.PLAIN, AbsoluteUnit.YEAR, "yr." }, { Direction.PLAIN, AbsoluteUnit.MONDAY, "Mo" }, { Direction.PLAIN, AbsoluteUnit.TUESDAY, "Tu" }, { Direction.PLAIN, AbsoluteUnit.WEDNESDAY, "We" }, { Direction.PLAIN, AbsoluteUnit.THURSDAY, "Th" }, { Direction.PLAIN, AbsoluteUnit.FRIDAY, "Fr" }, { Direction.PLAIN, AbsoluteUnit.SATURDAY, "Sa" }, { Direction.PLAIN, AbsoluteUnit.SUNDAY, "Su" }, { Direction.PLAIN, AbsoluteUnit.NOW, "now" } };
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(new ULocale("en_US"), null, RelativeDateTimeFormatter.Style.SHORT, DisplayContext.CAPITALIZATION_NONE);
    for (Object[] row : data) {
        String actual = fmt.format((Direction) row[0], (AbsoluteUnit) row[1]);
        assertEquals("Relative date without quantity short", row[2], actual);
    }
}
Also used : ULocale(android.icu.util.ULocale) RelativeDateTimeFormatter(android.icu.text.RelativeDateTimeFormatter) Test(org.junit.Test)

Example 14 with RelativeDateTimeFormatter

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

the class RelativeDateTimeFormatterTest method TestRelativeDateWithQuantitySrFallback.

@Test
public void TestRelativeDateWithQuantitySrFallback() {
    Object[][] data = { { 0.0, Direction.NEXT, RelativeUnit.MONTHS, "\u0437\u0430 0 \u043C." }, { 1.2, Direction.NEXT, RelativeUnit.MONTHS, "\u0437\u0430 1,2 \u043C." }, { 21.0, Direction.NEXT, RelativeUnit.MONTHS, "\u0437\u0430 21 \u043C." } };
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(new ULocale("sr"), null, RelativeDateTimeFormatter.Style.NARROW, DisplayContext.CAPITALIZATION_NONE);
    for (Object[] row : data) {
        String actual = fmt.format(((Double) row[0]).doubleValue(), (Direction) row[1], (RelativeUnit) row[2]);
        assertEquals("Relative date with quantity fallback", row[3], actual);
    }
}
Also used : ULocale(android.icu.util.ULocale) RelativeDateTimeFormatter(android.icu.text.RelativeDateTimeFormatter) Test(org.junit.Test)

Example 15 with RelativeDateTimeFormatter

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

the class RelativeDateTimeFormatterTest method TestRelativeDateWithoutQuantityNarrow.

@Test
public void TestRelativeDateWithoutQuantityNarrow() {
    Object[][] data = { { Direction.NEXT_2, AbsoluteUnit.DAY, null }, { Direction.NEXT, AbsoluteUnit.DAY, "tomorrow" }, { Direction.NEXT, AbsoluteUnit.WEEK, "next wk." }, { Direction.NEXT, AbsoluteUnit.MONTH, "next mo." }, { Direction.NEXT, AbsoluteUnit.YEAR, "next yr." }, { Direction.NEXT, AbsoluteUnit.MONDAY, "next M" }, { Direction.NEXT, AbsoluteUnit.TUESDAY, "next Tu" }, { Direction.NEXT, AbsoluteUnit.WEDNESDAY, "next W" }, { Direction.NEXT, AbsoluteUnit.THURSDAY, "next Th" }, { Direction.NEXT, AbsoluteUnit.FRIDAY, "next F" }, { Direction.NEXT, AbsoluteUnit.SATURDAY, "next Sa" }, { Direction.NEXT, AbsoluteUnit.SUNDAY, "next Su" }, { Direction.LAST_2, AbsoluteUnit.DAY, null }, { Direction.LAST, AbsoluteUnit.DAY, "yesterday" }, { Direction.LAST, AbsoluteUnit.WEEK, "last wk." }, { Direction.LAST, AbsoluteUnit.MONTH, "last mo." }, { Direction.LAST, AbsoluteUnit.YEAR, "last yr." }, { Direction.LAST, AbsoluteUnit.MONDAY, "last M" }, { Direction.LAST, AbsoluteUnit.TUESDAY, "last Tu" }, { Direction.LAST, AbsoluteUnit.WEDNESDAY, "last W" }, { Direction.LAST, AbsoluteUnit.THURSDAY, "last Th" }, { Direction.LAST, AbsoluteUnit.FRIDAY, "last F" }, { Direction.LAST, AbsoluteUnit.SATURDAY, "last Sa" }, { Direction.LAST, AbsoluteUnit.SUNDAY, "last Su" }, { Direction.THIS, AbsoluteUnit.DAY, "today" }, { Direction.THIS, AbsoluteUnit.WEEK, "this wk." }, { Direction.THIS, AbsoluteUnit.MONTH, "this mo." }, { Direction.THIS, AbsoluteUnit.YEAR, "this yr." }, { Direction.THIS, AbsoluteUnit.MONDAY, "this M" }, { Direction.THIS, AbsoluteUnit.TUESDAY, "this Tu" }, { Direction.THIS, AbsoluteUnit.WEDNESDAY, "this W" }, { Direction.THIS, AbsoluteUnit.THURSDAY, "this Th" }, { Direction.THIS, AbsoluteUnit.FRIDAY, "this F" }, { Direction.THIS, AbsoluteUnit.SATURDAY, "this Sa" }, { Direction.THIS, AbsoluteUnit.SUNDAY, "this Su" }, { Direction.PLAIN, AbsoluteUnit.DAY, "day" }, { Direction.PLAIN, AbsoluteUnit.WEEK, "wk." }, { Direction.PLAIN, AbsoluteUnit.MONTH, "mo." }, { Direction.PLAIN, AbsoluteUnit.YEAR, "yr." }, { Direction.PLAIN, AbsoluteUnit.MONDAY, "M" }, { Direction.PLAIN, AbsoluteUnit.TUESDAY, "T" }, { Direction.PLAIN, AbsoluteUnit.WEDNESDAY, "W" }, { Direction.PLAIN, AbsoluteUnit.THURSDAY, "T" }, { Direction.PLAIN, AbsoluteUnit.FRIDAY, "F" }, { Direction.PLAIN, AbsoluteUnit.SATURDAY, "S" }, { Direction.PLAIN, AbsoluteUnit.SUNDAY, "S" }, { Direction.PLAIN, AbsoluteUnit.NOW, "now" } };
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(new ULocale("en_US"), null, RelativeDateTimeFormatter.Style.NARROW, DisplayContext.CAPITALIZATION_NONE);
    for (Object[] row : data) {
        String actual = fmt.format((Direction) row[0], (AbsoluteUnit) row[1]);
        assertEquals("Relative date without quantity narrow", row[2], actual);
    }
}
Also used : ULocale(android.icu.util.ULocale) RelativeDateTimeFormatter(android.icu.text.RelativeDateTimeFormatter) Test(org.junit.Test)

Aggregations

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