]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/incaltst.cpp
1 /***********************************************************************
3 * Copyright (c) 1997-2005, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
7 /* Test Internationalized Calendars for C++ */
9 #include "unicode/utypes.h"
11 #include "unicode/locid.h"
14 #if !UCONFIG_NO_FORMATTING
19 #define CHECK(status, msg) \
20 if (U_FAILURE(status)) { \
21 errln((UnicodeString(u_errorName(status)) + UnicodeString(" : " ) )+ msg); \
26 static UnicodeString
escape( const UnicodeString
&src
)
30 for (int32_t i
= 0; i
< src
.length(); ++i
) {
35 dst
+= UnicodeString("[");
37 sprintf(buf
, "%#x", c
);
38 dst
+= UnicodeString(buf
);
39 dst
+= UnicodeString("]");
48 #include "unicode/gregocal.h"
49 #include "unicode/smpdtfmt.h"
50 #include "unicode/simpletz.h"
52 // *****************************************************************************
53 // class IntlCalendarTest
54 // *****************************************************************************
55 //--- move to CalendarTest?
57 static const double JULIAN_EPOCH
= -210866760000000.;
60 // Turn this on to dump the calendar fields
61 #define U_DEBUG_DUMPCALS
64 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break
67 void IntlCalendarTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
69 if (exec
) logln("TestSuite IntlCalendarTest");
72 CASE(1,TestGregorian
);
75 CASE(4,TestBuddhistFormat
);
76 CASE(5,TestJapaneseFormat
);
77 CASE(6,TestJapanese3860
);
78 default: name
= ""; break;
84 // ---------------------------------------------------------------------------------
88 * Test various API methods for API completeness.
91 IntlCalendarTest::TestTypes()
94 UErrorCode status
= U_ZERO_ERROR
;
96 const char *locs
[40] = { "en_US_VALLEYGIRL",
97 "en_US_VALLEYGIRL@collation=phonebook;calendar=japanese",
98 "en_US_VALLEYGIRL@collation=phonebook;calendar=gregorian",
99 "ja_JP@calendar=japanese",
100 "th_TH@calendar=buddhist",
103 "th_TH_TRADITIONAL@calendar=gregorian",
105 const char *types
[40] = { "gregorian",
115 for(j
=0;locs
[j
];j
++) {
116 logln(UnicodeString("Creating calendar of locale ") + locs
[j
]);
117 status
= U_ZERO_ERROR
;
118 c
= Calendar::createInstance(locs
[j
], status
);
119 CHECK(status
, "creating '" + UnicodeString(locs
[j
]) + "' calendar");
120 if(U_SUCCESS(status
)) {
121 logln(UnicodeString(" type is ") + c
->getType());
122 if(strcmp(c
->getType(), types
[j
])) {
123 errln(UnicodeString(locs
[j
]) + UnicodeString("Calendar type ") + c
->getType() + " instead of " + types
[j
]);
133 * Run a test of a quasi-Gregorian calendar. This is a calendar
134 * that behaves like a Gregorian but has different year/era mappings.
135 * The int[] data array should have the format:
137 * { era, year, gregorianYear, month, dayOfMonth, ... ... , -1 }
139 void IntlCalendarTest::quasiGregorianTest(Calendar
& cal
, const Locale
& gcl
, const int32_t *data
) {
140 UErrorCode status
= U_ZERO_ERROR
;
141 // As of JDK 1.4.1_01, using the Sun JDK GregorianCalendar as
142 // a reference throws us off by one hour. This is most likely
143 // due to the JDK 1.4 incorporation of historical time zones.
144 //java.util.Calendar grego = java.util.Calendar.getInstance();
145 Calendar
*grego
= Calendar::createInstance(gcl
, status
);
146 if (U_FAILURE(status
)) {
147 dataerrln("Error calling Calendar::createInstance");
151 int32_t tz1
= cal
.get(UCAL_ZONE_OFFSET
,status
);
152 int32_t tz2
= grego
-> get (UCAL_ZONE_OFFSET
, status
);
154 errln((UnicodeString
)"cal's tz " + tz1
+ " != grego's tz " + tz2
);
157 for (int32_t i
=0; data
[i
]!=-1; ) {
158 int32_t era
= data
[i
++];
159 int32_t year
= data
[i
++];
160 int32_t gregorianYear
= data
[i
++];
161 int32_t month
= data
[i
++];
162 int32_t dayOfMonth
= data
[i
++];
165 grego
->set(gregorianYear
, month
, dayOfMonth
);
166 UDate D
= grego
->getTime(status
);
169 cal
.set(UCAL_ERA
, era
);
170 cal
.set(year
, month
, dayOfMonth
);
171 UDate d
= cal
.getTime(status
);
172 #ifdef U_DEBUG_DUMPCALS
173 logln((UnicodeString
)"cal : " + CalendarTest::calToStr(cal
));
174 logln((UnicodeString
)"grego: " + CalendarTest::calToStr(*grego
));
177 logln(UnicodeString("OK: ") + era
+ ":" + year
+ "/" + (month
+1) + "/" + dayOfMonth
+
178 " => " + d
+ " (" + UnicodeString(cal
.getType()) + ")");
180 errln(UnicodeString("Fail: (fields to millis)") + era
+ ":" + year
+ "/" + (month
+1) + "/" + dayOfMonth
+
181 " => " + d
+ ", expected " + D
+ " (" + UnicodeString(cal
.getType()) + "Off by: " + (d
-D
));
184 // Now, set the gregorian millis on the other calendar
186 cal
.setTime(D
, status
);
187 int e
= cal
.get(UCAL_ERA
, status
);
188 int y
= cal
.get(UCAL_YEAR
, status
);
189 #ifdef U_DEBUG_DUMPCALS
190 logln((UnicodeString
)"cal : " + CalendarTest::calToStr(cal
));
191 logln((UnicodeString
)"grego: " + CalendarTest::calToStr(*grego
));
193 if (y
== year
&& e
== era
) {
194 logln((UnicodeString
)"OK: " + D
+ " => " + cal
.get(UCAL_ERA
, status
) + ":" +
195 cal
.get(UCAL_YEAR
, status
) + "/" +
196 (cal
.get(UCAL_MONTH
, status
)+1) + "/" + cal
.get(UCAL_DATE
, status
) + " (" + UnicodeString(cal
.getType()) + ")");
198 errln((UnicodeString
)"Fail: (millis to fields)" + D
+ " => " + cal
.get(UCAL_ERA
, status
) + ":" +
199 cal
.get(UCAL_YEAR
, status
) + "/" +
200 (cal
.get(UCAL_MONTH
, status
)+1) + "/" + cal
.get(UCAL_DATE
, status
) +
201 ", expected " + era
+ ":" + year
+ "/" + (month
+1) + "/" +
202 dayOfMonth
+ " (" + UnicodeString(cal
.getType()));
206 CHECK(status
, "err during quasiGregorianTest()");
209 // Verify that Gregorian works like Gregorian
210 void IntlCalendarTest::TestGregorian() {
211 UDate timeA
= Calendar::getNow();
213 GregorianCalendar::AD
, 1868, 1868, UCAL_SEPTEMBER
, 8,
214 GregorianCalendar::AD
, 1868, 1868, UCAL_SEPTEMBER
, 9,
215 GregorianCalendar::AD
, 1869, 1869, UCAL_JUNE
, 4,
216 GregorianCalendar::AD
, 1912, 1912, UCAL_JULY
, 29,
217 GregorianCalendar::AD
, 1912, 1912, UCAL_JULY
, 30,
218 GregorianCalendar::AD
, 1912, 1912, UCAL_AUGUST
, 1,
219 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
223 UErrorCode status
= U_ZERO_ERROR
;
224 cal
= Calendar::createInstance(/*"de_DE", */ status
);
225 CHECK(status
, UnicodeString("Creating de_CH calendar"));
226 // Sanity check the calendar
227 UDate timeB
= Calendar::getNow();
228 UDate timeCal
= cal
->getTime(status
);
230 if(!(timeA
<= timeCal
) || !(timeCal
<= timeB
)) {
231 errln((UnicodeString
)"Error: Calendar time " + timeCal
+
232 " is not within sampled times [" + timeA
+ " to " + timeB
+ "]!");
236 // Note, the following is a good way to test the sanity of the constructed calendars,
237 // using Collation as a delay-loop:
239 // $ intltest format/IntlCalendarTest collate/G7CollationTest format/IntlCalendarTest
241 quasiGregorianTest(*cal
,Locale("fr_FR"),data
);
246 * Verify that BuddhistCalendar shifts years to Buddhist Era but otherwise
247 * behaves like GregorianCalendar.
249 void IntlCalendarTest::TestBuddhist() {
250 // BE 2542 == 1999 CE
251 UDate timeA
= Calendar::getNow();
254 0, // B. era [928479600000]
260 0, // B. era [-79204842000000]
263 UCAL_FEBRUARY
, // month
266 0, // test month calculation: 4795 BE = 4252 AD is a leap year, but 4795 AD is not.
267 4795, // BE [72018057600000]
272 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
275 UErrorCode status
= U_ZERO_ERROR
;
276 cal
= Calendar::createInstance("th_TH@calendar=buddhist", status
);
277 CHECK(status
, UnicodeString("Creating th_TH@calendar=buddhist calendar"));
279 // Sanity check the calendar
280 UDate timeB
= Calendar::getNow();
281 UDate timeCal
= cal
->getTime(status
);
283 if(!(timeA
<= timeCal
) || !(timeCal
<= timeB
)) {
284 errln((UnicodeString
)"Error: Calendar time " + timeCal
+
285 " is not within sampled times [" + timeA
+ " to " + timeB
+ "]!");
290 quasiGregorianTest(*cal
,Locale("th_TH"),data
);
295 * Verify that JapaneseCalendar shifts years to Japanese Eras but otherwise
296 * behaves like GregorianCalendar.
298 void IntlCalendarTest::TestJapanese() {
299 UDate timeA
= Calendar::getNow();
301 /* Sorry.. japancal.h is private! */
302 #define JapaneseCalendar_MEIJI 232
303 #define JapaneseCalendar_TAISHO 233
304 #define JapaneseCalendar_SHOWA 234
305 #define JapaneseCalendar_HEISEI 235
307 // BE 2542 == 1999 CE
309 // Jera Jyr Gyear m d
310 JapaneseCalendar_MEIJI
, 1, 1868, UCAL_SEPTEMBER
, 8,
311 JapaneseCalendar_MEIJI
, 1, 1868, UCAL_SEPTEMBER
, 9,
312 JapaneseCalendar_MEIJI
, 2, 1869, UCAL_JUNE
, 4,
313 JapaneseCalendar_MEIJI
, 45, 1912, UCAL_JULY
, 29,
314 JapaneseCalendar_TAISHO
, 1, 1912, UCAL_JULY
, 30,
315 JapaneseCalendar_TAISHO
, 1, 1912, UCAL_AUGUST
, 1,
317 // new tests (not in java)
318 JapaneseCalendar_SHOWA
, 64, 1989, UCAL_JANUARY
, 7, // Test current era transition (different code path than others)
319 JapaneseCalendar_HEISEI
, 1, 1989, UCAL_JANUARY
, 8,
320 JapaneseCalendar_HEISEI
, 1, 1989, UCAL_JANUARY
, 9,
321 JapaneseCalendar_HEISEI
, 1, 1989, UCAL_DECEMBER
, 20,
322 JapaneseCalendar_HEISEI
, 15, 2003, UCAL_MAY
, 22,
323 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
327 UErrorCode status
= U_ZERO_ERROR
;
328 cal
= Calendar::createInstance("ja_JP@calendar=japanese", status
);
329 CHECK(status
, UnicodeString("Creating ja_JP@calendar=japanese calendar"));
330 // Sanity check the calendar
331 UDate timeB
= Calendar::getNow();
332 UDate timeCal
= cal
->getTime(status
);
334 if(!(timeA
<= timeCal
) || !(timeCal
<= timeB
)) {
335 errln((UnicodeString
)"Error: Calendar time " + timeCal
+
336 " is not within sampled times [" + timeA
+ " to " + timeB
+ "]!");
339 quasiGregorianTest(*cal
,Locale("ja_JP"),data
);
343 void IntlCalendarTest::TestBuddhistFormat() {
344 UErrorCode status
= U_ZERO_ERROR
;
346 // Test simple parse/format with adopt
348 // First, a contrived english test..
349 UDate aDate
= 999932400000.0;
350 SimpleDateFormat
*fmt
= new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("en_US@calendar=buddhist"), status
);
351 CHECK(status
, "creating date format instance");
352 SimpleDateFormat
*fmt2
= new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("en_US@calendar=gregorian"), status
);
353 CHECK(status
, "creating gregorian date format instance");
355 errln("Coudln't create en_US instance");
358 fmt2
->format(aDate
, str
);
359 logln(UnicodeString() + "Test Date: " + str
);
361 fmt
->format(aDate
, str
);
362 logln(UnicodeString() + "as Buddhist Calendar: " + escape(str
));
363 UnicodeString
expected("September 8, 2544 BE");
364 if(str
!= expected
) {
365 errln("Expected " + escape(expected
) + " but got " + escape(str
));
367 UDate otherDate
= fmt
->parse(expected
, status
);
368 if(otherDate
!= aDate
) {
370 fmt
->format(otherDate
, str3
);
371 errln("Parse incorrect of " + escape(expected
) + " - wanted " + aDate
+ " but got " + otherDate
+ ", " + escape(str3
));
373 logln("Parsed OK: " + expected
);
379 CHECK(status
, "Error occured testing Buddhist Calendar in English ");
381 status
= U_ZERO_ERROR
;
384 UnicodeString expect
= CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
385 " 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e1e.\\u0e28. 2544");
386 UDate expectDate
= 999932400000.0;
387 Locale
loc("th_TH_TRADITIONAL"); // legacy
389 simpleTest(loc
, expect
, expectDate
, status
);
391 status
= U_ZERO_ERROR
;
393 UnicodeString expect
= CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
394 " 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e1e.\\u0e28. 2544");
395 UDate expectDate
= 999932400000.0;
396 Locale
loc("th_TH@calendar=buddhist");
398 simpleTest(loc
, expect
, expectDate
, status
);
400 status
= U_ZERO_ERROR
;
402 UnicodeString expect
= CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
403 " 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e04.\\u0e28. 2001");
404 UDate expectDate
= 999932400000.0;
405 Locale
loc("th_TH@calendar=gregorian");
407 simpleTest(loc
, expect
, expectDate
, status
);
409 status
= U_ZERO_ERROR
;
411 UnicodeString expect
= CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
412 " 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e04.\\u0e28. 2001");
413 UDate expectDate
= 999932400000.0;
414 Locale
loc("th_TH_TRADITIONAL@calendar=gregorian");
416 simpleTest(loc
, expect
, expectDate
, status
);
421 void IntlCalendarTest::TestJapaneseFormat() {
423 UErrorCode status
= U_ZERO_ERROR
;
424 cal
= Calendar::createInstance("ja_JP_TRADITIONAL", status
);
425 CHECK(status
, UnicodeString("Creating ja_JP_TRADITIONAL calendar"));
427 Calendar
*cal2
= cal
->clone();
431 // Test simple parse/format with adopt
433 UDate aDate
= 999932400000.0;
434 SimpleDateFormat
*fmt
= new SimpleDateFormat(UnicodeString("MMMM d, yy G"), Locale("en_US@calendar=japanese"), status
);
435 SimpleDateFormat
*fmt2
= new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("en_US@calendar=gregorian"), status
);
436 CHECK(status
, "creating date format instance");
438 errln("Coudln't create en_US instance");
441 fmt2
->format(aDate
, str
);
442 logln(UnicodeString() + "Test Date: " + str
);
444 fmt
->format(aDate
, str
);
445 logln(UnicodeString() + "as Japanese Calendar: " + str
);
446 UnicodeString
expected("September 8, 13 Heisei");
447 if(str
!= expected
) {
448 errln("Expected " + expected
+ " but got " + str
);
450 UDate otherDate
= fmt
->parse(expected
, status
);
451 if(otherDate
!= aDate
) {
454 fmt
->parse(expected
, *cal2
, pp
);
455 fmt
->format(otherDate
, str3
);
456 errln("Parse incorrect of " + expected
+ " - wanted " + aDate
+ " but got " + " = " + otherDate
+ ", " + str3
+ " = " + CalendarTest::calToStr(*cal2
) );
459 logln("Parsed OK: " + expected
);
464 // Test parse with incomplete information
465 fmt
= new SimpleDateFormat(UnicodeString("G y"), Locale("en_US@calendar=japanese"), status
);
466 /* The test data below should points to 1868-09-08T00:00:00 in America/Los_Angeles.
467 * The time calculated by original test code uses -7:00 UTC offset, because it assumes
468 * DST is observed (because of a timezone bug, DST is observed for early 20th century
469 * day to infinite past time). The bug was fixed and DST is no longer used for time before
470 * 1900 for any zones. However, ICU timezone transition data is represented by 32-bit integer
471 * (sec) and cannot represent transitions before 1901 defined in Olson tzdata. For example,
472 * based on Olson definition, offset -7:52:58 should be used for Nov 18, 1883 or older dates.
473 * If ICU properly capture entire Olson zone definition, the start time of "Meiji 1" is
474 * -3197117222000. -Yoshito
476 /* TODO: When ICU support the Olson LMT offset for America/Los_Angeles, we need to update
477 * the reference data.
479 //aDate = -3197120400000.;
480 aDate
= -3197116800000.;
481 CHECK(status
, "creating date format instance");
483 errln("Coudln't create en_US instance");
486 fmt2
->format(aDate
, str
);
487 logln(UnicodeString() + "Test Date: " + str
);
489 fmt
->format(aDate
, str
);
490 logln(UnicodeString() + "as Japanese Calendar: " + str
);
491 UnicodeString
expected("Meiji 1");
492 if(str
!= expected
) {
493 errln("Expected " + expected
+ " but got " + str
);
495 UDate otherDate
= fmt
->parse(expected
, status
);
496 if(otherDate
!= aDate
) {
499 fmt
->parse(expected
, *cal2
, pp
);
500 fmt
->format(otherDate
, str3
);
501 errln("Parse incorrect of " + expected
+ " - wanted " + aDate
+ " but got " + " = " +
502 otherDate
+ ", " + str3
+ " = " + CalendarTest::calToStr(*cal2
) );
504 logln("Parsed OK: " + expected
);
511 CHECK(status
, "Error occured");
513 // Now, try in Japanese
515 UnicodeString expect
= CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
516 UDate expectDate
= 999932400000.0; // Testing a recent date
517 Locale
loc("ja_JP@calendar=japanese");
519 status
= U_ZERO_ERROR
;
520 simpleTest(loc
, expect
, expectDate
, status
);
523 UnicodeString expect
= CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
524 UDate expectDate
= 999932400000.0; // Testing a recent date
525 Locale
loc("ja_JP_TRADITIONAL"); // legacy
527 status
= U_ZERO_ERROR
;
528 simpleTest(loc
, expect
, expectDate
, status
);
531 UnicodeString expect
= CharsToUnicodeString("\\u5b89\\u6c385\\u5e747\\u67084\\u65e5\\u6728\\u66dc\\u65e5");
532 //UDate expectDate = -6106035600000.0;
533 UDate expectDate
= -6106032000000.0; // 1776-07-04T00:00:00Z-0800
534 Locale
loc("ja_JP@calendar=japanese");
536 status
= U_ZERO_ERROR
;
537 simpleTest(loc
, expect
, expectDate
, status
);
540 { // Jitterbug 1869 - this is an ambiguous era. (Showa 64 = Jan 6 1989, but Showa could be 2 other eras) )
541 UnicodeString expect
= CharsToUnicodeString("\\u662d\\u548c64\\u5e741\\u67086\\u65e5\\u91d1\\u66dc\\u65e5");
542 UDate expectDate
= 600076800000.0;
543 Locale
loc("ja_JP@calendar=japanese");
545 status
= U_ZERO_ERROR
;
546 simpleTest(loc
, expect
, expectDate
, status
);
549 { // This Feb 29th falls on a leap year by gregorian year, but not by Japanese year.
550 UnicodeString expect
= CharsToUnicodeString("\\u5EB7\\u6B632\\u5e742\\u670829\\u65e5\\u65e5\\u66dc\\u65e5");
551 // Add -1:00 to the following for historical TZ - aliu
552 //UDate expectDate = -16214403600000.0; // courtesy of date format round trip test
553 UDate expectDate
= -16214400000000.0; // 1456-03-09T00:00:00Z-0800
554 Locale
loc("ja_JP@calendar=japanese");
556 status
= U_ZERO_ERROR
;
557 simpleTest(loc
, expect
, expectDate
, status
);
562 void IntlCalendarTest::TestJapanese3860()
565 UErrorCode status
= U_ZERO_ERROR
;
566 cal
= Calendar::createInstance("ja_JP@calendar=japanese", status
);
567 CHECK(status
, UnicodeString("Creating ja_JP@calendar=japanese calendar"));
568 Calendar
*cal2
= cal
->clone();
569 SimpleDateFormat
*fmt2
= new SimpleDateFormat(UnicodeString("HH:mm:ss.S MMMM d, yyyy G"), Locale("en_US@calendar=gregorian"), status
);
574 // Test simple parse/format with adopt
577 // Test parse with missing era (should default to current era, heisei)
578 // Test parse with incomplete information
579 logln("Testing parse w/ missing era...");
580 SimpleDateFormat
*fmt
= new SimpleDateFormat(UnicodeString("y.M.d"), Locale("ja_JP@calendar=japanese"), status
);
581 CHECK(status
, "creating date format instance");
583 errln("Coudln't create en_US instance");
585 UErrorCode s2
= U_ZERO_ERROR
;
587 UnicodeString
samplestr("1.1.9");
588 logln(UnicodeString() + "Test Year: " + samplestr
);
589 aDate
= fmt
->parse(samplestr
, s2
);
591 fmt
->parse(samplestr
, *cal2
, pp
);
592 CHECK(s2
, "parsing the 1.1.9 string");
593 logln("*cal2 after 119 parse:");
595 fmt2
->format(aDate
, str
);
596 logln(UnicodeString() + "as Gregorian Calendar: " + str
);
598 cal2
->setTime(aDate
, s2
);
599 int32_t gotYear
= cal2
->get(UCAL_YEAR
, s2
);
600 int32_t gotEra
= cal2
->get(UCAL_ERA
, s2
);
601 int32_t expectYear
= 1;
602 int32_t expectEra
= JapaneseCalendar::kCurrentEra
;
603 if((gotYear
!=1) || (gotEra
!= expectEra
)) {
604 errln(UnicodeString("parse "+samplestr
+" of 'y.m.d' as Japanese Calendar, expected year ") + expectYear
+
605 UnicodeString(" and era ") + expectEra
+", but got year " + gotYear
+ " and era " + gotEra
+ " (Gregorian:" + str
+")");
607 logln(UnicodeString() + " year: " + gotYear
+ ", era: " + gotEra
);
614 // this will NOT work - *all the time*. If it is the 1st of the month, for example it will get Jan 1 heisei 1 => jan 1 showa 64, wrong era.
616 // Test simple parse/format with adopt
619 // Test parse with missing era (should default to current era, heisei)
620 // Test parse with incomplete information
621 logln("Testing parse w/ just year...");
622 SimpleDateFormat
*fmt
= new SimpleDateFormat(UnicodeString("y"), Locale("ja_JP@calendar=japanese"), status
);
623 CHECK(status
, "creating date format instance");
625 errln("Coudln't create en_US instance");
627 UErrorCode s2
= U_ZERO_ERROR
;
629 UnicodeString
samplestr("1");
630 logln(UnicodeString() + "Test Year: " + samplestr
);
631 aDate
= fmt
->parse(samplestr
, s2
);
633 fmt
->parse(samplestr
, *cal2
, pp
);
634 CHECK(s2
, "parsing the 1 string");
635 logln("*cal2 after 1 parse:");
637 fmt2
->format(aDate
, str
);
638 logln(UnicodeString() + "as Gregorian Calendar: " + str
);
640 cal2
->setTime(aDate
, s2
);
641 int32_t gotYear
= cal2
->get(UCAL_YEAR
, s2
);
642 int32_t gotEra
= cal2
->get(UCAL_ERA
, s2
);
643 int32_t expectYear
= 1;
644 int32_t expectEra
= JapaneseCalendar::kCurrentEra
;
645 if((gotYear
!=1) || (gotEra
!= expectEra
)) {
646 errln(UnicodeString("parse "+samplestr
+" of 'y' as Japanese Calendar, expected year ") + expectYear
+
647 UnicodeString(" and era ") + expectEra
+", but got year " + gotYear
+ " and era " + gotEra
+ " (Gregorian:" + str
+")");
649 logln(UnicodeString() + " year: " + gotYear
+ ", era: " + gotEra
);
660 void IntlCalendarTest::simpleTest(const Locale
& loc
, const UnicodeString
& expect
, UDate expectDate
, UErrorCode
& status
)
664 DateFormat
*fmt0
= DateFormat::createDateTimeInstance(DateFormat::kFull
, DateFormat::kFull
);
666 logln("Try format/parse of " + (UnicodeString
)loc
.getName());
667 DateFormat
*fmt2
= DateFormat::createDateInstance(DateFormat::kFull
, loc
);
669 fmt2
->format(expectDate
, tmp
);
670 logln(escape(tmp
) + " ( in locale " + loc
.getName() + ")");
672 errln(UnicodeString("Failed to format " ) + loc
.getName() + " expected " + escape(expect
) + " got " + escape(tmp
) );
675 d
= fmt2
->parse(expect
,status
);
676 CHECK(status
, "Error occured parsing " + UnicodeString(loc
.getName()));
677 if(d
!= expectDate
) {
679 errln(UnicodeString("Failed to parse " ) + escape(expect
) + ", " + loc
.getName() + " expect " + (double)expectDate
+ " got " + (double)d
+ " " + escape(tmp
));
680 logln( "wanted " + escape(fmt0
->format(expectDate
,tmp
.remove())) + " but got " + escape(fmt0
->format(d
,tmp
.remove())));
684 errln((UnicodeString
)"Can't create " + loc
.getName() + " date instance");
691 #endif /* #if !UCONFIG_NO_FORMATTING */