/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2006, International Business Machines Corporation and
+ * Copyright (c) 1997-2012, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
CASE(23,Test4210209)
CASE(24,Test714)
CASE(25,Test1684)
+ CASE(26,Test5554)
+ CASE(27,Test9237)
+ CASE(28,TestParsing)
default: name = ""; break;
}
}
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);
+ if(str != "5/3/97, 8:55 AM")
+ errln("Fail: Test broken; Want 5/3/97, 8:55 AM Got " + str);
UnicodeString expected[] = {
(UnicodeString) "", //"ERA_FIELD",
//try {
SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status);
- failure(status, "new SimpleDateFormat");
+ if (failure(status, "new SimpleDateFormat", TRUE)) {
+ delete fmt;
+ return;
+ }
UDate start = date(1809-1900, UCAL_DECEMBER, 25);
fmt->set2DigitYearStart(start, status);
failure(status, "fmt->setTwoDigitStartDate");
UnicodeString myDate;
fmt = new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status );
- if(failure(status, "new SimpleDateFormat")) return;
+ if (failure(status, "new SimpleDateFormat", TRUE)) return;
myDate = "1997/01/01";
aux917( fmt, myDate );
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")) return;
+ if (failure(status, "new SimpleDateFormat", TRUE)) return;
ParsePosition pos(0);
UDate myDate = formatter->parse( dateString, pos );
UnicodeString myString;
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;
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;
+ 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");
+ failure(status, "df->parse(\"35/01/1971\")");
//logln(df.parse("35/01/1971").toString());
//}
/*catch (ParseException e) {
US locale a string formatted according to mm/dd/yy and parses it
correctly.
- When given a string mm/dd/yyyy it only parses up to the first
+ When given a string mm/dd/yyyy [sic] it only parses up to the first
two y's, typically resulting in a date in the year 1919.
Please extend the parsing method(s) to handle strings with
{
//try {
UErrorCode ec = U_ZERO_ERROR;
- SimpleDateFormat fmt("dd/MM/yy", Locale::getUK(), ec);
+ SimpleDateFormat fmt("MM/dd/yy", Locale::getUK(), ec);
if (U_FAILURE(ec)) {
- errln("FAIL: SimpleDateFormat constructor");
+ dataerrln("FAIL: SimpleDateFormat constructor - %s", u_errorName(ec));
return;
}
UnicodeString tests [] = {
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *f = new SimpleDateFormat(status);
if(U_FAILURE(status)) {
- errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
+ dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
delete f;
delete def;
delete z;
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *sdf = new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status);
- if(failure(status, "new SimpleDateFormat")) return;
+ if (failure(status, "new SimpleDateFormat", TRUE)) return;
FieldPosition fp(UDAT_TIMEZONE_FIELD);
//Date d = date(9234567890L);
UDate d = 9234567890.0;
sdf->format(d, buf, fp);
//logln(sdf.format(d, buf, fp).toString());
logln(dateToString(d) + " => " + buf);
- logln("beginIndex = " + fp.getBeginIndex());
- logln("endIndex = " + fp.getEndIndex());
+ logln(UnicodeString("beginIndex = ") + fp.getBeginIndex());
+ logln(UnicodeString("endIndex = ") + fp.getEndIndex());
if (fp.getBeginIndex() == fp.getEndIndex())
errln("Fail: Empty field");
// and some arbitrary time
UDate d = date(97, 3, 1, 1, 1, 1);
SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status);
- if(failure(status, "new SimpleDateFormat")) return;
+ if (failure(status, "new SimpleDateFormat", TRUE)) return;
UnicodeString s;
s = dateToString(d, s);
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));
+ dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
delete simple;
return;
}
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *sdf = new SimpleDateFormat(status);
if(U_FAILURE(status)) {
- errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
+ dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
delete sdf;
return;
}
logln(" index: %d", pos.getIndex());
logln((UnicodeString) " result: " + d);
if(pos.getIndex() != finish.getIndex())
- errln("Fail: Expected pos " + finish.getIndex());
+ errln(UnicodeString("Fail: Expected pos ") + finish.getIndex());
if (! ((d == 0 && exp == -1) || (d == exp)))
errln((UnicodeString) "Fail: Expected result " + exp);
}
SimpleDateFormat *sdf = new SimpleDateFormat(status);
if(U_FAILURE(status)) {
- errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
+ dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
delete sdf;
return;
}
new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status)
};
if(U_FAILURE(status)) {
- errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
+ dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
delete sdfs[0];
delete sdfs[1];
delete sdfs[2];
UErrorCode status = U_ZERO_ERROR;
UnicodeString dateFormat = "MM/dd/yy HH:mm:ss zzz";
SimpleDateFormat *fmt = new SimpleDateFormat(dateFormat, status);
- if(failure(status, "new SimpleDateFormat")) return;
+ if (failure(status, "new SimpleDateFormat", TRUE)) return;
ParsePosition p0(0);
UDate d = fmt->parse("01/22/92 04:52:00 GMT", p0);
logln(dateToString(d));
logln("pattern=" + pattern);
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *format = new SimpleDateFormat(pattern, Locale::getUS(), status);
- if(failure(status, "new SimpleDateFormat")) return;
+ if (failure(status, "new SimpleDateFormat", TRUE)) return;
UnicodeString result;
FieldPosition pos(FieldPosition::DONT_CARE);
result = format->format(date(1998-1900, UCAL_JUNE, 30, 13, 30, 0), result, pos);
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);
- if(failure(status, "new SimpleDateFormat")) return;
+ if (failure(status, "new SimpleDateFormat", TRUE)) return;
//try {
UDate d = fmt.parse(dateString, status);
failure(status, "fmt->parse");
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat df(format, Locale::getUS(), status);
if(U_FAILURE(status)) {
- errln("Couldn't create SimpleDateFormat");
+ dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status));
return;
}
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");
+ dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status));
return;
}
DateFormat& fmt = *(DateFormat*)&sfmt; // Yuck: See j25
DateFormat& disp = *(DateFormat*)&sdisp; // Yuck: See j25
if (U_FAILURE(status)) {
- errln("Couldn't create SimpleDateFormat");
+ dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status));
return;
}
Calendar* calx = (Calendar*)fmt.getCalendar(); // cast away const!
delete sdf;
}
+void DateFormatRegressionTest::Test5554(void)
+{
+ UErrorCode status = U_ZERO_ERROR;
+ UnicodeString pattern("Z","");
+ UnicodeString newfoundland("Canada/Newfoundland", "");
+ TimeZone *zone = TimeZone::createTimeZone(newfoundland);
+ Calendar *cal = new GregorianCalendar(zone, status);
+ SimpleDateFormat *sdf = new SimpleDateFormat(pattern,status);
+ if (U_FAILURE(status)) {
+ dataerrln("Error constructing SimpleDateFormat");
+ delete cal;
+ delete sdf;
+ return;
+ }
+ cal->set(2007, 1, 14);
+ UDate date = cal->getTime(status);
+ if (U_FAILURE(status)) {
+ errln("Error getting time to format");
+ return;
+ };
+ sdf->adoptCalendar(cal);
+ UnicodeString result;
+ UnicodeString correct("-0330", "");
+ sdf->format(date, result);
+ if (result != correct) {
+ errln("\nError: Newfoundland Z of Jan 14, 2007 gave '" + result + "', expected '" + correct + "'");
+ }
+ delete sdf;
+}
+
+void DateFormatRegressionTest::Test9237(void)
+{
+ UErrorCode status = U_ZERO_ERROR;
+ UnicodeString pattern("VVVV");
+
+ SimpleDateFormat fmt(pattern, status); // default locale
+ SimpleDateFormat fmtDE(pattern, Locale("de_DE"), status);
+ if (U_FAILURE(status)) {
+ dataerrln("Error constructing SimpleDateFormat");
+ return;
+ }
+
+ // copy constructor
+ SimpleDateFormat fmtCopyDE(fmtDE);
+ UnicodeString resDE, resCopyDE;
+
+ fmtDE.format(0.0, resDE);
+ fmtCopyDE.format(0.0, resCopyDE);
+
+ if (resDE != resCopyDE) {
+ errln(UnicodeString("Error: different result by the copied instance - org:") + resDE + " copy:" + resCopyDE);
+ }
+
+ // test for assignment operator
+ fmt = fmtDE;
+
+ UnicodeString resAssigned;
+ fmt.format(0.0, resAssigned);
+
+ if (resDE != resAssigned) {
+ errln(UnicodeString("Error: different results by the assigned instance - org:") + resDE + " assigned:" + resAssigned);
+ }
+}
+
+void DateFormatRegressionTest::TestParsing(void) {
+ UErrorCode status = U_ZERO_ERROR;
+ UnicodeString pattern("EEE-WW-MMMM-yyyy");
+ UnicodeString text("mon-02-march-2011");
+ int32_t expectedDay = 7;
+
+ SimpleDateFormat format(pattern, status);
+ if (U_FAILURE(status)) {
+ dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status));
+ return;
+ }
+
+ Calendar *cal = new GregorianCalendar(status);
+ if (cal == NULL || U_FAILURE(status)) {
+ errln("Unable to create calendar - %s", u_errorName(status));
+ return;
+ }
+
+ ParsePosition pos(0);
+ format.parse(text, *cal, pos);
+
+ if (cal->get(UCAL_DAY_OF_MONTH, status) != expectedDay) {
+ errln("Parsing failed: day of month should be '7' with pattern: \"" + pattern + "\" for text: \"" + text + "\"");
+ }
+
+ delete cal;
+}
+
#endif /* #if !UCONFIG_NO_FORMATTING */
//eof