+
+
+
+/**
+ * Verify the Persian Calendar.
+ */
+void IntlCalendarTest::TestPersian() {
+ UDate timeA = Calendar::getNow();
+
+ Calendar *cal;
+ UErrorCode status = U_ZERO_ERROR;
+ cal = Calendar::createInstance("fa_IR@calendar=persian", status);
+ CHECK(status, UnicodeString("Creating fa_IR@calendar=persian calendar"));
+ // Sanity check the calendar
+ UDate timeB = Calendar::getNow();
+ UDate timeCal = cal->getTime(status);
+
+ if(!(timeA <= timeCal) || !(timeCal <= timeB)) {
+ errln((UnicodeString)"Error: Calendar time " + timeCal +
+ " is not within sampled times [" + timeA + " to " + timeB + "]!");
+ }
+ // end sanity check
+
+ // Test various dates to be sure of validity
+ int32_t data[] = {
+ 1925, 4, 24, 1304, 2, 4,
+ 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,
+
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
+ };
+
+ Calendar *grego = Calendar::createInstance("fa_IR@calendar=gregorian", status);
+ for (int32_t i=0; data[i]!=-1; ) {
+ int32_t gregYear = data[i++];
+ int32_t gregMonth = data[i++]-1;
+ int32_t gregDay = data[i++];
+ int32_t persYear = data[i++];
+ int32_t persMonth = data[i++]-1;
+ int32_t persDay = data[i++];
+
+ // Test conversion from Persian dates
+ grego->clear();
+ grego->set(gregYear, gregMonth, gregDay);
+
+ cal->clear();
+ cal->set(persYear, persMonth, persDay);
+
+ UDate persTime = cal->getTime(status);
+ UDate gregTime = grego->getTime(status);
+
+ if (persTime != gregTime) {
+ errln(UnicodeString("Expected ") + gregTime + " but got " + persTime);
+ }
+
+ // Test conversion to Persian dates
+ cal->clear();
+ cal->setTime(gregTime, status);
+
+ int32_t computedYear = cal->get(UCAL_YEAR, status);
+ int32_t computedMonth = cal->get(UCAL_MONTH, status);
+ int32_t computedDay = cal->get(UCAL_DATE, status);
+
+ if ((persYear != computedYear) ||
+ (persMonth != computedMonth) ||
+ (persDay != computedDay)) {
+ errln(UnicodeString("Expected ") + persYear + "/" + (persMonth+1) + "/" + persDay +
+ " but got " + computedYear + "/" + (computedMonth+1) + "/" + computedDay);
+ }
+
+ }
+
+ delete cal;
+ delete grego;
+}
+
+void IntlCalendarTest::TestPersianFormat() {
+ UErrorCode status = U_ZERO_ERROR;
+ SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale(" en_US@calendar=persian"), status);
+ CHECK(status, "creating date format instance");
+ SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("en_US@calendar=gregorian"), status);
+ CHECK(status, "creating gregorian date format instance");
+ UnicodeString gregorianDate("January 18, 2007 AD");
+ UDate aDate = fmt2->parse(gregorianDate, status);
+ if(!fmt) {
+ errln("Couldn't create en_US instance");
+ } else {
+ UnicodeString str;
+ fmt->format(aDate, str);
+ logln(UnicodeString() + "as Persian Calendar: " + escape(str));
+ UnicodeString expected("Dey 28, 1385 AP");
+ if(str != expected) {
+ errln("Expected " + escape(expected) + " but got " + escape(str));
+ }
+ UDate otherDate = fmt->parse(expected, status);
+ if(otherDate != aDate) {
+ UnicodeString str3;
+ fmt->format(otherDate, str3);
+ errln("Parse incorrect of " + escape(expected) + " - wanted " + aDate + " but got " + otherDate + ", " + escape(str3));
+ } else {
+ logln("Parsed OK: " + expected);
+ }
+ // Two digit year parsing problem #4732
+ fmt->applyPattern("yy-MM-dd");
+ str.remove();
+ fmt->format(aDate, str);
+ expected.setTo("85-10-28");
+ if(str != expected) {
+ errln("Expected " + escape(expected) + " but got " + escape(str));
+ }
+ otherDate = fmt->parse(expected, status);
+ if (otherDate != aDate) {
+ errln("Parse incorrect of " + escape(expected) + " - wanted " + aDate + " but got " + otherDate);
+ } else {
+ logln("Parsed OK: " + expected);
+ }
+ delete fmt;
+ }
+ delete fmt2;
+
+ CHECK(status, "Error occured testing Persian Calendar in English ");
+}
+
+