/***********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2004, International Business Machines Corporation
+ * Copyright (c) 1997-2012, International Business Machines Corporation
* and others. All Rights Reserved.
***********************************************************************/
#include "unicode/utypes.h"
#include "string.h"
#include "unicode/locid.h"
+#include "japancal.h"
#if !UCONFIG_NO_FORMATTING
#define CHECK(status, msg) \
if (U_FAILURE(status)) { \
- errln((UnicodeString(u_errorName(status)) + UnicodeString(" : " ) )+ msg); \
+ dataerrln((UnicodeString(u_errorName(status)) + UnicodeString(" : " ) )+ msg); \
return; \
}
CASE(3,TestJapanese);
CASE(4,TestBuddhistFormat);
CASE(5,TestJapaneseFormat);
+ CASE(6,TestJapanese3860);
+ CASE(7,TestPersian);
+ CASE(8,TestPersianFormat);
+ CASE(9,TestTaiwan);
default: name = ""; break;
}
}
"ja_JP_TRADITIONAL",
"th_TH_TRADITIONAL",
"th_TH_TRADITIONAL@calendar=gregorian",
- "en_US", NULL };
+ "en_US",
+ "th_TH", // Default calendar for th_TH is buddhist
+ "th", // th's default region is TH and buddhist is used as default for TH
+ "en_TH", // Default calendar for any locales with region TH is buddhist
+ "en-TH-u-ca-gregory",
+ NULL };
const char *types[40] = { "gregorian",
"japanese",
"gregorian",
"japanese",
- "buddhist",
+ "buddhist",
"japanese",
"buddhist",
"gregorian",
- "gregorian", NULL };
+ "gregorian",
+ "buddhist",
+ "buddhist",
+ "buddhist",
+ "gregorian",
+ NULL };
for(j=0;locs[j];j++) {
logln(UnicodeString("Creating calendar of locale ") + locs[j]);
if(U_SUCCESS(status)) {
logln(UnicodeString(" type is ") + c->getType());
if(strcmp(c->getType(), types[j])) {
- errln(UnicodeString(locs[j]) + UnicodeString("Calendar type ") + c->getType() + " instead of " + types[j]);
+ dataerrln(UnicodeString(locs[j]) + UnicodeString("Calendar type ") + c->getType() + " instead of " + types[j]);
}
}
delete c;
// due to the JDK 1.4 incorporation of historical time zones.
//java.util.Calendar grego = java.util.Calendar.getInstance();
Calendar *grego = Calendar::createInstance(gcl, status);
+ if (U_FAILURE(status)) {
+ dataerrln("Error calling Calendar::createInstance");
+ return;
+ }
int32_t tz1 = cal.get(UCAL_ZONE_OFFSET,status);
int32_t tz2 = grego -> get (UCAL_ZONE_OFFSET, status);
// end sanity check
- quasiGregorianTest(*cal,Locale("th_TH"),data);
+ quasiGregorianTest(*cal,Locale("th_TH@calendar=gregorian"),data);
delete cal;
}
+
+/**
+ * Verify that TaiWanCalendar shifts years to Minguo Era but otherwise
+ * behaves like GregorianCalendar.
+ */
+void IntlCalendarTest::TestTaiwan() {
+ // MG 1 == 1912 AD
+ UDate timeA = Calendar::getNow();
+
+ // TODO port these to the data items
+ int32_t data[] = {
+ 1, // B. era [928479600000]
+ 1, // B. year
+ 1912, // G. year
+ UCAL_JUNE, // month
+ 4, // day
+
+ 1, // B. era [-79204842000000]
+ 3, // B. year
+ 1914, // G. year
+ UCAL_FEBRUARY, // month
+ 12, // day
+
+ 1, // B. era [-79204842000000]
+ 96, // B. year
+ 2007, // G. year
+ UCAL_FEBRUARY, // month
+ 12, // day
+
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
+ };
+ Calendar *cal;
+ UErrorCode status = U_ZERO_ERROR;
+ cal = Calendar::createInstance("en_US@calendar=roc", status);
+ CHECK(status, UnicodeString("Creating en_US@calendar=roc 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
+
+
+ quasiGregorianTest(*cal,Locale("en_US"),data);
+ delete cal;
+}
+
+
+
/**
* Verify that JapaneseCalendar shifts years to Japanese Eras but otherwise
* behaves like GregorianCalendar.
delete cal;
}
+
+
void IntlCalendarTest::TestBuddhistFormat() {
UErrorCode status = U_ZERO_ERROR;
}
}
+// TaiwanFormat has been moved to testdata/format.txt
+
void IntlCalendarTest::TestJapaneseFormat() {
Calendar *cal;
UDate aDate = 999932400000.0;
SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yy G"), Locale("en_US@calendar=japanese"), status);
- SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("MMMM d, yy G"), Locale("en_US@calendar=gregorian"), status);
+ SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("en_US@calendar=gregorian"), status);
CHECK(status, "creating date format instance");
if(!fmt) {
errln("Coudln't create en_US instance");
// Test parse with incomplete information
fmt = new SimpleDateFormat(UnicodeString("G y"), Locale("en_US@calendar=japanese"), status);
- aDate = -3197120400000.;
+ aDate = -3197117222000.0;
CHECK(status, "creating date format instance");
if(!fmt) {
errln("Coudln't create en_US instance");
}
{
UnicodeString expect = CharsToUnicodeString("\\u5b89\\u6c385\\u5e747\\u67084\\u65e5\\u6728\\u66dc\\u65e5");
- UDate expectDate = -6106035600000.0;
+ UDate expectDate = -6106032422000.0; // 1776-07-04T00:00:00Z-075258
Locale loc("ja_JP@calendar=japanese");
status = U_ZERO_ERROR;
}
{ // This Feb 29th falls on a leap year by gregorian year, but not by Japanese year.
UnicodeString expect = CharsToUnicodeString("\\u5EB7\\u6B632\\u5e742\\u670829\\u65e5\\u65e5\\u66dc\\u65e5");
- // Add -1:00 to the following for historical TZ - aliu
- UDate expectDate = -16214403600000.0; // courtesy of date format round trip test
+ UDate expectDate = -16214400422000.0; // 1456-03-09T00:00Z-075258
Locale loc("ja_JP@calendar=japanese");
status = U_ZERO_ERROR;
}
}
+void IntlCalendarTest::TestJapanese3860()
+{
+ Calendar *cal;
+ UErrorCode status = U_ZERO_ERROR;
+ cal = Calendar::createInstance("ja_JP@calendar=japanese", status);
+ CHECK(status, UnicodeString("Creating ja_JP@calendar=japanese calendar"));
+ Calendar *cal2 = cal->clone();
+ SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("HH:mm:ss.S MMMM d, yyyy G"), Locale("en_US@calendar=gregorian"), status);
+ UnicodeString str;
+
+
+ {
+ // Test simple parse/format with adopt
+ UDate aDate = 0;
+
+ // Test parse with missing era (should default to current era, heisei)
+ // Test parse with incomplete information
+ logln("Testing parse w/ missing era...");
+ SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("y.M.d"), Locale("ja_JP@calendar=japanese"), status);
+ CHECK(status, "creating date format instance");
+ if(!fmt) {
+ errln("Coudln't create en_US instance");
+ } else {
+ UErrorCode s2 = U_ZERO_ERROR;
+ cal2->clear();
+ UnicodeString samplestr("1.1.9");
+ logln(UnicodeString() + "Test Year: " + samplestr);
+ aDate = fmt->parse(samplestr, s2);
+ ParsePosition pp=0;
+ fmt->parse(samplestr, *cal2, pp);
+ CHECK(s2, "parsing the 1.1.9 string");
+ logln("*cal2 after 119 parse:");
+ str.remove();
+ fmt2->format(aDate, str);
+ logln(UnicodeString() + "as Gregorian Calendar: " + str);
+
+ cal2->setTime(aDate, s2);
+ int32_t gotYear = cal2->get(UCAL_YEAR, s2);
+ int32_t gotEra = cal2->get(UCAL_ERA, s2);
+ int32_t expectYear = 1;
+ int32_t expectEra = JapaneseCalendar::getCurrentEra();
+ if((gotYear!=1) || (gotEra != expectEra)) {
+ errln(UnicodeString("parse "+samplestr+" of 'y.m.d' as Japanese Calendar, expected year ") + expectYear +
+ UnicodeString(" and era ") + expectEra +", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str +")");
+ } else {
+ logln(UnicodeString() + " year: " + gotYear + ", era: " + gotEra);
+ }
+ delete fmt;
+ }
+ }
+
+ {
+ // Test simple parse/format with adopt
+ UDate aDate = 0;
+
+ // Test parse with missing era (should default to current era, heisei)
+ // Test parse with incomplete information
+ logln("Testing parse w/ just year...");
+ SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("y"), Locale("ja_JP@calendar=japanese"), status);
+ CHECK(status, "creating date format instance");
+ if(!fmt) {
+ errln("Coudln't create en_US instance");
+ } else {
+ UErrorCode s2 = U_ZERO_ERROR;
+ cal2->clear();
+ UnicodeString samplestr("1");
+ logln(UnicodeString() + "Test Year: " + samplestr);
+ aDate = fmt->parse(samplestr, s2);
+ ParsePosition pp=0;
+ fmt->parse(samplestr, *cal2, pp);
+ CHECK(s2, "parsing the 1 string");
+ logln("*cal2 after 1 parse:");
+ str.remove();
+ fmt2->format(aDate, str);
+ logln(UnicodeString() + "as Gregorian Calendar: " + str);
+
+ cal2->setTime(aDate, s2);
+ int32_t gotYear = cal2->get(UCAL_YEAR, s2);
+ int32_t gotEra = cal2->get(UCAL_ERA, s2);
+ int32_t expectYear = 1;
+ int32_t expectEra = 235; //JapaneseCalendar::kCurrentEra;
+ if((gotYear!=1) || (gotEra != expectEra)) {
+ errln(UnicodeString("parse "+samplestr+" of 'y' as Japanese Calendar, expected year ") + expectYear +
+ UnicodeString(" and era ") + expectEra +", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str +")");
+ } else {
+ logln(UnicodeString() + " year: " + gotYear + ", era: " + gotEra);
+ }
+ delete fmt;
+ }
+ }
+
+ delete cal2;
+ delete cal;
+ delete fmt2;
+}
+
+
+
+
+/**
+ * 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("Coudln'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 ");
+}
+
+
void IntlCalendarTest::simpleTest(const Locale& loc, const UnicodeString& expect, UDate expectDate, UErrorCode& status)
{
UnicodeString tmp;