#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "dtfmrgts.h"
#include "unicode/timezone.h"
#include "unicode/gregocal.h"
#include "unicode/smpdtfmt.h"
#include "unicode/datefmt.h"
#include "unicode/simpletz.h"
#include "unicode/resbund.h"
#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
void
DateFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* )
{
switch (index) {
CASE(0,Test4029195)
CASE(1,Test4052408)
CASE(2,Test4056591)
CASE(3,Test4059917)
CASE(4,Test4060212)
CASE(5,Test4061287)
CASE(6,Test4065240)
CASE(7,Test4071441)
CASE(8,Test4073003)
CASE(9,Test4089106)
CASE(10,Test4100302)
CASE(11,Test4101483)
CASE(12,Test4103340)
CASE(13,Test4103341)
CASE(14,Test4104136)
CASE(15,Test4104522)
CASE(16,Test4106807)
CASE(17,Test4108407)
CASE(18,Test4134203)
CASE(19,Test4151631)
CASE(20,Test4151706)
CASE(21,Test4162071)
CASE(22,Test4182066)
CASE(23,Test4210209)
CASE(24,Test714)
CASE(25,Test1684)
default: name = ""; break;
}
}
void DateFormatRegressionTest::Test4029195(void)
{
UErrorCode status = U_ZERO_ERROR;
UDate today = Calendar::getNow();
logln((UnicodeString) "today: " + today);
SimpleDateFormat *sdf = (SimpleDateFormat*) DateFormat::createDateInstance();
failure(status, "SimpleDateFormat::createDateInstance");
UnicodeString pat;
pat = sdf->toPattern(pat);
logln("pattern: " + pat);
UnicodeString fmtd;
FieldPosition pos(FieldPosition::DONT_CARE);
fmtd = sdf->format(today, fmtd, pos);
logln("today: " + fmtd);
sdf->applyPattern("G yyyy DDD");
UnicodeString todayS;
todayS = sdf->format(today, todayS, pos);
logln("today: " + todayS);
today = sdf->parse(todayS, status);
failure(status, "sdf->parse");
logln((UnicodeString)"today date: " + today);
UnicodeString rt;
rt = sdf->format(sdf->parse(todayS, status), rt, pos);
failure(status, "sdf->parse");
logln("round trip: " + rt);
if(rt != todayS)
errln("Fail: Want " + todayS + " Got " + rt);
delete sdf;
}
void DateFormatRegressionTest::Test4052408(void)
{
DateFormat *fmt = DateFormat::createDateTimeInstance(DateFormat::SHORT,
DateFormat::SHORT, Locale::getUS());
UDate dt = date(97, UCAL_MAY, 3, 8, 55);
UnicodeString str;
str = fmt->format(dt, str);
logln(str);
if(str != "5/3/97 8:55 AM")
errln("Fail: Test broken; Want 5/3/97 8:55 AM Got " + str);
UnicodeString expected[] = {
(UnicodeString) "", (UnicodeString) "97", (UnicodeString) "5", (UnicodeString) "3", (UnicodeString) "", (UnicodeString) "", (UnicodeString) "55", (UnicodeString) "", (UnicodeString) "", (UnicodeString) "", (UnicodeString) "", (UnicodeString) "", (UnicodeString) "", (UnicodeString) "", (UnicodeString) "AM", (UnicodeString) "8", (UnicodeString) "", (UnicodeString) "" };
UnicodeString fieldNames[] = {
(UnicodeString) "ERA_FIELD",
(UnicodeString) "YEAR_FIELD",
(UnicodeString) "MONTH_FIELD",
(UnicodeString) "DATE_FIELD",
(UnicodeString) "HOUR_OF_DAY1_FIELD",
(UnicodeString) "HOUR_OF_DAY0_FIELD",
(UnicodeString) "MINUTE_FIELD",
(UnicodeString) "SECOND_FIELD",
(UnicodeString) "MILLISECOND_FIELD",
(UnicodeString) "DAY_OF_WEEK_FIELD",
(UnicodeString) "DAY_OF_YEAR_FIELD",
(UnicodeString) "DAY_OF_WEEK_IN_MONTH_FIELD",
(UnicodeString) "WEEK_OF_YEAR_FIELD",
(UnicodeString) "WEEK_OF_MONTH_FIELD",
(UnicodeString) "AM_PM_FIELD",
(UnicodeString) "HOUR1_FIELD",
(UnicodeString) "HOUR0_FIELD",
(UnicodeString) "TIMEZONE_FIELD"
};
UBool pass = TRUE;
for(int i = 0; i <= 17; ++i) {
FieldPosition pos(i);
UnicodeString buf;
fmt->format(dt, buf, pos);
UnicodeString dst;
buf.extractBetween(pos.getBeginIndex(), pos.getEndIndex(), dst);
UnicodeString str(dst);
logln((UnicodeString)"" + i + (UnicodeString)": " + fieldNames[i] +
(UnicodeString)", \"" + str + (UnicodeString)"\", " +
pos.getBeginIndex() + (UnicodeString)", " +
pos.getEndIndex());
UnicodeString exp = expected[i];
if((exp.length() == 0 && str.length() == 0) || str == exp)
logln(" ok");
else {
errln(UnicodeString(" expected ") + exp);
pass = FALSE;
}
}
if( ! pass)
errln("Fail: FieldPosition not set right by DateFormat");
delete fmt;
}
void DateFormatRegressionTest::Test4056591(void)
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status);
failure(status, "new SimpleDateFormat");
UDate start = date(1809-1900, UCAL_DECEMBER, 25);
fmt->set2DigitYearStart(start, status);
failure(status, "fmt->setTwoDigitStartDate");
if( (fmt->get2DigitYearStart(status) != start) || failure(status, "get2DigitStartDate"))
errln("get2DigitYearStart broken");
UDate dates [] = {
date(1809-1900, UCAL_DECEMBER, 25),
date(1909-1900, UCAL_DECEMBER, 24),
date(1809-1900, UCAL_DECEMBER, 26),
date(1861-1900, UCAL_DECEMBER, 25),
};
UnicodeString strings [] = {
(UnicodeString) "091225",
(UnicodeString) "091224",
(UnicodeString) "091226",
(UnicodeString) "611225"
};
for(int i = 0; i < 4; i++) {
UnicodeString s = strings[i];
UDate exp = dates[i];
UDate got = fmt->parse(s, status);
failure(status, "fmt->parse");
logln(s + " -> " + got + "; exp " + exp);
if(got != exp)
errln("set2DigitYearStart broken");
}
delete fmt;
}
void DateFormatRegressionTest::Test4059917(void)
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *fmt;
UnicodeString myDate;
fmt = new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status );
failure(status, "new SimpleDateFormat");
myDate = "1997/01/01";
aux917( fmt, myDate );
delete fmt;
fmt = NULL;
fmt = new SimpleDateFormat( UnicodeString("yyyyMMdd"), status );
failure(status, "new SimpleDateFormat");
myDate = "19970101";
aux917( fmt, myDate );
delete fmt;
}
void DateFormatRegressionTest::aux917( SimpleDateFormat *fmt, UnicodeString& str ) {
UnicodeString pat;
pat = fmt->toPattern(pat);
logln( "==================" );
logln( "testIt: pattern=" + pat +
" string=" + str );
Formattable o;
ParsePosition pos(0);
fmt->parseObject( str, o, pos );
UErrorCode status = U_ZERO_ERROR;
UnicodeString formatted;
FieldPosition poss(FieldPosition::DONT_CARE);
formatted = fmt->format( o, formatted, poss, status );
failure(status, "fmt->format");
logln( "Formatted string: " + formatted );
if( formatted != str)
errln("Fail: Want " + str + " Got " + formatted);
}
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);
failure(status, "new SimpleDateFormat");
ParsePosition pos(0);
UDate myDate = formatter->parse( dateString, pos );
UnicodeString myString;
DateFormat *fmt = DateFormat::createDateTimeInstance( DateFormat::FULL,
DateFormat::LONG);
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");
logln("Using yyyy-ddd.hh:mm:ss");
delete formatter;
formatter = NULL;
formatter = new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status);
failure(status, "new SimpleDateFormat");
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;
}
void DateFormatRegressionTest::Test4061287(void)
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status);
if(U_FAILURE(status)) {
errln("Couldn't create SimpleDateFormat, error: %s", u_errorName(status));
delete df;
return;
}
failure(status, "new SimpleDateFormat");
logln(UnicodeString("") + df->parse("35/01/1971", status));
failure(status, "df->parse");
df->setLenient(FALSE);
UBool ok = FALSE;
logln(UnicodeString("") + df->parse("35/01/1971", status));
if(U_FAILURE(status))
ok = TRUE;
if(!ok)
errln("Fail: Lenient not working");
delete df;
}
void DateFormatRegressionTest::Test4065240(void)
{
UDate curDate;
DateFormat *shortdate, *fulldate;
UnicodeString strShortDate, strFullDate;
Locale saveLocale = Locale::getDefault();
TimeZone *saveZone = TimeZone::createDefault();
UErrorCode status = U_ZERO_ERROR;
Locale *curLocale = new Locale("de","DE");
Locale::setDefault(*curLocale, status);
failure(status, "Locale::setDefault");
TimeZone::adoptDefault(TimeZone::createTimeZone("EST"));
curDate = date(98, 0, 1);
shortdate = DateFormat::createDateInstance(DateFormat::SHORT);
fulldate = DateFormat::createDateTimeInstance(DateFormat::LONG, DateFormat::LONG);
strShortDate = "The current date (short form) is ";
UnicodeString temp;
temp = shortdate->format(curDate, temp);
strShortDate += temp;
strFullDate = "The current date (long form) is ";
UnicodeString temp2;
fulldate->format(curDate, temp2);
strFullDate += temp2;
logln(strShortDate);
logln(strFullDate);
ResourceBundle *bundle = new ResourceBundle(
NULL, *curLocale, status);
failure(status, "new ResourceBundle");
Locale::setDefault(saveLocale, status);
failure(status, "Locale::setDefault");
TimeZone::setDefault(*saveZone);
delete shortdate;
delete fulldate;
delete saveZone;
delete curLocale;
delete bundle;
}
void DateFormatRegressionTest::Test4071441(void)
{
DateFormat *fmtA = DateFormat::createInstance();
DateFormat *fmtB = DateFormat::createInstance();
Calendar *calA = (Calendar*) fmtA->getCalendar();
Calendar *calB = (Calendar*) fmtB->getCalendar();
if(!calA || !calB) {
errln("Couldn't get proper calendars, exiting");
delete fmtA;
delete fmtB;
return;
}
UDate epoch = date(0, 0, 0);
UDate xmas = date(61, UCAL_DECEMBER, 25);
UErrorCode status = U_ZERO_ERROR;
calA->setTime(epoch, status);
failure(status, "calA->setTime");
calB->setTime(epoch, status);
failure(status, "calB->setTime");
if (*calA != *calB)
errln("Fail: Can't complete test; Calendar instances unequal");
if (*fmtA != *fmtB)
errln("Fail: DateFormat unequal when Calendars equal");
calB->setTime(xmas, status);
failure(status, "calB->setTime");
if (*calA == *calB)
errln("Fail: Can't complete test; Calendar instances equal");
if (*fmtA != *fmtB)
errln("Fail: DateFormat unequal when Calendars equivalent");
logln("DateFormat.equals ok");
delete fmtA;
delete fmtB;
}
void DateFormatRegressionTest::Test4073003(void)
{
UErrorCode ec = U_ZERO_ERROR;
SimpleDateFormat fmt("dd/MM/yy", Locale::getUK(), ec);
if (U_FAILURE(ec)) {
errln("FAIL: SimpleDateFormat constructor");
return;
}
UnicodeString tests [] = {
(UnicodeString) "12/25/61",
(UnicodeString) "12/25/1961",
(UnicodeString) "4/3/2010",
(UnicodeString) "4/3/10"
};
UErrorCode status = U_ZERO_ERROR;
for(int i= 0; i < 4; i+=2) {
UDate d = fmt.parse(tests[i], status);
failure(status, "fmt.parse");
UDate dd = fmt.parse(tests[i+1], status);
failure(status, "fmt.parse");
UnicodeString s;
s = fmt.format(d, s);
UnicodeString ss;
ss = fmt.format(dd, ss);
if (d != dd)
errln((UnicodeString) "Fail: " + d + " != " + dd);
if (s != ss)
errln((UnicodeString)"Fail: " + s + " != " + ss);
logln("Ok: " + s + " " + d);
}
}
void DateFormatRegressionTest::Test4089106(void)
{
TimeZone *def = TimeZone::createDefault();
TimeZone *z = new SimpleTimeZone((int)(1.25 * 3600000), "FAKEZONE");
TimeZone::setDefault(*z);
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *f = new SimpleDateFormat(status);
if(U_FAILURE(status)) {
errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
delete f;
delete def;
delete z;
return;
}
failure(status, "new SimpleDateFormat");
if (f->getTimeZone()!= *z)
errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
TimeZone::setDefault(*def);
delete z;
delete f;
delete def;
}
void DateFormatRegressionTest::Test4100302(void)
{
}
void DateFormatRegressionTest::Test4101483(void)
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *sdf = new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status);
failure(status, "new SimpleDateFormat");
FieldPosition fp(UDAT_TIMEZONE_FIELD);
UDate d = 9234567890.0;
UnicodeString buf;
sdf->format(d, buf, fp);
logln(dateToString(d) + " => " + buf);
logln("beginIndex = " + fp.getBeginIndex());
logln("endIndex = " + fp.getEndIndex());
if (fp.getBeginIndex() == fp.getEndIndex())
errln("Fail: Empty field");
delete sdf;
}
void DateFormatRegressionTest::Test4103340(void)
{
UErrorCode status = U_ZERO_ERROR;
UDate d = date(97, 3, 1, 1, 1, 1);
SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status);
failure(status, "new SimpleDateFormat");
UnicodeString s;
s = dateToString(d, s);
UnicodeString s2;
FieldPosition pos(FieldPosition::DONT_CARE);
s2 = df->format(d, s2, pos);
logln("Date=" + s);
logln("DF=" + s2);
UnicodeString substr;
s2.extract(0,2, substr);
if (s.indexOf(substr) == -1)
errln("Months should match");
delete df;
}
void DateFormatRegressionTest::Test4103341(void)
{
TimeZone *saveZone =TimeZone::createDefault();
TimeZone::adoptDefault(TimeZone::createTimeZone("CST"));
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *simple = new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status);
if(U_FAILURE(status)) {
errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
delete simple;
return;
}
failure(status, "new SimpleDateFormat");
TimeZone *temp = TimeZone::createDefault();
if(simple->getTimeZone() != *temp)
errln("Fail: SimpleDateFormat not using default zone");
TimeZone::adoptDefault(saveZone);
delete temp;
delete simple;
}
void DateFormatRegressionTest::Test4104136(void)
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *sdf = new SimpleDateFormat(status);
if(U_FAILURE(status)) {
errln("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 + "\"");
UnicodeString strings [] = {
(UnicodeString)"time 10:30",
(UnicodeString) "time 10:x",
(UnicodeString) "time 10x"
};
ParsePosition ppos [] = {
ParsePosition(10),
ParsePosition(0),
ParsePosition(0)
};
UDate dates [] = {
date(70, UCAL_JANUARY, 1, 10, 30),
-1,
-1
};
for(int i = 0; i < 3; i++) {
UnicodeString text = strings[i];
ParsePosition finish = ppos[i];
UDate exp = dates[i];
ParsePosition pos(0);
UDate d = sdf->parse(text, pos);
logln(" text: \"" + text + "\"");
logln(" index: %d", pos.getIndex());
logln((UnicodeString) " result: " + d);
if(pos.getIndex() != finish.getIndex())
errln("Fail: Expected pos " + finish.getIndex());
if (! ((d == 0 && exp == -1) || (d == exp)))
errln((UnicodeString) "Fail: Expected result " + exp);
}
delete sdf;
}
void DateFormatRegressionTest::Test4104522(void)
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *sdf = new SimpleDateFormat(status);
if(U_FAILURE(status)) {
errln("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 + "\"");
ParsePosition pp(0);
UnicodeString text = "time ";
UDate dt = sdf->parse(text, pp);
logln(" text: \"" + text + "\"" +
" date: " + dt);
pp.setIndex(0);
text = "time";
dt = sdf->parse(text, pp);
logln(" text: \"" + text + "\"" +
" date: " + dt);
delete sdf;
}
void DateFormatRegressionTest::Test4106807(void)
{
UDate dt;
DateFormat *df = DateFormat::createDateTimeInstance();
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *sdfs [] = {
new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status),
new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status),
new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status),
new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status),
new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status)
};
if(U_FAILURE(status)) {
errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
delete sdfs[0];
delete sdfs[1];
delete sdfs[2];
delete sdfs[3];
delete sdfs[4];
return;
}
failure(status, "new SimpleDateFormat");
UnicodeString strings [] = {
(UnicodeString) "19980211140000",
(UnicodeString) "19980211140000",
(UnicodeString) "19980211140000",
(UnicodeString) "19980211140000a",
(UnicodeString) "19980211140000 "
};
GregorianCalendar *gc = new GregorianCalendar(status);
failure(status, "new GregorianCalendar");
TimeZone *timeZone = TimeZone::createDefault();
TimeZone *gmt = timeZone->clone();
gmt->setRawOffset(0);
for(int32_t i = 0; i < 5; i++) {
SimpleDateFormat *format = sdfs[i];
UnicodeString dateString = strings[i];
format->setTimeZone(*gmt);
dt = format->parse(dateString, status);
if(U_FAILURE(status))
break;
status = U_ZERO_ERROR;
UnicodeString fmtd;
FieldPosition pos(FieldPosition::DONT_CARE);
fmtd = df->format(dt, fmtd, pos);
logln(fmtd);
gc->setTime(dt, status);
failure(status, "gc->getTime");
logln(UnicodeString("") + gc->get(UCAL_ZONE_OFFSET, status));
failure(status, "gc->get");
UnicodeString s;
s = format->format(dt, s, pos);
logln(s);
}
delete timeZone;
delete df;
for(int32_t j = 0; j < 5; j++)
delete sdfs [j];
delete gc;
delete gmt;
}
void DateFormatRegressionTest::Test4108407(void)
{
}
void DateFormatRegressionTest::Test4134203(void)
{
UErrorCode status = U_ZERO_ERROR;
UnicodeString dateFormat = "MM/dd/yy HH:mm:ss zzz";
SimpleDateFormat *fmt = new SimpleDateFormat(dateFormat, status);
failure(status, "new SimpleDateFormat");
ParsePosition p0(0);
UDate d = fmt->parse("01/22/92 04:52:00 GMT", p0);
logln(dateToString(d));
if(p0 == ParsePosition(0))
errln("Fail: failed to parse 'GMT'");
delete fmt;
}
void DateFormatRegressionTest::Test4151631(void)
{
UnicodeString pattern = "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
logln("pattern=" + pattern);
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *format = new SimpleDateFormat(pattern, Locale::getUS(), status);
failure(status, "new SimpleDateFormat");
UnicodeString result;
FieldPosition pos(FieldPosition::DONT_CARE);
result = format->format(date(1998-1900, UCAL_JUNE, 30, 13, 30, 0), result, pos);
if (result != "TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')") {
errln("Fail: result=" + result);
}
else {
logln("Pass: result=" + result);
}
delete format;
}
void DateFormatRegressionTest::Test4151706(void)
{
UnicodeString dateString("Thursday, 31-Dec-98 23:00:00 GMT");
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat fmt(UnicodeString("EEEE, dd-MMM-yy HH:mm:ss z"), Locale::getUS(), status);
failure(status, "new SimpleDateFormat");
UDate d = fmt.parse(dateString, status);
failure(status, "fmt->parse");
UnicodeString temp;
FieldPosition pos(0);
logln(dateString + " -> " + fmt.format(d, temp, pos));
}
void
DateFormatRegressionTest::Test4162071(void)
{
UnicodeString dateString("Thu, 30-Jul-1999 11:51:14 GMT");
UnicodeString format("EEE', 'dd-MMM-yyyy HH:mm:ss z"); UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat df(format, Locale::getUS(), status);
if(U_FAILURE(status))
errln("Couldn't create SimpleDateFormat");
UDate x = df.parse(dateString, status);
if(U_SUCCESS(status))
logln("Parse format \"" + format + "\" ok");
else
errln("Parse format \"" + format + "\" failed.");
UnicodeString temp;
FieldPosition pos(0);
logln(dateString + " -> " + df.format(x, temp, pos));
}
void DateFormatRegressionTest::Test4182066(void) {
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat fmt("MM/dd/yy", Locale::getUS(), status);
SimpleDateFormat dispFmt("MMM dd yyyy GG", Locale::getUS(), status);
if (U_FAILURE(status)) {
errln("Couldn't create SimpleDateFormat");
return;
}
const char* STRINGS[] = {
"02/29/00",
"01/23/01",
"04/05/-1",
"01/23/-9",
"11/12/1314",
"10/31/1",
"09/12/+1",
"09/12/001",
};
int32_t STRINGS_COUNT = (int32_t)(sizeof(STRINGS) / sizeof(STRINGS[0]));
UDate FAIL_DATE = (UDate) 0;
UDate DATES[] = {
date(2000-1900, UCAL_FEBRUARY, 29),
date(2001-1900, UCAL_JANUARY, 23),
date( -1-1900, UCAL_APRIL, 5),
date( -9-1900, UCAL_JANUARY, 23),
date(1314-1900, UCAL_NOVEMBER, 12),
date( 1-1900, UCAL_OCTOBER, 31),
FAIL_DATE, date( 1-1900, UCAL_SEPTEMBER,12),
};
UnicodeString out;
UBool pass = TRUE;
for (int32_t i=0; i<STRINGS_COUNT; ++i) {
UnicodeString str(STRINGS[i]);
UDate expected = DATES[i];
status = U_ZERO_ERROR;
UDate actual = fmt.parse(str, status);
if (U_FAILURE(status)) {
actual = FAIL_DATE;
}
UnicodeString actStr;
if (actual == FAIL_DATE) {
actStr.append("null");
} else {
((DateFormat*)&dispFmt)->format(actual, actStr);
}
if (expected == actual) {
out.append(str + " => " + actStr + "\n");
} else {
UnicodeString expStr;
if (expected == FAIL_DATE) {
expStr.append("null");
} else {
((DateFormat*)&dispFmt)->format(expected, expStr);
}
out.append("FAIL: " + str + " => " + actStr
+ ", expected " + expStr + "\n");
pass = FALSE;
}
}
if (pass) {
log(out);
} else {
err(out);
}
}
void
DateFormatRegressionTest::Test4210209(void) {
UErrorCode status = U_ZERO_ERROR;
UnicodeString pattern("MMM d, yyyy");
SimpleDateFormat sfmt(pattern, Locale::getUS(), status);
SimpleDateFormat sdisp("MMM dd yyyy GG", Locale::getUS(), status);
DateFormat& fmt = *(DateFormat*)&sfmt; DateFormat& disp = *(DateFormat*)&sdisp; if (U_FAILURE(status)) {
errln("Couldn't create SimpleDateFormat");
return;
}
Calendar* calx = (Calendar*)fmt.getCalendar(); calx->setLenient(FALSE);
UDate d = date(2000-1900, UCAL_FEBRUARY, 29);
UnicodeString s, ss;
fmt.format(d, s);
logln(disp.format(d, ss.remove()) + " f> " + pattern +
" => \"" + s + "\"");
ParsePosition pos(0);
d = fmt.parse(s, pos);
logln(UnicodeString("\"") + s + "\" p> " + pattern +
" => " + disp.format(d, ss.remove()));
logln(UnicodeString("Parse pos = ") + pos.getIndex() +
", error pos = " + pos.getErrorIndex());
if (pos.getErrorIndex() != -1) {
errln(UnicodeString("FAIL: Error index should be -1"));
}
GregorianCalendar cal(status);
if (U_FAILURE(status)) {
errln("FAIL: Unable to create Calendar");
return;
}
cal.clear();
cal.setLenient(FALSE);
cal.set(2000, UCAL_FEBRUARY, 29); logln(UnicodeString("Attempt to set Calendar to Feb 29 2000: ") +
disp.format(cal.getTime(status), ss.remove()));
if (U_FAILURE(status)) {
errln("FAIL: Unable to set Calendar to Feb 29 2000");
}
}
void DateFormatRegressionTest::Test714(void)
{
UDate d(978103543000.);
DateFormat *fmt = DateFormat::createDateTimeInstance(DateFormat::NONE,
DateFormat::MEDIUM,
Locale::getUS());
UnicodeString s;
UnicodeString tests =
(UnicodeString) "7:25:43 AM" ;
UErrorCode status = U_ZERO_ERROR;
fmt->format (d,s);
if(U_FAILURE(status))
{
errln((UnicodeString) "Fail, errmsg " + u_errorName(status));
return;
}
if(s != tests)
{
errln((UnicodeString) "Fail: " + s + " != " + tests);
}
else
{
logln("OK: " + s + " == " + tests);
}
delete fmt;
}
class Test1684Data {
public:
int32_t year;
int32_t month;
int32_t date;
int32_t womyear;
int32_t wommon;
int32_t wom;
int32_t dow;
UnicodeString data;
UnicodeString normalized;
Test1684Data(int32_t xyear, int32_t xmonth, int32_t xdate,
int32_t xwomyear, int32_t xwommon, int32_t xwom, int32_t xdow,
const char *xdata, const char *xnormalized) :
year(xyear),
month(xmonth-1),
date(xdate),
womyear(xwomyear),
wommon(xwommon-1),
wom(xwom),
dow(xdow),
data(xdata,""),
normalized((xnormalized==NULL)?xdata:xnormalized,"")
{ }
};
void DateFormatRegressionTest::Test1684(void)
{
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, 2001,8,2,UCAL_SUNDAY, "2001 08 02 Sun", NULL),
new Test1684Data(2001, 8,6, 2001,7,6,UCAL_MONDAY, "2001 07 06 Mon", "2001 08 02 Mon"),
new Test1684Data(2001, 8,7, 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, 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,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);
cal->setFirstDayOfWeek(UCAL_SUNDAY);
cal->setMinimalDaysInFirstWeek(1);
sdf->adoptCalendar(cal);
cal = sdf->getCalendar()->clone();
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" +
"\n '" + result + "'" +
"\n expected '" + test.normalized + "'");
warning++;
} else {
pass++;
}
UDate ms3;
ms3 = sdf->parse(result, status);
if(U_FAILURE(status)) {
errln("parse exception 2: " + (UnicodeString)u_errorName(status));
}
if (ms3!=ms) {
error++;
errln((UnicodeString)"\nError: Re-parse of '" + result + "' gave time of " +
"\n " + ms3 +
"\n not " + ms);
} else {
pass++;
}
}
UnicodeString info
= UnicodeString("Passed: ") + pass + ", Warnings: " + warning + ", Errors: " + error;
if (error > 0) {
errln(info);
} else {
logln(info);
}
for(i=0;i<kTest1684Count;i++) {
delete tests[i];
}
delete cal;
delete sdf;
}
#endif