1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /***********************************************************************
4 * Copyright (c) 1997-2011, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 ***********************************************************************/
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/format.h"
15 #include "unicode/decimfmt.h"
16 #include "unicode/datefmt.h"
17 #include "unicode/smpdtfmt.h"
18 #include "unicode/dtfmtsym.h"
19 #include "unicode/locid.h"
20 #include "unicode/msgfmt.h"
21 #include "unicode/numfmt.h"
22 #include "unicode/choicfmt.h"
23 #include "unicode/gregocal.h"
25 // *****************************************************************************
26 // class DateFormatMiscTests
27 // *****************************************************************************
29 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
32 DateFormatMiscTests::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
34 // if (exec) logln((UnicodeString)"TestSuite DateFormatMiscTests");
40 default: name
= ""; break;
45 DateFormatMiscTests::failure(UErrorCode status
, const char* msg
)
47 if(U_FAILURE(status
)) {
48 errcheckln(status
, UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
59 DateFormatMiscTests::test4097450()
62 // Date parse requiring 4 digit year.
64 UnicodeString dstring
[] = {
66 UnicodeString("1997"),
68 UnicodeString("1997"),
70 UnicodeString("2001"),
72 UnicodeString("2001"),
81 UnicodeString dformat
[] = {
84 UnicodeString("yyyy"),
85 UnicodeString("yyyy"),
88 UnicodeString("yyyy"),
89 UnicodeString("yyyy"),
91 UnicodeString("yyyy"),
93 UnicodeString("yyyy"),
98 /* UBool dresult [] = {
115 UErrorCode status
= U_ZERO_ERROR
;
116 SimpleDateFormat
*formatter
;
117 SimpleDateFormat
*resultFormatter
= new SimpleDateFormat((UnicodeString
)"yyyy", status
);
118 if (U_FAILURE(status
)) {
119 dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status
));
123 logln("Format\tSource\tResult");
124 logln("-------\t-------\t-------");
125 for (int i
= 0; i
< 14/*dstring.length*/; i
++)
127 log(dformat
[i
] + "\t" + dstring
[i
] + "\t");
128 formatter
= new SimpleDateFormat(dformat
[i
], status
);
129 if(failure(status
, "new SimpleDateFormat")) return;
132 FieldPosition
pos(FieldPosition::DONT_CARE
);
133 logln(resultFormatter
->format(formatter
->parse(dstring
[i
], status
), str
, pos
));
134 failure(status
, "resultFormatter->format");
135 //if ( !dresult[i] ) System.out.print(" <-- error!");
137 catch (ParseException exception) {
138 //if ( dresult[i] ) System.out.print(" <-- error!");
139 System.out.print("exception --> " + exception);
145 delete resultFormatter
;
152 DateFormatMiscTests::test4099975()
155 * Test Constructor SimpleDateFormat::SimpleDateFormat (const UnicodeString & pattern,
156 * const DateFormatSymbols & formatData, UErrorCode & status )
157 * The DateFormatSymbols object is NOT adopted; Modifying the original DateFormatSymbols
158 * should not change the SimpleDateFormat's behavior.
160 UDate d
= Calendar::getNow();
162 UErrorCode status
= U_ZERO_ERROR
;
163 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
164 if (U_FAILURE(status
)) {
165 dataerrln("Unable to create DateFormatSymbols - %s", u_errorName(status
));
168 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), *symbols
, status
);
169 if(failure(status
, "new SimpleDateFormat")) return;
170 UnicodeString format0
;
171 format0
= df
->format(d
, format0
);
172 UnicodeString localizedPattern0
;
173 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
174 failure(status
, "df->toLocalizedPattern");
175 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
176 UnicodeString format1
;
177 format1
= df
->format(d
, format1
);
178 if (format0
!= format1
) {
179 errln(UnicodeString("Formats are different. format0: ") + format0
180 + UnicodeString("; format1: ") + format1
);
182 UnicodeString localizedPattern1
;
183 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
184 failure(status
, "df->toLocalizedPattern");
185 if (localizedPattern0
!= localizedPattern1
) {
186 errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
187 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
193 * Test void SimpleDateFormat::setDateFormatSymbols ( const DateFormatSymbols & newFormatSymbols )
194 * Modifying the original DateFormatSymbols should not change the SimpleDateFormat's behavior.
197 UErrorCode status
= U_ZERO_ERROR
;
198 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
199 if(failure(status
, "new DateFormatSymbols")) return;
200 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), status
);
201 if(failure(status
, "new SimpleDateFormat")) return;
202 df
->setDateFormatSymbols(*symbols
);
203 UnicodeString format0
;
204 format0
= df
->format(d
, format0
);
205 UnicodeString localizedPattern0
;
206 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
207 failure(status
, "df->toLocalizedPattern");
208 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
209 UnicodeString format1
;
210 format1
= df
->format(d
, format1
);
211 if (format0
!= format1
) {
212 errln(UnicodeString("Formats are different. format0: ") + format0
213 + UnicodeString("; format1: ") + format1
);
215 UnicodeString localizedPattern1
;
216 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
217 failure(status
, "df->toLocalizedPattern");
218 if (localizedPattern0
!= localizedPattern1
) {
219 errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
220 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
226 //Test the pointer version of the constructor (and the adoptDateFormatSymbols method)
228 UErrorCode status
= U_ZERO_ERROR
;
229 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
230 if(failure(status
, "new DateFormatSymbols")) return;
231 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), symbols
, status
);
232 if(failure(status
, "new SimpleDateFormat")) return;
233 UnicodeString format0
;
234 format0
= df
->format(d
, format0
);
235 UnicodeString localizedPattern0
;
236 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
237 failure(status
, "df->toLocalizedPattern");
238 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
239 UnicodeString format1
;
240 format1
= df
->format(d
, format1
);
241 if (format0
!= format1
) {
242 errln(UnicodeString("Formats are different. format0: ") + format0
243 + UnicodeString("; format1: ") + format1
);
245 UnicodeString localizedPattern1
;
246 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
247 failure(status
, "df->toLocalizedPattern");
248 if (localizedPattern0
== localizedPattern1
) {
249 errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0
250 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
252 //delete symbols; the caller is no longer responsible for deleting the symbols
257 UErrorCode status
= U_ZERO_ERROR
;
258 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
259 failure(status
, "new DateFormatSymbols");
260 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), status
);
261 if(failure(status
, "new SimpleDateFormat")) return;
262 df
-> adoptDateFormatSymbols(symbols
);
263 UnicodeString format0
;
264 format0
= df
->format(d
, format0
);
265 UnicodeString localizedPattern0
;
266 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
267 failure(status
, "df->toLocalizedPattern");
268 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
269 UnicodeString format1
;
270 format1
= df
->format(d
, format1
);
271 if (format0
!= format1
) {
272 errln(UnicodeString("Formats are different. format0: ") + format0
273 + UnicodeString("; format1: ") + format1
);
275 UnicodeString localizedPattern1
;
276 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
277 failure(status
, "df->toLocalizedPattern");
278 if (localizedPattern0
== localizedPattern1
) {
279 errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0
280 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
282 //delete symbols; the caller is no longer responsible for deleting the symbols
288 * @test @(#)bug4117335.java 1.1 3/5/98
293 DateFormatMiscTests::test4117335()
295 //UnicodeString bc = "\u7d00\u5143\u524d";
301 UnicodeString
bc(bcC
, 3, 3);
303 //UnicodeString ad = "\u897f\u66a6";
308 UnicodeString
ad(adC
, 2, 2);
310 //UnicodeString jstLong = "\u65e5\u672c\u6a19\u6e96\u6642";
311 UChar jstLongC
[] = {
318 UChar jdtLongC
[] = {0x65E5, 0x672C, 0x590F, 0x6642, 0x9593};
320 UnicodeString
jstLong(jstLongC
, 5, 5);
322 // UnicodeString jstShort = "JST";
324 UnicodeString tzID
= "Asia/Tokyo";
326 UnicodeString
jdtLong(jdtLongC
, 5, 5);
328 // UnicodeString jdtShort = "JDT";
329 UErrorCode status
= U_ZERO_ERROR
;
330 DateFormatSymbols
*symbols
= new DateFormatSymbols(Locale::getJapan(), status
);
331 if(U_FAILURE(status
)) {
332 dataerrln("Failure creating DateFormatSymbols, %s", u_errorName(status
));
336 failure(status
, "new DateFormatSymbols");
337 int32_t eraCount
= 0;
338 const UnicodeString
*eras
= symbols
->getEraNames(eraCount
);
340 logln(UnicodeString("BC = ") + eras
[0]);
342 errln("*** Should have been " + bc
);
343 //throw new Exception("Error in BC");
346 logln(UnicodeString("AD = ") + eras
[1]);
348 errln("*** Should have been " + ad
);
349 //throw new Exception("Error in AD");
352 int32_t rowCount
, colCount
;
353 const UnicodeString
**zones
= symbols
->getZoneStrings(rowCount
, colCount
);
354 //don't hard code the index .. compute it.
356 for (int32_t i
= 0; i
< rowCount
; ++i
) {
357 if (tzID
== (zones
[i
][0])) {
362 logln(UnicodeString("Long zone name = ") + zones
[index
][1]);
363 if (zones
[index
][1] != jstLong
) {
364 errln("*** Should have been " + prettify(jstLong
)+ " but it is: " + prettify(zones
[index
][1]));
365 //throw new Exception("Error in long TZ name");
367 // logln(UnicodeString("Short zone name = ") + zones[index][2]);
368 // if (zones[index][2] != jstShort) {
369 // errln("*** Should have been " + prettify(jstShort) + " but it is: " + prettify(zones[index][2]));
370 // //throw new Exception("Error in short TZ name");
372 logln(UnicodeString("Long zone name = ") + zones
[index
][3]);
373 if (zones
[index
][3] != jdtLong
) {
374 errln("*** Should have been " + prettify(jstLong
) + " but it is: " + prettify(zones
[index
][3]));
375 //throw new Exception("Error in long TZ name");
377 // logln(UnicodeString("SHORT zone name = ") + zones[index][4]);
378 // if (zones[index][4] != jdtShort) {
379 // errln("*** Should have been " + prettify(jstShort)+ " but it is: " + prettify(zones[index][4]));
380 // //throw new Exception("Error in short TZ name");
386 #endif /* #if !UCONFIG_NO_FORMATTING */