use of android.icu.text.TimeZoneFormat in project j2objc by google.
the class DateFormatTest method TestGenericTime.
@Test
public void TestGenericTime() {
// any zone pattern should parse any zone
Locale en = new Locale("en", "", "");
String[] ZDATA = { "yyyy MM dd HH:mm zzz", // round trip
"y/M/d H:mm zzzz", "F", "2004 01 01 01:00 PST", "2004/1/1 1:00 Pacific Standard Time", "y/M/d H:mm zzz", "F", "2004 01 01 01:00 PST", "2004/1/1 1:00 PST", "y/M/d H:mm vvvv", "F", "2004 01 01 01:00 PST", "2004/1/1 1:00 Pacific Time", "y/M/d H:mm v", "F", "2004 01 01 01:00 PST", "2004/1/1 1:00 PT", // non-generic timezone string influences dst offset even if wrong for date/time
"y/M/d H:mm zzz", "pf", "2004/1/1 1:00 PDT", "2004 01 01 01:00 PDT", "2004/1/1 0:00 PST", "y/M/d H:mm vvvv", "pf", "2004/1/1 1:00 PDT", "2004 01 01 01:00 PDT", "2004/1/1 0:00 Pacific Time", "y/M/d H:mm zzz", "pf", "2004/7/1 1:00 PST", "2004 07 01 02:00 PDT", "2004/7/1 2:00 PDT", "y/M/d H:mm vvvv", "pf", "2004/7/1 1:00 PST", "2004 07 01 02:00 PDT", "2004/7/1 2:00 Pacific Time", // generic timezone generates dst offset appropriate for local time
"y/M/d H:mm zzz", "pf", "2004/1/1 1:00 PT", "2004 01 01 01:00 PST", "2004/1/1 1:00 PST", "y/M/d H:mm vvvv", "pf", "2004/1/1 1:00 PT", "2004 01 01 01:00 PST", "2004/1/1 1:00 Pacific Time", "y/M/d H:mm zzz", "pf", "2004/7/1 1:00 PT", "2004 07 01 01:00 PDT", "2004/7/1 1:00 PDT", "y/M/d H:mm vvvv", "pf", "2004/7/1 1:00 PT", "2004 07 01 01:00 PDT", "2004/7/1 1:00 Pacific Time", // time to parse does not really exist, PT interpreted as earlier time
"y/M/d H:mm zzz", "pf", "2005/4/3 2:30 PT", "2005 04 03 03:30 PDT", "2005/4/3 3:30 PDT", "y/M/d H:mm zzz", "pf", "2005/4/3 2:30 PST", "2005 04 03 03:30 PDT", "2005/4/3 3:30 PDT", "y/M/d H:mm zzz", "pf", "2005/4/3 2:30 PDT", "2005 04 03 01:30 PST", "2005/4/3 1:30 PST", "y/M/d H:mm v", "pf", "2005/4/3 2:30 PT", "2005 04 03 03:30 PDT", "2005/4/3 3:30 PT", "y/M/d H:mm v", "pf", "2005/4/3 2:30 PST", "2005 04 03 03:30 PDT", "2005/4/3 3:30 PT", "y/M/d H:mm v", "pf", "2005/4/3 2:30 PDT", "2005 04 03 01:30 PST", "2005/4/3 1:30 PT", "y/M/d H:mm", "pf", "2005/4/3 2:30", "2005 04 03 03:30 PDT", "2005/4/3 3:30", // time to parse is ambiguous, PT interpreted as later time
"y/M/d H:mm zzz", "pf", "2005/10/30 1:30 PT", "2005 10 30 01:30 PST", "2005/10/30 1:30 PST", "y/M/d H:mm v", "pf", "2005/10/30 1:30 PT", "2005 10 30 01:30 PST", "2005/10/30 1:30 PT", "y/M/d H:mm", "pf", "2005/10/30 1:30 PT", "2005 10 30 01:30 PST", "2005/10/30 1:30", "y/M/d H:mm zzz", "pf", "2004/10/31 1:30 PT", "2004 10 31 01:30 PST", "2004/10/31 1:30 PST", "y/M/d H:mm zzz", "pf", "2004/10/31 1:30 PST", "2004 10 31 01:30 PST", "2004/10/31 1:30 PST", "y/M/d H:mm zzz", "pf", "2004/10/31 1:30 PDT", "2004 10 31 01:30 PDT", "2004/10/31 1:30 PDT", "y/M/d H:mm v", "pf", "2004/10/31 1:30 PT", "2004 10 31 01:30 PST", "2004/10/31 1:30 PT", "y/M/d H:mm v", "pf", "2004/10/31 1:30 PST", "2004 10 31 01:30 PST", "2004/10/31 1:30 PT", "y/M/d H:mm v", "pf", "2004/10/31 1:30 PDT", "2004 10 31 01:30 PDT", "2004/10/31 1:30 PT", "y/M/d H:mm", "pf", "2004/10/31 1:30", "2004 10 31 01:30 PST", "2004/10/31 1:30" // Below is actually an invalid test case. See the note in #5910. Disable the case for now.
// TODO: Revisit after 3.8
// "y/M/d H:mm vvvv", "pf", "2004/10/31 1:30 Argentina Time", "2004 10 30 21:30 PDT", "2004/10/31 1:30 Argentina Time",
};
expect(ZDATA, en, true);
logln("cross format/parse tests");
final String basepat = "yy/MM/dd H:mm ";
final SimpleDateFormat[] formats = { new SimpleDateFormat(basepat + "v", en), new SimpleDateFormat(basepat + "vvvv", en), new SimpleDateFormat(basepat + "zzz", en), new SimpleDateFormat(basepat + "zzzz", en) };
final SimpleDateFormat univ = new SimpleDateFormat("yyyy MM dd HH:mm zzz", en);
// To allow cross pattern parsing, we need to set ParseOption.ALL_STYLES
TimeZoneFormat tzfmt = univ.getTimeZoneFormat().cloneAsThawed();
tzfmt.setDefaultParseOptions(EnumSet.of(ParseOption.ALL_STYLES));
tzfmt.freeze();
univ.setTimeZoneFormat(tzfmt);
for (SimpleDateFormat sdf : formats) {
sdf.setTimeZoneFormat(tzfmt);
}
final String[] times = { "2004 01 02 03:04 PST", "2004 07 08 09:10 PDT" };
for (int i = 0; i < times.length; ++i) {
try {
Date d = univ.parse(times[i]);
logln("time: " + d);
for (int j = 0; j < formats.length; ++j) {
String test = formats[j].format(d);
logln("test: '" + test + "'");
for (int k = 0; k < formats.length; ++k) {
try {
Date t = formats[k].parse(test);
if (!d.equals(t)) {
errln("format " + k + " incorrectly parsed output of format " + j + " (" + test + "), returned " + t + " instead of " + d);
} else {
logln("format " + k + " parsed ok");
}
} catch (ParseException e) {
errln("format " + k + " could not parse output of format " + j + " (" + test + ")");
}
}
}
} catch (ParseException e) {
errln("univ could not parse: " + times[i]);
}
}
}
use of android.icu.text.TimeZoneFormat in project j2objc by google.
the class TimeZoneFormatTest method TestParse.
@Test
public void TestParse() {
final Object[][] DATA = { // parseOptions expected outpos time type
{ "Z", 0, "en_US", Style.ISO_EXTENDED_FULL, null, "Etc/GMT", 1, TimeType.UNKNOWN }, { "Z", 0, "en_US", Style.SPECIFIC_LONG, null, "Etc/GMT", 1, TimeType.UNKNOWN }, { "Zambia time", 0, "en_US", Style.ISO_EXTENDED_FULL, EnumSet.of(ParseOption.ALL_STYLES), "Etc/GMT", 1, TimeType.UNKNOWN }, { "Zambia time", 0, "en_US", Style.GENERIC_LOCATION, null, "Africa/Lusaka", 11, TimeType.UNKNOWN }, { "Zambia time", 0, "en_US", Style.ISO_BASIC_LOCAL_FULL, EnumSet.of(ParseOption.ALL_STYLES), "Africa/Lusaka", 11, TimeType.UNKNOWN }, { "+00:00", 0, "en_US", Style.ISO_EXTENDED_FULL, null, "Etc/GMT", 6, TimeType.UNKNOWN }, { "-01:30:45", 0, "en_US", Style.ISO_EXTENDED_FULL, null, "GMT-01:30:45", 9, TimeType.UNKNOWN }, { "-7", 0, "en_US", Style.ISO_BASIC_LOCAL_FULL, null, "GMT-07:00", 2, TimeType.UNKNOWN }, { "-2222", 0, "en_US", Style.ISO_BASIC_LOCAL_FULL, null, "GMT-22:22", 5, TimeType.UNKNOWN }, { "-3333", 0, "en_US", Style.ISO_BASIC_LOCAL_FULL, null, "GMT-03:33", 4, TimeType.UNKNOWN }, { "XXX+01:30YYY", 3, "en_US", Style.LOCALIZED_GMT, null, "GMT+01:30", 9, TimeType.UNKNOWN }, { "GMT0", 0, "en_US", Style.SPECIFIC_SHORT, null, "Etc/GMT", 3, TimeType.UNKNOWN }, { "EST", 0, "en_US", Style.SPECIFIC_SHORT, null, "America/New_York", 3, TimeType.STANDARD }, { "ESTx", 0, "en_US", Style.SPECIFIC_SHORT, null, "America/New_York", 3, TimeType.STANDARD }, { "EDTx", 0, "en_US", Style.SPECIFIC_SHORT, null, "America/New_York", 3, TimeType.DAYLIGHT }, { "EST", 0, "en_US", Style.SPECIFIC_LONG, null, null, 0, TimeType.UNKNOWN }, { "EST", 0, "en_US", Style.SPECIFIC_LONG, EnumSet.of(ParseOption.ALL_STYLES), "America/New_York", 3, TimeType.STANDARD }, { "EST", 0, "en_CA", Style.SPECIFIC_SHORT, null, "America/Toronto", 3, TimeType.STANDARD }, { "CST", 0, "en_US", Style.SPECIFIC_SHORT, null, "America/Chicago", 3, TimeType.STANDARD }, { "CST", 0, "en_GB", Style.SPECIFIC_SHORT, null, null, 0, TimeType.UNKNOWN }, { "CST", 0, "en_GB", Style.SPECIFIC_SHORT, EnumSet.of(ParseOption.TZ_DATABASE_ABBREVIATIONS), "America/Chicago", 3, TimeType.STANDARD }, { "--CST--", 2, "en_GB", Style.SPECIFIC_SHORT, EnumSet.of(ParseOption.TZ_DATABASE_ABBREVIATIONS), "America/Chicago", 5, TimeType.STANDARD }, { "CST", 0, "zh_CN", Style.SPECIFIC_SHORT, EnumSet.of(ParseOption.TZ_DATABASE_ABBREVIATIONS), "Asia/Shanghai", 3, TimeType.STANDARD }, { "AEST", 0, "en_AU", Style.SPECIFIC_SHORT, EnumSet.of(ParseOption.TZ_DATABASE_ABBREVIATIONS), "Australia/Sydney", 4, TimeType.STANDARD }, { "AST", 0, "ar_SA", Style.SPECIFIC_SHORT, EnumSet.of(ParseOption.TZ_DATABASE_ABBREVIATIONS), "Asia/Riyadh", 3, TimeType.STANDARD }, { "AQTST", 0, "en", Style.SPECIFIC_LONG, null, null, 0, TimeType.UNKNOWN }, { "AQTST", 0, "en", Style.SPECIFIC_LONG, EnumSet.of(ParseOption.ALL_STYLES), null, 0, TimeType.UNKNOWN }, { "AQTST", 0, "en", Style.SPECIFIC_LONG, EnumSet.of(ParseOption.ALL_STYLES, ParseOption.TZ_DATABASE_ABBREVIATIONS), "Asia/Aqtobe", 5, TimeType.DAYLIGHT }, { "hora de verano británica", 0, "es", Style.SPECIFIC_LONG, null, "Europe/London", 24, TimeType.DAYLIGHT } };
for (Object[] test : DATA) {
String text = (String) test[0];
int inPos = (Integer) test[1];
ULocale loc = new ULocale((String) test[2]);
Style style = (Style) test[3];
EnumSet<ParseOption> options = (EnumSet<ParseOption>) test[4];
String expID = (String) test[5];
int expPos = (Integer) test[6];
TimeType expType = (TimeType) test[7];
TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(loc);
Output<TimeType> timeType = new Output<TimeType>(TimeType.UNKNOWN);
ParsePosition pos = new ParsePosition(inPos);
TimeZone tz = tzfmt.parse(style, text, pos, options, timeType);
String errMsg = null;
if (tz == null) {
if (expID != null) {
errMsg = "Parse failure - expected: " + expID;
}
} else if (!tz.getID().equals(expID)) {
errMsg = "Time zone ID: " + tz.getID() + " - expected: " + expID;
} else if (pos.getIndex() != expPos) {
errMsg = "Parsed pos: " + pos.getIndex() + " - expected: " + expPos;
} else if (timeType.value != expType) {
errMsg = "Time type: " + timeType + " - expected: " + expType;
}
if (errMsg != null) {
errln("Fail: " + errMsg + " [text=" + text + ", pos=" + inPos + ", locale=" + loc + ", style=" + style + "]");
}
}
}
use of android.icu.text.TimeZoneFormat in project j2objc by google.
the class TimeZoneFormatTest method TestAPI.
// Basic get/set test for methods not being called otherwise.
@Test
public void TestAPI() {
TimeZoneFormat tzfmtEn = TimeZoneFormat.getInstance(ULocale.ENGLISH);
TimeZoneFormat tzfmtAr = TimeZoneFormat.getInstance(new ULocale("ar")).cloneAsThawed();
TimeZoneNames tzn = TimeZoneNames.getInstance(Locale.ENGLISH);
String digits = tzfmtEn.getGMTOffsetDigits();
tzfmtAr.setGMTOffsetDigits(digits);
if (!digits.equals(tzfmtAr.getGMTOffsetDigits())) {
errln("ERROR: get/set GMTOffsetDigits failed");
}
String pattern = tzfmtEn.getGMTOffsetPattern(GMTOffsetPatternType.POSITIVE_H);
tzfmtAr.setGMTOffsetPattern(GMTOffsetPatternType.POSITIVE_H, pattern);
if (!pattern.equals(tzfmtAr.getGMTOffsetPattern(GMTOffsetPatternType.POSITIVE_H))) {
errln("ERROR: get/set GMTOffsetPattern failed");
}
String zeroFmt = tzfmtEn.getGMTZeroFormat();
tzfmtAr.setGMTZeroFormat(zeroFmt);
if (!zeroFmt.equals(tzfmtAr.getGMTZeroFormat())) {
errln("ERROR: get/set GMTZeroFormat failed");
}
Set<String> allAvailableMZIDs = tzn.getAvailableMetaZoneIDs();
if (allAvailableMZIDs.size() < 150 || !allAvailableMZIDs.contains("America_Central")) {
errln("ERROR: getAvailableMetaZoneIDs() did not return expected value");
}
Set<String> kinshasaAvailableMZIDs = tzn.getAvailableMetaZoneIDs("Africa/Kinshasa");
if (!kinshasaAvailableMZIDs.contains("Africa_Western") || kinshasaAvailableMZIDs.contains("America_Central")) {
errln("ERROR: getAvailableMetaZoneIDs('Africa/Kinshasa') did not return expected value");
}
try {
new TimeZoneNames.MatchInfo(null, null, null, -1);
assertTrue("MatchInfo doesn't throw IllegalArgumentException", false);
} catch (IllegalArgumentException e) {
assertEquals("MatchInfo constructor exception", "nameType is null", e.getMessage());
}
try {
new TimeZoneNames.MatchInfo(NameType.LONG_GENERIC, null, null, -1);
assertTrue("MatchInfo doesn't throw IllegalArgumentException", false);
} catch (IllegalArgumentException e) {
assertEquals("MatchInfo constructor exception", "Either tzID or mzID must be available", e.getMessage());
}
try {
new TimeZoneNames.MatchInfo(NameType.LONG_GENERIC, "America/Chicago", null, -1);
assertTrue("MatchInfo doesn't throw IllegalArgumentException", false);
} catch (IllegalArgumentException e) {
assertEquals("MatchInfo constructor exception", "matchLength must be positive value", e.getMessage());
}
}
use of android.icu.text.TimeZoneFormat in project j2objc by google.
the class TimeZone method _getDisplayName.
/**
* internal version (which is called by public APIs) accepts
* SHORT, LONG, SHORT_GENERIC, LONG_GENERIC, SHORT_GMT, LONG_GMT,
* SHORT_COMMONLY_USED and GENERIC_LOCATION.
*/
private String _getDisplayName(int style, boolean daylight, ULocale locale) {
if (locale == null) {
throw new NullPointerException("locale is null");
}
String result = null;
if (style == GENERIC_LOCATION || style == LONG_GENERIC || style == SHORT_GENERIC) {
// Generic format
TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale);
long date = System.currentTimeMillis();
Output<TimeType> timeType = new Output<TimeType>(TimeType.UNKNOWN);
switch(style) {
case GENERIC_LOCATION:
result = tzfmt.format(Style.GENERIC_LOCATION, this, date, timeType);
break;
case LONG_GENERIC:
result = tzfmt.format(Style.GENERIC_LONG, this, date, timeType);
break;
case SHORT_GENERIC:
result = tzfmt.format(Style.GENERIC_SHORT, this, date, timeType);
break;
}
// appropriate for the requested daylight value.
if (daylight && timeType.value == TimeType.STANDARD || !daylight && timeType.value == TimeType.DAYLIGHT) {
int offset = daylight ? getRawOffset() + getDSTSavings() : getRawOffset();
result = (style == SHORT_GENERIC) ? tzfmt.formatOffsetShortLocalizedGMT(offset) : tzfmt.formatOffsetLocalizedGMT(offset);
}
} else if (style == LONG_GMT || style == SHORT_GMT) {
// Offset format
TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale);
int offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset();
switch(style) {
case LONG_GMT:
result = tzfmt.formatOffsetLocalizedGMT(offset);
break;
case SHORT_GMT:
result = tzfmt.formatOffsetISO8601Basic(offset, false, false, false);
break;
}
} else {
// Specific format
assert (style == LONG || style == SHORT || style == SHORT_COMMONLY_USED);
// Gets the name directly from TimeZoneNames
long date = System.currentTimeMillis();
TimeZoneNames tznames = TimeZoneNames.getInstance(locale);
NameType nameType = null;
switch(style) {
case LONG:
nameType = daylight ? NameType.LONG_DAYLIGHT : NameType.LONG_STANDARD;
break;
case SHORT:
case SHORT_COMMONLY_USED:
nameType = daylight ? NameType.SHORT_DAYLIGHT : NameType.SHORT_STANDARD;
break;
}
result = tznames.getDisplayName(ZoneMeta.getCanonicalCLDRID(this), nameType, date);
if (result == null) {
// Fallback to localized GMT
TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(locale);
int offset = daylight && useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset();
result = (style == LONG) ? tzfmt.formatOffsetLocalizedGMT(offset) : tzfmt.formatOffsetShortLocalizedGMT(offset);
}
}
assert (result != null);
return result;
}
use of android.icu.text.TimeZoneFormat in project j2objc by google.
the class DateFormatTest method expect.
void expect(String[] data, Locale loc, boolean parseAllTZStyles) {
int i = 1;
SimpleDateFormat univ = new SimpleDateFormat("EE G yyyy MM dd HH:mm:ss.SSS zzz", loc);
String currentPat = null;
SimpleDateFormat ref = new SimpleDateFormat(data[0], loc);
while (i < data.length) {
SimpleDateFormat fmt = new SimpleDateFormat("", loc);
if (parseAllTZStyles) {
TimeZoneFormat tzfmt = fmt.getTimeZoneFormat().cloneAsThawed();
tzfmt.setDefaultParseOptions(EnumSet.of(ParseOption.ALL_STYLES)).freeze();
fmt.setTimeZoneFormat(tzfmt);
}
String pattern = data[i++];
if (pattern != null) {
fmt.applyPattern(pattern);
currentPat = pattern;
}
String control = data[i++];
if (control.equals("fp") || control.equals("F")) {
// 'f'
String datestr = data[i++];
String string = data[i++];
String datestr2 = datestr;
if (control.length() == 2) {
datestr2 = data[i++];
}
Date date = null;
try {
date = ref.parse(datestr);
} catch (ParseException e) {
errln("FAIL: Internal test error; can't parse " + datestr);
continue;
}
assertEquals("\"" + currentPat + "\".format(" + datestr + ")", string, fmt.format(date));
// 'p'
if (!datestr2.equals(datestr)) {
try {
date = ref.parse(datestr2);
} catch (ParseException e2) {
errln("FAIL: Internal test error; can't parse " + datestr2);
continue;
}
}
try {
Date parsedate = fmt.parse(string);
assertEquals("\"" + currentPat + "\".parse(" + string + ")", univ.format(date), univ.format(parsedate));
} catch (ParseException e3) {
errln("FAIL: \"" + currentPat + "\".parse(" + string + ") => " + e3);
continue;
}
} else if (control.equals("pf") || control.equals("P")) {
// 'p'
String string = data[i++];
String datestr = data[i++];
String string2 = string;
if (control.length() == 2) {
string2 = data[i++];
}
Date date = null;
try {
date = ref.parse(datestr);
} catch (ParseException e) {
errln("FAIL: Internal test error; can't parse " + datestr);
continue;
}
try {
Date parsedate = fmt.parse(string);
assertEquals("\"" + currentPat + "\".parse(" + string + ")", univ.format(date), univ.format(parsedate));
} catch (ParseException e2) {
errln("FAIL: \"" + currentPat + "\".parse(" + string + ") => " + e2);
continue;
}
// 'f'
assertEquals("\"" + currentPat + "\".format(" + datestr + ")", string2, fmt.format(date));
} else {
errln("FAIL: Invalid control string " + control);
return;
}
}
}
Aggregations