1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * Copyright (c) 2011-2016, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 ********************************************************************/
7 /* C API TEST FOR DATE INTERVAL FORMAT */
8
9 #include "unicode/utypes.h"
10
11 #if !UCONFIG_NO_FORMATTING
12
13 #include "unicode/udateintervalformat.h"
14 #include "unicode/udat.h"
15 #include "unicode/ucal.h"
16 #include "unicode/ustring.h"
17 #include "unicode/udisplaycontext.h"
18 #include "cintltst.h"
19 #include "cmemory.h"
20 #include "cformtst.h"
21
22 static void TestDateIntervalFormat(void);
23 static void TestFPos_SkelWithSeconds(void);
24 static void TestFormatToResult(void);
25 static void TestFormatCalendarToResult(void);
26
27 void addDateIntervalFormatTest(TestNode** root);
28
29 #define TESTCASE(x) addTest(root, &x, "tsformat/cdateintervalformattest/" #x)
30
addDateIntervalFormatTest(TestNode ** root)31 void addDateIntervalFormatTest(TestNode** root)
32 {
33 TESTCASE(TestDateIntervalFormat);
34 TESTCASE(TestFPos_SkelWithSeconds);
35 TESTCASE(TestFormatToResult);
36 TESTCASE(TestFormatCalendarToResult);
37 }
38
39 static const char tzUSPacific[] = "US/Pacific";
40 static const char tzAsiaTokyo[] = "Asia/Tokyo";
41 #define Date201103021030 1299090600000.0 /* 2011-Mar-02 1030 in US/Pacific, 2011-Mar-03 0330 in Asia/Tokyo */
42 #define Date201009270800 1285599629000.0 /* 2010-Sep-27 0800 in US/Pacific */
43 #define Date158210140000 -12219379142000.0
44 #define Date158210160000 -12219206342000.0
45 #define _MINUTE (60.0*1000.0)
46 #define _HOUR (60.0*60.0*1000.0)
47 #define _DAY (24.0*60.0*60.0*1000.0)
48
49 typedef struct {
50 const char * locale;
51 const char * skeleton;
52 UDisplayContext context;
53 const char * tzid;
54 const UDate from;
55 const UDate to;
56 const char * resultExpected;
57 } DateIntervalFormatTestItem;
58
59 #define CAP_NONE UDISPCTX_CAPITALIZATION_NONE
60 #define CAP_BEGIN UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE
61 #define CAP_LIST UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU
62 #define CAP_ALONE UDISPCTX_CAPITALIZATION_FOR_STANDALONE
63
64 /* Just a small set of tests for now, the real functionality is tested in the C++ tests */
65 static const DateIntervalFormatTestItem testItems[] = {
66 { "en", "MMMdHHmm", CAP_NONE, tzUSPacific, Date201103021030, Date201103021030 + 7.0*_HOUR, "Mar 2, 10:30 \\u2013 17:30" },
67 { "en", "MMMdHHmm", CAP_NONE, tzAsiaTokyo, Date201103021030, Date201103021030 + 7.0*_HOUR, "Mar 3, 03:30 \\u2013 10:30" },
68 { "en", "yMMMEd", CAP_NONE, tzUSPacific, Date201009270800, Date201009270800 + 12.0*_HOUR, "Mon, Sep 27, 2010" },
69 { "en", "yMMMEd", CAP_NONE, tzUSPacific, Date201009270800, Date201009270800 + 31.0*_DAY, "Mon, Sep 27 \\u2013 Thu, Oct 28, 2010" },
70 { "en", "yMMMEd", CAP_NONE, tzUSPacific, Date201009270800, Date201009270800 + 410.0*_DAY, "Mon, Sep 27, 2010 \\u2013 Fri, Nov 11, 2011" },
71 { "de", "Hm", CAP_NONE, tzUSPacific, Date201009270800, Date201009270800 + 12.0*_HOUR, "08:00\\u201320:00 Uhr" },
72 { "de", "Hm", CAP_NONE, tzUSPacific, Date201009270800, Date201009270800 + 31.0*_DAY, "27.9.2010, 08:00 \\u2013 28.10.2010, 08:00" },
73 { "ja", "MMMd", CAP_NONE, tzUSPacific, Date201009270800, Date201009270800 + 1.0*_DAY, "9\\u670827\\u65E5\\uFF5E28\\u65E5" },
74 { "cs", "MMMEd", CAP_NONE, tzUSPacific, Date201009270800, Date201009270800 + 60.0*_DAY, "po 27. 9. \\u2013 p\\u00E1 26. 11." },
75 { "cs", "yMMMM", CAP_NONE, tzUSPacific, Date201009270800, Date201009270800 + 60.0*_DAY, "z\\u00E1\\u0159\\u00ED\\u2013listopad 2010" },
76 { "cs", "yMMMM", CAP_NONE, tzUSPacific, Date201009270800, Date201009270800 + 1.0*_DAY, "z\\u00E1\\u0159\\u00ED 2010" },
77 #if !UCONFIG_NO_BREAK_ITERATION
78 { "cs", "MMMEd", CAP_BEGIN, tzUSPacific, Date201009270800, Date201009270800 + 60.0*_DAY, "Po 27. 9. \\u2013 p\\u00E1 26. 11." },
79 { "cs", "yMMMM", CAP_BEGIN, tzUSPacific, Date201009270800, Date201009270800 + 60.0*_DAY, "Z\\u00E1\\u0159\\u00ED\\u2013listopad 2010" },
80 { "cs", "yMMMM", CAP_BEGIN, tzUSPacific, Date201009270800, Date201009270800 + 1.0*_DAY, "Z\\u00E1\\u0159\\u00ED 2010" },
81 { "cs", "MMMEd", CAP_LIST, tzUSPacific, Date201009270800, Date201009270800 + 60.0*_DAY, "Po 27. 9. \\u2013 p\\u00E1 26. 11." },
82 { "cs", "yMMMM", CAP_LIST, tzUSPacific, Date201009270800, Date201009270800 + 60.0*_DAY, "Z\\u00E1\\u0159\\u00ED\\u2013listopad 2010" },
83 { "cs", "yMMMM", CAP_LIST, tzUSPacific, Date201009270800, Date201009270800 + 1.0*_DAY, "Z\\u00E1\\u0159\\u00ED 2010" },
84 { "cs", "MMMEd", CAP_ALONE, tzUSPacific, Date201009270800, Date201009270800 + 60.0*_DAY, "po 27. 9. \\u2013 p\\u00E1 26. 11." },
85 #endif
86 { "cs", "yMMMM", CAP_ALONE, tzUSPacific, Date201009270800, Date201009270800 + 60.0*_DAY, "z\\u00E1\\u0159\\u00ED\\u2013listopad 2010" },
87 { "cs", "yMMMM", CAP_ALONE, tzUSPacific, Date201009270800, Date201009270800 + 1.0*_DAY, "z\\u00E1\\u0159\\u00ED 2010" },
88 { NULL, NULL, CAP_NONE, NULL, 0, 0, NULL }
89 };
90
91 enum {
92 kSkelBufLen = 32,
93 kTZIDBufLen = 96,
94 kFormatBufLen = 128
95 };
96
TestDateIntervalFormat()97 static void TestDateIntervalFormat()
98 {
99 const DateIntervalFormatTestItem * testItemPtr;
100 UErrorCode status = U_ZERO_ERROR;
101 ctest_setTimeZone(NULL, &status);
102 log_verbose("\nTesting udtitvfmt_open() and udtitvfmt_format() with various parameters\n");
103 for ( testItemPtr = testItems; testItemPtr->locale != NULL; ++testItemPtr ) {
104 UDateIntervalFormat* udtitvfmt;
105 int32_t tzidLen;
106 UChar skelBuf[kSkelBufLen];
107 UChar tzidBuf[kTZIDBufLen];
108 const char * tzidForLog = (testItemPtr->tzid)? testItemPtr->tzid: "NULL";
109
110 status = U_ZERO_ERROR;
111 u_unescape(testItemPtr->skeleton, skelBuf, kSkelBufLen);
112 if ( testItemPtr->tzid ) {
113 u_unescape(testItemPtr->tzid, tzidBuf, kTZIDBufLen);
114 tzidLen = -1;
115 } else {
116 tzidLen = 0;
117 }
118 udtitvfmt = udtitvfmt_open(testItemPtr->locale, skelBuf, -1, tzidBuf, tzidLen, &status);
119 if ( U_SUCCESS(status) ) {
120 UChar result[kFormatBufLen];
121 UChar resultExpected[kFormatBufLen];
122
123 udtitvfmt_setContext(udtitvfmt, testItemPtr->context, &status);
124 if ( U_FAILURE(status) ) {
125 log_err("FAIL: udtitvfmt_setContext for locale %s, skeleton %s, context %04X - %s\n",
126 testItemPtr->locale, testItemPtr->skeleton, (unsigned)testItemPtr->context, myErrorName(status) );
127 } else {
128 UDisplayContext getContext = udtitvfmt_getContext(udtitvfmt, UDISPCTX_TYPE_CAPITALIZATION, &status);
129 if ( U_FAILURE(status) ) {
130 log_err("FAIL: udtitvfmt_getContext for locale %s, skeleton %s, context %04X - %s\n",
131 testItemPtr->locale, testItemPtr->skeleton, (unsigned)testItemPtr->context, myErrorName(status) );
132 } else if (getContext != testItemPtr->context) {
133 log_err("FAIL: udtitvfmt_getContext for locale %s, skeleton %s, context %04X - got context %04X\n",
134 testItemPtr->locale, testItemPtr->skeleton, (unsigned)testItemPtr->context, (unsigned)getContext );
135 }
136 }
137 status = U_ZERO_ERROR;
138 int32_t fmtLen = udtitvfmt_format(udtitvfmt, testItemPtr->from, testItemPtr->to, result, kFormatBufLen, NULL, &status);
139 if (fmtLen >= kFormatBufLen) {
140 result[kFormatBufLen-1] = 0;
141 }
142 if ( U_SUCCESS(status) ) {
143 u_unescape(testItemPtr->resultExpected, resultExpected, kFormatBufLen);
144 if ( u_strcmp(result, resultExpected) != 0 ) {
145 char bcharBuf[kFormatBufLen];
146 log_err("ERROR: udtitvfmt_format for locale %s, skeleton %s, tzid %s, from %.1f, to %.1f: expect %s, get %s\n",
147 testItemPtr->locale, testItemPtr->skeleton, tzidForLog, testItemPtr->from, testItemPtr->to,
148 testItemPtr->resultExpected, u_austrcpy(bcharBuf,result) );
149 }
150 } else {
151 log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, tzid %s, from %.1f, to %.1f: %s\n",
152 testItemPtr->locale, testItemPtr->skeleton, tzidForLog, testItemPtr->from, testItemPtr->to, myErrorName(status) );
153 }
154 udtitvfmt_close(udtitvfmt);
155 } else {
156 log_data_err("FAIL: udtitvfmt_open for locale %s, skeleton %s, tzid %s - %s\n",
157 testItemPtr->locale, testItemPtr->skeleton, tzidForLog, myErrorName(status) );
158 }
159 }
160 ctest_resetTimeZone();
161 }
162
163 /********************************************************************
164 * TestFPos_SkelWithSeconds and related data
165 ********************************************************************
166 */
167
168 static UChar zoneGMT[] = { 0x47,0x4D,0x54,0 }; // GMT
169 static const UDate startTime = 1416474000000.0; // 2014 Nov 20 09:00 GMT
170
171 static const double deltas[] = {
172 0.0, // none
173 200.0, // 200 millisec
174 20000.0, // 20 sec
175 1200000.0, // 20 min
176 7200000.0, // 2 hrs
177 43200000.0, // 12 hrs
178 691200000.0, // 8 days
179 1382400000.0, // 16 days,
180 8640000000.0, // 100 days
181 -1.0
182 };
183 enum { kNumDeltas = UPRV_LENGTHOF(deltas) - 1 };
184
185 typedef struct {
186 int32_t posBegin;
187 int32_t posEnd;
188 const char * format;
189 } ExpectPosAndFormat;
190
191 static const ExpectPosAndFormat exp_en_HHmm[kNumDeltas] = {
192 { 3, 5, "09:00" },
193 { 3, 5, "09:00" },
194 { 3, 5, "09:00" },
195 { 3, 5, "09:00 \\u2013 09:20" },
196 { 3, 5, "09:00 \\u2013 11:00" },
197 { 3, 5, "09:00 \\u2013 21:00" },
198 { 15, 17, "11/20/2014, 09:00 \\u2013 11/28/2014, 09:00" },
199 { 15, 17, "11/20/2014, 09:00 \\u2013 12/6/2014, 09:00" },
200 { 15, 17, "11/20/2014, 09:00 \\u2013 2/28/2015, 09:00" }
201 };
202
203 static const ExpectPosAndFormat exp_en_HHmmss[kNumDeltas] = {
204 { 3, 5, "09:00:00" },
205 { 3, 5, "09:00:00" },
206 { 3, 5, "09:00:00 \\u2013 09:00:20" },
207 { 3, 5, "09:00:00 \\u2013 09:20:00" },
208 { 3, 5, "09:00:00 \\u2013 11:00:00" },
209 { 3, 5, "09:00:00 \\u2013 21:00:00" },
210 { 15, 17, "11/20/2014, 09:00:00 \\u2013 11/28/2014, 09:00:00" },
211 { 15, 17, "11/20/2014, 09:00:00 \\u2013 12/6/2014, 09:00:00" },
212 { 15, 17, "11/20/2014, 09:00:00 \\u2013 2/28/2015, 09:00:00" }
213 };
214
215 static const ExpectPosAndFormat exp_en_yyMMdd[kNumDeltas] = {
216 { 0, 0, "11/20/14" },
217 { 0, 0, "11/20/14" },
218 { 0, 0, "11/20/14" },
219 { 0, 0, "11/20/14" },
220 { 0, 0, "11/20/14" },
221 { 0, 0, "11/20/14" },
222 { 0, 0, "11/20/14 \\u2013 11/28/14" },
223 { 0, 0, "11/20/14 \\u2013 12/6/14" },
224 { 0, 0, "11/20/14 \\u2013 2/28/15" }
225 };
226
227 static const ExpectPosAndFormat exp_en_yyMMddHHmm[kNumDeltas] = {
228 { 13, 15, "11/20/14, 09:00" },
229 { 13, 15, "11/20/14, 09:00" },
230 { 13, 15, "11/20/14, 09:00" },
231 { 13, 15, "11/20/14, 09:00 \\u2013 09:20" },
232 { 13, 15, "11/20/14, 09:00 \\u2013 11:00" },
233 { 13, 15, "11/20/14, 09:00 \\u2013 21:00" },
234 { 13, 15, "11/20/14, 09:00 \\u2013 11/28/14, 09:00" },
235 { 13, 15, "11/20/14, 09:00 \\u2013 12/06/14, 09:00" },
236 { 13, 15, "11/20/14, 09:00 \\u2013 02/28/15, 09:00" }
237 };
238
239 static const ExpectPosAndFormat exp_en_yyMMddHHmmss[kNumDeltas] = {
240 { 13, 15, "11/20/14, 09:00:00" },
241 { 13, 15, "11/20/14, 09:00:00" },
242 { 13, 15, "11/20/14, 09:00:00 \\u2013 09:00:20" },
243 { 13, 15, "11/20/14, 09:00:00 \\u2013 09:20:00" },
244 { 13, 15, "11/20/14, 09:00:00 \\u2013 11:00:00" },
245 { 13, 15, "11/20/14, 09:00:00 \\u2013 21:00:00" },
246 { 13, 15, "11/20/14, 09:00:00 \\u2013 11/28/14, 09:00:00" },
247 { 13, 15, "11/20/14, 09:00:00 \\u2013 12/06/14, 09:00:00" },
248 { 13, 15, "11/20/14, 09:00:00 \\u2013 02/28/15, 09:00:00" }
249 };
250
251 static const ExpectPosAndFormat exp_en_yMMMdhmmssz[kNumDeltas] = {
252 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT" },
253 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT" },
254 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 9:00:20 AM GMT" },
255 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 9:20:00 AM GMT" },
256 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 11:00:00 AM GMT" },
257 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 9:00:00 PM GMT" },
258 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 Nov 28, 2014, 9:00:00 AM GMT" },
259 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 Dec 6, 2014, 9:00:00 AM GMT" },
260 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 Feb 28, 2015, 9:00:00 AM GMT" }
261 };
262
263 static const ExpectPosAndFormat exp_ja_yyMMddHHmm[kNumDeltas] = {
264 { 11, 13, "14/11/20 9:00" },
265 { 11, 13, "14/11/20 9:00" },
266 { 11, 13, "14/11/20 9:00" },
267 { 11, 13, "14/11/20 9\\u664200\\u5206\\uFF5E9\\u664220\\u5206" },
268 { 11, 13, "14/11/20 9\\u664200\\u5206\\uFF5E11\\u664200\\u5206" },
269 { 11, 13, "14/11/20 9\\u664200\\u5206\\uFF5E21\\u664200\\u5206" },
270 { 11, 13, "14/11/20 9:00\\uFF5E14/11/28 9:00" },
271 { 11, 13, "14/11/20 9:00\\uFF5E14/12/06 9:00" },
272 { 11, 13, "14/11/20 9:00\\uFF5E15/02/28 9:00" }
273 };
274
275 static const ExpectPosAndFormat exp_ja_yyMMddHHmmss[kNumDeltas] = {
276 { 11, 13, "14/11/20 9:00:00" },
277 { 11, 13, "14/11/20 9:00:00" },
278 { 11, 13, "14/11/20 9:00:00\\uFF5E9:00:20" },
279 { 11, 13, "14/11/20 9:00:00\\uFF5E9:20:00" },
280 { 11, 13, "14/11/20 9:00:00\\uFF5E11:00:00" },
281 { 11, 13, "14/11/20 9:00:00\\uFF5E21:00:00" },
282 { 11, 13, "14/11/20 9:00:00\\uFF5E14/11/28 9:00:00" },
283 { 11, 13, "14/11/20 9:00:00\\uFF5E14/12/06 9:00:00" },
284 { 11, 13, "14/11/20 9:00:00\\uFF5E15/02/28 9:00:00" }
285 };
286
287 static const ExpectPosAndFormat exp_ja_yMMMdHHmmss[kNumDeltas] = {
288 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00" },
289 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00" },
290 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E9:00:20" },
291 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E9:20:00" },
292 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E11:00:00" },
293 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E21:00:00" },
294 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E2014\\u5E7411\\u670828\\u65E5 9:00:00" },
295 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E2014\\u5E7412\\u67086\\u65E5 9:00:00" },
296 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E2015\\u5E742\\u670828\\u65E5 9:00:00" }
297 };
298
299 typedef struct {
300 const char * locale;
301 const char * skeleton;
302 UDateFormatField fieldToCheck;
303 const ExpectPosAndFormat * expected;
304 } LocaleAndSkeletonItem;
305
306 static const LocaleAndSkeletonItem locSkelItems[] = {
307 { "en", "HHmm", UDAT_MINUTE_FIELD, exp_en_HHmm },
308 { "en", "HHmmss", UDAT_MINUTE_FIELD, exp_en_HHmmss },
309 { "en", "yyMMdd", UDAT_MINUTE_FIELD, exp_en_yyMMdd },
310 { "en", "yyMMddHHmm", UDAT_MINUTE_FIELD, exp_en_yyMMddHHmm },
311 { "en", "yyMMddHHmmss", UDAT_MINUTE_FIELD, exp_en_yyMMddHHmmss },
312 { "en", "yMMMdhmmssz", UDAT_MINUTE_FIELD, exp_en_yMMMdhmmssz },
313 { "ja", "yyMMddHHmm", UDAT_MINUTE_FIELD, exp_ja_yyMMddHHmm },
314 { "ja", "yyMMddHHmmss", UDAT_MINUTE_FIELD, exp_ja_yyMMddHHmmss },
315 { "ja", "yMMMdHHmmss", UDAT_MINUTE_FIELD, exp_ja_yMMMdHHmmss },
316 { NULL, NULL, (UDateFormatField)0, NULL }
317 };
318
319 enum { kSizeUBuf = 96, kSizeBBuf = 192 };
320
TestFPos_SkelWithSeconds()321 static void TestFPos_SkelWithSeconds()
322 {
323 const LocaleAndSkeletonItem * locSkelItemPtr;
324 for (locSkelItemPtr = locSkelItems; locSkelItemPtr->locale != NULL; locSkelItemPtr++) {
325 UDateIntervalFormat* udifmt;
326 UChar ubuf[kSizeUBuf];
327 int32_t ulen, uelen;
328 UErrorCode status = U_ZERO_ERROR;
329 ulen = u_unescape(locSkelItemPtr->skeleton, ubuf, kSizeUBuf);
330 udifmt = udtitvfmt_open(locSkelItemPtr->locale, ubuf, ulen, zoneGMT, -1, &status);
331 if ( U_FAILURE(status) ) {
332 log_data_err("FAIL: udtitvfmt_open for locale %s, skeleton %s: %s\n",
333 locSkelItemPtr->locale, locSkelItemPtr->skeleton, u_errorName(status));
334 } else {
335 const double * deltasPtr = deltas;
336 const ExpectPosAndFormat * expectedPtr = locSkelItemPtr->expected;
337 for (; *deltasPtr >= 0.0; deltasPtr++, expectedPtr++) {
338 UFieldPosition fpos = { locSkelItemPtr->fieldToCheck, 0, 0 };
339 UChar uebuf[kSizeUBuf];
340 char bbuf[kSizeBBuf];
341 char bebuf[kSizeBBuf];
342 status = U_ZERO_ERROR;
343 uelen = u_unescape(expectedPtr->format, uebuf, kSizeUBuf);
344 ulen = udtitvfmt_format(udifmt, startTime, startTime + *deltasPtr, ubuf, kSizeUBuf, &fpos, &status);
345 if ( U_FAILURE(status) ) {
346 log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, delta %.1f: %s\n",
347 locSkelItemPtr->locale, locSkelItemPtr->skeleton, *deltasPtr, u_errorName(status));
348 } else if ( ulen != uelen || u_strncmp(ubuf,uebuf,uelen) != 0 ||
349 fpos.beginIndex != expectedPtr->posBegin || fpos.endIndex != expectedPtr->posEnd ) {
350 u_strToUTF8(bbuf, kSizeBBuf, NULL, ubuf, ulen, &status);
351 u_strToUTF8(bebuf, kSizeBBuf, NULL, uebuf, uelen, &status); // convert back to get unescaped string
352 log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, delta %12.1f, expect %d-%d \"%s\", get %d-%d \"%s\"\n",
353 locSkelItemPtr->locale, locSkelItemPtr->skeleton, *deltasPtr,
354 expectedPtr->posBegin, expectedPtr->posEnd, bebuf,
355 fpos.beginIndex, fpos.endIndex, bbuf);
356 }
357 }
358 udtitvfmt_close(udifmt);
359 }
360 }
361 }
362
TestFormatToResult()363 static void TestFormatToResult() {
364 UErrorCode ec = U_ZERO_ERROR;
365 UDateIntervalFormat* fmt = udtitvfmt_open("de", u"dMMMMyHHmm", -1, zoneGMT, -1, &ec);
366 UFormattedDateInterval* fdi = udtitvfmt_openResult(&ec);
367 assertSuccess("Opening", &ec);
368
369 {
370 const char* message = "Field position test 1";
371 const UChar* expectedString = u"27. September 2010, 15:00 – 2. März 2011, 18:30";
372 udtitvfmt_formatToResult(fmt, Date201009270800, Date201103021030, fdi, &ec);
373 assertSuccess("Formatting", &ec);
374 static const UFieldPositionWithCategory expectedFieldPositions[] = {
375 // category, field, begin index, end index
376 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 0, 0, 25},
377 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 0, 2},
378 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 4, 13},
379 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 14, 18},
380 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 20, 22},
381 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 23, 25},
382 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 1, 28, 47},
383 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 28, 29},
384 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 31, 35},
385 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 36, 40},
386 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 42, 44},
387 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 45, 47}};
388 checkMixedFormattedValue(
389 message,
390 udtitvfmt_resultAsValue(fdi, &ec),
391 expectedString,
392 expectedFieldPositions,
393 UPRV_LENGTHOF(expectedFieldPositions));
394 }
395 {
396 const char* message = "Field position test 2";
397 const UChar* expectedString = u"27. September 2010, 15:00–22:00 Uhr";
398 udtitvfmt_formatToResult(fmt, Date201009270800, Date201009270800 + 7*_HOUR, fdi, &ec);
399 assertSuccess("Formatting", &ec);
400 static const UFieldPositionWithCategory expectedFieldPositions[] = {
401 // category, field, begin index, end index
402 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 0, 2},
403 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 4, 13},
404 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 14, 18},
405 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 0, 20, 25},
406 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 20, 22},
407 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 23, 25},
408 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 1, 26, 31},
409 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 26, 28},
410 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 29, 31},
411 {UFIELD_CATEGORY_DATE, UDAT_AM_PM_FIELD, 32, 35}};
412 checkMixedFormattedValue(
413 message,
414 udtitvfmt_resultAsValue(fdi, &ec),
415 expectedString,
416 expectedFieldPositions,
417 UPRV_LENGTHOF(expectedFieldPositions));
418 }
419
420 udtitvfmt_close(fmt);
421 udtitvfmt_closeResult(fdi);
422 }
423
TestFormatCalendarToResult()424 static void TestFormatCalendarToResult() {
425 UErrorCode ec = U_ZERO_ERROR;
426 UCalendar* ucal1 = ucal_open(zoneGMT, -1, "de", UCAL_DEFAULT, &ec);
427 ucal_setMillis(ucal1, Date201009270800, &ec);
428 UCalendar* ucal2 = ucal_open(zoneGMT, -1, "de", UCAL_DEFAULT, &ec);
429 ucal_setMillis(ucal2, Date201103021030, &ec);
430 UCalendar* ucal3 = ucal_open(zoneGMT, -1, "de", UCAL_DEFAULT, &ec);
431 ucal_setMillis(ucal3, Date201009270800 + 7*_HOUR, &ec);
432 UCalendar* ucal4 = ucal_open(zoneGMT, -1, "de", UCAL_DEFAULT, &ec);
433 UCalendar* ucal5 = ucal_open(zoneGMT, -1, "de", UCAL_DEFAULT, &ec);
434
435 UDateIntervalFormat* fmt = udtitvfmt_open("de", u"dMMMMyHHmm", -1, zoneGMT, -1, &ec);
436 UFormattedDateInterval* fdi = udtitvfmt_openResult(&ec);
437 assertSuccess("Opening", &ec);
438
439 {
440 const char* message = "Field position test 1";
441 const UChar* expectedString = u"27. September 2010, 15:00 – 2. März 2011, 18:30";
442 udtitvfmt_formatCalendarToResult(fmt, ucal1, ucal2, fdi, &ec);
443 assertSuccess("Formatting", &ec);
444 static const UFieldPositionWithCategory expectedFieldPositions[] = {
445 // category, field, begin index, end index
446 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 0, 0, 25},
447 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 0, 2},
448 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 4, 13},
449 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 14, 18},
450 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 20, 22},
451 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 23, 25},
452 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 1, 28, 47},
453 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 28, 29},
454 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 31, 35},
455 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 36, 40},
456 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 42, 44},
457 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 45, 47}};
458 checkMixedFormattedValue(
459 message,
460 udtitvfmt_resultAsValue(fdi, &ec),
461 expectedString,
462 expectedFieldPositions,
463 UPRV_LENGTHOF(expectedFieldPositions));
464 }
465 {
466 const char* message = "Field position test 2";
467 const UChar* expectedString = u"27. September 2010, 15:00–22:00 Uhr";
468 udtitvfmt_formatCalendarToResult(fmt, ucal1, ucal3, fdi, &ec);
469 assertSuccess("Formatting", &ec);
470 static const UFieldPositionWithCategory expectedFieldPositions[] = {
471 // category, field, begin index, end index
472 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 0, 2},
473 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 4, 13},
474 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 14, 18},
475 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 0, 20, 25},
476 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 20, 22},
477 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 23, 25},
478 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 1, 26, 31},
479 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 26, 28},
480 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 29, 31},
481 {UFIELD_CATEGORY_DATE, UDAT_AM_PM_FIELD, 32, 35}};
482 checkMixedFormattedValue(
483 message,
484 udtitvfmt_resultAsValue(fdi, &ec),
485 expectedString,
486 expectedFieldPositions,
487 UPRV_LENGTHOF(expectedFieldPositions));
488 }
489 {
490 const char* message = "Field position test 3";
491 // Date across Julian Gregorian change date.
492 ucal_setMillis(ucal4, Date158210140000, &ec);
493 ucal_setMillis(ucal5, Date158210160000, &ec);
494 // 1 2 3 4
495 // 012345678901234567890123456789012345678901234567890
496 const UChar* expectedString = u"4. Oktober 1582, 00:00 – 16. Oktober 1582, 00:00";
497 udtitvfmt_formatCalendarToResult(fmt, ucal4, ucal5, fdi, &ec);
498 assertSuccess("Formatting", &ec);
499 static const UFieldPositionWithCategory expectedFieldPositions[] = {
500 // category, field, begin index, end index
501 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 0, 0, 22},
502 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 0, 1},
503 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 3, 10},
504 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 11, 15},
505 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 17, 19},
506 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 20, 22},
507 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 1, 25, 48},
508 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 25, 27},
509 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 29, 36},
510 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 37, 41},
511 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 43, 45},
512 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 46, 48}};
513 checkMixedFormattedValue(
514 message,
515 udtitvfmt_resultAsValue(fdi, &ec),
516 expectedString,
517 expectedFieldPositions,
518 UPRV_LENGTHOF(expectedFieldPositions));
519 }
520 {
521 // Date across Julian Gregorian change date.
522 // We set the Gregorian Change way back.
523 ucal_setGregorianChange(ucal5, (UDate)(-8.64e15), &ec);
524 ucal_setGregorianChange(ucal4, (UDate)(-8.64e15), &ec);
525 ucal_setMillis(ucal4, Date158210140000, &ec);
526 ucal_setMillis(ucal5, Date158210160000, &ec);
527 const char* message = "Field position test 4";
528 // 1 2 3 4
529 // 012345678901234567890123456789012345678901234567890
530 const UChar* expectedString = u"14. Oktober 1582, 00:00 – 16. Oktober 1582, 00:00";
531 udtitvfmt_formatCalendarToResult(fmt, ucal4, ucal5, fdi, &ec);
532 assertSuccess("Formatting", &ec);
533 static const UFieldPositionWithCategory expectedFieldPositions[] = {
534 // category, field, begin index, end index
535 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 0, 0, 23},
536 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 0, 2},
537 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 4, 11},
538 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 12, 16},
539 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 18, 20},
540 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 21, 23},
541 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 1, 26, 49},
542 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 26, 28},
543 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 30, 37},
544 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 38, 42},
545 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 44, 46},
546 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 47, 49}};
547 checkMixedFormattedValue(
548 message,
549 udtitvfmt_resultAsValue(fdi, &ec),
550 expectedString,
551 expectedFieldPositions,
552 UPRV_LENGTHOF(expectedFieldPositions));
553 }
554
555 ucal_close(ucal1);
556 ucal_close(ucal2);
557 ucal_close(ucal3);
558 ucal_close(ucal4);
559 ucal_close(ucal5);
560 udtitvfmt_close(fmt);
561 udtitvfmt_closeResult(fdi);
562 }
563
564 #endif /* #if !UCONFIG_NO_FORMATTING */
565