status = U_ILLEGAL_ARGUMENT_ERROR;
return NULL;
}
- DateFormat *result = createInstanceForSkeleton(skeleton, locale, status);
+ Locale localeWithCalendar = locale;
+ localeWithCalendar.setKeywordValue("calendar", calendar->getType(), status);
+ if (U_FAILURE(status)) {
+ return NULL;
+ }
+ DateFormat *result = createInstanceForSkeleton(skeleton, localeWithCalendar, status);
if (U_FAILURE(status)) {
return NULL;
}
}
#if !UCONFIG_NO_BREAK_ITERATION
// if first field, check to see whether we need to and are able to titlecase it
- if (fieldNum == 0 && u_islower(appendTo.char32At(beginOffset)) && fCapitalizationBrkIter != NULL) {
+ if (fieldNum == 0 && fCapitalizationBrkIter != NULL && appendTo.length() > beginOffset &&
+ u_islower(appendTo.char32At(beginOffset))) {
UBool titlecase = FALSE;
switch (capitalizationContext) {
case UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE:
CASE(26,Test5554)
CASE(27,Test9237)
CASE(28,TestParsing)
- CASE(29,TestT10334)
- CASE(30,TestT10619)
- CASE(31,TestT10855)
- CASE(32,TestT10906)
- CASE(33,TestT13380)
+ CASE(29,Test12902_yWithGregoCalInThaiLoc)
+ CASE(30,TestT10334)
+ CASE(31,TestT10619)
+ CASE(32,TestT10855)
+ CASE(33,TestT10906)
+ CASE(34,TestT13380)
default: name = ""; break;
}
}
delete cal;
}
+void DateFormatRegressionTest::Test12902_yWithGregoCalInThaiLoc(void) {
+ UErrorCode status = U_ZERO_ERROR;
+ UDate testDate = 43200000.0; // 1970-Jan-01 12:00 GMT
+ const char* skeleton = "y";
+ // Note that in locale "th", the availableFormats for skeleton "y" differ by calendar:
+ // for buddhist (default calendar): y{"G y"}
+ // for gregorian: y{"y"}
+ const char* expectFormat = "1970"; // format for skeleton y in Thai locale with Gregorian calendar
+ UnicodeString getFmtStr;
+
+ const TimeZone* gmtZone = TimeZone::getGMT();
+ LocalPointer<GregorianCalendar> pureGregoCal(new GregorianCalendar(*gmtZone, Locale::getEnglish(), status));
+ if (U_FAILURE(status)) {
+ dataerrln("Fail in new GregorianCalendar(GMT, en): %s", u_errorName(status));
+ return;
+ }
+ LocalPointer<DateFormat> df1(DateFormat::createInstanceForSkeleton(pureGregoCal.orphan(), skeleton, "th", status));
+ if (U_FAILURE(status)) {
+ dataerrln("Fail in DateFormat::createInstanceForSkeleton for th with Grego cal: %s", u_errorName(status));
+ return;
+ }
+ status = U_ZERO_ERROR;
+ getFmtStr.remove();
+ getFmtStr = df1->format(testDate, getFmtStr);
+ if (U_FAILURE(status)) {
+ errln("Fail in DateFormat::format for th with Grego cal: %s", u_errorName(status));
+ } else if (getFmtStr != expectFormat) {
+ char getFormat[32];
+ getFmtStr.extract(0, getFmtStr.length(), getFormat, 32);
+ errln("Error in DateFormat::format for th with Grego cal, expect: %s, get: %s", expectFormat, getFormat);
+ }
+
+ LocalPointer<DateFormat> df2(DateFormat::createInstanceForSkeleton(skeleton, "th-u-ca-gregory", status));
+ if (U_FAILURE(status)) {
+ dataerrln("Fail in DateFormat::createInstanceForSkeleton for th-u-ca-gregory: %s", u_errorName(status));
+ return;
+ }
+ status = U_ZERO_ERROR;
+ getFmtStr.remove();
+ getFmtStr = df2->format(testDate, getFmtStr);
+ if (U_FAILURE(status)) {
+ errln("Fail in DateFormat::format for th-u-ca-gregory: %s", u_errorName(status));
+ } else if (getFmtStr != expectFormat) {
+ char getFormat[32];
+ getFmtStr.extract(0, getFmtStr.length(), getFormat, 32);
+ errln("Error in DateFormat::format for th with Grego cal, expect: %s, get: %s", expectFormat, getFormat);
+ }
+}
+
void DateFormatRegressionTest::TestT10334(void) {
UErrorCode status = U_ZERO_ERROR;
UnicodeString pattern("'--: 'EEE-WW-MMMM-yyyy");
void Test5554(void);
void Test9237(void);
void TestParsing(void);
+ void Test12902_yWithGregoCalInThaiLoc(void);
void TestT10334(void);
void TestT10619(void);
void TestT10855(void);
*/
public final static DateFormat getInstanceForSkeleton(
Calendar cal, String skeleton, ULocale locale) {
+ if (cal != null) {
+ locale = locale.setKeywordValue("calendar", cal.getType());
+ }
DateTimePatternGenerator generator = DateTimePatternGenerator.getInstance(locale);
final String bestPattern = generator.getBestPattern(skeleton);
SimpleDateFormat format = new SimpleDateFormat(bestPattern, locale);
break;
} // switch (patternCharIndex)
- if (fieldNum == 0 && capitalizationContext != null && UCharacter.isLowerCase(buf.codePointAt(bufstart))) {
+ if (fieldNum == 0 && capitalizationContext != null && buf.length() > bufstart &&
+ UCharacter.isLowerCase(buf.codePointAt(bufstart))) {
boolean titlecase = false;
switch (capitalizationContext) {
case CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE:
assertEquals("Bad date format", "Mo1 20, 2013", dangiDateStr);
}
+ @Test
+ public void Test12902_yWithGregoCalInThaiLoc() {
+ final Date testDate = new Date(43200000); // 1970-Jan-01 12:00 GMT
+ final String skeleton = "y";
+ // Note that in locale "th", the availableFormats for skeleton "y" differ by calendar:
+ // for buddhist (default calendar): y{"G y"}
+ // for gregorian: y{"y"}
+ final String expectFormat = "1970"; // format for skeleton y in Thai locale with Gregorian calendar
+
+ GregorianCalendar pureGregorianCalendar = new GregorianCalendar(TimeZone.GMT_ZONE, ULocale.ENGLISH);
+ pureGregorianCalendar.setGregorianChange(new Date(Long.MIN_VALUE)); // Per original bug, but not relevant
+ DateFormat df1 = DateFormat.getPatternInstance(pureGregorianCalendar, skeleton, ULocale.forLanguageTag("th"));
+ try {
+ String getFormat = df1.format(testDate);
+ if (!getFormat.equals(expectFormat)) {
+ errln("Error in DateFormat.format for th with Grego cal, expect: " + expectFormat + ", get: " + getFormat);
+ }
+ } catch (Exception e) {
+ errln("Fail in DateFormat.format for th with Grego cal: " + e);
+ }
+
+ DateFormat df2 = DateFormat.getPatternInstance(skeleton, new ULocale("th-u-ca-gregory"));
+ try {
+ String getFormat = df2.format(testDate);
+ if (!getFormat.equals(expectFormat)) {
+ errln("Error in DateFormat.format for th-u-ca-gregory, expect: " + expectFormat + ", get: " + getFormat);
+ }
+ } catch (Exception e) {
+ errln("Fail in DateFormat.format for th-u-ca-gregory: " + e);
+ }
+ }
+
@Test
public void TestT10110() {
try {