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