本文整理汇总了C++中SimpleDateFormat::parse方法的典型用法代码示例。如果您正苦于以下问题:C++ SimpleDateFormat::parse方法的具体用法?C++ SimpleDateFormat::parse怎么用?C++ SimpleDateFormat::parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SimpleDateFormat
的用法示例。
在下文中一共展示了SimpleDateFormat::parse方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SimpleDateFormat
/**
* @bug 4061287
*/
void DateFormatRegressionTest::Test4061287(void)
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status);
if (U_FAILURE(status)) {
dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status));
delete df;
return;
}
failure(status, "new SimpleDateFormat");
//try {
logln(UnicodeString("") + df->parse("35/01/1971", status));
failure(status, "df->parse(\"35/01/1971\")");
//logln(df.parse("35/01/1971").toString());
//}
/*catch (ParseException e) {
errln("Fail: " + e);
e.printStackTrace();
}*/
df->setLenient(FALSE);
UBool ok = FALSE;
//try {
logln(UnicodeString("") + df->parse("35/01/1971", status));
if(U_FAILURE(status))
ok = TRUE;
//logln(df.parse("35/01/1971").toString());
//} catch (ParseException e) {ok=TRUE;}
if(!ok)
errln("Fail: Lenient not working");
delete df;
}
示例2: pp
/**
* @bug 4104522
* CANNOT REPRODUCE
* According to the bug report, this test should throw a
* StringIndexOutOfBoundsException during the second parse. However,
* this is not seen.
*/
void DateFormatRegressionTest::Test4104522(void)
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *sdf = new SimpleDateFormat(status);
if(U_FAILURE(status)) {
dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
delete sdf;
return;
}
failure(status, "new SimpleDateFormat");
UnicodeString pattern = "'time' hh:mm";
sdf->applyPattern(pattern);
logln("pattern: \"" + pattern + "\"");
// works correctly
ParsePosition pp(0);
UnicodeString text = "time ";
UDate dt = sdf->parse(text, pp);
logln(" text: \"" + text + "\"" +
" date: " + dt);
// works wrong
pp.setIndex(0);
text = "time";
dt = sdf->parse(text, pp);
logln(" text: \"" + text + "\"" +
" date: " + dt);
delete sdf;
}
示例3: pos
/**
* @bug 4060212
*/
void DateFormatRegressionTest::Test4060212(void)
{
UnicodeString dateString = "1995-040.05:01:29";
logln( "dateString= " + dateString );
logln("Using yyyy-DDD.hh:mm:ss");
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *formatter = new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status);
if (failure(status, "new SimpleDateFormat", TRUE)) return;
ParsePosition pos(0);
UDate myDate = formatter->parse( dateString, pos );
UnicodeString myString;
DateFormat *fmt = DateFormat::createDateTimeInstance( DateFormat::FULL,
DateFormat::LONG);
if (fmt == NULL) {
dataerrln("Error calling DateFormat::createDateTimeInstance");
delete formatter;
return;
}
myString = fmt->format( myDate, myString);
logln( myString );
Calendar *cal = new GregorianCalendar(status);
failure(status, "new GregorianCalendar");
cal->setTime(myDate, status);
failure(status, "cal->setTime");
if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get"))
errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) +
" Want 40");
// this is an odd usage of "ddd" and it doesn't
// work now that date values are range checked per #3579.
logln("Using yyyy-ddd.hh:mm:ss");
delete formatter;
formatter = NULL;
formatter = new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status);
if(failure(status, "new SimpleDateFormat")) return;
pos.setIndex(0);
myDate = formatter->parse( dateString, pos );
myString = fmt->format( myDate, myString );
logln( myString );
cal->setTime(myDate, status);
failure(status, "cal->setTime");
if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get"))
errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) +
" Want 40");
delete formatter;
delete fmt;
delete cal;
}
示例4: TestPersianFormat
void IntlCalendarTest::TestPersianFormat() {
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale(" [email protected]=persian"), status);
CHECK(status, "creating date format instance");
SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("[email protected]=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 ");
}
示例5: samplestr
void IntlCalendarTest::TestJapanese3860()
{
Calendar *cal;
UErrorCode status = U_ZERO_ERROR;
cal = Calendar::createInstance("[email protected]=japanese", status);
CHECK(status, UnicodeString("Creating [email protected]=japanese calendar"));
Calendar *cal2 = cal->clone();
SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("HH:mm:ss.S MMMM d, yyyy G"), Locale("[email protected]=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("[email protected]=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("[email protected]=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;
}
示例6: TestJapaneseFormat
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"));
Calendar *cal2 = cal->clone();
delete cal;
cal = NULL;
// Test simple parse/format with adopt
UDate aDate = 999932400000.0;
SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yy G"), Locale("[email protected]=japanese"), status);
SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("[email protected]=gregorian"), status);
CHECK(status, "creating date format instance");
if(!fmt) {
errln("Coudln't create en_US instance");
} else {
UnicodeString str;
fmt2->format(aDate, str);
logln(UnicodeString() + "Test Date: " + str);
str.remove();
fmt->format(aDate, str);
logln(UnicodeString() + "as Japanese Calendar: " + str);
UnicodeString expected("September 8, 13 Heisei");
if(str != expected) {
errln("Expected " + expected + " but got " + str);
}
UDate otherDate = fmt->parse(expected, status);
if(otherDate != aDate) {
UnicodeString str3;
ParsePosition pp;
fmt->parse(expected, *cal2, pp);
fmt->format(otherDate, str3);
errln("Parse incorrect of " + expected + " - wanted " + aDate + " but got " + " = " + otherDate + ", " + str3 + " = " + CalendarTest::calToStr(*cal2) );
} else {
logln("Parsed OK: " + expected);
}
delete fmt;
}
// Test parse with incomplete information
fmt = new SimpleDateFormat(UnicodeString("G y"), Locale("[email protected]=japanese"), status);
aDate = -3197117222000.0;
CHECK(status, "creating date format instance");
if(!fmt) {
errln("Coudln't create en_US instance");
} else {
UnicodeString str;
fmt2->format(aDate, str);
logln(UnicodeString() + "Test Date: " + str);
str.remove();
fmt->format(aDate, str);
logln(UnicodeString() + "as Japanese Calendar: " + str);
UnicodeString expected("Meiji 1");
if(str != expected) {
errln("Expected " + expected + " but got " + str);
}
UDate otherDate = fmt->parse(expected, status);
if(otherDate != aDate) {
UnicodeString str3;
ParsePosition pp;
fmt->parse(expected, *cal2, pp);
fmt->format(otherDate, str3);
errln("Parse incorrect of " + expected + " - wanted " + aDate + " but got " + " = " +
otherDate + ", " + str3 + " = " + CalendarTest::calToStr(*cal2) );
} else {
logln("Parsed OK: " + expected);
}
delete fmt;
}
delete cal2;
delete fmt2;
CHECK(status, "Error occured");
// Now, try in Japanese
{
UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
UDate expectDate = 999932400000.0; // Testing a recent date
Locale loc("[email protected]=japanese");
status = U_ZERO_ERROR;
simpleTest(loc, expect, expectDate, status);
}
{
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
status = U_ZERO_ERROR;
simpleTest(loc, expect, expectDate, status);
}
{
UnicodeString expect = CharsToUnicodeString("\\u5b89\\u6c385\\u5e747\\u67084\\u65e5\\u6728\\u66dc\\u65e5");
UDate expectDate = -6106032422000.0; // 1776-07-04T00:00:00Z-075258
Locale loc("[email protected]=japanese");
//.........这里部分代码省略.........
示例7: TestBuddhistFormat
void IntlCalendarTest::TestBuddhistFormat() {
UErrorCode status = U_ZERO_ERROR;
// Test simple parse/format with adopt
// First, a contrived english test..
UDate aDate = 999932400000.0;
SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("[email protected]=buddhist"), status);
CHECK(status, "creating date format instance");
SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("[email protected]=gregorian"), status);
CHECK(status, "creating gregorian date format instance");
if(!fmt) {
errln("Coudln't create en_US instance");
} else {
UnicodeString str;
fmt2->format(aDate, str);
logln(UnicodeString() + "Test Date: " + str);
str.remove();
fmt->format(aDate, str);
logln(UnicodeString() + "as Buddhist Calendar: " + escape(str));
UnicodeString expected("September 8, 2544 BE");
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);
}
delete fmt;
}
delete fmt2;
CHECK(status, "Error occured testing Buddhist Calendar in English ");
status = U_ZERO_ERROR;
// Now, try in Thai
{
UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
" 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e1e.\\u0e28. 2544");
UDate expectDate = 999932400000.0;
Locale loc("th_TH_TRADITIONAL"); // legacy
simpleTest(loc, expect, expectDate, status);
}
status = U_ZERO_ERROR;
{
UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
" 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e1e.\\u0e28. 2544");
UDate expectDate = 999932400000.0;
Locale loc("[email protected]=buddhist");
simpleTest(loc, expect, expectDate, status);
}
status = U_ZERO_ERROR;
{
UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
" 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e04.\\u0e28. 2001");
UDate expectDate = 999932400000.0;
Locale loc("[email protected]=gregorian");
simpleTest(loc, expect, expectDate, status);
}
status = U_ZERO_ERROR;
{
UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
" 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e04.\\u0e28. 2001");
UDate expectDate = 999932400000.0;
Locale loc("[email protected]=gregorian");
simpleTest(loc, expect, expectDate, status);
}
}
示例8: pos
void
DateFormatMiscTests::test4097450()
{
//
// Date parse requiring 4 digit year.
//
UnicodeString dstring [] = {
UnicodeString("97"),
UnicodeString("1997"),
UnicodeString("97"),
UnicodeString("1997"),
UnicodeString("01"),
UnicodeString("2001"),
UnicodeString("01"),
UnicodeString("2001"),
UnicodeString("1"),
UnicodeString("1"),
UnicodeString("11"),
UnicodeString("11"),
UnicodeString("111"),
UnicodeString("111")
};
UnicodeString dformat [] = {
UnicodeString("yy"),
UnicodeString("yy"),
UnicodeString("yyyy"),
UnicodeString("yyyy"),
UnicodeString("yy"),
UnicodeString("yy"),
UnicodeString("yyyy"),
UnicodeString("yyyy"),
UnicodeString("yy"),
UnicodeString("yyyy"),
UnicodeString("yy"),
UnicodeString("yyyy"),
UnicodeString("yy"),
UnicodeString("yyyy")
};
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *formatter;
SimpleDateFormat *resultFormatter = new SimpleDateFormat((UnicodeString)"yyyy", status);
if (U_FAILURE(status)) {
dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status));
return;
}
logln("Format\tSource\tResult");
logln("-------\t-------\t-------");
for (int i = 0; i < 14/*dstring.length*/; i++)
{
log(dformat[i] + "\t" + dstring[i] + "\t");
formatter = new SimpleDateFormat(dformat[i], status);
if(failure(status, "new SimpleDateFormat")) return;
//try {
UnicodeString str;
FieldPosition pos(FieldPosition::DONT_CARE);
logln(resultFormatter->format(formatter->parse(dstring[i], status), str, pos));
failure(status, "resultFormatter->format");
//if ( !dresult[i] ) System.out.print(" <-- error!");
/*}
catch (ParseException exception) {
//if ( dresult[i] ) System.out.print(" <-- error!");
System.out.print("exception --> " + exception);
}*/
delete formatter;
logln();
}
delete resultFormatter;
}
示例9: pattern
void DateFormatRegressionTest::Test1684(void)
{
// July 2001 August 2001 January 2002
// Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
// 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 5
// 8 9 10 11 12 13 14 5 6 7 8 9 10 11 6 7 8 9 10 11 12
// 15 16 17 18 19 20 21 12 13 14 15 16 17 18 13 14 15 16 17 18 19
// 22 23 24 25 26 27 28 19 20 21 22 23 24 25 20 21 22 23 24 25 26
// 29 30 31 26 27 28 29 30 31 27 28 29 30 31
Test1684Data *tests[] = {
new Test1684Data(2001, 8, 6, 2001,8,2,UCAL_MONDAY, "2001 08 02 Mon", NULL),
new Test1684Data(2001, 8, 7, 2001,8,2,UCAL_TUESDAY, "2001 08 02 Tue", NULL),
new Test1684Data(2001, 8, 5,/*12,*/ 2001,8,2,UCAL_SUNDAY, "2001 08 02 Sun", NULL),
new Test1684Data(2001, 8,6, /*7, 30,*/ 2001,7,6,UCAL_MONDAY, "2001 07 06 Mon", "2001 08 02 Mon"),
new Test1684Data(2001, 8,7, /*7, 31,*/ 2001,7,6,UCAL_TUESDAY, "2001 07 06 Tue", "2001 08 02 Tue"),
new Test1684Data(2001, 8, 5, 2001,7,6,UCAL_SUNDAY, "2001 07 06 Sun", "2001 08 02 Sun"),
new Test1684Data(2001, 7, 30, 2001,8,1,UCAL_MONDAY, "2001 08 01 Mon", "2001 07 05 Mon"),
new Test1684Data(2001, 7, 31, 2001,8,1,UCAL_TUESDAY, "2001 08 01 Tue", "2001 07 05 Tue"),
new Test1684Data(2001, 7,29, /*8, 5,*/ 2001,8,1,UCAL_SUNDAY, "2001 08 01 Sun", "2001 07 05 Sun"),
new Test1684Data(2001, 12, 31, 2001,12,6,UCAL_MONDAY, "2001 12 06 Mon", NULL),
new Test1684Data(2002, 1, 1, 2002,1,1,UCAL_TUESDAY, "2002 01 01 Tue", NULL),
new Test1684Data(2002, 1, 2, 2002,1,1,UCAL_WEDNESDAY, "2002 01 01 Wed", NULL),
new Test1684Data(2002, 1, 3, 2002,1,1,UCAL_THURSDAY, "2002 01 01 Thu", NULL),
new Test1684Data(2002, 1, 4, 2002,1,1,UCAL_FRIDAY, "2002 01 01 Fri", NULL),
new Test1684Data(2002, 1, 5, 2002,1,1,UCAL_SATURDAY, "2002 01 01 Sat", NULL),
new Test1684Data(2001,12,30, /*2002, 1, 6,*/ 2002,1,1,UCAL_SUNDAY, "2002 01 01 Sun", "2001 12 06 Sun")
};
#define kTest1684Count ((int32_t)(sizeof(tests)/sizeof(tests[0])))
int32_t pass = 0, error = 0, warning = 0;
int32_t i;
UErrorCode status = U_ZERO_ERROR;
UnicodeString pattern("yyyy MM WW EEE","");
Calendar *cal = new GregorianCalendar(status);
SimpleDateFormat *sdf = new SimpleDateFormat(pattern,status);
if (U_FAILURE(status)) {
dataerrln("Error constructing SimpleDateFormat");
for(i=0;i<kTest1684Count;i++) {
delete tests[i];
}
delete cal;
delete sdf;
return;
}
cal->setFirstDayOfWeek(UCAL_SUNDAY);
cal->setMinimalDaysInFirstWeek(1);
sdf->adoptCalendar(cal);
cal = sdf->getCalendar()->clone(); // sdf may have deleted calendar
if(!cal || !sdf || U_FAILURE(status)) {
errln(UnicodeString("Error setting up test: ") + u_errorName(status));
}
for (i = 0; i < kTest1684Count; ++i) {
Test1684Data &test = *(tests[i]);
logln(UnicodeString("#") + i + UnicodeString("\n-----\nTesting round trip of ") + test.year +
" " + (test.month + 1) +
" " + test.date +
" (written as) " + test.data);
cal->clear();
cal->set(test.year, test.month, test.date);
UDate ms = cal->getTime(status);
cal->clear();
cal->set(UCAL_YEAR, test.womyear);
cal->set(UCAL_MONTH, test.wommon);
cal->set(UCAL_WEEK_OF_MONTH, test.wom);
cal->set(UCAL_DAY_OF_WEEK, test.dow);
UDate ms2 = cal->getTime(status);
if (ms2 != ms) {
errln((UnicodeString)"\nError: GregorianUCAL_DOM gave " + ms +
"\n GregorianUCAL_WOM gave " + ms2);
error++;
} else {
pass++;
}
ms2 = sdf->parse(test.data, status);
if(U_FAILURE(status)) {
errln("parse exception: " + UnicodeString(u_errorName(status)));
}
if (ms2!=ms) {
errln((UnicodeString)"\nError: GregorianCalendar gave " + ms +
"\n SimpleDateFormat.parse gave " + ms2);
error++;
} else {
pass++;
}
UnicodeString result;
sdf->format(ms, result);
if (result != test.normalized) {
errln("\nWarning: format of '" + test.data + "' gave" +
//.........这里部分代码省略.........
示例10: TestJapaneseFormat
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"));
Calendar *cal2 = cal->clone();
delete cal;
cal = NULL;
// Test simple parse/format with adopt
UDate aDate = 999932400000.0;
SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yy G"), Locale("[email protected]=japanese"), status);
SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("[email protected]=gregorian"), status);
CHECK(status, "creating date format instance");
if(!fmt) {
errln("Coudln't create en_US instance");
} else {
UnicodeString str;
fmt2->format(aDate, str);
logln(UnicodeString() + "Test Date: " + str);
str.remove();
fmt->format(aDate, str);
logln(UnicodeString() + "as Japanese Calendar: " + str);
UnicodeString expected("September 8, 13 Heisei");
if(str != expected) {
errln("Expected " + expected + " but got " + str);
}
UDate otherDate = fmt->parse(expected, status);
if(otherDate != aDate) {
UnicodeString str3;
ParsePosition pp;
fmt->parse(expected, *cal2, pp);
fmt->format(otherDate, str3);
errln("Parse incorrect of " + expected + " - wanted " + aDate + " but got " + " = " + otherDate + ", " + str3 + " = " + CalendarTest::calToStr(*cal2) );
} else {
logln("Parsed OK: " + expected);
}
delete fmt;
}
// Test parse with incomplete information
fmt = new SimpleDateFormat(UnicodeString("G y"), Locale("[email protected]=japanese"), status);
/* The test data below should points to 1868-09-08T00:00:00 in America/Los_Angeles.
* The time calculated by original test code uses -7:00 UTC offset, because it assumes
* DST is observed (because of a timezone bug, DST is observed for early 20th century
* day to infinite past time). The bug was fixed and DST is no longer used for time before
* 1900 for any zones. However, ICU timezone transition data is represented by 32-bit integer
* (sec) and cannot represent transitions before 1901 defined in Olson tzdata. For example,
* based on Olson definition, offset -7:52:58 should be used for Nov 18, 1883 or older dates.
* If ICU properly capture entire Olson zone definition, the start time of "Meiji 1" is
* -3197117222000. -Yoshito
*/
/* TODO: When ICU support the Olson LMT offset for America/Los_Angeles, we need to update
* the reference data.
*/
//aDate = -3197120400000.;
aDate = -3197116800000.;
CHECK(status, "creating date format instance");
if(!fmt) {
errln("Coudln't create en_US instance");
} else {
UnicodeString str;
fmt2->format(aDate, str);
logln(UnicodeString() + "Test Date: " + str);
str.remove();
fmt->format(aDate, str);
logln(UnicodeString() + "as Japanese Calendar: " + str);
UnicodeString expected("Meiji 1");
if(str != expected) {
errln("Expected " + expected + " but got " + str);
}
UDate otherDate = fmt->parse(expected, status);
if(otherDate != aDate) {
UnicodeString str3;
ParsePosition pp;
fmt->parse(expected, *cal2, pp);
fmt->format(otherDate, str3);
errln("Parse incorrect of " + expected + " - wanted " + aDate + " but got " + " = " +
otherDate + ", " + str3 + " = " + CalendarTest::calToStr(*cal2) );
} else {
logln("Parsed OK: " + expected);
}
delete fmt;
}
delete cal2;
delete fmt2;
CHECK(status, "Error occured");
// Now, try in Japanese
{
UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
UDate expectDate = 999932400000.0; // Testing a recent date
Locale loc("[email protected]=japanese");
status = U_ZERO_ERROR;
simpleTest(loc, expect, expectDate, status);
//.........这里部分代码省略.........