]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
46f4442e | 3 | /******************************************************************************* |
2ca993e8 | 4 | * Copyright (C) 2008-2016, International Business Machines Corporation and |
46f4442e A |
5 | * others. All Rights Reserved. |
6 | ******************************************************************************* | |
7 | * | |
f3c0d7a5 | 8 | * File DTITVFMT.CPP |
46f4442e A |
9 | * |
10 | ******************************************************************************* | |
11 | */ | |
12 | ||
51004dcb | 13 | #include "utypeinfo.h" // for 'typeid' to work |
729e4ab9 | 14 | |
46f4442e A |
15 | #include "unicode/dtitvfmt.h" |
16 | ||
17 | #if !UCONFIG_NO_FORMATTING | |
18 | ||
729e4ab9 | 19 | //TODO: put in compilation |
46f4442e A |
20 | //#define DTITVFMT_DEBUG 1 |
21 | ||
2ca993e8 | 22 | #include "unicode/calendar.h" |
46f4442e A |
23 | #include "unicode/dtptngen.h" |
24 | #include "unicode/dtitvinf.h" | |
4388f060 | 25 | #include "unicode/udateintervalformat.h" |
2ca993e8 A |
26 | #include "unicode/simpleformatter.h" |
27 | #include "cmemory.h" | |
28 | #include "cstring.h" | |
46f4442e | 29 | #include "dtitv_impl.h" |
2ca993e8 | 30 | #include "mutex.h" |
f3c0d7a5 | 31 | #include "uresimp.h" |
46f4442e | 32 | |
f3c0d7a5 | 33 | #ifdef DTITVFMT_DEBUG |
46f4442e | 34 | #include <iostream> |
46f4442e A |
35 | #endif |
36 | ||
46f4442e A |
37 | U_NAMESPACE_BEGIN |
38 | ||
39 | ||
40 | ||
f3c0d7a5 | 41 | #ifdef DTITVFMT_DEBUG |
46f4442e A |
42 | #define PRINTMESG(msg) { std::cout << "(" << __FILE__ << ":" << __LINE__ << ") " << msg << "\n"; } |
43 | #endif | |
44 | ||
45 | ||
46 | static const UChar gDateFormatSkeleton[][11] = { | |
47 | //yMMMMEEEEd | |
48 | {LOW_Y, CAP_M, CAP_M, CAP_M, CAP_M, CAP_E, CAP_E, CAP_E, CAP_E, LOW_D, 0}, | |
49 | //yMMMMd | |
50 | {LOW_Y, CAP_M, CAP_M, CAP_M, CAP_M, LOW_D, 0}, | |
51 | //yMMMd | |
52 | {LOW_Y, CAP_M, CAP_M, CAP_M, LOW_D, 0}, | |
53 | //yMd | |
54 | {LOW_Y, CAP_M, LOW_D, 0} }; | |
55 | ||
56 | ||
f3c0d7a5 A |
57 | static const char gCalendarTag[] = "calendar"; |
58 | static const char gGregorianTag[] = "gregorian"; | |
59 | static const char gDateTimePatternsTag[] = "DateTimePatterns"; | |
46f4442e A |
60 | |
61 | ||
62 | // latestFirst: | |
729e4ab9 | 63 | static const UChar gLaterFirstPrefix[] = {LOW_L, LOW_A, LOW_T, LOW_E, LOW_S,LOW_T, CAP_F, LOW_I, LOW_R, LOW_S, LOW_T, COLON}; |
46f4442e A |
64 | |
65 | // earliestFirst: | |
729e4ab9 | 66 | static const UChar gEarlierFirstPrefix[] = {LOW_E, LOW_A, LOW_R, LOW_L, LOW_I, LOW_E, LOW_S, LOW_T, CAP_F, LOW_I, LOW_R, LOW_S, LOW_T, COLON}; |
46f4442e A |
67 | |
68 | ||
69 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateIntervalFormat) | |
70 | ||
f3c0d7a5 | 71 | // Mutex, protects access to fDateFormat, fFromCalendar and fToCalendar. |
2ca993e8 | 72 | // Needed because these data members are modified by const methods of DateIntervalFormat. |
46f4442e | 73 | |
2ca993e8 | 74 | static UMutex gFormatterMutex = U_MUTEX_INITIALIZER; |
46f4442e A |
75 | |
76 | DateIntervalFormat* U_EXPORT2 | |
f3c0d7a5 | 77 | DateIntervalFormat::createInstance(const UnicodeString& skeleton, |
46f4442e A |
78 | UErrorCode& status) { |
79 | return createInstance(skeleton, Locale::getDefault(), status); | |
80 | } | |
81 | ||
82 | ||
83 | DateIntervalFormat* U_EXPORT2 | |
f3c0d7a5 A |
84 | DateIntervalFormat::createInstance(const UnicodeString& skeleton, |
85 | const Locale& locale, | |
46f4442e | 86 | UErrorCode& status) { |
46f4442e A |
87 | #ifdef DTITVFMT_DEBUG |
88 | char result[1000]; | |
89 | char result_1[1000]; | |
90 | char mesg[2000]; | |
91 | skeleton.extract(0, skeleton.length(), result, "UTF-8"); | |
92 | UnicodeString pat; | |
93 | ((SimpleDateFormat*)dtfmt)->toPattern(pat); | |
94 | pat.extract(0, pat.length(), result_1, "UTF-8"); | |
95 | sprintf(mesg, "skeleton: %s; pattern: %s\n", result, result_1); | |
96 | PRINTMESG(mesg) | |
97 | #endif | |
98 | ||
99 | DateIntervalInfo* dtitvinf = new DateIntervalInfo(locale, status); | |
729e4ab9 | 100 | return create(locale, dtitvinf, &skeleton, status); |
46f4442e A |
101 | } |
102 | ||
103 | ||
104 | ||
105 | DateIntervalFormat* U_EXPORT2 | |
106 | DateIntervalFormat::createInstance(const UnicodeString& skeleton, | |
107 | const DateIntervalInfo& dtitvinf, | |
108 | UErrorCode& status) { | |
109 | return createInstance(skeleton, Locale::getDefault(), dtitvinf, status); | |
110 | } | |
111 | ||
112 | ||
113 | DateIntervalFormat* U_EXPORT2 | |
114 | DateIntervalFormat::createInstance(const UnicodeString& skeleton, | |
115 | const Locale& locale, | |
116 | const DateIntervalInfo& dtitvinf, | |
117 | UErrorCode& status) { | |
46f4442e | 118 | DateIntervalInfo* ptn = dtitvinf.clone(); |
729e4ab9 | 119 | return create(locale, ptn, &skeleton, status); |
46f4442e A |
120 | } |
121 | ||
122 | ||
123 | DateIntervalFormat::DateIntervalFormat() | |
124 | : fInfo(NULL), | |
125 | fDateFormat(NULL), | |
126 | fFromCalendar(NULL), | |
729e4ab9 | 127 | fToCalendar(NULL), |
2ca993e8 | 128 | fLocale(Locale::getRoot()), |
b331163b A |
129 | fDatePattern(NULL), |
130 | fTimePattern(NULL), | |
131 | fDateTimeFormat(NULL), | |
4388f060 | 132 | fMinimizeType(UDTITVFMT_MINIMIZE_NONE) |
46f4442e A |
133 | {} |
134 | ||
135 | ||
136 | DateIntervalFormat::DateIntervalFormat(const DateIntervalFormat& itvfmt) | |
137 | : Format(itvfmt), | |
138 | fInfo(NULL), | |
139 | fDateFormat(NULL), | |
140 | fFromCalendar(NULL), | |
729e4ab9 | 141 | fToCalendar(NULL), |
2ca993e8 | 142 | fLocale(itvfmt.fLocale), |
b331163b A |
143 | fDatePattern(NULL), |
144 | fTimePattern(NULL), | |
145 | fDateTimeFormat(NULL), | |
4388f060 | 146 | fMinimizeType(UDTITVFMT_MINIMIZE_NONE) { |
46f4442e A |
147 | *this = itvfmt; |
148 | } | |
149 | ||
150 | ||
151 | DateIntervalFormat& | |
152 | DateIntervalFormat::operator=(const DateIntervalFormat& itvfmt) { | |
153 | if ( this != &itvfmt ) { | |
154 | delete fDateFormat; | |
155 | delete fInfo; | |
156 | delete fFromCalendar; | |
157 | delete fToCalendar; | |
b331163b A |
158 | delete fDatePattern; |
159 | delete fTimePattern; | |
160 | delete fDateTimeFormat; | |
2ca993e8 A |
161 | { |
162 | Mutex lock(&gFormatterMutex); | |
163 | if ( itvfmt.fDateFormat ) { | |
164 | fDateFormat = (SimpleDateFormat*)itvfmt.fDateFormat->clone(); | |
165 | } else { | |
166 | fDateFormat = NULL; | |
167 | } | |
168 | if ( itvfmt.fFromCalendar ) { | |
169 | fFromCalendar = itvfmt.fFromCalendar->clone(); | |
170 | } else { | |
171 | fFromCalendar = NULL; | |
172 | } | |
173 | if ( itvfmt.fToCalendar ) { | |
174 | fToCalendar = itvfmt.fToCalendar->clone(); | |
175 | } else { | |
176 | fToCalendar = NULL; | |
177 | } | |
46f4442e A |
178 | } |
179 | if ( itvfmt.fInfo ) { | |
180 | fInfo = itvfmt.fInfo->clone(); | |
181 | } else { | |
182 | fInfo = NULL; | |
183 | } | |
46f4442e A |
184 | fSkeleton = itvfmt.fSkeleton; |
185 | int8_t i; | |
186 | for ( i = 0; i< DateIntervalInfo::kIPI_MAX_INDEX; ++i ) { | |
187 | fIntervalPatterns[i] = itvfmt.fIntervalPatterns[i]; | |
188 | } | |
2ca993e8 | 189 | fLocale = itvfmt.fLocale; |
b331163b A |
190 | fDatePattern = (itvfmt.fDatePattern)? (UnicodeString*)itvfmt.fDatePattern->clone(): NULL; |
191 | fTimePattern = (itvfmt.fTimePattern)? (UnicodeString*)itvfmt.fTimePattern->clone(): NULL; | |
192 | fDateTimeFormat = (itvfmt.fDateTimeFormat)? (UnicodeString*)itvfmt.fDateTimeFormat->clone(): NULL; | |
46f4442e A |
193 | } |
194 | return *this; | |
195 | } | |
196 | ||
197 | ||
198 | DateIntervalFormat::~DateIntervalFormat() { | |
199 | delete fInfo; | |
200 | delete fDateFormat; | |
201 | delete fFromCalendar; | |
202 | delete fToCalendar; | |
b331163b A |
203 | delete fDatePattern; |
204 | delete fTimePattern; | |
205 | delete fDateTimeFormat; | |
46f4442e A |
206 | } |
207 | ||
208 | ||
209 | Format* | |
210 | DateIntervalFormat::clone(void) const { | |
211 | return new DateIntervalFormat(*this); | |
212 | } | |
213 | ||
214 | ||
215 | UBool | |
216 | DateIntervalFormat::operator==(const Format& other) const { | |
2ca993e8 A |
217 | if (typeid(*this) != typeid(other)) {return FALSE;} |
218 | const DateIntervalFormat* fmt = (DateIntervalFormat*)&other; | |
219 | if (this == fmt) {return TRUE;} | |
220 | if (!Format::operator==(other)) {return FALSE;} | |
221 | if ((fInfo != fmt->fInfo) && (fInfo == NULL || fmt->fInfo == NULL)) {return FALSE;} | |
222 | if (fInfo && fmt->fInfo && (*fInfo != *fmt->fInfo )) {return FALSE;} | |
223 | { | |
224 | Mutex lock(&gFormatterMutex); | |
225 | if (fDateFormat != fmt->fDateFormat && (fDateFormat == NULL || fmt->fDateFormat == NULL)) {return FALSE;} | |
226 | if (fDateFormat && fmt->fDateFormat && (*fDateFormat != *fmt->fDateFormat)) {return FALSE;} | |
2ca993e8 | 227 | } |
f3c0d7a5 A |
228 | // note: fFromCalendar and fToCalendar hold no persistent state, and therefore do not participate in operator ==. |
229 | // fDateFormat has the master calendar for the DateIntervalFormat. | |
2ca993e8 A |
230 | if (fSkeleton != fmt->fSkeleton) {return FALSE;} |
231 | if (fDatePattern != fmt->fDatePattern && (fDatePattern == NULL || fmt->fDatePattern == NULL)) {return FALSE;} | |
232 | if (fDatePattern && fmt->fDatePattern && (*fDatePattern != *fmt->fDatePattern)) {return FALSE;} | |
233 | if (fTimePattern != fmt->fTimePattern && (fTimePattern == NULL || fmt->fTimePattern == NULL)) {return FALSE;} | |
234 | if (fTimePattern && fmt->fTimePattern && (*fTimePattern != *fmt->fTimePattern)) {return FALSE;} | |
235 | if (fDateTimeFormat != fmt->fDateTimeFormat && (fDateTimeFormat == NULL || fmt->fDateTimeFormat == NULL)) {return FALSE;} | |
236 | if (fDateTimeFormat && fmt->fDateTimeFormat && (*fDateTimeFormat != *fmt->fDateTimeFormat)) {return FALSE;} | |
237 | if (fLocale != fmt->fLocale) {return FALSE;} | |
238 | ||
239 | for (int32_t i = 0; i< DateIntervalInfo::kIPI_MAX_INDEX; ++i ) { | |
240 | if (fIntervalPatterns[i].firstPart != fmt->fIntervalPatterns[i].firstPart) {return FALSE;} | |
241 | if (fIntervalPatterns[i].secondPart != fmt->fIntervalPatterns[i].secondPart ) {return FALSE;} | |
242 | if (fIntervalPatterns[i].laterDateFirst != fmt->fIntervalPatterns[i].laterDateFirst) {return FALSE;} | |
243 | } | |
244 | return TRUE; | |
46f4442e A |
245 | } |
246 | ||
247 | ||
46f4442e A |
248 | UnicodeString& |
249 | DateIntervalFormat::format(const Formattable& obj, | |
250 | UnicodeString& appendTo, | |
251 | FieldPosition& fieldPosition, | |
252 | UErrorCode& status) const { | |
253 | if ( U_FAILURE(status) ) { | |
254 | return appendTo; | |
255 | } | |
256 | ||
257 | if ( obj.getType() == Formattable::kObject ) { | |
258 | const UObject* formatObj = obj.getObject(); | |
729e4ab9 | 259 | const DateInterval* interval = dynamic_cast<const DateInterval*>(formatObj); |
2ca993e8 | 260 | if (interval != NULL) { |
729e4ab9 | 261 | return format(interval, appendTo, fieldPosition, status); |
46f4442e A |
262 | } |
263 | } | |
264 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
265 | return appendTo; | |
266 | } | |
267 | ||
268 | ||
269 | UnicodeString& | |
270 | DateIntervalFormat::format(const DateInterval* dtInterval, | |
271 | UnicodeString& appendTo, | |
272 | FieldPosition& fieldPosition, | |
273 | UErrorCode& status) const { | |
274 | if ( U_FAILURE(status) ) { | |
275 | return appendTo; | |
276 | } | |
2ca993e8 A |
277 | if (fFromCalendar == NULL || fToCalendar == NULL || fDateFormat == NULL || fInfo == NULL) { |
278 | status = U_INVALID_STATE_ERROR; | |
279 | return appendTo; | |
46f4442e | 280 | } |
2ca993e8 A |
281 | |
282 | Mutex lock(&gFormatterMutex); | |
283 | fFromCalendar->setTime(dtInterval->getFromDate(), status); | |
284 | fToCalendar->setTime(dtInterval->getToDate(), status); | |
285 | return formatImpl(*fFromCalendar, *fToCalendar, appendTo,fieldPosition, status); | |
46f4442e A |
286 | } |
287 | ||
288 | ||
289 | UnicodeString& | |
290 | DateIntervalFormat::format(Calendar& fromCalendar, | |
291 | Calendar& toCalendar, | |
292 | UnicodeString& appendTo, | |
293 | FieldPosition& pos, | |
294 | UErrorCode& status) const { | |
2ca993e8 A |
295 | Mutex lock(&gFormatterMutex); |
296 | return formatImpl(fromCalendar, toCalendar, appendTo, pos, status); | |
297 | } | |
f3c0d7a5 | 298 | |
2ca993e8 A |
299 | |
300 | UnicodeString& | |
301 | DateIntervalFormat::formatImpl(Calendar& fromCalendar, | |
302 | Calendar& toCalendar, | |
303 | UnicodeString& appendTo, | |
304 | FieldPosition& pos, | |
305 | UErrorCode& status) const { | |
46f4442e A |
306 | if ( U_FAILURE(status) ) { |
307 | return appendTo; | |
308 | } | |
309 | ||
310 | // not support different calendar types and time zones | |
311 | //if ( fromCalendar.getType() != toCalendar.getType() ) { | |
729e4ab9 | 312 | if ( !fromCalendar.isEquivalentTo(toCalendar) ) { |
46f4442e A |
313 | status = U_ILLEGAL_ARGUMENT_ERROR; |
314 | return appendTo; | |
315 | } | |
316 | ||
317 | // First, find the largest different calendar field. | |
318 | UCalendarDateFields field = UCAL_FIELD_COUNT; | |
a62d09fc A |
319 | UChar patternDay = 0x0064; // d |
320 | UChar patternYear = 0x0079; // y | |
46f4442e A |
321 | |
322 | if ( fromCalendar.get(UCAL_ERA,status) != toCalendar.get(UCAL_ERA,status)) { | |
323 | field = UCAL_ERA; | |
f3c0d7a5 | 324 | } else if ( fromCalendar.get(UCAL_YEAR, status) != |
46f4442e A |
325 | toCalendar.get(UCAL_YEAR, status) ) { |
326 | field = UCAL_YEAR; | |
a62d09fc A |
327 | if (fMinimizeType == UDTITVFMT_MINIMIZE_ADJACENT_MONTHS && fSkeleton.indexOf(patternDay) >= 0 && fSkeleton.indexOf(patternYear) < 0) { |
328 | UDate fromDate = fromCalendar.getTime(status); | |
329 | UDate toDate = toCalendar.getTime(status); | |
330 | int32_t fromDay = fromCalendar.get(UCAL_DATE, status); | |
331 | int32_t toDay = toCalendar.get(UCAL_DATE, status); | |
332 | fromCalendar.add(UCAL_MONTH, 1, status); | |
333 | if ( fromDate < toDate && fromCalendar.getTime(status) > toDate && fromDay > toDay ) { | |
334 | field = UCAL_DATE; | |
335 | } | |
336 | fromCalendar.setTime(fromDate, status); | |
337 | } | |
46f4442e A |
338 | } else if ( fromCalendar.get(UCAL_MONTH, status) != |
339 | toCalendar.get(UCAL_MONTH, status) ) { | |
340 | field = UCAL_MONTH; | |
4388f060 A |
341 | if (fMinimizeType == UDTITVFMT_MINIMIZE_ADJACENT_MONTHS && fSkeleton.indexOf(patternDay) >= 0) { |
342 | UDate fromDate = fromCalendar.getTime(status); | |
343 | UDate toDate = toCalendar.getTime(status); | |
344 | int32_t fromDay = fromCalendar.get(UCAL_DATE, status); | |
345 | int32_t toDay = toCalendar.get(UCAL_DATE, status); | |
346 | fromCalendar.add(UCAL_MONTH, 1, status); | |
347 | if ( fromDate < toDate && fromCalendar.getTime(status) > toDate && fromDay > toDay ) { | |
348 | field = UCAL_DATE; | |
349 | } | |
350 | fromCalendar.setTime(fromDate, status); | |
351 | } | |
46f4442e A |
352 | } else if ( fromCalendar.get(UCAL_DATE, status) != |
353 | toCalendar.get(UCAL_DATE, status) ) { | |
354 | field = UCAL_DATE; | |
2ca993e8 A |
355 | if (fMinimizeType == UDTITVFMT_MINIMIZE_ADJACENT_DAYS && |
356 | // check normalized skeleton for 'H', 'h', 'j' | |
357 | (fSkeleton.indexOf(0x0048) >= 0 || fSkeleton.indexOf(0x0068) >= 0 || fSkeleton.indexOf(0x006A) >= 0)) { | |
358 | UDate fromDate = fromCalendar.getTime(status); | |
359 | UDate toDate = toCalendar.getTime(status); | |
360 | int32_t fromHour = fromCalendar.get(UCAL_HOUR, status); | |
361 | int32_t toHour = toCalendar.get(UCAL_HOUR, status); | |
362 | fromCalendar.add(UCAL_HOUR_OF_DAY, 12, status); | |
363 | if ( fromDate < toDate && fromCalendar.getTime(status) > toDate && fromHour > toHour ) { | |
364 | field = UCAL_AM_PM; | |
365 | } | |
366 | fromCalendar.setTime(fromDate, status); | |
367 | } | |
46f4442e A |
368 | } else if ( fromCalendar.get(UCAL_AM_PM, status) != |
369 | toCalendar.get(UCAL_AM_PM, status) ) { | |
370 | field = UCAL_AM_PM; | |
371 | } else if ( fromCalendar.get(UCAL_HOUR, status) != | |
372 | toCalendar.get(UCAL_HOUR, status) ) { | |
373 | field = UCAL_HOUR; | |
374 | } else if ( fromCalendar.get(UCAL_MINUTE, status) != | |
375 | toCalendar.get(UCAL_MINUTE, status) ) { | |
376 | field = UCAL_MINUTE; | |
b331163b A |
377 | } else if ( fromCalendar.get(UCAL_SECOND, status) != |
378 | toCalendar.get(UCAL_SECOND, status) ) { | |
379 | field = UCAL_SECOND; | |
46f4442e A |
380 | } |
381 | ||
382 | if ( U_FAILURE(status) ) { | |
383 | return appendTo; | |
384 | } | |
385 | if ( field == UCAL_FIELD_COUNT ) { | |
b331163b | 386 | /* ignore the millisecond etc. small fields' difference. |
46f4442e A |
387 | * use single date when all the above are the same. |
388 | */ | |
389 | return fDateFormat->format(fromCalendar, appendTo, pos); | |
390 | } | |
b331163b | 391 | UBool fromToOnSameDay = (field==UCAL_AM_PM || field==UCAL_HOUR || field==UCAL_MINUTE || field==UCAL_SECOND); |
f3c0d7a5 | 392 | |
46f4442e A |
393 | // following call should not set wrong status, |
394 | // all the pass-in fields are valid till here | |
395 | int32_t itvPtnIndex = DateIntervalInfo::calendarFieldToIntervalIndex(field, | |
396 | status); | |
397 | const PatternInfo& intervalPattern = fIntervalPatterns[itvPtnIndex]; | |
398 | ||
399 | if ( intervalPattern.firstPart.isEmpty() && | |
400 | intervalPattern.secondPart.isEmpty() ) { | |
401 | if ( fDateFormat->isFieldUnitIgnored(field) ) { | |
402 | /* the largest different calendar field is small than | |
403 | * the smallest calendar field in pattern, | |
404 | * return single date format. | |
405 | */ | |
406 | return fDateFormat->format(fromCalendar, appendTo, pos); | |
407 | } | |
b331163b | 408 | return fallbackFormat(fromCalendar, toCalendar, fromToOnSameDay, appendTo, pos, status); |
46f4442e | 409 | } |
f3c0d7a5 | 410 | // If the first part in interval pattern is empty, |
46f4442e A |
411 | // the 2nd part of it saves the full-pattern used in fall-back. |
412 | // For a 'real' interval pattern, the first part will never be empty. | |
413 | if ( intervalPattern.firstPart.isEmpty() ) { | |
414 | // fall back | |
415 | UnicodeString originalPattern; | |
416 | fDateFormat->toPattern(originalPattern); | |
417 | fDateFormat->applyPattern(intervalPattern.secondPart); | |
b331163b | 418 | appendTo = fallbackFormat(fromCalendar, toCalendar, fromToOnSameDay, appendTo, pos, status); |
46f4442e A |
419 | fDateFormat->applyPattern(originalPattern); |
420 | return appendTo; | |
421 | } | |
422 | Calendar* firstCal; | |
423 | Calendar* secondCal; | |
424 | if ( intervalPattern.laterDateFirst ) { | |
425 | firstCal = &toCalendar; | |
426 | secondCal = &fromCalendar; | |
427 | } else { | |
428 | firstCal = &fromCalendar; | |
429 | secondCal = &toCalendar; | |
430 | } | |
431 | // break the interval pattern into 2 parts, | |
f3c0d7a5 | 432 | // first part should not be empty, |
46f4442e A |
433 | UnicodeString originalPattern; |
434 | fDateFormat->toPattern(originalPattern); | |
435 | fDateFormat->applyPattern(intervalPattern.firstPart); | |
436 | fDateFormat->format(*firstCal, appendTo, pos); | |
437 | if ( !intervalPattern.secondPart.isEmpty() ) { | |
438 | fDateFormat->applyPattern(intervalPattern.secondPart); | |
b331163b A |
439 | FieldPosition otherPos; |
440 | otherPos.setField(pos.getField()); | |
441 | fDateFormat->format(*secondCal, appendTo, otherPos); | |
442 | if (pos.getEndIndex() == 0 && otherPos.getEndIndex() > 0) { | |
443 | pos = otherPos; | |
444 | } | |
46f4442e A |
445 | } |
446 | fDateFormat->applyPattern(originalPattern); | |
447 | return appendTo; | |
448 | } | |
449 | ||
450 | ||
451 | ||
452 | void | |
f3c0d7a5 | 453 | DateIntervalFormat::parseObject(const UnicodeString& /* source */, |
46f4442e A |
454 | Formattable& /* result */, |
455 | ParsePosition& /* parse_pos */) const { | |
456 | // parseObject(const UnicodeString&, Formattable&, UErrorCode&) const | |
f3c0d7a5 | 457 | // will set status as U_INVALID_FORMAT_ERROR if |
46f4442e A |
458 | // parse_pos is still 0 |
459 | } | |
460 | ||
461 | ||
462 | ||
463 | ||
464 | const DateIntervalInfo* | |
465 | DateIntervalFormat::getDateIntervalInfo() const { | |
466 | return fInfo; | |
467 | } | |
468 | ||
469 | ||
470 | void | |
471 | DateIntervalFormat::setDateIntervalInfo(const DateIntervalInfo& newItvPattern, | |
472 | UErrorCode& status) { | |
473 | delete fInfo; | |
474 | fInfo = new DateIntervalInfo(newItvPattern); | |
2ca993e8 A |
475 | |
476 | // Delete patterns that get reset by initializePattern | |
477 | delete fDatePattern; | |
478 | fDatePattern = NULL; | |
479 | delete fTimePattern; | |
480 | fTimePattern = NULL; | |
481 | delete fDateTimeFormat; | |
482 | fDateTimeFormat = NULL; | |
483 | ||
484 | if (fDateFormat) { | |
46f4442e A |
485 | initializePattern(status); |
486 | } | |
487 | } | |
488 | ||
489 | ||
f3c0d7a5 | 490 | |
46f4442e A |
491 | const DateFormat* |
492 | DateIntervalFormat::getDateFormat() const { | |
493 | return fDateFormat; | |
494 | } | |
495 | ||
496 | ||
4388f060 A |
497 | void |
498 | DateIntervalFormat::adoptTimeZone(TimeZone* zone) | |
499 | { | |
500 | if (fDateFormat != NULL) { | |
501 | fDateFormat->adoptTimeZone(zone); | |
502 | } | |
503 | // The fDateFormat has the master calendar for the DateIntervalFormat and has | |
504 | // ownership of any adopted TimeZone; fFromCalendar and fToCalendar are internal | |
505 | // work clones of that calendar (and should not also be given ownership of the | |
506 | // adopted TimeZone). | |
507 | if (fFromCalendar) { | |
2ca993e8 | 508 | fFromCalendar->setTimeZone(*zone); |
4388f060 A |
509 | } |
510 | if (fToCalendar) { | |
2ca993e8 | 511 | fToCalendar->setTimeZone(*zone); |
4388f060 A |
512 | } |
513 | } | |
514 | ||
515 | void | |
516 | DateIntervalFormat::setTimeZone(const TimeZone& zone) | |
517 | { | |
518 | if (fDateFormat != NULL) { | |
519 | fDateFormat->setTimeZone(zone); | |
520 | } | |
521 | // The fDateFormat has the master calendar for the DateIntervalFormat; | |
522 | // fFromCalendar and fToCalendar are internal work clones of that calendar. | |
523 | if (fFromCalendar) { | |
2ca993e8 | 524 | fFromCalendar->setTimeZone(zone); |
4388f060 A |
525 | } |
526 | if (fToCalendar) { | |
2ca993e8 | 527 | fToCalendar->setTimeZone(zone); |
4388f060 A |
528 | } |
529 | } | |
530 | ||
531 | const TimeZone& | |
532 | DateIntervalFormat::getTimeZone() const | |
533 | { | |
534 | if (fDateFormat != NULL) { | |
2ca993e8 | 535 | Mutex lock(&gFormatterMutex); |
4388f060 A |
536 | return fDateFormat->getTimeZone(); |
537 | } | |
538 | // If fDateFormat is NULL (unexpected), create default timezone. | |
539 | return *(TimeZone::createDefault()); | |
540 | } | |
541 | ||
542 | void | |
543 | DateIntervalFormat::setAttribute(UDateIntervalFormatAttribute attr, | |
544 | UDateIntervalFormatAttributeValue value, | |
545 | UErrorCode &status) | |
546 | { | |
547 | if ( U_FAILURE(status) ) { | |
548 | return; | |
549 | } | |
550 | if (attr == UDTITVFMT_MINIMIZE_TYPE) { | |
551 | fMinimizeType = value; | |
552 | } else { | |
553 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
554 | } | |
555 | } | |
556 | ||
729e4ab9 | 557 | DateIntervalFormat::DateIntervalFormat(const Locale& locale, |
46f4442e A |
558 | DateIntervalInfo* dtItvInfo, |
559 | const UnicodeString* skeleton, | |
f3c0d7a5 | 560 | UErrorCode& status) |
729e4ab9 A |
561 | : fInfo(NULL), |
562 | fDateFormat(NULL), | |
563 | fFromCalendar(NULL), | |
564 | fToCalendar(NULL), | |
2ca993e8 | 565 | fLocale(locale), |
b331163b A |
566 | fDatePattern(NULL), |
567 | fTimePattern(NULL), | |
568 | fDateTimeFormat(NULL), | |
4388f060 | 569 | fMinimizeType(UDTITVFMT_MINIMIZE_NONE) |
46f4442e | 570 | { |
2ca993e8 A |
571 | LocalPointer<DateIntervalInfo> info(dtItvInfo, status); |
572 | LocalPointer<SimpleDateFormat> dtfmt(static_cast<SimpleDateFormat *>( | |
573 | DateFormat::createInstanceForSkeleton(*skeleton, locale, status)), status); | |
574 | if (U_FAILURE(status)) { | |
46f4442e A |
575 | return; |
576 | } | |
2ca993e8 | 577 | |
46f4442e A |
578 | if ( skeleton ) { |
579 | fSkeleton = *skeleton; | |
580 | } | |
2ca993e8 A |
581 | fInfo = info.orphan(); |
582 | fDateFormat = dtfmt.orphan(); | |
583 | if ( fDateFormat->getCalendar() ) { | |
584 | fFromCalendar = fDateFormat->getCalendar()->clone(); | |
585 | fToCalendar = fDateFormat->getCalendar()->clone(); | |
46f4442e A |
586 | } |
587 | initializePattern(status); | |
588 | } | |
589 | ||
46f4442e | 590 | DateIntervalFormat* U_EXPORT2 |
729e4ab9 | 591 | DateIntervalFormat::create(const Locale& locale, |
46f4442e A |
592 | DateIntervalInfo* dtitvinf, |
593 | const UnicodeString* skeleton, | |
594 | UErrorCode& status) { | |
f3c0d7a5 | 595 | DateIntervalFormat* f = new DateIntervalFormat(locale, dtitvinf, |
46f4442e A |
596 | skeleton, status); |
597 | if ( f == NULL ) { | |
598 | status = U_MEMORY_ALLOCATION_ERROR; | |
46f4442e A |
599 | delete dtitvinf; |
600 | } else if ( U_FAILURE(status) ) { | |
601 | // safe to delete f, although nothing acutally is saved | |
602 | delete f; | |
603 | f = 0; | |
604 | } | |
605 | return f; | |
606 | } | |
607 | ||
608 | ||
609 | ||
f3c0d7a5 | 610 | /** |
46f4442e | 611 | * Initialize interval patterns locale to this formatter |
f3c0d7a5 A |
612 | * |
613 | * This code is a bit complicated since | |
46f4442e A |
614 | * 1. the interval patterns saved in resource bundle files are interval |
615 | * patterns based on date or time only. | |
616 | * It does not have interval patterns based on both date and time. | |
617 | * Interval patterns on both date and time are algorithm generated. | |
618 | * | |
619 | * For example, it has interval patterns on skeleton "dMy" and "hm", | |
620 | * but it does not have interval patterns on skeleton "dMyhm". | |
f3c0d7a5 | 621 | * |
46f4442e | 622 | * The rule to genearte interval patterns for both date and time skeleton are |
f3c0d7a5 A |
623 | * 1) when the year, month, or day differs, concatenate the two original |
624 | * expressions with a separator between, | |
625 | * For example, interval pattern from "Jan 10, 2007 10:10 am" | |
626 | * to "Jan 11, 2007 10:10am" is | |
627 | * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am" | |
46f4442e | 628 | * |
f3c0d7a5 | 629 | * 2) otherwise, present the date followed by the range expression |
46f4442e | 630 | * for the time. |
f3c0d7a5 A |
631 | * For example, interval pattern from "Jan 10, 2007 10:10 am" |
632 | * to "Jan 10, 2007 11:10am" is | |
633 | * "Jan 10, 2007 10:10 am - 11:10am" | |
46f4442e A |
634 | * |
635 | * 2. even a pattern does not request a certion calendar field, | |
636 | * the interval pattern needs to include such field if such fields are | |
637 | * different between 2 dates. | |
f3c0d7a5 | 638 | * For example, a pattern/skeleton is "hm", but the interval pattern |
46f4442e | 639 | * includes year, month, and date when year, month, and date differs. |
f3c0d7a5 | 640 | * |
46f4442e | 641 | * @param status output param set to success/failure code on exit |
f3c0d7a5 | 642 | * @stable ICU 4.0 |
46f4442e | 643 | */ |
f3c0d7a5 | 644 | void |
46f4442e A |
645 | DateIntervalFormat::initializePattern(UErrorCode& status) { |
646 | if ( U_FAILURE(status) ) { | |
647 | return; | |
648 | } | |
649 | const Locale& locale = fDateFormat->getSmpFmtLocale(); | |
46f4442e A |
650 | if ( fSkeleton.isEmpty() ) { |
651 | UnicodeString fullPattern; | |
652 | fDateFormat->toPattern(fullPattern); | |
653 | #ifdef DTITVFMT_DEBUG | |
654 | char result[1000]; | |
655 | char result_1[1000]; | |
656 | char mesg[2000]; | |
657 | fSkeleton.extract(0, fSkeleton.length(), result, "UTF-8"); | |
658 | sprintf(mesg, "in getBestSkeleton: fSkeleton: %s; \n", result); | |
659 | PRINTMESG(mesg) | |
660 | #endif | |
661 | // fSkeleton is already set by createDateIntervalInstance() | |
662 | // or by createInstance(UnicodeString skeleton, .... ) | |
2ca993e8 A |
663 | fSkeleton = DateTimePatternGenerator::staticGetSkeleton( |
664 | fullPattern, status); | |
46f4442e | 665 | if ( U_FAILURE(status) ) { |
f3c0d7a5 | 666 | return; |
46f4442e A |
667 | } |
668 | } | |
669 | ||
670 | // initialize the fIntervalPattern ordering | |
671 | int8_t i; | |
672 | for ( i = 0; i < DateIntervalInfo::kIPI_MAX_INDEX; ++i ) { | |
673 | fIntervalPatterns[i].laterDateFirst = fInfo->getDefaultOrder(); | |
674 | } | |
675 | ||
676 | /* Check whether the skeleton is a combination of date and time. | |
677 | * For the complication reason 1 explained above. | |
678 | */ | |
679 | UnicodeString dateSkeleton; | |
680 | UnicodeString timeSkeleton; | |
681 | UnicodeString normalizedTimeSkeleton; | |
682 | UnicodeString normalizedDateSkeleton; | |
683 | ||
684 | ||
685 | /* the difference between time skeleton and normalizedTimeSkeleton are: | |
729e4ab9 | 686 | * 1. (Formerly, normalized time skeleton folded 'H' to 'h'; no longer true) |
46f4442e | 687 | * 2. 'a' is omitted in normalized time skeleton. |
f3c0d7a5 | 688 | * 3. there is only one appearance for 'h' or 'H', 'm','v', 'z' in normalized |
46f4442e A |
689 | * time skeleton |
690 | * | |
691 | * The difference between date skeleton and normalizedDateSkeleton are: | |
692 | * 1. both 'y' and 'd' appear only once in normalizeDateSkeleton | |
693 | * 2. 'E' and 'EE' are normalized into 'EEE' | |
694 | * 3. 'MM' is normalized into 'M' | |
695 | */ | |
696 | getDateTimeSkeleton(fSkeleton, dateSkeleton, normalizedDateSkeleton, | |
697 | timeSkeleton, normalizedTimeSkeleton); | |
698 | ||
699 | #ifdef DTITVFMT_DEBUG | |
700 | char result[1000]; | |
701 | char result_1[1000]; | |
702 | char mesg[2000]; | |
703 | fSkeleton.extract(0, fSkeleton.length(), result, "UTF-8"); | |
704 | sprintf(mesg, "in getBestSkeleton: fSkeleton: %s; \n", result); | |
705 | PRINTMESG(mesg) | |
706 | #endif | |
707 | ||
b331163b A |
708 | // move this up here since we need it for fallbacks |
709 | if ( timeSkeleton.length() > 0 && dateSkeleton.length() > 0 ) { | |
2ca993e8 A |
710 | // Need the Date/Time pattern for concatenation of the date |
711 | // with the time interval. | |
b331163b A |
712 | // The date/time pattern ( such as {0} {1} ) is saved in |
713 | // calendar, that is why need to get the CalendarData here. | |
f3c0d7a5 A |
714 | LocalUResourceBundlePointer dateTimePatternsRes(ures_open(NULL, locale.getBaseName(), &status)); |
715 | ures_getByKey(dateTimePatternsRes.getAlias(), gCalendarTag, | |
716 | dateTimePatternsRes.getAlias(), &status); | |
717 | ures_getByKeyWithFallback(dateTimePatternsRes.getAlias(), gGregorianTag, | |
718 | dateTimePatternsRes.getAlias(), &status); | |
719 | ures_getByKeyWithFallback(dateTimePatternsRes.getAlias(), gDateTimePatternsTag, | |
720 | dateTimePatternsRes.getAlias(), &status); | |
721 | ||
b331163b A |
722 | int32_t dateTimeFormatLength; |
723 | const UChar* dateTimeFormat = ures_getStringByIndex( | |
f3c0d7a5 | 724 | dateTimePatternsRes.getAlias(), |
b331163b A |
725 | (int32_t)DateFormat::kDateTime, |
726 | &dateTimeFormatLength, &status); | |
727 | if ( U_SUCCESS(status) && dateTimeFormatLength >= 3 ) { | |
728 | fDateTimeFormat = new UnicodeString(dateTimeFormat, dateTimeFormatLength); | |
729 | } | |
b331163b | 730 | } |
46f4442e | 731 | |
f3c0d7a5 | 732 | UBool found = setSeparateDateTimePtn(normalizedDateSkeleton, |
46f4442e A |
733 | normalizedTimeSkeleton); |
734 | ||
b331163b | 735 | // for skeletons with seconds, found is false and we enter this block |
46f4442e A |
736 | if ( found == false ) { |
737 | // use fallback | |
738 | // TODO: if user asks "m"(minute), but "d"(day) differ | |
739 | if ( timeSkeleton.length() != 0 ) { | |
740 | if ( dateSkeleton.length() == 0 ) { | |
741 | // prefix with yMd | |
4388f060 | 742 | timeSkeleton.insert(0, gDateFormatSkeleton[DateFormat::kShort], -1); |
2ca993e8 A |
743 | UnicodeString pattern = DateFormat::getBestPattern( |
744 | locale, timeSkeleton, status); | |
46f4442e | 745 | if ( U_FAILURE(status) ) { |
f3c0d7a5 | 746 | return; |
46f4442e A |
747 | } |
748 | // for fall back interval patterns, | |
749 | // the first part of the pattern is empty, | |
750 | // the second part of the pattern is the full-pattern | |
751 | // should be used in fall-back. | |
f3c0d7a5 A |
752 | setPatternInfo(UCAL_DATE, NULL, &pattern, fInfo->getDefaultOrder()); |
753 | setPatternInfo(UCAL_MONTH, NULL, &pattern, fInfo->getDefaultOrder()); | |
754 | setPatternInfo(UCAL_YEAR, NULL, &pattern, fInfo->getDefaultOrder()); | |
46f4442e A |
755 | } else { |
756 | // TODO: fall back | |
757 | } | |
758 | } else { | |
759 | // TODO: fall back | |
760 | } | |
46f4442e A |
761 | return; |
762 | } // end of skeleton not found | |
f3c0d7a5 | 763 | // interval patterns for skeleton are found in resource |
46f4442e A |
764 | if ( timeSkeleton.length() == 0 ) { |
765 | // done | |
766 | } else if ( dateSkeleton.length() == 0 ) { | |
767 | // prefix with yMd | |
4388f060 | 768 | timeSkeleton.insert(0, gDateFormatSkeleton[DateFormat::kShort], -1); |
2ca993e8 A |
769 | UnicodeString pattern = DateFormat::getBestPattern( |
770 | locale, timeSkeleton, status); | |
46f4442e | 771 | if ( U_FAILURE(status) ) { |
f3c0d7a5 | 772 | return; |
46f4442e A |
773 | } |
774 | // for fall back interval patterns, | |
775 | // the first part of the pattern is empty, | |
776 | // the second part of the pattern is the full-pattern | |
777 | // should be used in fall-back. | |
f3c0d7a5 A |
778 | setPatternInfo(UCAL_DATE, NULL, &pattern, fInfo->getDefaultOrder()); |
779 | setPatternInfo(UCAL_MONTH, NULL, &pattern, fInfo->getDefaultOrder()); | |
780 | setPatternInfo(UCAL_YEAR, NULL, &pattern, fInfo->getDefaultOrder()); | |
46f4442e A |
781 | } else { |
782 | /* if both present, | |
f3c0d7a5 A |
783 | * 1) when the year, month, or day differs, |
784 | * concatenate the two original expressions with a separator between, | |
785 | * 2) otherwise, present the date followed by the | |
786 | * range expression for the time. | |
46f4442e A |
787 | */ |
788 | /* | |
f3c0d7a5 A |
789 | * 1) when the year, month, or day differs, |
790 | * concatenate the two original expressions with a separator between, | |
46f4442e A |
791 | */ |
792 | // if field exists, use fall back | |
793 | UnicodeString skeleton = fSkeleton; | |
794 | if ( !fieldExistsInSkeleton(UCAL_DATE, dateSkeleton) ) { | |
795 | // prefix skeleton with 'd' | |
796 | skeleton.insert(0, LOW_D); | |
729e4ab9 | 797 | setFallbackPattern(UCAL_DATE, skeleton, status); |
46f4442e A |
798 | } |
799 | if ( !fieldExistsInSkeleton(UCAL_MONTH, dateSkeleton) ) { | |
800 | // then prefix skeleton with 'M' | |
801 | skeleton.insert(0, CAP_M); | |
729e4ab9 | 802 | setFallbackPattern(UCAL_MONTH, skeleton, status); |
46f4442e A |
803 | } |
804 | if ( !fieldExistsInSkeleton(UCAL_YEAR, dateSkeleton) ) { | |
805 | // then prefix skeleton with 'y' | |
806 | skeleton.insert(0, LOW_Y); | |
729e4ab9 | 807 | setFallbackPattern(UCAL_YEAR, skeleton, status); |
46f4442e | 808 | } |
f3c0d7a5 | 809 | |
46f4442e | 810 | /* |
f3c0d7a5 A |
811 | * 2) otherwise, present the date followed by the |
812 | * range expression for the time. | |
46f4442e | 813 | */ |
46f4442e | 814 | |
2ca993e8 | 815 | if ( fDateTimeFormat == NULL ) { |
b331163b | 816 | // earlier failure getting dateTimeFormat |
46f4442e A |
817 | return; |
818 | } | |
819 | ||
2ca993e8 A |
820 | UnicodeString datePattern = DateFormat::getBestPattern( |
821 | locale, dateSkeleton, status); | |
46f4442e | 822 | |
b331163b A |
823 | concatSingleDate2TimeInterval(*fDateTimeFormat, datePattern, UCAL_AM_PM, status); |
824 | concatSingleDate2TimeInterval(*fDateTimeFormat, datePattern, UCAL_HOUR, status); | |
825 | concatSingleDate2TimeInterval(*fDateTimeFormat, datePattern, UCAL_MINUTE, status); | |
46f4442e | 826 | } |
46f4442e A |
827 | } |
828 | ||
829 | ||
830 | ||
f3c0d7a5 A |
831 | void U_EXPORT2 |
832 | DateIntervalFormat::getDateTimeSkeleton(const UnicodeString& skeleton, | |
833 | UnicodeString& dateSkeleton, | |
834 | UnicodeString& normalizedDateSkeleton, | |
46f4442e A |
835 | UnicodeString& timeSkeleton, |
836 | UnicodeString& normalizedTimeSkeleton) { | |
837 | // dateSkeleton follows the sequence of y*M*E*d* | |
838 | // timeSkeleton follows the sequence of hm*[v|z]? | |
839 | int32_t ECount = 0; | |
840 | int32_t dCount = 0; | |
841 | int32_t MCount = 0; | |
842 | int32_t yCount = 0; | |
843 | int32_t hCount = 0; | |
729e4ab9 | 844 | int32_t HCount = 0; |
46f4442e A |
845 | int32_t mCount = 0; |
846 | int32_t vCount = 0; | |
847 | int32_t zCount = 0; | |
848 | int32_t i; | |
849 | ||
850 | for (i = 0; i < skeleton.length(); ++i) { | |
851 | UChar ch = skeleton[i]; | |
852 | switch ( ch ) { | |
853 | case CAP_E: | |
854 | dateSkeleton.append(ch); | |
855 | ++ECount; | |
856 | break; | |
857 | case LOW_D: | |
858 | dateSkeleton.append(ch); | |
859 | ++dCount; | |
860 | break; | |
861 | case CAP_M: | |
862 | dateSkeleton.append(ch); | |
863 | ++MCount; | |
864 | break; | |
865 | case LOW_Y: | |
866 | dateSkeleton.append(ch); | |
867 | ++yCount; | |
868 | break; | |
869 | case CAP_G: | |
870 | case CAP_Y: | |
871 | case LOW_U: | |
872 | case CAP_Q: | |
873 | case LOW_Q: | |
874 | case CAP_L: | |
875 | case LOW_L: | |
876 | case CAP_W: | |
877 | case LOW_W: | |
878 | case CAP_D: | |
879 | case CAP_F: | |
880 | case LOW_G: | |
881 | case LOW_E: | |
882 | case LOW_C: | |
57a6839d A |
883 | case CAP_U: |
884 | case LOW_R: | |
46f4442e A |
885 | normalizedDateSkeleton.append(ch); |
886 | dateSkeleton.append(ch); | |
887 | break; | |
888 | case LOW_A: | |
f3c0d7a5 | 889 | // 'a' is implicitly handled |
46f4442e A |
890 | timeSkeleton.append(ch); |
891 | break; | |
892 | case LOW_H: | |
46f4442e A |
893 | timeSkeleton.append(ch); |
894 | ++hCount; | |
895 | break; | |
729e4ab9 A |
896 | case CAP_H: |
897 | timeSkeleton.append(ch); | |
898 | ++HCount; | |
899 | break; | |
46f4442e A |
900 | case LOW_M: |
901 | timeSkeleton.append(ch); | |
902 | ++mCount; | |
903 | break; | |
904 | case LOW_Z: | |
905 | ++zCount; | |
906 | timeSkeleton.append(ch); | |
907 | break; | |
908 | case LOW_V: | |
909 | ++vCount; | |
910 | timeSkeleton.append(ch); | |
911 | break; | |
46f4442e A |
912 | case CAP_V: |
913 | case CAP_Z: | |
914 | case LOW_K: | |
915 | case CAP_K: | |
916 | case LOW_J: | |
917 | case LOW_S: | |
918 | case CAP_S: | |
919 | case CAP_A: | |
920 | timeSkeleton.append(ch); | |
921 | normalizedTimeSkeleton.append(ch); | |
f3c0d7a5 | 922 | break; |
46f4442e A |
923 | } |
924 | } | |
925 | ||
926 | /* generate normalized form for date*/ | |
927 | if ( yCount != 0 ) { | |
57a6839d A |
928 | for (i = 0; i < yCount; ++i) { |
929 | normalizedDateSkeleton.append(LOW_Y); | |
930 | } | |
46f4442e A |
931 | } |
932 | if ( MCount != 0 ) { | |
933 | if ( MCount < 3 ) { | |
934 | normalizedDateSkeleton.append(CAP_M); | |
935 | } else { | |
936 | int32_t i; | |
937 | for ( i = 0; i < MCount && i < MAX_M_COUNT; ++i ) { | |
938 | normalizedDateSkeleton.append(CAP_M); | |
939 | } | |
940 | } | |
941 | } | |
942 | if ( ECount != 0 ) { | |
943 | if ( ECount <= 3 ) { | |
944 | normalizedDateSkeleton.append(CAP_E); | |
945 | } else { | |
946 | int32_t i; | |
947 | for ( i = 0; i < ECount && i < MAX_E_COUNT; ++i ) { | |
948 | normalizedDateSkeleton.append(CAP_E); | |
949 | } | |
950 | } | |
951 | } | |
952 | if ( dCount != 0 ) { | |
953 | normalizedDateSkeleton.append(LOW_D); | |
954 | } | |
955 | ||
956 | /* generate normalized form for time */ | |
729e4ab9 A |
957 | if ( HCount != 0 ) { |
958 | normalizedTimeSkeleton.append(CAP_H); | |
959 | } | |
960 | else if ( hCount != 0 ) { | |
46f4442e A |
961 | normalizedTimeSkeleton.append(LOW_H); |
962 | } | |
963 | if ( mCount != 0 ) { | |
964 | normalizedTimeSkeleton.append(LOW_M); | |
965 | } | |
966 | if ( zCount != 0 ) { | |
967 | normalizedTimeSkeleton.append(LOW_Z); | |
968 | } | |
969 | if ( vCount != 0 ) { | |
970 | normalizedTimeSkeleton.append(LOW_V); | |
971 | } | |
972 | } | |
973 | ||
974 | ||
975 | /** | |
976 | * Generate date or time interval pattern from resource, | |
977 | * and set them into the interval pattern locale to this formatter. | |
978 | * | |
f3c0d7a5 | 979 | * It needs to handle the following: |
46f4442e A |
980 | * 1. need to adjust field width. |
981 | * For example, the interval patterns saved in DateIntervalInfo | |
982 | * includes "dMMMy", but not "dMMMMy". | |
983 | * Need to get interval patterns for dMMMMy from dMMMy. | |
984 | * Another example, the interval patterns saved in DateIntervalInfo | |
985 | * includes "hmv", but not "hmz". | |
986 | * Need to get interval patterns for "hmz' from 'hmv' | |
987 | * | |
988 | * 2. there might be no pattern for 'y' differ for skeleton "Md", | |
989 | * in order to get interval patterns for 'y' differ, | |
990 | * need to look for it from skeleton 'yMd' | |
991 | * | |
992 | * @param dateSkeleton normalized date skeleton | |
993 | * @param timeSkeleton normalized time skeleton | |
994 | * @return whether the resource is found for the skeleton. | |
995 | * TRUE if interval pattern found for the skeleton, | |
996 | * FALSE otherwise. | |
729e4ab9 | 997 | * @stable ICU 4.0 |
46f4442e | 998 | */ |
f3c0d7a5 | 999 | UBool |
46f4442e A |
1000 | DateIntervalFormat::setSeparateDateTimePtn( |
1001 | const UnicodeString& dateSkeleton, | |
1002 | const UnicodeString& timeSkeleton) { | |
1003 | const UnicodeString* skeleton; | |
1004 | // if both date and time skeleton present, | |
1005 | // the final interval pattern might include time interval patterns | |
1006 | // ( when, am_pm, hour, minute differ ), | |
1007 | // but not date interval patterns ( when year, month, day differ ). | |
1008 | // For year/month/day differ, it falls back to fall-back pattern. | |
1009 | if ( timeSkeleton.length() != 0 ) { | |
1010 | skeleton = &timeSkeleton; | |
1011 | } else { | |
1012 | skeleton = &dateSkeleton; | |
1013 | } | |
1014 | ||
f3c0d7a5 | 1015 | /* interval patterns for skeleton "dMMMy" (but not "dMMMMy") |
46f4442e A |
1016 | * are defined in resource, |
1017 | * interval patterns for skeleton "dMMMMy" are calculated by | |
1018 | * 1. get the best match skeleton for "dMMMMy", which is "dMMMy" | |
1019 | * 2. get the interval patterns for "dMMMy", | |
f3c0d7a5 | 1020 | * 3. extend "MMM" to "MMMM" in above interval patterns for "dMMMMy" |
46f4442e A |
1021 | * getBestSkeleton() is step 1. |
1022 | */ | |
1023 | // best skeleton, and the difference information | |
1024 | int8_t differenceInfo = 0; | |
f3c0d7a5 | 1025 | const UnicodeString* bestSkeleton = fInfo->getBestSkeleton(*skeleton, |
46f4442e A |
1026 | differenceInfo); |
1027 | /* best skeleton could be NULL. | |
1028 | For example: in "ca" resource file, | |
1029 | interval format is defined as following | |
1030 | intervalFormats{ | |
1031 | fallback{"{0} - {1}"} | |
1032 | } | |
1033 | there is no skeletons/interval patterns defined, | |
1034 | and the best skeleton match could be NULL | |
1035 | */ | |
1036 | if ( bestSkeleton == NULL ) { | |
f3c0d7a5 A |
1037 | return false; |
1038 | } | |
1039 | ||
b331163b A |
1040 | // Set patterns for fallback use, need to do this |
1041 | // before returning if differenceInfo == -1 | |
1042 | UErrorCode status; | |
2ca993e8 | 1043 | if ( dateSkeleton.length() != 0) { |
b331163b | 1044 | status = U_ZERO_ERROR; |
2ca993e8 A |
1045 | fDatePattern = new UnicodeString(DateFormat::getBestPattern( |
1046 | fLocale, dateSkeleton, status)); | |
b331163b | 1047 | } |
2ca993e8 | 1048 | if ( timeSkeleton.length() != 0) { |
b331163b | 1049 | status = U_ZERO_ERROR; |
2ca993e8 A |
1050 | fTimePattern = new UnicodeString(DateFormat::getBestPattern( |
1051 | fLocale, timeSkeleton, status)); | |
b331163b A |
1052 | } |
1053 | ||
46f4442e A |
1054 | // difference: |
1055 | // 0 means the best matched skeleton is the same as input skeleton | |
1056 | // 1 means the fields are the same, but field width are different | |
1057 | // 2 means the only difference between fields are v/z, | |
f3c0d7a5 | 1058 | // -1 means there are other fields difference |
b331163b A |
1059 | // (this will happen, for instance, if the supplied skeleton has seconds, |
1060 | // but no skeletons in the intervalFormats data do) | |
f3c0d7a5 | 1061 | if ( differenceInfo == -1 ) { |
46f4442e A |
1062 | // skeleton has different fields, not only v/z difference |
1063 | return false; | |
1064 | } | |
1065 | ||
1066 | if ( timeSkeleton.length() == 0 ) { | |
1067 | UnicodeString extendedSkeleton; | |
1068 | UnicodeString extendedBestSkeleton; | |
1069 | // only has date skeleton | |
1070 | setIntervalPattern(UCAL_DATE, skeleton, bestSkeleton, differenceInfo, | |
1071 | &extendedSkeleton, &extendedBestSkeleton); | |
1072 | ||
f3c0d7a5 | 1073 | UBool extended = setIntervalPattern(UCAL_MONTH, skeleton, bestSkeleton, |
46f4442e A |
1074 | differenceInfo, |
1075 | &extendedSkeleton, &extendedBestSkeleton); | |
f3c0d7a5 | 1076 | |
46f4442e A |
1077 | if ( extended ) { |
1078 | bestSkeleton = &extendedBestSkeleton; | |
1079 | skeleton = &extendedSkeleton; | |
1080 | } | |
1081 | setIntervalPattern(UCAL_YEAR, skeleton, bestSkeleton, differenceInfo, | |
1082 | &extendedSkeleton, &extendedBestSkeleton); | |
1546d4af A |
1083 | setIntervalPattern(UCAL_ERA, skeleton, bestSkeleton, differenceInfo, |
1084 | &extendedSkeleton, &extendedBestSkeleton); | |
46f4442e | 1085 | } else { |
1546d4af | 1086 | // what about seconds handling? |
46f4442e A |
1087 | setIntervalPattern(UCAL_MINUTE, skeleton, bestSkeleton, differenceInfo); |
1088 | setIntervalPattern(UCAL_HOUR, skeleton, bestSkeleton, differenceInfo); | |
1089 | setIntervalPattern(UCAL_AM_PM, skeleton, bestSkeleton, differenceInfo); | |
1090 | } | |
1091 | return true; | |
1092 | } | |
1093 | ||
1094 | ||
1095 | ||
1096 | void | |
1097 | DateIntervalFormat::setFallbackPattern(UCalendarDateFields field, | |
1098 | const UnicodeString& skeleton, | |
46f4442e A |
1099 | UErrorCode& status) { |
1100 | if ( U_FAILURE(status) ) { | |
1101 | return; | |
1102 | } | |
2ca993e8 A |
1103 | UnicodeString pattern = DateFormat::getBestPattern( |
1104 | fLocale, skeleton, status); | |
46f4442e A |
1105 | if ( U_FAILURE(status) ) { |
1106 | return; | |
1107 | } | |
1108 | setPatternInfo(field, NULL, &pattern, fInfo->getDefaultOrder()); | |
1109 | } | |
1110 | ||
1111 | ||
1112 | ||
1113 | ||
1114 | void | |
f3c0d7a5 | 1115 | DateIntervalFormat::setPatternInfo(UCalendarDateFields field, |
46f4442e | 1116 | const UnicodeString* firstPart, |
f3c0d7a5 | 1117 | const UnicodeString* secondPart, |
46f4442e A |
1118 | UBool laterDateFirst) { |
1119 | // for fall back interval patterns, | |
1120 | // the first part of the pattern is empty, | |
1121 | // the second part of the pattern is the full-pattern | |
1122 | // should be used in fall-back. | |
1123 | UErrorCode status = U_ZERO_ERROR; | |
1124 | // following should not set any wrong status. | |
1125 | int32_t itvPtnIndex = DateIntervalInfo::calendarFieldToIntervalIndex(field, | |
1126 | status); | |
1127 | if ( U_FAILURE(status) ) { | |
1128 | return; | |
1129 | } | |
1130 | PatternInfo& ptn = fIntervalPatterns[itvPtnIndex]; | |
1131 | if ( firstPart ) { | |
1132 | ptn.firstPart = *firstPart; | |
1133 | } | |
1134 | if ( secondPart ) { | |
1135 | ptn.secondPart = *secondPart; | |
1136 | } | |
1137 | ptn.laterDateFirst = laterDateFirst; | |
1138 | } | |
1139 | ||
1140 | void | |
1141 | DateIntervalFormat::setIntervalPattern(UCalendarDateFields field, | |
1142 | const UnicodeString& intervalPattern) { | |
1143 | UBool order = fInfo->getDefaultOrder(); | |
1144 | setIntervalPattern(field, intervalPattern, order); | |
1145 | } | |
1146 | ||
1147 | ||
1148 | void | |
1149 | DateIntervalFormat::setIntervalPattern(UCalendarDateFields field, | |
1150 | const UnicodeString& intervalPattern, | |
1151 | UBool laterDateFirst) { | |
1152 | const UnicodeString* pattern = &intervalPattern; | |
1153 | UBool order = laterDateFirst; | |
1154 | // check for "latestFirst:" or "earliestFirst:" prefix | |
2ca993e8 A |
1155 | int8_t prefixLength = UPRV_LENGTHOF(gLaterFirstPrefix); |
1156 | int8_t earliestFirstLength = UPRV_LENGTHOF(gEarlierFirstPrefix); | |
46f4442e A |
1157 | UnicodeString realPattern; |
1158 | if ( intervalPattern.startsWith(gLaterFirstPrefix, prefixLength) ) { | |
1159 | order = true; | |
f3c0d7a5 | 1160 | intervalPattern.extract(prefixLength, |
46f4442e A |
1161 | intervalPattern.length() - prefixLength, |
1162 | realPattern); | |
1163 | pattern = &realPattern; | |
1164 | } else if ( intervalPattern.startsWith(gEarlierFirstPrefix, | |
1165 | earliestFirstLength) ) { | |
1166 | order = false; | |
1167 | intervalPattern.extract(earliestFirstLength, | |
1168 | intervalPattern.length() - earliestFirstLength, | |
1169 | realPattern); | |
1170 | pattern = &realPattern; | |
1171 | } | |
1172 | ||
1173 | int32_t splitPoint = splitPatternInto2Part(*pattern); | |
f3c0d7a5 | 1174 | |
46f4442e A |
1175 | UnicodeString firstPart; |
1176 | UnicodeString secondPart; | |
1177 | pattern->extract(0, splitPoint, firstPart); | |
1178 | if ( splitPoint < pattern->length() ) { | |
1179 | pattern->extract(splitPoint, pattern->length()-splitPoint, secondPart); | |
1180 | } | |
1181 | setPatternInfo(field, &firstPart, &secondPart, order); | |
1182 | } | |
1183 | ||
1184 | ||
1185 | ||
1186 | ||
1187 | /** | |
1188 | * Generate interval pattern from existing resource | |
1189 | * | |
1190 | * It not only save the interval patterns, | |
1191 | * but also return the extended skeleton and its best match skeleton. | |
1192 | * | |
1193 | * @param field largest different calendar field | |
1194 | * @param skeleton skeleton | |
1195 | * @param bestSkeleton the best match skeleton which has interval pattern | |
1196 | * defined in resource | |
1197 | * @param differenceInfo the difference between skeleton and best skeleton | |
1198 | * 0 means the best matched skeleton is the same as input skeleton | |
1199 | * 1 means the fields are the same, but field width are different | |
1200 | * 2 means the only difference between fields are v/z, | |
f3c0d7a5 | 1201 | * -1 means there are other fields difference |
46f4442e A |
1202 | * |
1203 | * @param extendedSkeleton extended skeleton | |
1204 | * @param extendedBestSkeleton extended best match skeleton | |
f3c0d7a5 | 1205 | * @return whether the interval pattern is found |
46f4442e A |
1206 | * through extending skeleton or not. |
1207 | * TRUE if interval pattern is found by | |
1208 | * extending skeleton, FALSE otherwise. | |
729e4ab9 | 1209 | * @stable ICU 4.0 |
46f4442e A |
1210 | */ |
1211 | UBool | |
1212 | DateIntervalFormat::setIntervalPattern(UCalendarDateFields field, | |
1213 | const UnicodeString* skeleton, | |
1214 | const UnicodeString* bestSkeleton, | |
1215 | int8_t differenceInfo, | |
1216 | UnicodeString* extendedSkeleton, | |
1217 | UnicodeString* extendedBestSkeleton) { | |
1218 | UErrorCode status = U_ZERO_ERROR; | |
1219 | // following getIntervalPattern() should not generate error status | |
1220 | UnicodeString pattern; | |
1221 | fInfo->getIntervalPattern(*bestSkeleton, field, pattern, status); | |
1222 | if ( pattern.isEmpty() ) { | |
1223 | // single date | |
1224 | if ( SimpleDateFormat::isFieldUnitIgnored(*bestSkeleton, field) ) { | |
1225 | // do nothing, format will handle it | |
1226 | return false; | |
1227 | } | |
1228 | ||
1229 | // for 24 hour system, interval patterns in resource file | |
f3c0d7a5 | 1230 | // might not include pattern when am_pm differ, |
46f4442e A |
1231 | // which should be the same as hour differ. |
1232 | // add it here for simplicity | |
1233 | if ( field == UCAL_AM_PM ) { | |
1234 | fInfo->getIntervalPattern(*bestSkeleton, UCAL_HOUR, pattern,status); | |
1235 | if ( !pattern.isEmpty() ) { | |
1236 | setIntervalPattern(field, pattern); | |
1237 | } | |
1238 | return false; | |
f3c0d7a5 | 1239 | } |
46f4442e A |
1240 | // else, looking for pattern when 'y' differ for 'dMMMM' skeleton, |
1241 | // first, get best match pattern "MMMd", | |
1242 | // since there is no pattern for 'y' differs for skeleton 'MMMd', | |
1243 | // need to look for it from skeleton 'yMMMd', | |
1244 | // if found, adjust field width in interval pattern from | |
1245 | // "MMM" to "MMMM". | |
1246 | UChar fieldLetter = fgCalendarFieldToPatternLetter[field]; | |
1247 | if ( extendedSkeleton ) { | |
1248 | *extendedSkeleton = *skeleton; | |
1249 | *extendedBestSkeleton = *bestSkeleton; | |
1250 | extendedSkeleton->insert(0, fieldLetter); | |
1251 | extendedBestSkeleton->insert(0, fieldLetter); | |
1252 | // for example, looking for patterns when 'y' differ for | |
f3c0d7a5 | 1253 | // skeleton "MMMM". |
46f4442e A |
1254 | fInfo->getIntervalPattern(*extendedBestSkeleton,field,pattern,status); |
1255 | if ( pattern.isEmpty() && differenceInfo == 0 ) { | |
1256 | // if there is no skeleton "yMMMM" defined, | |
f3c0d7a5 | 1257 | // look for the best match skeleton, for example: "yMMM" |
46f4442e A |
1258 | const UnicodeString* tmpBest = fInfo->getBestSkeleton( |
1259 | *extendedBestSkeleton, differenceInfo); | |
1260 | if ( tmpBest != 0 && differenceInfo != -1 ) { | |
1261 | fInfo->getIntervalPattern(*tmpBest, field, pattern, status); | |
1262 | bestSkeleton = tmpBest; | |
1263 | } | |
1264 | } | |
1265 | } | |
f3c0d7a5 | 1266 | } |
46f4442e A |
1267 | if ( !pattern.isEmpty() ) { |
1268 | if ( differenceInfo != 0 ) { | |
1269 | UnicodeString adjustIntervalPattern; | |
1270 | adjustFieldWidth(*skeleton, *bestSkeleton, pattern, differenceInfo, | |
1271 | adjustIntervalPattern); | |
1272 | setIntervalPattern(field, adjustIntervalPattern); | |
1273 | } else { | |
1274 | setIntervalPattern(field, pattern); | |
1275 | } | |
1276 | if ( extendedSkeleton && !extendedSkeleton->isEmpty() ) { | |
1277 | return TRUE; | |
1278 | } | |
1279 | } | |
1280 | return FALSE; | |
1281 | } | |
1282 | ||
1283 | ||
1284 | ||
f3c0d7a5 | 1285 | int32_t U_EXPORT2 |
46f4442e A |
1286 | DateIntervalFormat::splitPatternInto2Part(const UnicodeString& intervalPattern) { |
1287 | UBool inQuote = false; | |
1288 | UChar prevCh = 0; | |
1289 | int32_t count = 0; | |
1290 | ||
1291 | /* repeatedPattern used to record whether a pattern has already seen. | |
1292 | It is a pattern applies to first calendar if it is first time seen, | |
1293 | otherwise, it is a pattern applies to the second calendar | |
1294 | */ | |
f3c0d7a5 | 1295 | UBool patternRepeated[] = |
46f4442e A |
1296 | { |
1297 | // A B C D E F G H I J K L M N O | |
1298 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1299 | // P Q R S T U V W X Y Z | |
1300 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1301 | // a b c d e f g h i j k l m n o | |
1302 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1303 | // p q r s t u v w x y z | |
1304 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
1305 | }; | |
1306 | ||
1307 | int8_t PATTERN_CHAR_BASE = 0x41; | |
f3c0d7a5 | 1308 | |
46f4442e A |
1309 | /* loop through the pattern string character by character looking for |
1310 | * the first repeated pattern letter, which breaks the interval pattern | |
f3c0d7a5 | 1311 | * into 2 parts. |
46f4442e A |
1312 | */ |
1313 | int32_t i; | |
1314 | UBool foundRepetition = false; | |
1315 | for (i = 0; i < intervalPattern.length(); ++i) { | |
1316 | UChar ch = intervalPattern.charAt(i); | |
f3c0d7a5 | 1317 | |
46f4442e A |
1318 | if (ch != prevCh && count > 0) { |
1319 | // check the repeativeness of pattern letter | |
1320 | UBool repeated = patternRepeated[(int)(prevCh - PATTERN_CHAR_BASE)]; | |
1321 | if ( repeated == FALSE ) { | |
1322 | patternRepeated[prevCh - PATTERN_CHAR_BASE] = TRUE; | |
1323 | } else { | |
1324 | foundRepetition = true; | |
1325 | break; | |
1326 | } | |
1327 | count = 0; | |
1328 | } | |
f3c0d7a5 | 1329 | if (ch == 0x0027 /*'*/) { |
46f4442e A |
1330 | // Consecutive single quotes are a single quote literal, |
1331 | // either outside of quotes or between quotes | |
f3c0d7a5 A |
1332 | if ((i+1) < intervalPattern.length() && |
1333 | intervalPattern.charAt(i+1) == 0x0027 /*'*/) { | |
46f4442e A |
1334 | ++i; |
1335 | } else { | |
1336 | inQuote = ! inQuote; | |
1337 | } | |
f3c0d7a5 | 1338 | } |
46f4442e A |
1339 | else if (!inQuote && ((ch >= 0x0061 /*'a'*/ && ch <= 0x007A /*'z'*/) |
1340 | || (ch >= 0x0041 /*'A'*/ && ch <= 0x005A /*'Z'*/))) { | |
f3c0d7a5 | 1341 | // ch is a date-time pattern character |
46f4442e A |
1342 | prevCh = ch; |
1343 | ++count; | |
1344 | } | |
1345 | } | |
1346 | // check last pattern char, distinguish | |
f3c0d7a5 A |
1347 | // "dd MM" ( no repetition ), |
1348 | // "d-d"(last char repeated ), and | |
46f4442e A |
1349 | // "d-d MM" ( repetition found ) |
1350 | if ( count > 0 && foundRepetition == FALSE ) { | |
1351 | if ( patternRepeated[(int)(prevCh - PATTERN_CHAR_BASE)] == FALSE ) { | |
1352 | count = 0; | |
1353 | } | |
1354 | } | |
1355 | return (i - count); | |
1356 | } | |
1357 | ||
b331163b A |
1358 | static const UChar bracketedZero[] = {0x7B,0x30,0x7D}; |
1359 | static const UChar bracketedOne[] = {0x7B,0x31,0x7D}; | |
46f4442e | 1360 | |
b331163b A |
1361 | void |
1362 | DateIntervalFormat::adjustPosition(UnicodeString& combiningPattern, // has {0} and {1} in it | |
1363 | UnicodeString& pat0, FieldPosition& pos0, // pattern and pos corresponding to {0} | |
1364 | UnicodeString& pat1, FieldPosition& pos1, // pattern and pos corresponding to {1} | |
1365 | FieldPosition& posResult) { | |
1366 | int32_t index0 = combiningPattern.indexOf(bracketedZero, 3, 0); | |
1367 | int32_t index1 = combiningPattern.indexOf(bracketedOne, 3, 0); | |
1368 | if (index0 < 0 || index1 < 0) { | |
1369 | return; | |
1370 | } | |
2ca993e8 | 1371 | int32_t placeholderLen = 3; // length of "{0}" or "{1}" |
b331163b A |
1372 | if (index0 < index1) { |
1373 | if (pos0.getEndIndex() > 0) { | |
1374 | posResult.setBeginIndex(pos0.getBeginIndex() + index0); | |
1375 | posResult.setEndIndex(pos0.getEndIndex() + index0); | |
1376 | } else if (pos1.getEndIndex() > 0) { | |
1377 | // here index1 >= 3 | |
2ca993e8 | 1378 | index1 += pat0.length() - placeholderLen; // adjust for pat0 replacing {0} |
b331163b A |
1379 | posResult.setBeginIndex(pos1.getBeginIndex() + index1); |
1380 | posResult.setEndIndex(pos1.getEndIndex() + index1); | |
1381 | } | |
1382 | } else { | |
1383 | if (pos1.getEndIndex() > 0) { | |
1384 | posResult.setBeginIndex(pos1.getBeginIndex() + index1); | |
1385 | posResult.setEndIndex(pos1.getEndIndex() + index1); | |
1386 | } else if (pos0.getEndIndex() > 0) { | |
1387 | // here index0 >= 3 | |
2ca993e8 | 1388 | index0 += pat1.length() - placeholderLen; // adjust for pat1 replacing {1} |
b331163b A |
1389 | posResult.setBeginIndex(pos0.getBeginIndex() + index0); |
1390 | posResult.setEndIndex(pos0.getEndIndex() + index0); | |
1391 | } | |
1392 | } | |
1393 | } | |
46f4442e | 1394 | |
f3c0d7a5 | 1395 | UnicodeString& |
46f4442e A |
1396 | DateIntervalFormat::fallbackFormat(Calendar& fromCalendar, |
1397 | Calendar& toCalendar, | |
b331163b | 1398 | UBool fromToOnSameDay, // new |
46f4442e A |
1399 | UnicodeString& appendTo, |
1400 | FieldPosition& pos, | |
1401 | UErrorCode& status) const { | |
1402 | if ( U_FAILURE(status) ) { | |
1403 | return appendTo; | |
1404 | } | |
b331163b A |
1405 | UnicodeString fullPattern; // for saving the pattern in fDateFormat |
1406 | UBool formatDatePlusTimeRange = (fromToOnSameDay && fDatePattern && fTimePattern); | |
46f4442e | 1407 | // the fall back |
b331163b A |
1408 | if (formatDatePlusTimeRange) { |
1409 | fDateFormat->toPattern(fullPattern); // save current pattern, restore later | |
1410 | fDateFormat->applyPattern(*fTimePattern); | |
1411 | } | |
1412 | FieldPosition otherPos; | |
1413 | otherPos.setField(pos.getField()); | |
2ca993e8 A |
1414 | UnicodeString earlierDate; |
1415 | fDateFormat->format(fromCalendar, earlierDate, pos); | |
1416 | UnicodeString laterDate; | |
1417 | fDateFormat->format(toCalendar, laterDate, otherPos); | |
46f4442e A |
1418 | UnicodeString fallbackPattern; |
1419 | fInfo->getFallbackIntervalPattern(fallbackPattern); | |
2ca993e8 | 1420 | adjustPosition(fallbackPattern, earlierDate, pos, laterDate, otherPos, pos); |
b331163b | 1421 | UnicodeString fallbackRange; |
2ca993e8 A |
1422 | SimpleFormatter(fallbackPattern, 2, 2, status). |
1423 | format(earlierDate, laterDate, fallbackRange, status); | |
1424 | if ( U_SUCCESS(status) && formatDatePlusTimeRange ) { | |
1425 | // fallbackRange has just the time range, need to format the date part and combine that | |
1426 | UnicodeString dateTimeFormatNoQuote(*fDateTimeFormat); | |
1427 | dateTimeFormatNoQuote.findAndReplace(UnicodeString(0x0027), UnicodeString()); | |
1428 | fDateFormat->applyPattern(*fDatePattern); | |
1429 | UnicodeString datePortion; | |
1430 | otherPos.setBeginIndex(0); | |
1431 | otherPos.setEndIndex(0); | |
1432 | fDateFormat->format(fromCalendar, datePortion, otherPos); | |
1433 | adjustPosition(dateTimeFormatNoQuote, fallbackRange, pos, datePortion, otherPos, pos); | |
1434 | const UnicodeString *values[2] = { | |
1435 | &fallbackRange, // {0} is time range | |
1436 | &datePortion, // {1} is single date portion | |
1437 | }; | |
1438 | SimpleFormatter(dateTimeFormatNoQuote, 2, 2, status). | |
1439 | formatAndReplace(values, 2, fallbackRange, NULL, 0, status); | |
1440 | } | |
46f4442e | 1441 | if ( U_SUCCESS(status) ) { |
2ca993e8 | 1442 | appendTo.append(fallbackRange); |
b331163b A |
1443 | } |
1444 | if (formatDatePlusTimeRange) { | |
1445 | // restore full pattern | |
1446 | fDateFormat->applyPattern(fullPattern); | |
46f4442e A |
1447 | } |
1448 | return appendTo; | |
1449 | } | |
1450 | ||
1451 | ||
1452 | ||
1453 | ||
f3c0d7a5 | 1454 | UBool U_EXPORT2 |
46f4442e A |
1455 | DateIntervalFormat::fieldExistsInSkeleton(UCalendarDateFields field, |
1456 | const UnicodeString& skeleton) | |
1457 | { | |
1458 | const UChar fieldChar = fgCalendarFieldToPatternLetter[field]; | |
1459 | return ( (skeleton.indexOf(fieldChar) == -1)?FALSE:TRUE ) ; | |
1460 | } | |
1461 | ||
1462 | ||
1463 | ||
f3c0d7a5 | 1464 | void U_EXPORT2 |
46f4442e A |
1465 | DateIntervalFormat::adjustFieldWidth(const UnicodeString& inputSkeleton, |
1466 | const UnicodeString& bestMatchSkeleton, | |
1467 | const UnicodeString& bestIntervalPattern, | |
1468 | int8_t differenceInfo, | |
1469 | UnicodeString& adjustedPtn) { | |
1470 | adjustedPtn = bestIntervalPattern; | |
f3c0d7a5 | 1471 | int32_t inputSkeletonFieldWidth[] = |
46f4442e A |
1472 | { |
1473 | // A B C D E F G H I J K L M N O | |
1474 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1475 | // P Q R S T U V W X Y Z | |
1476 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1477 | // a b c d e f g h i j k l m n o | |
1478 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1479 | // p q r s t u v w x y z | |
1480 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
1481 | }; | |
1482 | ||
f3c0d7a5 | 1483 | int32_t bestMatchSkeletonFieldWidth[] = |
46f4442e A |
1484 | { |
1485 | // A B C D E F G H I J K L M N O | |
1486 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1487 | // P Q R S T U V W X Y Z | |
1488 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1489 | // a b c d e f g h i j k l m n o | |
1490 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1491 | // p q r s t u v w x y z | |
1492 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
1493 | }; | |
1494 | ||
1495 | DateIntervalInfo::parseSkeleton(inputSkeleton, inputSkeletonFieldWidth); | |
1496 | DateIntervalInfo::parseSkeleton(bestMatchSkeleton, bestMatchSkeletonFieldWidth); | |
1497 | if ( differenceInfo == 2 ) { | |
4388f060 A |
1498 | adjustedPtn.findAndReplace(UnicodeString((UChar)0x76 /* v */), |
1499 | UnicodeString((UChar)0x7a /* z */)); | |
46f4442e A |
1500 | } |
1501 | ||
1502 | UBool inQuote = false; | |
1503 | UChar prevCh = 0; | |
1504 | int32_t count = 0; | |
1505 | ||
1506 | const int8_t PATTERN_CHAR_BASE = 0x41; | |
f3c0d7a5 A |
1507 | |
1508 | // loop through the pattern string character by character | |
46f4442e A |
1509 | int32_t adjustedPtnLength = adjustedPtn.length(); |
1510 | int32_t i; | |
1511 | for (i = 0; i < adjustedPtnLength; ++i) { | |
1512 | UChar ch = adjustedPtn.charAt(i); | |
1513 | if (ch != prevCh && count > 0) { | |
1514 | // check the repeativeness of pattern letter | |
1515 | UChar skeletonChar = prevCh; | |
1516 | if ( skeletonChar == CAP_L ) { | |
f3c0d7a5 | 1517 | // there is no "L" (always be "M") in skeleton, |
46f4442e | 1518 | // but there is "L" in pattern. |
f3c0d7a5 | 1519 | // for skeleton "M+", the pattern might be "...L..." |
46f4442e A |
1520 | skeletonChar = CAP_M; |
1521 | } | |
1522 | int32_t fieldCount = bestMatchSkeletonFieldWidth[(int)(skeletonChar - PATTERN_CHAR_BASE)]; | |
1523 | int32_t inputFieldCount = inputSkeletonFieldWidth[(int)(skeletonChar - PATTERN_CHAR_BASE)]; | |
1524 | if ( fieldCount == count && inputFieldCount > fieldCount ) { | |
1525 | count = inputFieldCount - fieldCount; | |
1526 | int32_t j; | |
1527 | for ( j = 0; j < count; ++j ) { | |
f3c0d7a5 A |
1528 | adjustedPtn.insert(i, prevCh); |
1529 | } | |
46f4442e A |
1530 | i += count; |
1531 | adjustedPtnLength += count; | |
1532 | } | |
1533 | count = 0; | |
1534 | } | |
f3c0d7a5 | 1535 | if (ch == 0x0027 /*'*/) { |
46f4442e A |
1536 | // Consecutive single quotes are a single quote literal, |
1537 | // either outside of quotes or between quotes | |
f3c0d7a5 | 1538 | if ((i+1) < adjustedPtn.length() && adjustedPtn.charAt(i+1) == 0x0027 /* ' */) { |
46f4442e A |
1539 | ++i; |
1540 | } else { | |
1541 | inQuote = ! inQuote; | |
1542 | } | |
f3c0d7a5 A |
1543 | } |
1544 | else if ( ! inQuote && ((ch >= 0x0061 /*'a'*/ && ch <= 0x007A /*'z'*/) | |
46f4442e | 1545 | || (ch >= 0x0041 /*'A'*/ && ch <= 0x005A /*'Z'*/))) { |
f3c0d7a5 | 1546 | // ch is a date-time pattern character |
46f4442e A |
1547 | prevCh = ch; |
1548 | ++count; | |
1549 | } | |
1550 | } | |
1551 | if ( count > 0 ) { | |
1552 | // last item | |
1553 | // check the repeativeness of pattern letter | |
1554 | UChar skeletonChar = prevCh; | |
1555 | if ( skeletonChar == CAP_L ) { | |
f3c0d7a5 | 1556 | // there is no "L" (always be "M") in skeleton, |
46f4442e | 1557 | // but there is "L" in pattern. |
f3c0d7a5 | 1558 | // for skeleton "M+", the pattern might be "...L..." |
46f4442e A |
1559 | skeletonChar = CAP_M; |
1560 | } | |
1561 | int32_t fieldCount = bestMatchSkeletonFieldWidth[(int)(skeletonChar - PATTERN_CHAR_BASE)]; | |
1562 | int32_t inputFieldCount = inputSkeletonFieldWidth[(int)(skeletonChar - PATTERN_CHAR_BASE)]; | |
1563 | if ( fieldCount == count && inputFieldCount > fieldCount ) { | |
1564 | count = inputFieldCount - fieldCount; | |
1565 | int32_t j; | |
1566 | for ( j = 0; j < count; ++j ) { | |
f3c0d7a5 A |
1567 | adjustedPtn.append(prevCh); |
1568 | } | |
46f4442e A |
1569 | } |
1570 | } | |
1571 | } | |
1572 | ||
1573 | ||
1574 | ||
f3c0d7a5 | 1575 | void |
b331163b | 1576 | DateIntervalFormat::concatSingleDate2TimeInterval(UnicodeString& format, |
46f4442e A |
1577 | const UnicodeString& datePattern, |
1578 | UCalendarDateFields field, | |
1579 | UErrorCode& status) { | |
1580 | // following should not set wrong status | |
1581 | int32_t itvPtnIndex = DateIntervalInfo::calendarFieldToIntervalIndex(field, | |
1582 | status); | |
1583 | if ( U_FAILURE(status) ) { | |
1584 | return; | |
1585 | } | |
1586 | PatternInfo& timeItvPtnInfo = fIntervalPatterns[itvPtnIndex]; | |
1587 | if ( !timeItvPtnInfo.firstPart.isEmpty() ) { | |
2ca993e8 A |
1588 | UnicodeString timeIntervalPattern(timeItvPtnInfo.firstPart); |
1589 | timeIntervalPattern.append(timeItvPtnInfo.secondPart); | |
46f4442e | 1590 | UnicodeString combinedPattern; |
2ca993e8 A |
1591 | SimpleFormatter(format, 2, 2, status). |
1592 | format(timeIntervalPattern, datePattern, combinedPattern, status); | |
46f4442e A |
1593 | if ( U_FAILURE(status) ) { |
1594 | return; | |
1595 | } | |
1596 | setIntervalPattern(field, combinedPattern, timeItvPtnInfo.laterDateFirst); | |
f3c0d7a5 | 1597 | } |
46f4442e A |
1598 | // else: fall back |
1599 | // it should not happen if the interval format defined is valid | |
1600 | } | |
1601 | ||
1602 | ||
1603 | ||
1604 | const UChar | |
1605 | DateIntervalFormat::fgCalendarFieldToPatternLetter[] = | |
1606 | { | |
1607 | /*GyM*/ CAP_G, LOW_Y, CAP_M, | |
1608 | /*wWd*/ LOW_W, CAP_W, LOW_D, | |
1609 | /*DEF*/ CAP_D, CAP_E, CAP_F, | |
1610 | /*ahH*/ LOW_A, LOW_H, CAP_H, | |
57a6839d A |
1611 | /*msS*/ LOW_M, LOW_S, CAP_S, // MINUTE, SECOND, MILLISECOND |
1612 | /*z.Y*/ LOW_Z, SPACE, CAP_Y, // ZONE_OFFSET, DST_OFFSET, YEAR_WOY, | |
1613 | /*eug*/ LOW_E, LOW_U, LOW_G, // DOW_LOCAL, EXTENDED_YEAR, JULIAN_DAY, | |
1614 | /*A..*/ CAP_A, SPACE, SPACE, // MILLISECONDS_IN_DAY, IS_LEAP_MONTH, FIELD_COUNT | |
46f4442e A |
1615 | }; |
1616 | ||
1617 | ||
1618 | U_NAMESPACE_END | |
1619 | ||
1620 | #endif |