+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/***********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2010, International Business Machines Corporation
+ * Copyright (c) 1997-2014, International Business Machines Corporation
* and others. All Rights Reserved.
***********************************************************************/
#include "string.h"
#include "unicode/locid.h"
#include "japancal.h"
+#include "unicode/localpointer.h"
+#include "unicode/datefmt.h"
+#include "unicode/smpdtfmt.h"
+#include "unicode/dtptngen.h"
#if !UCONFIG_NO_FORMATTING
// *****************************************************************************
//--- move to CalendarTest?
-static const double JULIAN_EPOCH = -210866760000000.;
-
-
// Turn this on to dump the calendar fields
#define U_DEBUG_DUMPCALS
CASE(4,TestBuddhistFormat);
CASE(5,TestJapaneseFormat);
CASE(6,TestJapanese3860);
- CASE(7,TestPersian);
- CASE(8,TestPersianFormat);
- CASE(9,TestTaiwan);
+ CASE(7,TestForceGannenNumbering);
+ CASE(8,TestPersian);
+ CASE(9,TestPersianFormat);
+ CASE(10,TestTaiwan);
default: name = ""; break;
}
}
"en_US_VALLEYGIRL@collation=phonebook;calendar=gregorian",
"ja_JP@calendar=japanese",
"th_TH@calendar=buddhist",
- "ja_JP_TRADITIONAL",
"th_TH_TRADITIONAL",
"th_TH_TRADITIONAL@calendar=gregorian",
"en_US",
"gregorian",
"japanese",
"buddhist",
- "japanese",
"buddhist",
"gregorian",
"gregorian",
// Test simple parse/format with adopt
- // First, a contrived english test..
+ // First, a contrived English test..
UDate aDate = 999932400000.0;
SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("en_US@calendar=buddhist"), 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");
if(!fmt) {
- errln("Coudln't create en_US instance");
+ errln("Couldn't create en_US instance");
} else {
UnicodeString str;
fmt2->format(aDate, str);
}
delete fmt2;
- CHECK(status, "Error occured testing Buddhist Calendar in English ");
+ CHECK(status, "Error occurred testing Buddhist Calendar in English ");
status = U_ZERO_ERROR;
// Now, try in Thai
void IntlCalendarTest::TestJapaneseFormat() {
Calendar *cal;
UErrorCode status = U_ZERO_ERROR;
- cal = Calendar::createInstance("ja_JP_TRADITIONAL", status);
- CHECK(status, UnicodeString("Creating ja_JP_TRADITIONAL calendar"));
+ cal = Calendar::createInstance("ja_JP@calendar=japanese", status);
+ CHECK(status, UnicodeString("Creating ja_JP@calendar=japanese calendar"));
Calendar *cal2 = cal->clone();
delete cal;
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");
+ errln("Couldn't create en_US instance");
} else {
UnicodeString str;
fmt2->format(aDate, str);
delete cal2;
delete fmt2;
- CHECK(status, "Error occured");
+ CHECK(status, "Error occurred");
// Now, try in Japanese
{
- UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
+ UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5 \\u571f\\u66dc\\u65e5");
UDate expectDate = 999932400000.0; // Testing a recent date
Locale loc("ja_JP@calendar=japanese");
simpleTest(loc, expect, expectDate, status);
}
{
- UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
+ UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5 \\u571f\\u66dc\\u65e5");
UDate expectDate = 999932400000.0; // Testing a recent date
- Locale loc("ja_JP_TRADITIONAL"); // legacy
+ Locale loc("ja_JP@calendar=japanese");
status = U_ZERO_ERROR;
simpleTest(loc, expect, expectDate, status);
}
{
- UnicodeString expect = CharsToUnicodeString("\\u5b89\\u6c385\\u5e747\\u67084\\u65e5\\u6728\\u66dc\\u65e5");
+ UnicodeString expect = CharsToUnicodeString("\\u5b89\\u6c385\\u5e747\\u67084\\u65e5 \\u6728\\u66dc\\u65e5");
UDate expectDate = -6106032422000.0; // 1776-07-04T00:00:00Z-075258
Locale loc("ja_JP@calendar=japanese");
}
{ // Jitterbug 1869 - this is an ambiguous era. (Showa 64 = Jan 6 1989, but Showa could be 2 other eras) )
- UnicodeString expect = CharsToUnicodeString("\\u662d\\u548c64\\u5e741\\u67086\\u65e5\\u91d1\\u66dc\\u65e5");
+ UnicodeString expect = CharsToUnicodeString("\\u662d\\u548c64\\u5e741\\u67086\\u65e5 \\u91d1\\u66dc\\u65e5");
UDate expectDate = 600076800000.0;
Locale loc("ja_JP@calendar=japanese");
status = U_ZERO_ERROR;
simpleTest(loc, expect, expectDate, status);
+ }
+ { // 1989 Jan 9 Monday = Heisei 1; full is Gy年M月d日EEEE => 平成元年1月9日月曜日
+ UnicodeString expect = CharsToUnicodeString("\\u5E73\\u6210\\u5143\\u5E741\\u67089\\u65E5 \\u6708\\u66DC\\u65E5");
+ UDate expectDate = 600336000000.0;
+ Locale loc("ja_JP@calendar=japanese");
+
+ status = U_ZERO_ERROR;
+ simpleTest(loc, expect, expectDate, status);
+
}
{ // 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");
+ UnicodeString expect = CharsToUnicodeString("\\u5EB7\\u6B632\\u5e742\\u670829\\u65e5 \\u65e5\\u66dc\\u65e5");
UDate expectDate = -16214400422000.0; // 1456-03-09T00:00Z-075258
Locale loc("ja_JP@calendar=japanese");
// 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);
+ 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");
+ errln("Couldn't create en_US instance");
} else {
UErrorCode s2 = U_ZERO_ERROR;
cal2->clear();
- UnicodeString samplestr("1.1.9");
+ UnicodeString samplestr("1/5/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:");
+ CHECK(s2, "parsing the 1/5/9 string");
+ logln("*cal2 after 159 parse:");
str.remove();
fmt2->format(aDate, str);
logln(UnicodeString() + "as Gregorian Calendar: " + str);
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 +
+ 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);
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");
+ errln("Couldn't create en_US instance");
} else {
UErrorCode s2 = U_ZERO_ERROR;
cal2->clear();
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;
+ int32_t expectEra = JapaneseCalendar::getCurrentEra();
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 +")");
delete fmt2;
}
+void IntlCalendarTest::TestForceGannenNumbering()
+{
+ UErrorCode status;
+ const char* locID = "ja_JP@calendar=japanese";
+ Locale loc(locID);
+ UDate refDate = 600336000000.0; // 1989 Jan 9 Monday = Heisei 1
+ UnicodeString patText(u"Gy年M月d日",-1);
+ UnicodeString patNumr(u"GGGGGy/MM/dd",-1);
+ UnicodeString skelText(u"yMMMM",-1);
+
+ // Test Gannen year forcing
+ status = U_ZERO_ERROR;
+ LocalPointer<SimpleDateFormat> testFmt1(new SimpleDateFormat(patText, loc, status));
+ LocalPointer<SimpleDateFormat> testFmt2(new SimpleDateFormat(patNumr, loc, status));
+ if (U_FAILURE(status)) {
+ dataerrln("Fail in new SimpleDateFormat locale %s: %s", locID, u_errorName(status));
+ } else {
+ UnicodeString testString1, testString2;
+ testString1 = testFmt1->format(refDate, testString1);
+ if (testString1.length() < 3 || testString1.charAt(2) != 0x5143) {
+ errln(UnicodeString("Formatting year 1 in created text style, got " + testString1 + " but expected 3rd char to be 0x5143"));
+ }
+ testString2 = testFmt2->format(refDate, testString2);
+ if (testString2.length() < 2 || testString2.charAt(1) != 0x0031) {
+ errln(UnicodeString("Formatting year 1 in created numeric style, got " + testString2 + " but expected 2nd char to be 1"));
+ }
+ // Now switch the patterns and verify that Gannen use follows the pattern
+ testFmt1->applyPattern(patNumr);
+ testString1.remove();
+ testString1 = testFmt1->format(refDate, testString1);
+ if (testString1.length() < 2 || testString1.charAt(1) != 0x0031) {
+ errln(UnicodeString("Formatting year 1 in applied numeric style, got " + testString1 + " but expected 2nd char to be 1"));
+ }
+ testFmt2->applyPattern(patText);
+ testString2.remove();
+ testString2 = testFmt2->format(refDate, testString2);
+ if (testString2.length() < 3 || testString2.charAt(2) != 0x5143) {
+ errln(UnicodeString("Formatting year 1 in applied text style, got " + testString2 + " but expected 3rd char to be 0x5143"));
+ }
+ }
-
+ // Test disabling of Gannen year forcing
+ status = U_ZERO_ERROR;
+ LocalPointer<DateTimePatternGenerator> dtpgen(DateTimePatternGenerator::createInstance(loc, status));
+ if (U_FAILURE(status)) {
+ dataerrln("Fail in DateTimePatternGenerator::createInstance locale %s: %s", locID, u_errorName(status));
+ } else {
+ UnicodeString pattern = dtpgen->getBestPattern(skelText, status);
+ if (U_FAILURE(status)) {
+ dataerrln("Fail in DateTimePatternGenerator::getBestPattern locale %s: %s", locID, u_errorName(status));
+ } else {
+ // Use override string of ""
+ LocalPointer<SimpleDateFormat> testFmt3(new SimpleDateFormat(pattern, UnicodeString(""), loc, status));
+ if (U_FAILURE(status)) {
+ dataerrln("Fail in new SimpleDateFormat locale %s: %s", locID, u_errorName(status));
+ } else {
+ UnicodeString testString3;
+ testString3 = testFmt3->format(refDate, testString3);
+ if (testString3.length() < 3 || testString3.charAt(2) != 0x0031) {
+ errln(UnicodeString("Formatting year 1 with Gannen disabled, got " + testString3 + " but expected 3rd char to be 1"));
+ }
+ }
+ }
+ }
+}
/**
* Verify the Persian Calendar.
" is not within sampled times [" + timeA + " to " + timeB + "]!");
}
// end sanity check
-// quasiGregorianTest(*cal,Locale("ja_JP"),data);
+
+ // 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() {
UnicodeString gregorianDate("January 18, 2007 AD");
UDate aDate = fmt2->parse(gregorianDate, status);
if(!fmt) {
- errln("Coudln't create en_US instance");
+ errln("Couldn't create en_US instance");
} else {
UnicodeString str;
fmt->format(aDate, str);
}
d = fmt2->parse(expect,status);
- CHECK(status, "Error occured parsing " + UnicodeString(loc.getName()));
+ CHECK(status, "Error occurred parsing " + UnicodeString(loc.getName()));
if(d != expectDate) {
fmt2->format(d,tmp);
errln(UnicodeString("Failed to parse " ) + escape(expect) + ", " + loc.getName() + " expect " + (double)expectDate + " got " + (double)d + " " + escape(tmp));