Search in sources :

Example 6 with TimeZoneFormat

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

the class TimeZoneFormatTest method TestFormat.

@Test
public void TestFormat() {
    // 2013-01-15T00:00:00Z
    final Date dateJan = new Date(1358208000000L);
    // 2013-07-15T00:00:00Z
    final Date dateJul = new Date(1373846400000L);
    final Object[][] TESTDATA = { { "en", "America/Los_Angeles", dateJan, Style.GENERIC_LOCATION, "Los Angeles Time", TimeType.UNKNOWN }, { "en", "America/Los_Angeles", dateJan, Style.GENERIC_LONG, "Pacific Time", TimeType.UNKNOWN }, { "en", "America/Los_Angeles", dateJan, Style.SPECIFIC_LONG, "Pacific Standard Time", TimeType.STANDARD }, { "en", "America/Los_Angeles", dateJul, Style.SPECIFIC_LONG, "Pacific Daylight Time", TimeType.DAYLIGHT }, { "ja", "America/Los_Angeles", dateJan, Style.ZONE_ID, "America/Los_Angeles", TimeType.UNKNOWN }, { "fr", "America/Los_Angeles", dateJul, Style.ZONE_ID_SHORT, "uslax", TimeType.UNKNOWN }, { "en", "America/Los_Angeles", dateJan, Style.EXEMPLAR_LOCATION, "Los Angeles", TimeType.UNKNOWN }, { "ja", "Asia/Tokyo", dateJan, Style.GENERIC_LONG, // "日本標準時"
    "\u65E5\u672C\u6A19\u6E96\u6642", TimeType.UNKNOWN } };
    for (Object[] testCase : TESTDATA) {
        TimeZone tz = TimeZone.getTimeZone((String) testCase[1]);
        Output<TimeType> timeType = new Output<TimeType>();
        ULocale uloc = new ULocale((String) testCase[0]);
        TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(uloc);
        String out = tzfmt.format((Style) testCase[3], tz, ((Date) testCase[2]).getTime(), timeType);
        if (!out.equals(testCase[4]) || timeType.value != testCase[5]) {
            errln("Format result for [locale=" + testCase[0] + ",tzid=" + testCase[1] + ",date=" + testCase[2] + ",style=" + testCase[3] + "]: expected [output=" + testCase[4] + ",type=" + testCase[5] + "]; actual [output=" + out + ",type=" + timeType.value + "]");
        }
        // with equivalent Java Locale
        Locale loc = uloc.toLocale();
        tzfmt = TimeZoneFormat.getInstance(loc);
        out = tzfmt.format((Style) testCase[3], tz, ((Date) testCase[2]).getTime(), timeType);
        if (!out.equals(testCase[4]) || timeType.value != testCase[5]) {
            errln("Format result for [locale(Java)=" + testCase[0] + ",tzid=" + testCase[1] + ",date=" + testCase[2] + ",style=" + testCase[3] + "]: expected [output=" + testCase[4] + ",type=" + testCase[5] + "]; actual [output=" + out + ",type=" + timeType.value + "]");
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ULocale(android.icu.util.ULocale) TimeZoneFormat(android.icu.text.TimeZoneFormat) Date(java.util.Date) TimeType(android.icu.text.TimeZoneFormat.TimeType) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) Output(android.icu.util.Output) Style(android.icu.text.TimeZoneFormat.Style) Test(org.junit.Test)

Example 7 with TimeZoneFormat

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

the class TimeZoneFormatTest method TestFormatTZDBNames.

@Test
public void TestFormatTZDBNames() {
    // 2013-01-15T00:00:00Z
    final Date dateJan = new Date(1358208000000L);
    // 2013-07-15T00:00:00Z
    final Date dateJul = new Date(1373846400000L);
    final Object[][] TESTDATA = { { "en", "America/Chicago", dateJan, Style.SPECIFIC_SHORT, "CST", TimeType.STANDARD }, { "en", "Asia/Shanghai", dateJan, Style.SPECIFIC_SHORT, "CST", TimeType.STANDARD }, { "zh_Hans", "Asia/Shanghai", dateJan, Style.SPECIFIC_SHORT, "CST", TimeType.STANDARD }, { "en", "America/Los_Angeles", dateJul, Style.SPECIFIC_LONG, // No long display names
    "GMT-07:00", TimeType.DAYLIGHT }, { "ja", "America/Los_Angeles", dateJul, Style.SPECIFIC_SHORT, "PDT", TimeType.DAYLIGHT }, { "en", "Australia/Sydney", dateJan, Style.SPECIFIC_SHORT, "AEDT", TimeType.DAYLIGHT }, { "en", "Australia/Sydney", dateJul, Style.SPECIFIC_SHORT, "AEST", TimeType.STANDARD } };
    for (Object[] testCase : TESTDATA) {
        ULocale loc = new ULocale((String) testCase[0]);
        TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(loc).cloneAsThawed();
        TimeZoneNames tzdbNames = TimeZoneNames.getTZDBInstance(loc);
        tzfmt.setTimeZoneNames(tzdbNames);
        TimeZone tz = TimeZone.getTimeZone((String) testCase[1]);
        Output<TimeType> timeType = new Output<TimeType>();
        String out = tzfmt.format((Style) testCase[3], tz, ((Date) testCase[2]).getTime(), timeType);
        if (!out.equals(testCase[4]) || timeType.value != testCase[5]) {
            errln("Format result for [locale=" + testCase[0] + ",tzid=" + testCase[1] + ",date=" + testCase[2] + ",style=" + testCase[3] + "]: expected [output=" + testCase[4] + ",type=" + testCase[5] + "]; actual [output=" + out + ",type=" + timeType.value + "]");
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) ULocale(android.icu.util.ULocale) Output(android.icu.util.Output) TZDBTimeZoneNames(android.icu.impl.TZDBTimeZoneNames) TimeZoneNames(android.icu.text.TimeZoneNames) TimeZoneFormat(android.icu.text.TimeZoneFormat) Date(java.util.Date) TimeType(android.icu.text.TimeZoneFormat.TimeType) Test(org.junit.Test)

Example 8 with TimeZoneFormat

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

the class TimeZoneFormatTest method TestParseCoverage.

// Coverage tests for other versions of the parse() method. All of them end up
// calling the full parse() method tested on the TestParse() test.
public void TestParseCoverage() {
    TimeZone expectedTZ = TimeZone.getTimeZone("America/Los_Angeles");
    TimeZoneFormat fmt = TimeZoneFormat.getInstance(ULocale.ENGLISH);
    // Test parse(String)
    try {
        TimeZone tz1 = fmt.parse("America/Los_Angeles");
        if (tz1 == null) {
            errln("Parse failure using parse(String) - expected: " + expectedTZ.getID());
        } else if (!expectedTZ.equals(tz1)) {
            errln("Parsed TimeZone: '" + tz1.getID() + "' using parse(String) - expected: " + expectedTZ.getID());
        }
    } catch (ParseException e) {
        errln("Parse failure using parse(String) - expected: " + expectedTZ.getID() + " exception: " + e.getMessage());
    }
    // Test parse(String, ParsePosition)
    TimeZone tz2 = fmt.parse("++America/Los_Angeles", new ParsePosition(2));
    if (tz2 == null) {
        errln("Parse failure using parse(String, ParsePosition) - expected: " + expectedTZ.getID());
    } else if (!expectedTZ.equals(tz2)) {
        errln("Parsed TimeZone: '" + tz2.getID() + "' using parse(String, ParsePosition) - expected: " + expectedTZ.getID());
    }
    // Test parseObject(String, ParsePosition)
    Object tz3 = fmt.parseObject("++America/Los_Angeles", new ParsePosition(2));
    if (tz3 == null) {
        errln("Parse failure using parseObject(String, ParsePosition) - expected: " + expectedTZ.getID());
    } else if (!expectedTZ.equals(tz3)) {
        errln("Parsed TimeZone: '" + ((TimeZone) tz3).getID() + "' using parseObject(String, ParsePosition) - expected: " + expectedTZ.getID());
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) TimeZoneFormat(android.icu.text.TimeZoneFormat) ParseException(java.text.ParseException) ParsePosition(java.text.ParsePosition)

Example 9 with TimeZoneFormat

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

the class TimeZoneFormatTest method TestInheritedFormat.

// Tests format(Object, StringBuffer, FieldPosition):StringBuffer method
// inherited from Format class
public void TestInheritedFormat() {
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = Calendar.getInstance(tz);
    // Mar 28, 2016
    cal.setTimeInMillis(1459187377690L);
    StringBuffer sb = new StringBuffer();
    FieldPosition fp = new FieldPosition(DateFormat.Field.TIME_ZONE);
    TimeZoneFormat fmt = TimeZoneFormat.getInstance(ULocale.ENGLISH);
    // Test formatting a non-timezone related object
    try {
        fmt.format(new Object(), sb, fp);
        errln("ERROR: format non-timezone related object failed");
    } catch (IllegalArgumentException e) {
    /* Expected */
    }
    // Test formatting a TimeZone object
    sb = new StringBuffer();
    fmt.format(tz, sb, fp);
    // When formatting a TimeZone object the formatter uses the current date.
    String fmtOutput = tz.inDaylightTime(new Date()) ? "GMT-07:00" : "GMT-08:00";
    if (!sb.toString().equals(fmtOutput)) {
        errln("ERROR: format TimerZone object failed. Expected: " + fmtOutput + ", actual: " + sb);
    }
    // Test formatting a Calendar object
    sb = new StringBuffer();
    fmt.format(cal, sb, fp);
    if (!sb.toString().equals("GMT-07:00")) {
        errln("ERROR: format Calendar object failed. Expected: GMT-07:00, actual: " + sb);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) Calendar(android.icu.util.Calendar) TimeZoneFormat(android.icu.text.TimeZoneFormat) FieldPosition(java.text.FieldPosition) Date(java.util.Date)

Aggregations

TimeZoneFormat (android.icu.text.TimeZoneFormat)9 BasicTimeZone (android.icu.util.BasicTimeZone)5 SimpleTimeZone (android.icu.util.SimpleTimeZone)5 TimeZone (android.icu.util.TimeZone)5 ULocale (android.icu.util.ULocale)5 Date (java.util.Date)5 Test (org.junit.Test)5 TimeType (android.icu.text.TimeZoneFormat.TimeType)4 TimeZoneNames (android.icu.text.TimeZoneNames)3 Output (android.icu.util.Output)3 ParseException (java.text.ParseException)3 TZDBTimeZoneNames (android.icu.impl.TZDBTimeZoneNames)2 SimpleDateFormat (android.icu.text.SimpleDateFormat)2 Style (android.icu.text.TimeZoneFormat.Style)2 ParsePosition (java.text.ParsePosition)2 Locale (java.util.Locale)2 ParseOption (android.icu.text.TimeZoneFormat.ParseOption)1 NameType (android.icu.text.TimeZoneNames.NameType)1 Calendar (android.icu.util.Calendar)1 FieldPosition (java.text.FieldPosition)1