use of android.icu.util.TimeZone in project j2objc by google.
the class DateIntervalFormatTest method TestTicket11669.
// TestTicket11669 - Check the thread safety of DateIntervalFormat.format().
// This test failed with ICU 56.
@Test
public void TestTicket11669() {
// These final variables are accessed directly by the concurrent threads.
final DateIntervalFormat formatter = DateIntervalFormat.getInstance(DateFormat.YEAR_MONTH_DAY, ULocale.US);
final ArrayList<DateInterval> testIntervals = new ArrayList<DateInterval>();
final ArrayList<String> expectedResults = new ArrayList<String>();
// Create and save the input test data.
TimeZone tz = TimeZone.getTimeZone("Americal/Los_Angeles");
Calendar intervalStart = Calendar.getInstance(tz, ULocale.US);
Calendar intervalEnd = Calendar.getInstance(tz, ULocale.US);
intervalStart.set(2009, 6, 1);
intervalEnd.set(2009, 6, 2);
testIntervals.add(new DateInterval(intervalStart.getTimeInMillis(), intervalEnd.getTimeInMillis()));
intervalStart.set(2015, 2, 27);
intervalEnd.set(2015, 3, 1);
testIntervals.add(new DateInterval(intervalStart.getTimeInMillis(), intervalEnd.getTimeInMillis()));
// Run the formatter single-threaded to create and save the expected results.
for (DateInterval interval : testIntervals) {
FieldPosition pos = new FieldPosition(0);
StringBuffer result = new StringBuffer();
formatter.format(interval, result, pos);
expectedResults.add(result.toString());
}
class TestThread extends Thread {
public String errorMessage;
public void run() {
for (int loop = 0; loop < 2000; ++loop) {
ListIterator<String> expectedItr = expectedResults.listIterator();
for (DateInterval interval : testIntervals) {
String expected = expectedItr.next();
FieldPosition pos = new FieldPosition(0);
StringBuffer result = new StringBuffer();
formatter.format(interval, result, pos);
if (!expected.equals(result.toString())) {
// Note: The ICU test framework doesn't support reporting failures from within a sub-thread.
// Save the failure for the main thread to pick up later.
errorMessage = String.format("Expected \"%s\", actual \"%s\"", expected, result);
return;
}
}
}
}
}
List<TestThread> threads = new ArrayList<TestThread>();
for (int i = 0; i < 4; ++i) {
threads.add(new TestThread());
}
for (Thread t : threads) {
t.start();
}
for (TestThread t : threads) {
try {
t.join();
} catch (InterruptedException e) {
fail("Unexpected exception: " + e.toString());
}
if (t.errorMessage != null) {
fail(t.errorMessage);
}
}
}
use of android.icu.util.TimeZone in project j2objc by google.
the class DateIntervalFormatTest method TestGetSetTimeZone.
@Test
public void TestGetSetTimeZone() {
DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("MMMdHHmm", Locale.ENGLISH);
// 2011-Mar-02 1030 in US/Pacific, 2011-Mar-03 0330 in Asia/Tokyo
long date1 = 1299090600000L;
// 2011-Mar-02 1730 in US/Pacific, 2011-Mar-03 1030 in Asia/Tokyo
long date2 = 1299115800000L;
DateInterval dtitv = new DateInterval(date1, date2);
TimeZone tzCalif = TimeZone.getFrozenTimeZone("US/Pacific");
TimeZone tzTokyo = TimeZone.getFrozenTimeZone("Asia/Tokyo");
// ICU4C result is "Mar 2, 10:30 \u2013 17:30" (does not duplicate day)
String fmtCalif = "Mar 2, 10:30 \u2013 17:30";
// ICU4C result is "Mar 3, 03:30 \u2013 10:30" (does not duplicate day)
String fmtTokyo = "Mar 3, 03:30 \u2013 10:30";
StringBuffer buf = new StringBuffer();
FieldPosition pos = new FieldPosition(0);
dtitvfmt.setTimeZone(tzCalif);
dtitvfmt.format(dtitv, buf, pos);
if (!buf.toString().equals(fmtCalif)) {
errln("DateIntervalFormat for tzCalif, expect \"" + fmtCalif + "\", get \"" + buf + "\"");
}
buf.setLength(0);
pos.setBeginIndex(0);
dtitvfmt.setTimeZone(tzTokyo);
dtitvfmt.format(dtitv, buf, pos);
if (!buf.toString().equals(fmtTokyo)) {
errln("DateIntervalFormat for tzTokyo, expect \"" + fmtTokyo + "\", get \"" + buf + "\"");
}
if (!dtitvfmt.getTimeZone().equals(tzTokyo)) {
errln("DateIntervalFormat.getTimeZone() returns mismatch");
}
}
use of android.icu.util.TimeZone in project j2objc by google.
the class DateTimeGeneratorTest method TestReplacingZoneString.
@Test
public void TestReplacingZoneString() {
Date testDate = new Date();
TimeZone testTimeZone = TimeZone.getTimeZone("America/New_York");
TimeZone bogusTimeZone = new SimpleTimeZone(1234, "Etc/Unknown");
Calendar calendar = Calendar.getInstance();
ParsePosition parsePosition = new ParsePosition(0);
ULocale[] locales = ULocale.getAvailableLocales();
int count = 0;
for (int i = 0; i < locales.length; ++i) {
// skip the country locales unless we are doing exhaustive tests
if (getExhaustiveness() < 6) {
if (locales[i].getCountry().length() > 0) {
continue;
}
}
count++;
// ticket#6503
if (getExhaustiveness() <= 5 && count % 3 != 0) {
continue;
}
logln(locales[i].toString());
DateTimePatternGenerator dtpgen = DateTimePatternGenerator.getInstance(locales[i]);
for (int style1 = DateFormat.FULL; style1 <= DateFormat.SHORT; ++style1) {
final SimpleDateFormat oldFormat = (SimpleDateFormat) DateFormat.getTimeInstance(style1, locales[i]);
String pattern = oldFormat.toPattern();
// replaceZoneString(pattern, "VVVV");
String newPattern = dtpgen.replaceFieldTypes(pattern, "VVVV");
if (newPattern.equals(pattern)) {
continue;
}
// verify that it roundtrips parsing
SimpleDateFormat newFormat = new SimpleDateFormat(newPattern, locales[i]);
newFormat.setTimeZone(testTimeZone);
String formatted = newFormat.format(testDate);
calendar.setTimeZone(bogusTimeZone);
parsePosition.setIndex(0);
newFormat.parse(formatted, calendar, parsePosition);
if (parsePosition.getErrorIndex() >= 0) {
errln("Failed parse with VVVV:\t" + locales[i] + ",\t\"" + pattern + "\",\t\"" + newPattern + "\",\t\"" + formatted.substring(0, parsePosition.getErrorIndex()) + "{}" + formatted.substring(parsePosition.getErrorIndex()) + "\"");
} else if (!calendar.getTimeZone().getID().equals(testTimeZone.getID())) {
errln("Failed timezone roundtrip with VVVV:\t" + locales[i] + ",\t\"" + pattern + "\",\t\"" + newPattern + "\",\t\"" + formatted + "\",\t" + calendar.getTimeZone().getID() + " != " + testTimeZone.getID());
} else {
logln(locales[i] + ":\t\"" + pattern + "\" => \t\"" + newPattern + "\"\t" + formatted);
}
}
}
}
use of android.icu.util.TimeZone in project j2objc by google.
the class DateTimeGeneratorTest method TestSimple.
@Test
public void TestSimple() {
// some simple use cases
ULocale locale = ULocale.GERMANY;
TimeZone zone = TimeZone.getTimeZone("Europe/Paris");
// make from locale
DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(locale);
SimpleDateFormat format = new SimpleDateFormat(gen.getBestPattern("MMMddHmm"), locale);
format.setTimeZone(zone);
assertEquals("simple format: MMMddHmm", "14. Okt., 08:58", format.format(sampleDate));
// (a generator can be built from scratch, but that is not a typical use case)
// modify the generator by adding patterns
DateTimePatternGenerator.PatternInfo returnInfo = new DateTimePatternGenerator.PatternInfo();
gen.addPattern("d'. von' MMMM", true, returnInfo);
// the returnInfo is mostly useful for debugging problem cases
format.applyPattern(gen.getBestPattern("MMMMdHmm"));
assertEquals("modified format: MMMdHmm", "14. von Oktober, 08:58", format.format(sampleDate));
// get a pattern and modify it
format = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
format.setTimeZone(zone);
String pattern = format.toPattern();
assertEquals("full-date", "Donnerstag, 14. Oktober 1999 um 08:58:59 Mitteleurop\u00E4ische Sommerzeit", format.format(sampleDate));
// modify it to change the zone.
String newPattern = gen.replaceFieldTypes(pattern, "vvvv");
format.applyPattern(newPattern);
assertEquals("full-date: modified zone", "Donnerstag, 14. Oktober 1999 um 08:58:59 Mitteleurop\u00E4ische Zeit", format.format(sampleDate));
// add test of basic cases
// lang YYYYMMM MMMd MMMdhmm hmm hhmm Full Date-Time
// en Mar 2007 Mar 4 6:05 PM Mar 4 6:05 PM 06:05 PM Sunday, March 4, 2007 6:05:05 PM PT
DateTimePatternGenerator enGen = DateTimePatternGenerator.getInstance(ULocale.ENGLISH);
TimeZone enZone = TimeZone.getTimeZone("Etc/GMT");
SimpleDateFormat enFormat = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, ULocale.ENGLISH);
enFormat.setTimeZone(enZone);
String[][] tests = { { "yyyyMMMdd", "Oct 14, 1999" }, { "yyyyqqqq", "4th quarter 1999" }, { "yMMMdd", "Oct 14, 1999" }, { "EyyyyMMMdd", "Thu, Oct 14, 1999" }, { "yyyyMMdd", "10/14/1999" }, { "yyyyMMM", "Oct 1999" }, { "yyyyMM", "10/1999" }, { "yyMM", "10/99" }, // narrow format
{ "yMMMMMd", "O 14, 1999" }, // narrow format
{ "EEEEEMMMMMd", "T, O 14" }, { "MMMd", "Oct 14" }, { "MMMdhmm", "Oct 14, 6:58 AM" }, { "EMMMdhmms", "Thu, Oct 14, 6:58:59 AM" }, { "MMdhmm", "10/14, 6:58 AM" }, { "EEEEMMMdhmms", "Thursday, Oct 14, 6:58:59 AM" }, // (fixed expected result per ticket 6872<-7180)
{ "yyyyMMMddhhmmss", "Oct 14, 1999, 6:58:59 AM" }, // (fixed expected result per ticket 6872<-7180)
{ "EyyyyMMMddhhmmss", "Thu, Oct 14, 1999, 6:58:59 AM" }, { "hmm", "6:58 AM" }, // (fixed expected result per ticket 6872<-7180)
{ "hhmm", "6:58 AM" }, // (fixed expected result per ticket 6872<-7180)
{ "hhmmVVVV", "6:58 AM GMT" } };
for (int i = 0; i < tests.length; ++i) {
final String testSkeleton = tests[i][0];
String pat = enGen.getBestPattern(testSkeleton);
enFormat.applyPattern(pat);
String formattedDate = enFormat.format(sampleDate);
assertEquals("Testing skeleton '" + testSkeleton + "' with " + sampleDate, tests[i][1], formattedDate);
}
}
use of android.icu.util.TimeZone in project j2objc by google.
the class DateFormatTest method TestFormatsWithNumberSystems.
@Test
public void TestFormatsWithNumberSystems() {
TimeZone zone = TimeZone.getFrozenTimeZone("UTC");
// for UTC: grego 31-Dec-2015 10 AM, hebrew 19 tevet 5776, chinese yi-wei 11mo 21day
long date = 1451556000000L;
class TestFmtWithNumSysItem {
public String localeID;
public int style;
public String expectPattern;
public String expectFormat;
// Simple constructor
public TestFmtWithNumSysItem(String loc, int styl, String pat, String exp) {
localeID = loc;
style = styl;
expectPattern = pat;
expectFormat = exp;
}
}
;
final TestFmtWithNumSysItem[] items = { new TestFmtWithNumSysItem("haw@calendar=gregorian", DateFormat.SHORT, "d/M/yy", "31/xii/15"), new TestFmtWithNumSysItem("he@calendar=hebrew", DateFormat.LONG, "d \u05D1MMMM y", "\u05D9\u05F4\u05D8 \u05D1\u05D8\u05D1\u05EA \u05EA\u05E9\u05E2\u05F4\u05D5"), // "2015乙未年十一月廿一"
new TestFmtWithNumSysItem("zh@calendar=chinese", DateFormat.LONG, "rU\u5E74MMMd", "2015\u4E59\u672A\u5E74\u5341\u4E00\u6708\u5EFF\u4E00"), // "2015乙未年冬月廿一"
new TestFmtWithNumSysItem("zh_Hant@calendar=chinese", DateFormat.LONG, "rU\u5E74MMMd", "2015\u4E59\u672A\u5E74\u51AC\u6708\u5EFF\u4E00"), // "乙未年十一月二一日"
new TestFmtWithNumSysItem("ja@calendar=chinese", DateFormat.LONG, "U\u5E74MMMd\u65E5", "\u4E59\u672A\u5E74\u5341\u4E00\u6708\u4E8C\u4E00\u65E5") };
for (TestFmtWithNumSysItem item : items) {
ULocale locale = new ULocale(item.localeID);
Calendar cal = Calendar.getInstance(zone, locale);
cal.setTimeInMillis(date);
SimpleDateFormat sdfmt = (SimpleDateFormat) DateFormat.getDateInstance(item.style, locale);
StringBuffer getFormat = new StringBuffer();
FieldPosition fp = new FieldPosition(0);
sdfmt.format(cal, getFormat, fp);
if (getFormat.toString().compareTo(item.expectFormat) != 0) {
errln("FAIL: date format for locale " + item.localeID + ", expected \"" + item.expectFormat + "\", got \"" + getFormat.toString() + "\"");
}
String getPattern = sdfmt.toPattern();
if (getPattern.compareTo(item.expectPattern) != 0) {
errln("FAIL: date pattern for locale " + item.localeID + ", expected \"" + item.expectPattern + "\", got \"" + getPattern + "\"");
}
}
}
Aggregations