use of android.icu.util.Calendar in project j2objc by google.
the class IBMCalendarTest method TestWeekData.
@Test
public void TestWeekData() {
// Each line contains two locales using the same set of week rule data.
final String[] LOCALE_PAIRS = { "en", "en_US", "de", "de_DE", "de_DE", "en_DE", "en_GB", "und_GB", "ar_EG", "en_EG", "ar_SA", "fr_SA" };
for (int i = 0; i < LOCALE_PAIRS.length; i += 2) {
Calendar cal1 = Calendar.getInstance(new ULocale(LOCALE_PAIRS[i]));
Calendar cal2 = Calendar.getInstance(new ULocale(LOCALE_PAIRS[i + 1]));
// First day of week
int dow1 = cal1.getFirstDayOfWeek();
int dow2 = cal2.getFirstDayOfWeek();
if (dow1 != dow2) {
errln("getFirstDayOfWeek: " + LOCALE_PAIRS[i] + "->" + dow1 + ", " + LOCALE_PAIRS[i + 1] + "->" + dow2);
}
// Minimum days in first week
int minDays1 = cal1.getMinimalDaysInFirstWeek();
int minDays2 = cal2.getMinimalDaysInFirstWeek();
if (minDays1 != minDays2) {
errln("getMinimalDaysInFirstWeek: " + LOCALE_PAIRS[i] + "->" + minDays1 + ", " + LOCALE_PAIRS[i + 1] + "->" + minDays2);
}
// Weekdays and Weekends
for (int d = Calendar.SUNDAY; d <= Calendar.SATURDAY; d++) {
int wdt1 = cal1.getDayOfWeekType(d);
int wdt2 = cal2.getDayOfWeekType(d);
if (wdt1 != wdt2) {
errln("getDayOfWeekType(" + d + "): " + LOCALE_PAIRS[i] + "->" + wdt1 + ", " + LOCALE_PAIRS[i + 1] + "->" + wdt2);
}
}
}
}
use of android.icu.util.Calendar in project j2objc by google.
the class IBMCalendarTest method TestAddRollEra0AndEraBounds.
@Test
public void TestAddRollEra0AndEraBounds() {
final String[] localeIDs = { // calendars with non-modern era 0 that goes backwards, max era == 1
"en@calendar=gregorian", "en@calendar=roc", "en@calendar=coptic", // calendars with non-modern era 0 that goes forwards, max era > 1
"en@calendar=japanese", "en@calendar=chinese", // calendars with non-modern era 0 that goes forwards, max era == 1
"en@calendar=ethiopic", // calendars with only one era = 0, forwards
"en@calendar=buddhist", "en@calendar=hebrew", "en@calendar=islamic", "en@calendar=indian", // "en@calendar=persian", // no persian calendar in ICU4J yet
"en@calendar=ethiopic-amete-alem" };
TimeZone zoneGMT = TimeZone.getFrozenTimeZone("GMT");
for (String localeID : localeIDs) {
Calendar ucalTest = Calendar.getInstance(zoneGMT, new ULocale(localeID));
String calType = ucalTest.getType();
boolean era0YearsGoBackwards = (calType.equals("gregorian") || calType.equals("roc") || calType.equals("coptic"));
int yrBefore, yrAfter, yrMax, eraAfter, eraMax, eraNow;
ucalTest.clear();
ucalTest.set(Calendar.YEAR, 2);
ucalTest.set(Calendar.ERA, 0);
yrBefore = ucalTest.get(Calendar.YEAR);
ucalTest.add(Calendar.YEAR, 1);
yrAfter = ucalTest.get(Calendar.YEAR);
if ((era0YearsGoBackwards && yrAfter > yrBefore) || (!era0YearsGoBackwards && yrAfter < yrBefore)) {
errln("Fail: era 0 add 1 year does not move forward in time for " + localeID);
}
ucalTest.clear();
ucalTest.set(Calendar.YEAR, 2);
ucalTest.set(Calendar.ERA, 0);
yrBefore = ucalTest.get(Calendar.YEAR);
ucalTest.roll(Calendar.YEAR, 1);
yrAfter = ucalTest.get(Calendar.YEAR);
if ((era0YearsGoBackwards && yrAfter > yrBefore) || (!era0YearsGoBackwards && yrAfter < yrBefore)) {
errln("Fail: era 0 roll 1 year does not move forward in time for " + localeID);
}
ucalTest.clear();
ucalTest.set(Calendar.YEAR, 1);
ucalTest.set(Calendar.ERA, 0);
if (era0YearsGoBackwards) {
ucalTest.roll(Calendar.YEAR, 1);
yrAfter = ucalTest.get(Calendar.YEAR);
eraAfter = ucalTest.get(Calendar.ERA);
if (eraAfter != 0 || yrAfter != 1) {
errln("Fail: era 0 roll 1 year from year 1 does not stay within era or pin to year 1 for " + localeID + " (get era " + eraAfter + " year " + yrAfter + ")");
}
} else {
// roll backward in time to where era 0 years go negative, except for the Chinese
// calendar, which uses negative eras instead of having years outside the range 1-60
ucalTest.roll(Calendar.YEAR, -2);
yrAfter = ucalTest.get(Calendar.YEAR);
eraAfter = ucalTest.get(Calendar.ERA);
if (!calType.equals("chinese") && (eraAfter != 0 || yrAfter != -1)) {
errln("Fail: era 0 roll -2 years from year 1 does not stay within era or produce year -1 for " + localeID + " (get era " + eraAfter + " year " + yrAfter + ")");
}
}
ucalTest.clear();
{
int eraMin = ucalTest.getMinimum(Calendar.ERA);
if (eraMin != 0 && calType.compareTo("chinese") != 0) {
errln("Fail: getMinimum returns minimum era " + eraMin + " (should be 0) for calType " + calType);
}
}
ucalTest.clear();
ucalTest.set(Calendar.YEAR, 1);
ucalTest.set(Calendar.ERA, 0);
eraMax = ucalTest.getMaximum(Calendar.ERA);
if (eraMax > 0) {
// try similar tests for era 1 (if calendar has it), in which years always go forward
ucalTest.clear();
ucalTest.set(Calendar.YEAR, 2);
ucalTest.set(Calendar.ERA, 1);
yrBefore = ucalTest.get(Calendar.YEAR);
ucalTest.add(Calendar.YEAR, 1);
yrAfter = ucalTest.get(Calendar.YEAR);
if (yrAfter < yrBefore) {
errln("Fail: era 1 add 1 year does not move forward in time for " + localeID);
}
ucalTest.clear();
ucalTest.set(Calendar.YEAR, 2);
ucalTest.set(Calendar.ERA, 1);
yrBefore = ucalTest.get(Calendar.YEAR);
ucalTest.roll(Calendar.YEAR, 1);
yrAfter = ucalTest.get(Calendar.YEAR);
if (yrAfter < yrBefore) {
errln("Fail: era 1 roll 1 year does not move forward in time for " + localeID);
}
ucalTest.clear();
ucalTest.set(Calendar.YEAR, 1);
ucalTest.set(Calendar.ERA, 1);
yrMax = ucalTest.getActualMaximum(Calendar.YEAR);
// roll down which should pin or wrap to end
ucalTest.roll(Calendar.YEAR, -1);
yrAfter = ucalTest.get(Calendar.YEAR);
eraAfter = ucalTest.get(Calendar.ERA);
// if yrMax is reasonable we should wrap to that, else we should pin at yr 1
if (yrMax >= 32768) {
if (eraAfter != 1 || yrAfter != 1) {
errln("Fail: era 1 roll -1 year from year 1 does not stay within era or pin to year 1 for " + localeID + " (get era " + eraAfter + " year " + yrAfter + ")");
}
} else if (eraAfter != 1 || yrAfter != yrMax) {
errln("Fail: era 1 roll -1 year from year 1 does not stay within era or wrap to year " + yrMax + " for " + localeID + " (get era " + eraAfter + " year " + yrAfter + ")");
} else {
// now roll up which should wrap to beginning
ucalTest.roll(Calendar.YEAR, 1);
yrAfter = ucalTest.get(Calendar.YEAR);
eraAfter = ucalTest.get(Calendar.ERA);
if (eraAfter != 1 || yrAfter != 1) {
errln("Fail: era 1 roll 1 year from year " + yrMax + " does not stay within era or wrap to year 1 for " + localeID + " (get era " + eraAfter + " year " + yrAfter + ")");
}
}
// if current era > 1, try the same roll tests for current era
ucalTest.setTime(new Date());
eraNow = ucalTest.get(Calendar.ERA);
if (eraNow > 1) {
ucalTest.clear();
ucalTest.set(Calendar.YEAR, 1);
ucalTest.set(Calendar.ERA, eraNow);
// max year value for this era
yrMax = ucalTest.getActualMaximum(Calendar.YEAR);
ucalTest.roll(Calendar.YEAR, -1);
yrAfter = ucalTest.get(Calendar.YEAR);
eraAfter = ucalTest.get(Calendar.ERA);
// if yrMax is reasonable we should wrap to that, else we should pin at yr 1
if (yrMax >= 32768) {
if (eraAfter != eraNow || yrAfter != 1) {
errln("Fail: era " + eraNow + " roll -1 year from year 1 does not stay within era or pin to year 1 for " + localeID + " (get era " + eraAfter + " year " + yrAfter + ")");
}
} else if (eraAfter != eraNow || yrAfter != yrMax) {
errln("Fail: era " + eraNow + " roll -1 year from year 1 does not stay within era or wrap to year " + yrMax + " for " + localeID + " (get era " + eraAfter + " year " + yrAfter + ")");
} else {
// now roll up which should wrap to beginning
ucalTest.roll(Calendar.YEAR, 1);
yrAfter = ucalTest.get(Calendar.YEAR);
eraAfter = ucalTest.get(Calendar.ERA);
if (eraAfter != eraNow || yrAfter != 1) {
errln("Fail: era " + eraNow + " roll 1 year from year " + yrMax + " does not stay within era or wrap to year 1 for " + localeID + " (get era " + eraAfter + " year " + yrAfter + ")");
}
}
}
}
}
}
use of android.icu.util.Calendar in project j2objc by google.
the class IBMCalendarTest method TestRepeatedWallTime.
@Test
public void TestRepeatedWallTime() {
final Object[][] TESTDATA = { // Time zone Input wall time WALLTIME_LAST in GMT WALLTIME_FIRST in GMT
{ "America/New_York", new CalFields(2011, 11, 6, 0, 59, 59), new CalFields(2011, 11, 6, 4, 59, 59), new CalFields(2011, 11, 6, 4, 59, 59) }, { "America/New_York", new CalFields(2011, 11, 6, 1, 0, 0), new CalFields(2011, 11, 6, 6, 0, 0), new CalFields(2011, 11, 6, 5, 0, 0) }, { "America/New_York", new CalFields(2011, 11, 6, 1, 0, 1), new CalFields(2011, 11, 6, 6, 0, 1), new CalFields(2011, 11, 6, 5, 0, 1) }, { "America/New_York", new CalFields(2011, 11, 6, 1, 30, 0), new CalFields(2011, 11, 6, 6, 30, 0), new CalFields(2011, 11, 6, 5, 30, 0) }, { "America/New_York", new CalFields(2011, 11, 6, 1, 59, 59), new CalFields(2011, 11, 6, 6, 59, 59), new CalFields(2011, 11, 6, 5, 59, 59) }, { "America/New_York", new CalFields(2011, 11, 6, 2, 0, 0), new CalFields(2011, 11, 6, 7, 0, 0), new CalFields(2011, 11, 6, 7, 0, 0) }, { "America/New_York", new CalFields(2011, 11, 6, 2, 0, 1), new CalFields(2011, 11, 6, 7, 0, 1), new CalFields(2011, 11, 6, 7, 0, 1) }, { "Australia/Lord_Howe", new CalFields(2011, 4, 3, 1, 29, 59), new CalFields(2011, 4, 2, 14, 29, 59), new CalFields(2011, 4, 2, 14, 29, 59) }, { "Australia/Lord_Howe", new CalFields(2011, 4, 3, 1, 30, 0), new CalFields(2011, 4, 2, 15, 0, 0), new CalFields(2011, 4, 2, 14, 30, 0) }, { "Australia/Lord_Howe", new CalFields(2011, 4, 3, 1, 45, 0), new CalFields(2011, 4, 2, 15, 15, 0), new CalFields(2011, 4, 2, 14, 45, 0) }, { "Australia/Lord_Howe", new CalFields(2011, 4, 3, 1, 59, 59), new CalFields(2011, 4, 2, 15, 29, 59), new CalFields(2011, 4, 2, 14, 59, 59) }, { "Australia/Lord_Howe", new CalFields(2011, 4, 3, 2, 0, 0), new CalFields(2011, 4, 2, 15, 30, 0), new CalFields(2011, 4, 2, 15, 30, 0) }, { "Australia/Lord_Howe", new CalFields(2011, 4, 3, 2, 0, 1), new CalFields(2011, 4, 2, 15, 30, 1), new CalFields(2011, 4, 2, 15, 30, 1) } };
Calendar calGMT = Calendar.getInstance(TimeZone.GMT_ZONE);
Calendar calDefault = Calendar.getInstance();
Calendar calLast = Calendar.getInstance();
Calendar calFirst = Calendar.getInstance();
calFirst.setRepeatedWallTimeOption(Calendar.WALLTIME_FIRST);
calLast.setRepeatedWallTimeOption(Calendar.WALLTIME_LAST);
for (Object[] test : TESTDATA) {
String tzid = (String) test[0];
TimeZone tz = TimeZone.getTimeZone(tzid);
CalFields in = (CalFields) test[1];
CalFields expLastGMT = (CalFields) test[2];
CalFields expFirstGMT = (CalFields) test[3];
// WALLTIME_LAST
calLast.setTimeZone(tz);
in.setTo(calLast);
calGMT.setTimeInMillis(calLast.getTimeInMillis());
CalFields outLastGMT = CalFields.createFrom(calGMT);
if (!outLastGMT.equals(expLastGMT)) {
errln("Fail: WALLTIME_LAST " + in + "[" + tzid + "] is parsed as " + outLastGMT + "[GMT]. Expected: " + expLastGMT + "[GMT]");
}
// default
calDefault.setTimeZone(tz);
in.setTo(calDefault);
calGMT.setTimeInMillis(calDefault.getTimeInMillis());
CalFields outDefGMT = CalFields.createFrom(calGMT);
if (!outDefGMT.equals(expLastGMT)) {
errln("Fail: (default) " + in + "[" + tzid + "] is parsed as " + outDefGMT + "[GMT]. Expected: " + expLastGMT + "[GMT]");
}
// WALLTIME_FIRST
calFirst.setTimeZone(tz);
in.setTo(calFirst);
calGMT.setTimeInMillis(calFirst.getTimeInMillis());
CalFields outFirstGMT = CalFields.createFrom(calGMT);
if (!outFirstGMT.equals(expFirstGMT)) {
errln("Fail: WALLTIME_FIRST " + in + "[" + tzid + "] is parsed as " + outFirstGMT + "[GMT]. Expected: " + expFirstGMT + "[GMT]");
}
}
}
use of android.icu.util.Calendar in project j2objc by google.
the class CalendarRegressionTest method Test4070502.
@Test
public void Test4070502() {
java.util.Calendar tempcal = java.util.Calendar.getInstance();
tempcal.clear();
tempcal.set(1998, 0, 30);
Date d = getAssociatedDate(tempcal.getTime());
Calendar cal = new GregorianCalendar();
cal.setTime(d);
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
errln("Fail: Want weekday Got " + d);
}
use of android.icu.util.Calendar in project j2objc by google.
the class CalendarRegressionTest method TestWeekendData_10560.
@Test
public void TestWeekendData_10560() {
final Calendar.WeekData worldWeekData = new Calendar.WeekData(2, 1, 7, 0, 1, 86400000);
final Calendar.WeekData usWeekData = new Calendar.WeekData(1, 1, 7, 0, 1, 86400000);
final Calendar.WeekData testWeekData = new Calendar.WeekData(1, 2, 3, 4, 5, 86400000);
assertEquals("World", worldWeekData, Calendar.getWeekDataForRegion("001"));
assertEquals("Illegal code => world", Calendar.getWeekDataForRegion("001"), Calendar.getWeekDataForRegion("xx"));
assertEquals("FR = DE", Calendar.getWeekDataForRegion("FR"), Calendar.getWeekDataForRegion("DE"));
assertNotEquals("IN ≠ world", Calendar.getWeekDataForRegion("001"), Calendar.getWeekDataForRegion("IN"));
assertNotEquals("FR ≠ EG", Calendar.getWeekDataForRegion("FR"), Calendar.getWeekDataForRegion("EG"));
Calendar aCalendar = Calendar.getInstance(Locale.US);
assertEquals("US", usWeekData, aCalendar.getWeekData());
Calendar rgusCalendar = Calendar.getInstance(new ULocale("hi_IN@rg=USzzzz"));
assertEquals("IN@rg=US", usWeekData, rgusCalendar.getWeekData());
aCalendar.setWeekData(testWeekData);
assertEquals("Custom", testWeekData, aCalendar.getWeekData());
}
Aggregations