use of android.icu.util.Calendar in project j2objc by google.
the class PersianTest method TestMapping.
/**
* Test basic mapping to and from Gregorian.
*/
@Test
public void TestMapping() {
final int[] DATA = { // (Note: months are 1-based)
2011, 1, 11, 1389, 10, 21, 1986, 2, 25, 1364, 12, 6, 1934, 3, 14, 1312, 12, 23, 2090, 3, 19, 1468, 12, 29, 2007, 2, 22, 1385, 12, 3, 1969, 12, 31, 1348, 10, 10, 1945, 11, 12, 1324, 8, 21, 1925, 3, 31, 1304, 1, 11, 1996, 3, 19, 1374, 12, 29, 1996, 3, 20, 1375, 1, 1, 1997, 3, 20, 1375, 12, 30, 1997, 3, 21, 1376, 1, 1, 2008, 3, 19, 1386, 12, 29, 2008, 3, 20, 1387, 1, 1, 2004, 3, 19, 1382, 12, 29, 2004, 3, 20, 1383, 1, 1, 2006, 3, 20, 1384, 12, 29, 2006, 3, 21, 1385, 1, 1, 2005, 4, 20, 1384, 1, 31, 2005, 4, 21, 1384, 2, 1, 2005, 5, 21, 1384, 2, 31, 2005, 5, 22, 1384, 3, 1, 2005, 6, 21, 1384, 3, 31, 2005, 6, 22, 1384, 4, 1, 2005, 7, 22, 1384, 4, 31, 2005, 7, 23, 1384, 5, 1, 2005, 8, 22, 1384, 5, 31, 2005, 8, 23, 1384, 6, 1, 2005, 9, 22, 1384, 6, 31, 2005, 9, 23, 1384, 7, 1, 2005, 10, 22, 1384, 7, 30, 2005, 10, 23, 1384, 8, 1, 2005, 11, 21, 1384, 8, 30, 2005, 11, 22, 1384, 9, 1, 2005, 12, 21, 1384, 9, 30, 2005, 12, 22, 1384, 10, 1, 2006, 1, 20, 1384, 10, 30, 2006, 1, 21, 1384, 11, 1, 2006, 2, 19, 1384, 11, 30, 2006, 2, 20, 1384, 12, 1, 2006, 3, 20, 1384, 12, 29, 2006, 3, 21, 1385, 1, 1, // The 2820-year cycle arithmetical algorithm would fail this one.
2025, 3, 21, 1404, 1, 1 };
Calendar cal = Calendar.getInstance(new ULocale("fa_IR@calendar=persian"));
StringBuilder buf = new StringBuilder();
logln("Gregorian -> Persian");
Calendar grego = Calendar.getInstance();
grego.clear();
for (int i = 0; i < DATA.length; ) {
grego.set(DATA[i++], DATA[i++] - 1, DATA[i++]);
Date date = grego.getTime();
cal.setTime(date);
int y = cal.get(Calendar.YEAR);
// 0-based -> 1-based
int m = cal.get(Calendar.MONTH) + 1;
int d = cal.get(Calendar.DAY_OF_MONTH);
// Expected y, m, d
int yE = DATA[i++];
// 1-based
int mE = DATA[i++];
int dE = DATA[i++];
buf.setLength(0);
buf.append(date + " -> ");
buf.append(y + "/" + m + "/" + d);
if (y == yE && m == mE && d == dE) {
logln("OK: " + buf.toString());
} else {
errln("Fail: " + buf.toString() + ", expected " + yE + "/" + mE + "/" + dE);
}
}
logln("Persian -> Gregorian");
for (int i = 0; i < DATA.length; ) {
grego.set(DATA[i++], DATA[i++] - 1, DATA[i++]);
Date dexp = grego.getTime();
int cyear = DATA[i++];
int cmonth = DATA[i++];
int cdayofmonth = DATA[i++];
cal.clear();
cal.set(Calendar.YEAR, cyear);
cal.set(Calendar.MONTH, cmonth - 1);
cal.set(Calendar.DAY_OF_MONTH, cdayofmonth);
Date date = cal.getTime();
buf.setLength(0);
buf.append(cyear + "/" + cmonth + "/" + cdayofmonth);
buf.append(" -> " + date);
if (date.equals(dexp)) {
logln("OK: " + buf.toString());
} else {
errln("Fail: " + buf.toString() + ", expected " + dexp);
}
}
}
use of android.icu.util.Calendar in project j2objc by google.
the class DateIntervalFormat method format.
/**
* Format 2 Calendars to produce a string.
*
* @param fromCalendar calendar set to the from date in date interval
* to be formatted into date interval string
* @param toCalendar calendar set to the to date in date interval
* to be formatted into date interval string
* @param appendTo Output parameter to receive result.
* Result is appended to existing contents.
* @param pos On input: an alignment field, if desired.
* On output: the offsets of the alignment field.
* There may be multiple instances of a given field type
* in an interval format; in this case the fieldPosition
* offsets refer to the first instance.
* @return Reference to 'appendTo' parameter.
* @throws IllegalArgumentException if the two calendars are not equivalent.
*/
public final synchronized StringBuffer format(Calendar fromCalendar, Calendar toCalendar, StringBuffer appendTo, FieldPosition pos) {
// not support different calendar types and time zones
if (!fromCalendar.isEquivalentTo(toCalendar)) {
throw new IllegalArgumentException("can not format on two different calendars");
}
// First, find the largest different calendar field.
// init with an invalid value.
int field = -1;
if (fromCalendar.get(Calendar.ERA) != toCalendar.get(Calendar.ERA)) {
field = Calendar.ERA;
} else if (fromCalendar.get(Calendar.YEAR) != toCalendar.get(Calendar.YEAR)) {
field = Calendar.YEAR;
} else if (fromCalendar.get(Calendar.MONTH) != toCalendar.get(Calendar.MONTH)) {
field = Calendar.MONTH;
} else if (fromCalendar.get(Calendar.DATE) != toCalendar.get(Calendar.DATE)) {
field = Calendar.DATE;
} else if (fromCalendar.get(Calendar.AM_PM) != toCalendar.get(Calendar.AM_PM)) {
field = Calendar.AM_PM;
} else if (fromCalendar.get(Calendar.HOUR) != toCalendar.get(Calendar.HOUR)) {
field = Calendar.HOUR;
} else if (fromCalendar.get(Calendar.MINUTE) != toCalendar.get(Calendar.MINUTE)) {
field = Calendar.MINUTE;
} else if (fromCalendar.get(Calendar.SECOND) != toCalendar.get(Calendar.SECOND)) {
field = Calendar.SECOND;
} else {
/* ignore the millisecond etc. small fields' difference.
* use single date when all the above are the same.
*/
return fDateFormat.format(fromCalendar, appendTo, pos);
}
boolean fromToOnSameDay = (field == Calendar.AM_PM || field == Calendar.HOUR || field == Calendar.MINUTE || field == Calendar.SECOND);
// get interval pattern
PatternInfo intervalPattern = fIntervalPatterns.get(DateIntervalInfo.CALENDAR_FIELD_TO_PATTERN_LETTER[field]);
if (intervalPattern == null) {
if (fDateFormat.isFieldUnitIgnored(field)) {
/* the largest different calendar field is small than
* the smallest calendar field in pattern,
* return single date format.
*/
return fDateFormat.format(fromCalendar, appendTo, pos);
}
return fallbackFormat(fromCalendar, toCalendar, fromToOnSameDay, appendTo, pos);
}
// For a 'real' interval pattern, the first part will never be empty.
if (intervalPattern.getFirstPart() == null) {
// fall back
return fallbackFormat(fromCalendar, toCalendar, fromToOnSameDay, appendTo, pos, intervalPattern.getSecondPart());
}
Calendar firstCal;
Calendar secondCal;
if (intervalPattern.firstDateInPtnIsLaterDate()) {
firstCal = toCalendar;
secondCal = fromCalendar;
} else {
firstCal = fromCalendar;
secondCal = toCalendar;
}
// break the interval pattern into 2 parts
// first part should not be empty,
String originalPattern = fDateFormat.toPattern();
fDateFormat.applyPattern(intervalPattern.getFirstPart());
fDateFormat.format(firstCal, appendTo, pos);
if (intervalPattern.getSecondPart() != null) {
fDateFormat.applyPattern(intervalPattern.getSecondPart());
FieldPosition otherPos = new FieldPosition(pos.getField());
fDateFormat.format(secondCal, appendTo, otherPos);
if (pos.getEndIndex() == 0 && otherPos.getEndIndex() > 0) {
pos.setBeginIndex(otherPos.getBeginIndex());
pos.setEndIndex(otherPos.getEndIndex());
}
}
fDateFormat.applyPattern(originalPattern);
return appendTo;
}
use of android.icu.util.Calendar in project j2objc by google.
the class DateIntervalFormatTest method testGetInstance_String_DateIntervalInfo.
@Test
public void testGetInstance_String_DateIntervalInfo() {
DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(new ULocale("ca"));
DateIntervalFormat dateIntervalFormat = DateIntervalFormat.getInstance(DateFormat.YEAR_MONTH, Locale.ENGLISH, dateIntervalInfo);
Calendar from = Calendar.getInstance();
from.set(2000, Calendar.JANUARY, 1, 12, 0);
Calendar to = Calendar.getInstance();
to.set(2001, Calendar.FEBRUARY, 1, 12, 0);
DateInterval interval = new DateInterval(from.getTimeInMillis(), to.getTimeInMillis());
dateIntervalFormat.setTimeZone(from.getTimeZone());
// Month names are default (English), format is Catalan
assertEquals("Wrong date interval", "January de 2000 – February de 2001", dateIntervalFormat.format(interval));
}
use of android.icu.util.Calendar in project j2objc by google.
the class DateIntervalFormatTest method TestTicket9919Setter.
@Test
public void TestTicket9919Setter() {
// Creating a DateIntervalFormat with a custom DateIntervalInfo
// object used to corrupt the cache.
DateIntervalFormat dif = DateIntervalFormat.getInstance("yMd", ULocale.ENGLISH);
Calendar from = Calendar.getInstance();
Calendar to = Calendar.getInstance();
from.set(2013, 3, 26);
to.set(2013, 3, 28);
// Save. This is the correct answer
String expected = dif.format(from, to, new StringBuffer(), new FieldPosition(0)).toString();
// Now create a DateIntervalFormat with same skeleton and
// locale, but with a custom DateIntervalInfo. This used
// to corrupt the cache.
DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(ULocale.ENGLISH);
dateIntervalInfo.setIntervalPattern("yMd", Calendar.DATE, "M/d/y \u2013 d");
DateIntervalFormat bad = DateIntervalFormat.getInstance("yMd", ULocale.ENGLISH);
bad.setDateIntervalInfo(dateIntervalInfo);
// Now create a DateIntervalFormat with same skeleton and
// locale, but with default DateIntervalInfo. The cache should
// not be corrupted, and we should get the same answer as before.
dif = DateIntervalFormat.getInstance("yMd", ULocale.ENGLISH);
assertEquals("Custom DateIntervalInfo objects should not mess up cache", expected, dif.format(from, to, new StringBuffer(), new FieldPosition(0)).toString());
}
use of android.icu.util.Calendar 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);
}
}
}
Aggregations