+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2012, International Business Machines Corporation and
+ * Copyright (c) 1997-2016, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
-
#include "dtfmrgts.h"
#include "unicode/timezone.h"
#include "unicode/datefmt.h"
#include "unicode/simpletz.h"
#include "unicode/resbund.h"
+#include "cmemory.h"
// *****************************************************************************
// class DateFormatRegressionTest
CASE(25,Test1684)
CASE(26,Test5554)
CASE(27,Test9237)
+ CASE(28,TestParsing)
+ CASE(29,TestT10334)
+ CASE(30,TestT10619)
+ CASE(31,TestT10855)
+ CASE(32,TestT10906)
default: name = ""; break;
}
}
pat = sdf->toPattern(pat);
logln("pattern: " + pat);
UnicodeString fmtd;
+
FieldPosition pos(FieldPosition::DONT_CARE);
fmtd = sdf->format(today, fmtd, pos);
logln("today: " + fmtd);
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",
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");
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);
}
errln("Fail: " + e);
}*/
UnicodeString temp;
- FieldPosition pos(0);
+ FieldPosition pos(FieldPosition::DONT_CARE);
logln(dateString + " -> " + fmt.format(d, temp, pos));
}
else
errln("Parse format \"" + format + "\" failed.");
UnicodeString temp;
- FieldPosition pos(0);
+ FieldPosition pos(FieldPosition::DONT_CARE);
logln(dateString + " -> " + df.format(x, temp, pos));
//} catch (Exception e) {
// errln("Parse format \"" + format + "\" failed.");
"09/12/+1",
"09/12/001",
};
- int32_t STRINGS_COUNT = (int32_t)(sizeof(STRINGS) / sizeof(STRINGS[0]));
+ int32_t STRINGS_COUNT = UPRV_LENGTHOF(STRINGS);
UDate FAIL_DATE = (UDate) 0;
UDate DATES[] = {
date(2000-1900, UCAL_FEBRUARY, 29),
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])))
+#define kTest1684Count UPRV_LENGTHOF(tests)
int32_t pass = 0, error = 0, warning = 0;
int32_t i;
}
}
+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;
+}
+
+void DateFormatRegressionTest::TestT10334(void) {
+ UErrorCode status = U_ZERO_ERROR;
+ UnicodeString pattern("'--: 'EEE-WW-MMMM-yyyy");
+ UnicodeString text("--mon-02-march-2011");
+ SimpleDateFormat format(pattern, status);
+
+ logln("pattern["+pattern+"] text["+text+"]");
+
+ if (U_FAILURE(status)) {
+ dataerrln("Fail creating SimpleDateFormat object - %s", u_errorName(status));
+ return;
+ }
+
+ format.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH, FALSE, status);
+ format.parse(text, status);
+ if (!U_FAILURE(status)) {
+ errln("parse partial match did NOT fail in strict mode - %s", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ format.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH, TRUE, status);
+ format.parse(text, status);
+ if (U_FAILURE(status)) {
+ errln("parse partial match failure in lenient mode - %s", u_errorName(status));
+ }
+
+ status = U_ZERO_ERROR;
+ pattern = UnicodeString("YYYY MM dd");
+ text = UnicodeString("2013 12 10");
+ format.applyPattern(pattern);
+ UDate referenceDate = format.parse(text, status);
+
+ FieldPosition fp(FieldPosition::DONT_CARE);
+ UnicodeString formattedString("");
+ pattern = UnicodeString("YYYY LL dd ee cc qq QQ");
+ format.applyPattern(pattern);
+ format.format(referenceDate, formattedString, fp, status);
+ logln("ref date: " + formattedString);
+
+
+ char patternArray[] = "YYYY LLL dd eee ccc qqq QQQ";
+ pattern = UnicodeString(patternArray);
+ text = UnicodeString("2013 12 10 03 3 04 04");
+ status = U_ZERO_ERROR;
+ format.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, TRUE, status);
+ format.applyPattern(pattern);
+ ParsePosition pp(0);
+ format.parse(text, pp);
+ if (pp.getErrorIndex() != -1) {
+ errln("numeric parse error");
+ }
+
+ status = U_ZERO_ERROR;
+ format.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, FALSE, status);
+ format.parse(text, status);
+ if (!U_FAILURE(status)) {
+ errln("numeric parse did NOT fail in strict mode", u_errorName(status));
+ }
+
+}
+
+
+typedef struct {
+ const char * locale;
+ UBool leniency;
+ UnicodeString parseString;
+ UnicodeString pattern;
+ UnicodeString expectedResult; // null indicates expected error
+} TestDateFormatLeniencyItem;
+
+
+void DateFormatRegressionTest::TestT10619(void) {
+ const UDate july022008 = 1215000001979.0;
+ const TestDateFormatLeniencyItem items[] = {
+ //locale leniency parse String pattern expected result
+ { "en", true, UnicodeString("2008-07 02"), UnicodeString("yyyy-LLLL dd"), UnicodeString("2008-July 02") },
+ { "en", false, UnicodeString("2008-07 03"), UnicodeString("yyyy-LLLL dd"), UnicodeString("") },
+ { "en", true, UnicodeString("2008-Jan. 04"), UnicodeString("yyyy-LLL dd"), UnicodeString("2008-Jan 04") },
+ { "en", false, UnicodeString("2008-Jan. 05"), UnicodeString("yyyy-LLL dd"), UnicodeString("") },
+ { "en", true, UnicodeString("2008-Jan--06"), UnicodeString("yyyy-MMM -- dd"), UnicodeString("2008-Jan 06") },
+ { "en", false, UnicodeString("2008-Jan--07"), UnicodeString("yyyy-MMM -- dd"), UnicodeString("") },
+ { "en", true, UnicodeString("6 Jan 08 2008"), UnicodeString("eee MMM dd yyyy"), UnicodeString("Sat Jan 08 2008") },
+ { "en", false, UnicodeString("6 Jan 09 2008"), UnicodeString("eee MMM dd yyyy"), UnicodeString("") },
+ // terminator
+ { NULL, true, UnicodeString(""), UnicodeString(""), UnicodeString("") }
+ };
+ UErrorCode status = U_ZERO_ERROR;
+ Calendar* cal = Calendar::createInstance(status);
+ if (U_FAILURE(status)) {
+ dataerrln(UnicodeString("FAIL: Unable to create Calendar for default timezone and locale."));
+ } else {
+ cal->setTime(july022008, status);
+ const TestDateFormatLeniencyItem * itemPtr;
+ for (itemPtr = items; itemPtr->locale != NULL; itemPtr++ ) {
+
+ Locale locale = Locale::createFromName(itemPtr->locale);
+ status = U_ZERO_ERROR;
+ ParsePosition pos(0);
+ SimpleDateFormat * sdmft = new SimpleDateFormat(itemPtr->pattern, locale, status);
+ if (U_FAILURE(status)) {
+ dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status));
+ continue;
+ }
+ logln("parsing " + itemPtr->parseString);
+ sdmft->setLenient(itemPtr->leniency);
+ sdmft->setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, itemPtr->leniency, status);
+ sdmft->setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, itemPtr->leniency, status);
+ sdmft->setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH, itemPtr->leniency, status);
+ sdmft->parse(itemPtr->parseString, pos);
+
+ delete sdmft;
+ if(pos.getErrorIndex() > -1) {
+ if(itemPtr->expectedResult.length() != 0) {
+ errln("error: unexpected error - " + itemPtr->parseString + " - error index " + pos.getErrorIndex() +
+ " - leniency " + itemPtr->leniency);
+ continue;
+ } else {
+ continue;
+ }
+ }
+ }
+ }
+ delete cal;
+
+}
+
+
+typedef struct {
+ UnicodeString text;
+ UnicodeString pattern;
+ int initialParsePos;
+} T10855Data;
+
+void DateFormatRegressionTest::TestT10855(void) {
+ // NOTE: these should NOT parse
+ const T10855Data items[] = {
+ //parse String pattern initial parse pos
+// { UnicodeString("September 30, 1998"), UnicodeString("MM-dd-yyyy"), 0}, // for Apple, this can succeed
+ { UnicodeString("123-73-1950"), UnicodeString("MM-dd-yyyy"), -1},
+ { UnicodeString("12-23-1950"), UnicodeString("MM-dd-yyyy"), -1},
+ // terminator
+ { UnicodeString(""), UnicodeString(""), 0}
+ };
+ UErrorCode status = U_ZERO_ERROR;
+
+ int x = 0;
+ while(items[x].pattern.length() > 0)
+ {
+ status = U_ZERO_ERROR;
+ logln("Date to parse: \""+items[x].text+"\"");
+ logln("Starting Index: %d", items[x].initialParsePos);
+
+ SimpleDateFormat dateFmt(items[x].pattern, status);
+ if(U_FAILURE(status)) {
+ errcheckln(status, "Failed dateFmt: %s", u_errorName(status));
+ ++x;
+ continue;
+ }
+ status = U_ZERO_ERROR;
+
+ dateFmt.setLenient(false);
+ dateFmt.setTimeZone(*TimeZone::getGMT());
+
+ ParsePosition position(items[x].initialParsePos);
+ logln("set position is now: %d", position.getIndex());
+ UDate d = dateFmt.parse(items[x].text, position);
+ if (position.getErrorIndex() != -1 || position.getIndex() == items[x].initialParsePos) {
+ logln("Parse Failed. ErrorIndex is %d - Index is %d", position.getErrorIndex(), position.getIndex());
+ } else {
+ errln("Parse Succeeded...should have failed. Index is %d - ErrorIndex is %d", position.getIndex(), position.getErrorIndex());
+ }
+ logln("Parsed date returns %d\n", d);
+
+ ++x;
+ }
+}
+
+void DateFormatRegressionTest::TestT10906(void) {
+
+ UErrorCode status = U_ZERO_ERROR;
+ UnicodeString pattern = "MM-dd-yyyy";
+ UnicodeString text = "06-10-2014";
+ SimpleDateFormat format(pattern, status);
+ int32_t errorIdx = 0;
+ ParsePosition pp(-1);
+ format.parse(text, pp);
+ errorIdx = pp.getErrorIndex();
+ if (errorIdx == -1) {
+ errln("failed to report invalid (negative) starting parse position");
+ }
+}
+
#endif /* #if !UCONFIG_NO_FORMATTING */
//eof