]>
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 |
729e4ab9 | 5 | * others. All Rights Reserved. |
46f4442e A |
6 | ******************************************************************************* |
7 | * | |
8 | * File DTITVFMT.H | |
9 | * | |
10 | ******************************************************************************* | |
11 | */ | |
12 | ||
13 | #ifndef __DTITVFMT_H__ | |
14 | #define __DTITVFMT_H__ | |
15 | ||
16 | ||
17 | #include "unicode/utypes.h" | |
18 | ||
340931cb A |
19 | #if U_SHOW_CPLUSPLUS_API |
20 | ||
46f4442e | 21 | /** |
729e4ab9 | 22 | * \file |
46f4442e A |
23 | * \brief C++ API: Format and parse date interval in a language-independent manner. |
24 | */ | |
729e4ab9 | 25 | |
46f4442e A |
26 | #if !UCONFIG_NO_FORMATTING |
27 | ||
28 | #include "unicode/ucal.h" | |
29 | #include "unicode/smpdtfmt.h" | |
30 | #include "unicode/dtintrv.h" | |
31 | #include "unicode/dtitvinf.h" | |
729e4ab9 | 32 | #include "unicode/dtptngen.h" |
3d1f044b A |
33 | #include "unicode/formattedvalue.h" |
34 | #include "unicode/udisplaycontext.h" | |
35 | // Apple-specific | |
4388f060 | 36 | #include "unicode/udateintervalformat.h" |
46f4442e A |
37 | |
38 | U_NAMESPACE_BEGIN | |
39 | ||
40 | ||
3d1f044b A |
41 | class FormattedDateIntervalData; |
42 | class DateIntervalFormat; | |
43 | ||
44 | #ifndef U_HIDE_DRAFT_API | |
45 | /** | |
46 | * An immutable class containing the result of a date interval formatting operation. | |
47 | * | |
48 | * Instances of this class are immutable and thread-safe. | |
49 | * | |
50 | * When calling nextPosition(): | |
51 | * The fields are returned from left to right. The special field category | |
52 | * UFIELD_CATEGORY_DATE_INTERVAL_SPAN is used to indicate which datetime | |
53 | * primitives came from which arguments: 0 means fromCalendar, and 1 means | |
54 | * toCalendar. The span category will always occur before the | |
55 | * corresponding fields in UFIELD_CATEGORY_DATE | |
56 | * in the nextPosition() iterator. | |
57 | * | |
58 | * Not intended for public subclassing. | |
59 | * | |
60 | * @draft ICU 64 | |
61 | */ | |
62 | class U_I18N_API FormattedDateInterval : public UMemory, public FormattedValue { | |
63 | public: | |
64 | /** | |
65 | * Default constructor; makes an empty FormattedDateInterval. | |
66 | * @draft ICU 64 | |
67 | */ | |
68 | FormattedDateInterval() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {} | |
69 | ||
70 | /** | |
71 | * Move constructor: Leaves the source FormattedDateInterval in an undefined state. | |
72 | * @draft ICU 64 | |
73 | */ | |
74 | FormattedDateInterval(FormattedDateInterval&& src) U_NOEXCEPT; | |
75 | ||
76 | /** | |
77 | * Destruct an instance of FormattedDateInterval. | |
78 | * @draft ICU 64 | |
79 | */ | |
80 | virtual ~FormattedDateInterval() U_OVERRIDE; | |
81 | ||
82 | /** Copying not supported; use move constructor instead. */ | |
83 | FormattedDateInterval(const FormattedDateInterval&) = delete; | |
84 | ||
85 | /** Copying not supported; use move assignment instead. */ | |
86 | FormattedDateInterval& operator=(const FormattedDateInterval&) = delete; | |
87 | ||
88 | /** | |
89 | * Move assignment: Leaves the source FormattedDateInterval in an undefined state. | |
90 | * @draft ICU 64 | |
91 | */ | |
92 | FormattedDateInterval& operator=(FormattedDateInterval&& src) U_NOEXCEPT; | |
93 | ||
94 | /** @copydoc FormattedValue::toString() */ | |
95 | UnicodeString toString(UErrorCode& status) const U_OVERRIDE; | |
96 | ||
97 | /** @copydoc FormattedValue::toTempString() */ | |
98 | UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE; | |
99 | ||
100 | /** @copydoc FormattedValue::appendTo() */ | |
101 | Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE; | |
102 | ||
103 | /** @copydoc FormattedValue::nextPosition() */ | |
104 | UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE; | |
105 | ||
106 | private: | |
107 | FormattedDateIntervalData *fData; | |
108 | UErrorCode fErrorCode; | |
109 | explicit FormattedDateInterval(FormattedDateIntervalData *results) | |
110 | : fData(results), fErrorCode(U_ZERO_ERROR) {} | |
111 | explicit FormattedDateInterval(UErrorCode errorCode) | |
112 | : fData(nullptr), fErrorCode(errorCode) {} | |
113 | friend class DateIntervalFormat; | |
114 | }; | |
115 | #endif /* U_HIDE_DRAFT_API */ | |
116 | ||
46f4442e A |
117 | |
118 | /** | |
729e4ab9 A |
119 | * DateIntervalFormat is a class for formatting and parsing date |
120 | * intervals in a language-independent manner. | |
121 | * Only formatting is supported, parsing is not supported. | |
46f4442e A |
122 | * |
123 | * <P> | |
124 | * Date interval means from one date to another date, | |
125 | * for example, from "Jan 11, 2008" to "Jan 18, 2008". | |
126 | * We introduced class DateInterval to represent it. | |
729e4ab9 | 127 | * DateInterval is a pair of UDate, which is |
46f4442e A |
128 | * the standard milliseconds since 24:00 GMT, Jan 1, 1970. |
129 | * | |
130 | * <P> | |
131 | * DateIntervalFormat formats a DateInterval into | |
729e4ab9 | 132 | * text as compactly as possible. |
46f4442e A |
133 | * For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008" |
134 | * is "Jan 11-18, 2008" for English. | |
729e4ab9 A |
135 | * And it parses text into DateInterval, |
136 | * although initially, parsing is not supported. | |
46f4442e A |
137 | * |
138 | * <P> | |
729e4ab9 A |
139 | * There is no structural information in date time patterns. |
140 | * For any punctuations and string literals inside a date time pattern, | |
141 | * we do not know whether it is just a separator, or a prefix, or a suffix. | |
142 | * Without such information, so, it is difficult to generate a sub-pattern | |
46f4442e A |
143 | * (or super-pattern) by algorithm. |
144 | * So, formatting a DateInterval is pattern-driven. It is very | |
145 | * similar to formatting in SimpleDateFormat. | |
729e4ab9 | 146 | * We introduce class DateIntervalInfo to save date interval |
46f4442e A |
147 | * patterns, similar to date time pattern in SimpleDateFormat. |
148 | * | |
149 | * <P> | |
150 | * Logically, the interval patterns are mappings | |
151 | * from (skeleton, the_largest_different_calendar_field) | |
152 | * to (date_interval_pattern). | |
153 | * | |
154 | * <P> | |
729e4ab9 | 155 | * A skeleton |
46f4442e A |
156 | * <ol> |
157 | * <li> | |
729e4ab9 | 158 | * only keeps the field pattern letter and ignores all other parts |
46f4442e A |
159 | * in a pattern, such as space, punctuations, and string literals. |
160 | * </li> | |
161 | * <li> | |
729e4ab9 | 162 | * hides the order of fields. |
46f4442e A |
163 | * </li> |
164 | * <li> | |
165 | * might hide a field's pattern letter length. | |
166 | * </li> | |
167 | * </ol> | |
168 | * | |
729e4ab9 A |
169 | * For those non-digit calendar fields, the pattern letter length is |
170 | * important, such as MMM, MMMM, and MMMMM; EEE and EEEE, | |
46f4442e | 171 | * and the field's pattern letter length is honored. |
729e4ab9 A |
172 | * |
173 | * For the digit calendar fields, such as M or MM, d or dd, yy or yyyy, | |
174 | * the field pattern length is ignored and the best match, which is defined | |
46f4442e A |
175 | * in date time patterns, will be returned without honor the field pattern |
176 | * letter length in skeleton. | |
177 | * | |
178 | * <P> | |
179 | * The calendar fields we support for interval formatting are: | |
b331163b A |
180 | * year, month, date, day-of-week, am-pm, hour, hour-of-day, minute, and second |
181 | * (though we do not currently have specific intervalFormat date for skeletons | |
182 | * with seconds). | |
46f4442e | 183 | * Those calendar fields can be defined in the following order: |
b331163b | 184 | * year > month > date > hour (in day) > minute > second |
729e4ab9 | 185 | * |
46f4442e A |
186 | * The largest different calendar fields between 2 calendars is the |
187 | * first different calendar field in above order. | |
188 | * | |
729e4ab9 | 189 | * For example: the largest different calendar fields between "Jan 10, 2007" |
46f4442e A |
190 | * and "Feb 20, 2008" is year. |
191 | * | |
192 | * <P> | |
193 | * For other calendar fields, the compact interval formatting is not | |
194 | * supported. And the interval format will be fall back to fall-back | |
195 | * patterns, which is mostly "{date0} - {date1}". | |
729e4ab9 | 196 | * |
46f4442e A |
197 | * <P> |
198 | * There is a set of pre-defined static skeleton strings. | |
199 | * There are pre-defined interval patterns for those pre-defined skeletons | |
200 | * in locales' resource files. | |
729e4ab9 A |
201 | * For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is "yMMMd", |
202 | * in en_US, if the largest different calendar field between date1 and date2 | |
203 | * is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy", | |
204 | * such as "Jan 10, 2007 - Jan 10, 2008". | |
205 | * If the largest different calendar field between date1 and date2 is "month", | |
206 | * the date interval pattern is "MMM d - MMM d, yyyy", | |
207 | * such as "Jan 10 - Feb 10, 2007". | |
208 | * If the largest different calendar field between date1 and date2 is "day", | |
209 | * the date interval pattern is "MMM d-d, yyyy", such as "Jan 10-20, 2007". | |
46f4442e | 210 | * |
729e4ab9 | 211 | * For date skeleton, the interval patterns when year, or month, or date is |
46f4442e A |
212 | * different are defined in resource files. |
213 | * For time skeleton, the interval patterns when am/pm, or hour, or minute is | |
214 | * different are defined in resource files. | |
215 | * | |
216 | * <P> | |
217 | * If a skeleton is not found in a locale's DateIntervalInfo, which means | |
218 | * the interval patterns for the skeleton is not defined in resource file, | |
729e4ab9 | 219 | * the interval pattern will falls back to the interval "fallback" pattern |
46f4442e A |
220 | * defined in resource file. |
221 | * If the interval "fallback" pattern is not defined, the default fall-back | |
222 | * is "{date0} - {data1}". | |
223 | * | |
224 | * <P> | |
729e4ab9 | 225 | * For the combination of date and time, |
46f4442e A |
226 | * The rule to generate interval patterns are: |
227 | * <ol> | |
228 | * <li> | |
229 | * when the year, month, or day differs, falls back to fall-back | |
729e4ab9 A |
230 | * interval pattern, which mostly is the concatenate the two original |
231 | * expressions with a separator between, | |
232 | * For example, interval pattern from "Jan 10, 2007 10:10 am" | |
233 | * to "Jan 11, 2007 10:10am" is | |
234 | * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am" | |
46f4442e A |
235 | * </li> |
236 | * <li> | |
729e4ab9 | 237 | * otherwise, present the date followed by the range expression |
46f4442e | 238 | * for the time. |
729e4ab9 A |
239 | * For example, interval pattern from "Jan 10, 2007 10:10 am" |
240 | * to "Jan 10, 2007 11:10am" is "Jan 10, 2007 10:10 am - 11:10am" | |
46f4442e A |
241 | * </li> |
242 | * </ol> | |
243 | * | |
244 | * | |
245 | * <P> | |
246 | * If two dates are the same, the interval pattern is the single date pattern. | |
729e4ab9 | 247 | * For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is |
46f4442e A |
248 | * "Jan 10, 2007". |
249 | * | |
250 | * Or if the presenting fields between 2 dates have the exact same values, | |
729e4ab9 | 251 | * the interval pattern is the single date pattern. |
46f4442e A |
252 | * For example, if user only requests year and month, |
253 | * the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007". | |
254 | * | |
255 | * <P> | |
729e4ab9 A |
256 | * DateIntervalFormat needs the following information for correct |
257 | * formatting: time zone, calendar type, pattern, date format symbols, | |
46f4442e A |
258 | * and date interval patterns. |
259 | * It can be instantiated in 2 ways: | |
260 | * <ol> | |
261 | * <li> | |
262 | * create an instance using default or given locale plus given skeleton. | |
729e4ab9 | 263 | * Users are encouraged to created date interval formatter this way and |
46f4442e A |
264 | * to use the pre-defined skeleton macros, such as |
265 | * UDAT_YEAR_NUM_MONTH, which consists the calendar fields and | |
729e4ab9 | 266 | * the format style. |
46f4442e A |
267 | * </li> |
268 | * <li> | |
269 | * create an instance using default or given locale plus given skeleton | |
270 | * plus a given DateIntervalInfo. | |
729e4ab9 A |
271 | * This factory method is for powerful users who want to provide their own |
272 | * interval patterns. | |
46f4442e A |
273 | * Locale provides the timezone, calendar, and format symbols information. |
274 | * Local plus skeleton provides full pattern information. | |
275 | * DateIntervalInfo provides the date interval patterns. | |
276 | * </li> | |
277 | * </ol> | |
278 | * | |
279 | * <P> | |
280 | * For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc. | |
281 | * DateIntervalFormat uses the same syntax as that of | |
282 | * DateTime format. | |
729e4ab9 | 283 | * |
46f4442e A |
284 | * <P> |
285 | * Code Sample: general usage | |
286 | * <pre> | |
287 | * \code | |
288 | * // the date interval object which the DateIntervalFormat formats on | |
289 | * // and parses into | |
290 | * DateInterval* dtInterval = new DateInterval(1000*3600*24, 1000*3600*24*2); | |
291 | * UErrorCode status = U_ZERO_ERROR; | |
292 | * DateIntervalFormat* dtIntervalFmt = DateIntervalFormat::createInstance( | |
729e4ab9 | 293 | * UDAT_YEAR_MONTH_DAY, |
46f4442e A |
294 | * Locale("en", "GB", ""), status); |
295 | * UnicodeUnicodeString dateIntervalString; | |
296 | * FieldPosition pos = 0; | |
297 | * // formatting | |
298 | * dtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status); | |
299 | * delete dtIntervalFmt; | |
300 | * \endcode | |
301 | * </pre> | |
302 | */ | |
46f4442e A |
303 | class U_I18N_API DateIntervalFormat : public Format { |
304 | public: | |
305 | ||
306 | /** | |
307 | * Construct a DateIntervalFormat from skeleton and the default locale. | |
308 | * | |
729e4ab9 | 309 | * This is a convenient override of |
46f4442e | 310 | * createInstance(const UnicodeString& skeleton, const Locale& locale, |
729e4ab9 | 311 | * UErrorCode&) |
46f4442e A |
312 | * with the value of locale as default locale. |
313 | * | |
314 | * @param skeleton the skeleton on which interval format based. | |
315 | * @param status output param set to success/failure code on exit | |
316 | * @return a date time interval formatter which the caller owns. | |
729e4ab9 | 317 | * @stable ICU 4.0 |
46f4442e A |
318 | */ |
319 | static DateIntervalFormat* U_EXPORT2 createInstance( | |
320 | const UnicodeString& skeleton, | |
321 | UErrorCode& status); | |
322 | ||
323 | /** | |
324 | * Construct a DateIntervalFormat from skeleton and a given locale. | |
325 | * <P> | |
729e4ab9 | 326 | * In this factory method, |
46f4442e | 327 | * the date interval pattern information is load from resource files. |
729e4ab9 | 328 | * Users are encouraged to created date interval formatter this way and |
46f4442e A |
329 | * to use the pre-defined skeleton macros. |
330 | * | |
331 | * <P> | |
729e4ab9 | 332 | * There are pre-defined skeletons (defined in udate.h) having predefined |
46f4442e A |
333 | * interval patterns in resource files. |
334 | * Users are encouraged to use those macros. | |
729e4ab9 A |
335 | * For example: |
336 | * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status) | |
46f4442e A |
337 | * |
338 | * The given Locale provides the interval patterns. | |
339 | * For example, for en_GB, if skeleton is UDAT_YEAR_ABBR_MONTH_WEEKDAY_DAY, | |
340 | * which is "yMMMEEEd", | |
341 | * the interval patterns defined in resource file to above skeleton are: | |
342 | * "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs, | |
343 | * "EEE, d MMM - EEE, d MMM, yyyy" for month differs, | |
344 | * "EEE, d - EEE, d MMM, yyyy" for day differs, | |
4388f060 | 345 | * @param skeleton the skeleton on which the interval format is based. |
46f4442e A |
346 | * @param locale the given locale |
347 | * @param status output param set to success/failure code on exit | |
348 | * @return a date time interval formatter which the caller owns. | |
729e4ab9 | 349 | * @stable ICU 4.0 |
57a6839d A |
350 | * <p> |
351 | * <h4>Sample code</h4> | |
352 | * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1 | |
353 | * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined | |
354 | * <p> | |
46f4442e A |
355 | */ |
356 | ||
357 | static DateIntervalFormat* U_EXPORT2 createInstance( | |
358 | const UnicodeString& skeleton, | |
359 | const Locale& locale, | |
360 | UErrorCode& status); | |
361 | ||
362 | /** | |
363 | * Construct a DateIntervalFormat from skeleton | |
364 | * DateIntervalInfo, and default locale. | |
365 | * | |
366 | * This is a convenient override of | |
729e4ab9 | 367 | * createInstance(const UnicodeString& skeleton, const Locale& locale, |
46f4442e A |
368 | * const DateIntervalInfo& dtitvinf, UErrorCode&) |
369 | * with the locale value as default locale. | |
370 | * | |
371 | * @param skeleton the skeleton on which interval format based. | |
729e4ab9 | 372 | * @param dtitvinf the DateIntervalInfo object. |
46f4442e A |
373 | * @param status output param set to success/failure code on exit |
374 | * @return a date time interval formatter which the caller owns. | |
729e4ab9 | 375 | * @stable ICU 4.0 |
46f4442e A |
376 | */ |
377 | static DateIntervalFormat* U_EXPORT2 createInstance( | |
378 | const UnicodeString& skeleton, | |
379 | const DateIntervalInfo& dtitvinf, | |
380 | UErrorCode& status); | |
381 | ||
382 | /** | |
383 | * Construct a DateIntervalFormat from skeleton | |
384 | * a DateIntervalInfo, and the given locale. | |
385 | * | |
386 | * <P> | |
387 | * In this factory method, user provides its own date interval pattern | |
729e4ab9 A |
388 | * information, instead of using those pre-defined data in resource file. |
389 | * This factory method is for powerful users who want to provide their own | |
390 | * interval patterns. | |
46f4442e | 391 | * <P> |
729e4ab9 | 392 | * There are pre-defined skeletons (defined in udate.h) having predefined |
46f4442e A |
393 | * interval patterns in resource files. |
394 | * Users are encouraged to use those macros. | |
729e4ab9 A |
395 | * For example: |
396 | * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status) | |
46f4442e A |
397 | * |
398 | * The DateIntervalInfo provides the interval patterns. | |
729e4ab9 | 399 | * and the DateIntervalInfo ownership remains to the caller. |
46f4442e A |
400 | * |
401 | * User are encouraged to set default interval pattern in DateIntervalInfo | |
402 | * as well, if they want to set other interval patterns ( instead of | |
403 | * reading the interval patterns from resource files). | |
404 | * When the corresponding interval pattern for a largest calendar different | |
405 | * field is not found ( if user not set it ), interval format fallback to | |
406 | * the default interval pattern. | |
407 | * If user does not provide default interval pattern, it fallback to | |
729e4ab9 | 408 | * "{date0} - {date1}" |
46f4442e A |
409 | * |
410 | * @param skeleton the skeleton on which interval format based. | |
411 | * @param locale the given locale | |
412 | * @param dtitvinf the DateIntervalInfo object. | |
413 | * @param status output param set to success/failure code on exit | |
414 | * @return a date time interval formatter which the caller owns. | |
729e4ab9 | 415 | * @stable ICU 4.0 |
57a6839d A |
416 | * <p> |
417 | * <h4>Sample code</h4> | |
418 | * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1 | |
419 | * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtCustomized | |
420 | * <p> | |
46f4442e A |
421 | */ |
422 | static DateIntervalFormat* U_EXPORT2 createInstance( | |
423 | const UnicodeString& skeleton, | |
424 | const Locale& locale, | |
425 | const DateIntervalInfo& dtitvinf, | |
426 | UErrorCode& status); | |
427 | ||
428 | /** | |
429 | * Destructor. | |
729e4ab9 | 430 | * @stable ICU 4.0 |
46f4442e A |
431 | */ |
432 | virtual ~DateIntervalFormat(); | |
433 | ||
434 | /** | |
435 | * Clone this Format object polymorphically. The caller owns the result and | |
436 | * should delete it when done. | |
437 | * @return A copy of the object. | |
729e4ab9 | 438 | * @stable ICU 4.0 |
46f4442e | 439 | */ |
340931cb | 440 | virtual DateIntervalFormat* clone() const; |
46f4442e A |
441 | |
442 | /** | |
443 | * Return true if the given Format objects are semantically equal. Objects | |
444 | * of different subclasses are considered unequal. | |
445 | * @param other the object to be compared with. | |
446 | * @return true if the given Format objects are semantically equal. | |
729e4ab9 | 447 | * @stable ICU 4.0 |
46f4442e A |
448 | */ |
449 | virtual UBool operator==(const Format& other) const; | |
450 | ||
451 | /** | |
729e4ab9 | 452 | * Return true if the given Format objects are not semantically equal. |
46f4442e A |
453 | * Objects of different subclasses are considered unequal. |
454 | * @param other the object to be compared with. | |
455 | * @return true if the given Format objects are not semantically equal. | |
729e4ab9 | 456 | * @stable ICU 4.0 |
46f4442e A |
457 | */ |
458 | UBool operator!=(const Format& other) const; | |
459 | ||
729e4ab9 A |
460 | |
461 | using Format::format; | |
462 | ||
46f4442e A |
463 | /** |
464 | * Format an object to produce a string. This method handles Formattable | |
729e4ab9 | 465 | * objects with a DateInterval type. |
46f4442e A |
466 | * If a the Formattable object type is not a DateInterval, |
467 | * then it returns a failing UErrorCode. | |
468 | * | |
729e4ab9 | 469 | * @param obj The object to format. |
46f4442e A |
470 | * Must be a DateInterval. |
471 | * @param appendTo Output parameter to receive result. | |
472 | * Result is appended to existing contents. | |
473 | * @param fieldPosition On input: an alignment field, if desired. | |
474 | * On output: the offsets of the alignment field. | |
2ca993e8 A |
475 | * There may be multiple instances of a given field type |
476 | * in an interval format; in this case the fieldPosition | |
477 | * offsets refer to the first instance. | |
46f4442e A |
478 | * @param status Output param filled with success/failure status. |
479 | * @return Reference to 'appendTo' parameter. | |
729e4ab9 | 480 | * @stable ICU 4.0 |
46f4442e A |
481 | */ |
482 | virtual UnicodeString& format(const Formattable& obj, | |
483 | UnicodeString& appendTo, | |
484 | FieldPosition& fieldPosition, | |
485 | UErrorCode& status) const ; | |
729e4ab9 A |
486 | |
487 | ||
46f4442e A |
488 | |
489 | /** | |
729e4ab9 | 490 | * Format a DateInterval to produce a string. |
46f4442e A |
491 | * |
492 | * @param dtInterval DateInterval to be formatted. | |
493 | * @param appendTo Output parameter to receive result. | |
494 | * Result is appended to existing contents. | |
495 | * @param fieldPosition On input: an alignment field, if desired. | |
496 | * On output: the offsets of the alignment field. | |
2ca993e8 A |
497 | * There may be multiple instances of a given field type |
498 | * in an interval format; in this case the fieldPosition | |
499 | * offsets refer to the first instance. | |
46f4442e A |
500 | * @param status Output param filled with success/failure status. |
501 | * @return Reference to 'appendTo' parameter. | |
729e4ab9 | 502 | * @stable ICU 4.0 |
46f4442e A |
503 | */ |
504 | UnicodeString& format(const DateInterval* dtInterval, | |
505 | UnicodeString& appendTo, | |
506 | FieldPosition& fieldPosition, | |
507 | UErrorCode& status) const ; | |
729e4ab9 | 508 | |
3d1f044b A |
509 | #ifndef U_HIDE_DRAFT_API |
510 | /** | |
511 | * Format a DateInterval to produce a FormattedDateInterval. | |
512 | * | |
513 | * The FormattedDateInterval exposes field information about the formatted string. | |
514 | * | |
515 | * @param dtInterval DateInterval to be formatted. | |
516 | * @param status Set if an error occurs. | |
517 | * @return A FormattedDateInterval containing the format result. | |
518 | * @draft ICU 64 | |
519 | */ | |
520 | FormattedDateInterval formatToValue( | |
521 | const DateInterval& dtInterval, | |
522 | UErrorCode& status) const; | |
523 | #endif /* U_HIDE_DRAFT_API */ | |
729e4ab9 | 524 | |
46f4442e | 525 | /** |
729e4ab9 | 526 | * Format 2 Calendars to produce a string. |
46f4442e A |
527 | * |
528 | * Note: "fromCalendar" and "toCalendar" are not const, | |
529 | * since calendar is not const in SimpleDateFormat::format(Calendar&), | |
530 | * | |
531 | * @param fromCalendar calendar set to the from date in date interval | |
532 | * to be formatted into date interval string | |
533 | * @param toCalendar calendar set to the to date in date interval | |
534 | * to be formatted into date interval string | |
535 | * @param appendTo Output parameter to receive result. | |
536 | * Result is appended to existing contents. | |
537 | * @param fieldPosition On input: an alignment field, if desired. | |
538 | * On output: the offsets of the alignment field. | |
2ca993e8 A |
539 | * There may be multiple instances of a given field type |
540 | * in an interval format; in this case the fieldPosition | |
541 | * offsets refer to the first instance. | |
46f4442e A |
542 | * @param status Output param filled with success/failure status. |
543 | * Caller needs to make sure it is SUCCESS | |
544 | * at the function entrance | |
545 | * @return Reference to 'appendTo' parameter. | |
729e4ab9 | 546 | * @stable ICU 4.0 |
46f4442e A |
547 | */ |
548 | UnicodeString& format(Calendar& fromCalendar, | |
549 | Calendar& toCalendar, | |
550 | UnicodeString& appendTo, | |
551 | FieldPosition& fieldPosition, | |
552 | UErrorCode& status) const ; | |
553 | ||
3d1f044b A |
554 | #ifndef U_HIDE_DRAFT_API |
555 | /** | |
556 | * Format 2 Calendars to produce a FormattedDateInterval. | |
557 | * | |
558 | * The FormattedDateInterval exposes field information about the formatted string. | |
559 | * | |
560 | * Note: "fromCalendar" and "toCalendar" are not const, | |
561 | * since calendar is not const in SimpleDateFormat::format(Calendar&), | |
562 | * | |
563 | * @param fromCalendar calendar set to the from date in date interval | |
564 | * to be formatted into date interval string | |
565 | * @param toCalendar calendar set to the to date in date interval | |
566 | * to be formatted into date interval string | |
567 | * @param status Set if an error occurs. | |
568 | * @return A FormattedDateInterval containing the format result. | |
569 | * @draft ICU 64 | |
570 | */ | |
571 | FormattedDateInterval formatToValue( | |
572 | Calendar& fromCalendar, | |
573 | Calendar& toCalendar, | |
574 | UErrorCode& status) const; | |
575 | #endif /* U_HIDE_DRAFT_API */ | |
576 | ||
46f4442e A |
577 | /** |
578 | * Date interval parsing is not supported. Please do not use. | |
579 | * <P> | |
580 | * This method should handle parsing of | |
729e4ab9 | 581 | * date time interval strings into Formattable objects with |
46f4442e A |
582 | * DateInterval type, which is a pair of UDate. |
583 | * <P> | |
584 | * Before calling, set parse_pos.index to the offset you want to start | |
585 | * parsing at in the source. After calling, parse_pos.index is the end of | |
586 | * the text you parsed. If error occurs, index is unchanged. | |
587 | * <P> | |
588 | * When parsing, leading whitespace is discarded (with a successful parse), | |
589 | * while trailing whitespace is left as is. | |
590 | * <P> | |
591 | * See Format::parseObject() for more. | |
592 | * | |
593 | * @param source The string to be parsed into an object. | |
594 | * @param result Formattable to be set to the parse result. | |
595 | * If parse fails, return contents are undefined. | |
596 | * @param parse_pos The position to start parsing at. Since no parsing | |
597 | * is supported, upon return this param is unchanged. | |
598 | * @return A newly created Formattable* object, or NULL | |
599 | * on failure. The caller owns this and should | |
600 | * delete it when done. | |
601 | * @internal ICU 4.0 | |
602 | */ | |
603 | virtual void parseObject(const UnicodeString& source, | |
604 | Formattable& result, | |
605 | ParsePosition& parse_pos) const; | |
606 | ||
607 | ||
608 | /** | |
609 | * Gets the date time interval patterns. | |
610 | * @return the date time interval patterns associated with | |
611 | * this date interval formatter. | |
729e4ab9 | 612 | * @stable ICU 4.0 |
46f4442e A |
613 | */ |
614 | const DateIntervalInfo* getDateIntervalInfo(void) const; | |
615 | ||
616 | ||
617 | /** | |
729e4ab9 | 618 | * Set the date time interval patterns. |
46f4442e A |
619 | * @param newIntervalPatterns the given interval patterns to copy. |
620 | * @param status output param set to success/failure code on exit | |
729e4ab9 | 621 | * @stable ICU 4.0 |
46f4442e A |
622 | */ |
623 | void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns, | |
624 | UErrorCode& status); | |
625 | ||
626 | ||
627 | /** | |
2ca993e8 A |
628 | * Gets the date formatter. The DateIntervalFormat instance continues to own |
629 | * the returned DateFormatter object, and will use and possibly modify it | |
630 | * during format operations. In a multi-threaded environment, the returned | |
631 | * DateFormat can only be used if it is certain that no other threads are | |
632 | * concurrently using this DateIntervalFormatter, even for nominally const | |
633 | * functions. | |
634 | * | |
46f4442e | 635 | * @return the date formatter associated with this date interval formatter. |
729e4ab9 | 636 | * @stable ICU 4.0 |
46f4442e A |
637 | */ |
638 | const DateFormat* getDateFormat(void) const; | |
639 | ||
4388f060 A |
640 | /** |
641 | * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar. | |
642 | * @return the time zone associated with the calendar of DateIntervalFormat. | |
643 | * @stable ICU 4.8 | |
644 | */ | |
645 | virtual const TimeZone& getTimeZone(void) const; | |
646 | ||
647 | /** | |
648 | * Sets the time zone for the calendar used by this DateIntervalFormat object. The | |
649 | * caller no longer owns the TimeZone object and should not delete it after this call. | |
650 | * @param zoneToAdopt the TimeZone to be adopted. | |
651 | * @stable ICU 4.8 | |
652 | */ | |
653 | virtual void adoptTimeZone(TimeZone* zoneToAdopt); | |
654 | ||
655 | /** | |
656 | * Sets the time zone for the calendar used by this DateIntervalFormat object. | |
657 | * @param zone the new time zone. | |
658 | * @stable ICU 4.8 | |
659 | */ | |
660 | virtual void setTimeZone(const TimeZone& zone); | |
661 | ||
662 | /** | |
663 | * Change attributes for the DateIntervalFormat object. | |
664 | * @param attr | |
665 | * The attribute to change. | |
666 | * @param value | |
667 | * The new value for the attribute. | |
668 | * @param status | |
669 | * A UErrorCode to receive any errors. | |
670 | * @internal | |
671 | */ | |
672 | virtual void setAttribute(UDateIntervalFormatAttribute attr, | |
673 | UDateIntervalFormatAttributeValue value, | |
674 | UErrorCode &status); | |
675 | ||
3d1f044b A |
676 | /** |
677 | * Set a particular UDisplayContext value in the formatter, such as | |
678 | * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. This causes the formatted | |
679 | * result to be capitalized appropriately for the context in which | |
680 | * it is intended to be used, considering both the locale and the | |
681 | * type of field at the beginning of the formatted result. | |
682 | * @param value The UDisplayContext value to set. | |
683 | * @param status Input/output status. If at entry this indicates a failure | |
684 | * status, the function will do nothing; otherwise this will be | |
685 | * updated with any new status from the function. | |
686 | * @draft ICU 65 | |
687 | */ | |
688 | virtual void setContext(UDisplayContext value, UErrorCode& status); | |
689 | ||
690 | /** | |
691 | * Get the formatter's UDisplayContext value for the specified UDisplayContextType, | |
692 | * such as UDISPCTX_TYPE_CAPITALIZATION. | |
693 | * @param type The UDisplayContextType whose value to return | |
694 | * @param status Input/output status. If at entry this indicates a failure | |
695 | * status, the function will do nothing; otherwise this will be | |
696 | * updated with any new status from the function. | |
697 | * @return The UDisplayContextValue for the specified type. | |
698 | * @draft ICU 65 | |
699 | */ | |
700 | virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const; | |
701 | ||
46f4442e A |
702 | /** |
703 | * Return the class ID for this class. This is useful only for comparing to | |
704 | * a return value from getDynamicClassID(). For example: | |
705 | * <pre> | |
706 | * . Base* polymorphic_pointer = createPolymorphicObject(); | |
707 | * . if (polymorphic_pointer->getDynamicClassID() == | |
708 | * . erived::getStaticClassID()) ... | |
709 | * </pre> | |
710 | * @return The class ID for all objects of this class. | |
729e4ab9 | 711 | * @stable ICU 4.0 |
46f4442e A |
712 | */ |
713 | static UClassID U_EXPORT2 getStaticClassID(void); | |
714 | ||
715 | /** | |
716 | * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This | |
717 | * method is to implement a simple version of RTTI, since not all C++ | |
718 | * compilers support genuine RTTI. Polymorphic operator==() and clone() | |
719 | * methods call this method. | |
720 | * | |
721 | * @return The class ID for this object. All objects of a | |
722 | * given class have the same class ID. Objects of | |
723 | * other classes have different class IDs. | |
729e4ab9 | 724 | * @stable ICU 4.0 |
46f4442e A |
725 | */ |
726 | virtual UClassID getDynamicClassID(void) const; | |
727 | ||
728 | protected: | |
729 | ||
730 | /** | |
731 | * Copy constructor. | |
729e4ab9 | 732 | * @stable ICU 4.0 |
46f4442e A |
733 | */ |
734 | DateIntervalFormat(const DateIntervalFormat&); | |
735 | ||
736 | /** | |
737 | * Assignment operator. | |
729e4ab9 | 738 | * @stable ICU 4.0 |
46f4442e A |
739 | */ |
740 | DateIntervalFormat& operator=(const DateIntervalFormat&); | |
741 | ||
742 | private: | |
743 | ||
744 | /* | |
745 | * This is for ICU internal use only. Please do not use. | |
746 | * Save the interval pattern information. | |
747 | * Interval pattern consists of 2 single date patterns and the separator. | |
748 | * For example, interval pattern "MMM d - MMM d, yyyy" consists | |
749 | * a single date pattern "MMM d", another single date pattern "MMM d, yyyy", | |
750 | * and a separator "-". | |
751 | * The pattern is divided into 2 parts. For above example, | |
752 | * the first part is "MMM d - ", and the second part is "MMM d, yyyy". | |
729e4ab9 | 753 | * Also, the first date appears in an interval pattern could be |
46f4442e A |
754 | * the earlier date or the later date. |
755 | * And such information is saved in the interval pattern as well. | |
46f4442e A |
756 | */ |
757 | struct PatternInfo { | |
758 | UnicodeString firstPart; | |
759 | UnicodeString secondPart; | |
760 | /** | |
761 | * Whether the first date in interval pattern is later date or not. | |
762 | * Fallback format set the default ordering. | |
729e4ab9 A |
763 | * And for a particular interval pattern, the order can be |
764 | * overriden by prefixing the interval pattern with "latestFirst:" or | |
46f4442e A |
765 | * "earliestFirst:" |
766 | * For example, given 2 date, Jan 10, 2007 to Feb 10, 2007. | |
729e4ab9 | 767 | * if the fallback format is "{0} - {1}", |
46f4442e A |
768 | * and the pattern is "d MMM - d MMM yyyy", the interval format is |
769 | * "10 Jan - 10 Feb, 2007". | |
729e4ab9 | 770 | * If the pattern is "latestFirst:d MMM - d MMM yyyy", |
46f4442e A |
771 | * the interval format is "10 Feb - 10 Jan, 2007" |
772 | */ | |
773 | UBool laterDateFirst; | |
774 | }; | |
775 | ||
729e4ab9 | 776 | |
46f4442e | 777 | /** |
729e4ab9 | 778 | * default constructor |
2ca993e8 | 779 | * @internal (private) |
46f4442e A |
780 | */ |
781 | DateIntervalFormat(); | |
782 | ||
783 | /** | |
784 | * Construct a DateIntervalFormat from DateFormat, | |
785 | * a DateIntervalInfo, and skeleton. | |
786 | * DateFormat provides the timezone, calendar, | |
787 | * full pattern, and date format symbols information. | |
729e4ab9 | 788 | * It should be a SimpleDateFormat object which |
46f4442e A |
789 | * has a pattern in it. |
790 | * the DateIntervalInfo provides the interval patterns. | |
791 | * | |
729e4ab9 A |
792 | * Note: the DateIntervalFormat takes ownership of both |
793 | * DateFormat and DateIntervalInfo objects. | |
46f4442e A |
794 | * Caller should not delete them. |
795 | * | |
729e4ab9 | 796 | * @param locale the locale of this date interval formatter. |
4388f060 | 797 | * @param dtItvInfo the DateIntervalInfo object to be adopted. |
46f4442e A |
798 | * @param skeleton the skeleton of the date formatter |
799 | * @param status output param set to success/failure code on exit | |
46f4442e | 800 | */ |
729e4ab9 | 801 | DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo, |
46f4442e A |
802 | const UnicodeString* skeleton, UErrorCode& status); |
803 | ||
729e4ab9 | 804 | |
46f4442e A |
805 | /** |
806 | * Construct a DateIntervalFormat from DateFormat | |
807 | * and a DateIntervalInfo. | |
808 | * | |
809 | * It is a wrapper of the constructor. | |
810 | * | |
729e4ab9 | 811 | * @param locale the locale of this date interval formatter. |
46f4442e A |
812 | * @param dtitvinf the DateIntervalInfo object to be adopted. |
813 | * @param skeleton the skeleton of this formatter. | |
814 | * @param status Output param set to success/failure code. | |
815 | * @return a date time interval formatter which the caller owns. | |
46f4442e | 816 | */ |
729e4ab9 | 817 | static DateIntervalFormat* U_EXPORT2 create(const Locale& locale, |
46f4442e A |
818 | DateIntervalInfo* dtitvinf, |
819 | const UnicodeString* skeleton, | |
820 | UErrorCode& status); | |
821 | ||
46f4442e | 822 | /** |
729e4ab9 | 823 | * Below are for generating interval patterns local to the formatter |
46f4442e A |
824 | */ |
825 | ||
3d1f044b A |
826 | /** Like fallbackFormat, but only formats the range part of the fallback. */ |
827 | void fallbackFormatRange( | |
828 | Calendar& fromCalendar, | |
829 | Calendar& toCalendar, | |
830 | UnicodeString& appendTo, | |
831 | int8_t& firstIndex, | |
832 | FieldPositionHandler& fphandler, | |
833 | UErrorCode& status) const; | |
b331163b | 834 | |
46f4442e A |
835 | /** |
836 | * Format 2 Calendars using fall-back interval pattern | |
837 | * | |
838 | * The full pattern used in this fall-back format is the | |
839 | * full pattern of the date formatter. | |
840 | * | |
2ca993e8 A |
841 | * gFormatterMutex must already be locked when calling this function. |
842 | * | |
46f4442e A |
843 | * @param fromCalendar calendar set to the from date in date interval |
844 | * to be formatted into date interval string | |
845 | * @param toCalendar calendar set to the to date in date interval | |
846 | * to be formatted into date interval string | |
b331163b A |
847 | * @param fromToOnSameDay TRUE iff from and to dates are on the same day |
848 | * (any difference is in ampm/hours or below) | |
46f4442e A |
849 | * @param appendTo Output parameter to receive result. |
850 | * Result is appended to existing contents. | |
3d1f044b A |
851 | * @param firstIndex See formatImpl for more information. |
852 | * @param fphandler See formatImpl for more information. | |
46f4442e A |
853 | * @param status output param set to success/failure code on exit |
854 | * @return Reference to 'appendTo' parameter. | |
2ca993e8 | 855 | * @internal (private) |
46f4442e A |
856 | */ |
857 | UnicodeString& fallbackFormat(Calendar& fromCalendar, | |
858 | Calendar& toCalendar, | |
b331163b | 859 | UBool fromToOnSameDay, |
46f4442e | 860 | UnicodeString& appendTo, |
3d1f044b A |
861 | int8_t& firstIndex, |
862 | FieldPositionHandler& fphandler, | |
46f4442e A |
863 | UErrorCode& status) const; |
864 | ||
865 | ||
866 | ||
729e4ab9 | 867 | /** |
46f4442e | 868 | * Initialize interval patterns locale to this formatter |
729e4ab9 A |
869 | * |
870 | * This code is a bit complicated since | |
46f4442e A |
871 | * 1. the interval patterns saved in resource bundle files are interval |
872 | * patterns based on date or time only. | |
873 | * It does not have interval patterns based on both date and time. | |
874 | * Interval patterns on both date and time are algorithm generated. | |
875 | * | |
876 | * For example, it has interval patterns on skeleton "dMy" and "hm", | |
877 | * but it does not have interval patterns on skeleton "dMyhm". | |
729e4ab9 | 878 | * |
46f4442e | 879 | * The rule to generate interval patterns for both date and time skeleton are |
729e4ab9 A |
880 | * 1) when the year, month, or day differs, concatenate the two original |
881 | * expressions with a separator between, | |
882 | * For example, interval pattern from "Jan 10, 2007 10:10 am" | |
883 | * to "Jan 11, 2007 10:10am" is | |
884 | * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am" | |
46f4442e | 885 | * |
729e4ab9 | 886 | * 2) otherwise, present the date followed by the range expression |
46f4442e | 887 | * for the time. |
729e4ab9 A |
888 | * For example, interval pattern from "Jan 10, 2007 10:10 am" |
889 | * to "Jan 10, 2007 11:10am" is | |
890 | * "Jan 10, 2007 10:10 am - 11:10am" | |
46f4442e A |
891 | * |
892 | * 2. even a pattern does not request a certain calendar field, | |
893 | * the interval pattern needs to include such field if such fields are | |
894 | * different between 2 dates. | |
729e4ab9 | 895 | * For example, a pattern/skeleton is "hm", but the interval pattern |
46f4442e | 896 | * includes year, month, and date when year, month, and date differs. |
729e4ab9 | 897 | * |
46f4442e A |
898 | * |
899 | * @param status output param set to success/failure code on exit | |
46f4442e | 900 | */ |
729e4ab9 A |
901 | void initializePattern(UErrorCode& status); |
902 | ||
46f4442e A |
903 | |
904 | ||
905 | /** | |
906 | * Set fall back interval pattern given a calendar field, | |
907 | * a skeleton, and a date time pattern generator. | |
908 | * @param field the largest different calendar field | |
909 | * @param skeleton a skeleton | |
46f4442e | 910 | * @param status output param set to success/failure code on exit |
46f4442e | 911 | */ |
729e4ab9 | 912 | void setFallbackPattern(UCalendarDateFields field, |
46f4442e | 913 | const UnicodeString& skeleton, |
46f4442e | 914 | UErrorCode& status); |
46f4442e A |
915 | |
916 | ||
729e4ab9 A |
917 | |
918 | /** | |
46f4442e A |
919 | * get separated date and time skeleton from a combined skeleton. |
920 | * | |
921 | * The difference between date skeleton and normalizedDateSkeleton are: | |
922 | * 1. both 'y' and 'd' are appeared only once in normalizeDateSkeleton | |
923 | * 2. 'E' and 'EE' are normalized into 'EEE' | |
924 | * 3. 'MM' is normalized into 'M' | |
925 | * | |
926 | ** the difference between time skeleton and normalizedTimeSkeleton are: | |
927 | * 1. both 'H' and 'h' are normalized as 'h' in normalized time skeleton, | |
928 | * 2. 'a' is omitted in normalized time skeleton. | |
929 | * 3. there is only one appearance for 'h', 'm','v', 'z' in normalized time | |
930 | * skeleton | |
931 | * | |
932 | * | |
933 | * @param skeleton given combined skeleton. | |
934 | * @param date Output parameter for date only skeleton. | |
935 | * @param normalizedDate Output parameter for normalized date only | |
936 | * | |
937 | * @param time Output parameter for time only skeleton. | |
938 | * @param normalizedTime Output parameter for normalized time only | |
939 | * skeleton. | |
940 | * | |
46f4442e A |
941 | */ |
942 | static void U_EXPORT2 getDateTimeSkeleton(const UnicodeString& skeleton, | |
943 | UnicodeString& date, | |
944 | UnicodeString& normalizedDate, | |
945 | UnicodeString& time, | |
946 | UnicodeString& normalizedTime); | |
947 | ||
948 | ||
949 | ||
950 | /** | |
951 | * Generate date or time interval pattern from resource, | |
952 | * and set them into the interval pattern locale to this formatter. | |
953 | * | |
729e4ab9 | 954 | * It needs to handle the following: |
46f4442e A |
955 | * 1. need to adjust field width. |
956 | * For example, the interval patterns saved in DateIntervalInfo | |
957 | * includes "dMMMy", but not "dMMMMy". | |
958 | * Need to get interval patterns for dMMMMy from dMMMy. | |
959 | * Another example, the interval patterns saved in DateIntervalInfo | |
960 | * includes "hmv", but not "hmz". | |
961 | * Need to get interval patterns for "hmz' from 'hmv' | |
962 | * | |
963 | * 2. there might be no pattern for 'y' differ for skeleton "Md", | |
964 | * in order to get interval patterns for 'y' differ, | |
965 | * need to look for it from skeleton 'yMd' | |
966 | * | |
967 | * @param dateSkeleton normalized date skeleton | |
968 | * @param timeSkeleton normalized time skeleton | |
969 | * @return whether the resource is found for the skeleton. | |
970 | * TRUE if interval pattern found for the skeleton, | |
971 | * FALSE otherwise. | |
46f4442e | 972 | */ |
729e4ab9 | 973 | UBool setSeparateDateTimePtn(const UnicodeString& dateSkeleton, |
46f4442e | 974 | const UnicodeString& timeSkeleton); |
729e4ab9 | 975 | |
46f4442e A |
976 | |
977 | ||
978 | ||
979 | /** | |
980 | * Generate interval pattern from existing resource | |
981 | * | |
982 | * It not only save the interval patterns, | |
983 | * but also return the extended skeleton and its best match skeleton. | |
984 | * | |
985 | * @param field largest different calendar field | |
986 | * @param skeleton skeleton | |
987 | * @param bestSkeleton the best match skeleton which has interval pattern | |
988 | * defined in resource | |
989 | * @param differenceInfo the difference between skeleton and best skeleton | |
990 | * 0 means the best matched skeleton is the same as input skeleton | |
991 | * 1 means the fields are the same, but field width are different | |
992 | * 2 means the only difference between fields are v/z, | |
729e4ab9 | 993 | * -1 means there are other fields difference |
46f4442e A |
994 | * |
995 | * @param extendedSkeleton extended skeleton | |
996 | * @param extendedBestSkeleton extended best match skeleton | |
729e4ab9 | 997 | * @return whether the interval pattern is found |
46f4442e A |
998 | * through extending skeleton or not. |
999 | * TRUE if interval pattern is found by | |
1000 | * extending skeleton, FALSE otherwise. | |
46f4442e | 1001 | */ |
729e4ab9 A |
1002 | UBool setIntervalPattern(UCalendarDateFields field, |
1003 | const UnicodeString* skeleton, | |
1004 | const UnicodeString* bestSkeleton, | |
1005 | int8_t differenceInfo, | |
46f4442e A |
1006 | UnicodeString* extendedSkeleton = NULL, |
1007 | UnicodeString* extendedBestSkeleton = NULL); | |
1008 | ||
1009 | /** | |
1010 | * Adjust field width in best match interval pattern to match | |
1011 | * the field width in input skeleton. | |
1012 | * | |
1013 | * TODO (xji) make a general solution | |
1014 | * The adjusting rule can be: | |
1015 | * 1. always adjust | |
1016 | * 2. never adjust | |
1017 | * 3. default adjust, which means adjust according to the following rules | |
1018 | * 3.1 always adjust string, such as MMM and MMMM | |
1019 | * 3.2 never adjust between string and numeric, such as MM and MMM | |
1020 | * 3.3 always adjust year | |
1021 | * 3.4 do not adjust 'd', 'h', or 'm' if h presents | |
1022 | * 3.5 do not adjust 'M' if it is numeric(?) | |
1023 | * | |
1024 | * Since date interval format is well-formed format, | |
1025 | * date and time skeletons are normalized previously, | |
1026 | * till this stage, the adjust here is only "adjust strings, such as MMM | |
1027 | * and MMMM, EEE and EEEE. | |
1028 | * | |
1029 | * @param inputSkeleton the input skeleton | |
1030 | * @param bestMatchSkeleton the best match skeleton | |
4388f060 | 1031 | * @param bestMatchIntervalPattern the best match interval pattern |
46f4442e A |
1032 | * @param differenceInfo the difference between 2 skeletons |
1033 | * 1 means only field width differs | |
1034 | * 2 means v/z exchange | |
1035 | * @param adjustedIntervalPattern adjusted interval pattern | |
46f4442e A |
1036 | */ |
1037 | static void U_EXPORT2 adjustFieldWidth( | |
1038 | const UnicodeString& inputSkeleton, | |
1039 | const UnicodeString& bestMatchSkeleton, | |
1040 | const UnicodeString& bestMatchIntervalPattern, | |
1041 | int8_t differenceInfo, | |
1042 | UnicodeString& adjustedIntervalPattern); | |
1043 | ||
1044 | /** | |
1045 | * Concat a single date pattern with a time interval pattern, | |
1046 | * set it into the intervalPatterns, while field is time field. | |
1047 | * This is used to handle time interval patterns on skeleton with | |
729e4ab9 | 1048 | * both time and date. Present the date followed by |
46f4442e A |
1049 | * the range expression for the time. |
1050 | * @param format date and time format | |
46f4442e A |
1051 | * @param datePattern date pattern |
1052 | * @param field time calendar field: AM_PM, HOUR, MINUTE | |
1053 | * @param status output param set to success/failure code on exit | |
46f4442e | 1054 | */ |
b331163b | 1055 | void concatSingleDate2TimeInterval(UnicodeString& format, |
46f4442e A |
1056 | const UnicodeString& datePattern, |
1057 | UCalendarDateFields field, | |
729e4ab9 | 1058 | UErrorCode& status); |
46f4442e A |
1059 | |
1060 | /** | |
1061 | * check whether a calendar field present in a skeleton. | |
1062 | * @param field calendar field need to check | |
1063 | * @param skeleton given skeleton on which to check the calendar field | |
1064 | * @return true if field present in a skeleton. | |
46f4442e | 1065 | */ |
729e4ab9 | 1066 | static UBool U_EXPORT2 fieldExistsInSkeleton(UCalendarDateFields field, |
46f4442e A |
1067 | const UnicodeString& skeleton); |
1068 | ||
1069 | ||
1070 | /** | |
1071 | * Split interval patterns into 2 part. | |
1072 | * @param intervalPattern interval pattern | |
1073 | * @return the index in interval pattern which split the pattern into 2 part | |
46f4442e A |
1074 | */ |
1075 | static int32_t U_EXPORT2 splitPatternInto2Part(const UnicodeString& intervalPattern); | |
1076 | ||
1077 | ||
1078 | /** | |
1079 | * Break interval patterns as 2 part and save them into pattern info. | |
1080 | * @param field calendar field | |
1081 | * @param intervalPattern interval pattern | |
46f4442e A |
1082 | */ |
1083 | void setIntervalPattern(UCalendarDateFields field, | |
1084 | const UnicodeString& intervalPattern); | |
1085 | ||
1086 | ||
1087 | /** | |
1088 | * Break interval patterns as 2 part and save them into pattern info. | |
1089 | * @param field calendar field | |
1090 | * @param intervalPattern interval pattern | |
1091 | * @param laterDateFirst whether later date appear first in interval pattern | |
46f4442e A |
1092 | */ |
1093 | void setIntervalPattern(UCalendarDateFields field, | |
1094 | const UnicodeString& intervalPattern, | |
1095 | UBool laterDateFirst); | |
1096 | ||
1097 | ||
1098 | /** | |
1099 | * Set pattern information. | |
1100 | * | |
1101 | * @param field calendar field | |
1102 | * @param firstPart the first part in interval pattern | |
1103 | * @param secondPart the second part in interval pattern | |
1104 | * @param laterDateFirst whether the first date in intervalPattern | |
1105 | * is earlier date or later date | |
46f4442e A |
1106 | */ |
1107 | void setPatternInfo(UCalendarDateFields field, | |
1108 | const UnicodeString* firstPart, | |
4388f060 | 1109 | const UnicodeString* secondPart, |
46f4442e A |
1110 | UBool laterDateFirst); |
1111 | ||
2ca993e8 A |
1112 | /** |
1113 | * Format 2 Calendars to produce a string. | |
1114 | * Implementation of the similar public format function. | |
1115 | * Must be called with gFormatterMutex already locked. | |
1116 | * | |
1117 | * Note: "fromCalendar" and "toCalendar" are not const, | |
1118 | * since calendar is not const in SimpleDateFormat::format(Calendar&), | |
1119 | * | |
1120 | * @param fromCalendar calendar set to the from date in date interval | |
1121 | * to be formatted into date interval string | |
1122 | * @param toCalendar calendar set to the to date in date interval | |
1123 | * to be formatted into date interval string | |
1124 | * @param appendTo Output parameter to receive result. | |
1125 | * Result is appended to existing contents. | |
3d1f044b A |
1126 | * @param firstIndex 0 if the first output date is fromCalendar; |
1127 | * 1 if it corresponds to toCalendar; | |
1128 | * -1 if there is only one date printed. | |
1129 | * @param fphandler Handler for field position information. | |
1130 | * The fields will be from the UDateFormatField enum. | |
2ca993e8 A |
1131 | * @param status Output param filled with success/failure status. |
1132 | * Caller needs to make sure it is SUCCESS | |
1133 | * at the function entrance | |
1134 | * @return Reference to 'appendTo' parameter. | |
1135 | * @internal (private) | |
1136 | */ | |
1137 | UnicodeString& formatImpl(Calendar& fromCalendar, | |
1138 | Calendar& toCalendar, | |
1139 | UnicodeString& appendTo, | |
3d1f044b A |
1140 | int8_t& firstIndex, |
1141 | FieldPositionHandler& fphandler, | |
2ca993e8 A |
1142 | UErrorCode& status) const ; |
1143 | ||
3d1f044b A |
1144 | /** Version of formatImpl for DateInterval. */ |
1145 | UnicodeString& formatIntervalImpl(const DateInterval& dtInterval, | |
1146 | UnicodeString& appendTo, | |
1147 | int8_t& firstIndex, | |
1148 | FieldPositionHandler& fphandler, | |
1149 | UErrorCode& status) const; | |
1150 | ||
46f4442e A |
1151 | |
1152 | // from calendar field to pattern letter | |
f3c0d7a5 | 1153 | static const char16_t fgCalendarFieldToPatternLetter[]; |
46f4442e A |
1154 | |
1155 | ||
1156 | /** | |
1157 | * The interval patterns for this locale. | |
1158 | */ | |
1159 | DateIntervalInfo* fInfo; | |
1160 | ||
1161 | /** | |
1162 | * The DateFormat object used to format single pattern | |
1163 | */ | |
1164 | SimpleDateFormat* fDateFormat; | |
1165 | ||
1166 | /** | |
1167 | * The 2 calendars with the from and to date. | |
1168 | * could re-use the calendar in fDateFormat, | |
1169 | * but keeping 2 calendars make it clear and clean. | |
1170 | */ | |
1171 | Calendar* fFromCalendar; | |
1172 | Calendar* fToCalendar; | |
1173 | ||
2ca993e8 | 1174 | Locale fLocale; |
729e4ab9 | 1175 | |
46f4442e | 1176 | /** |
4388f060 | 1177 | * Following are interval information relevant (locale) to this formatter. |
46f4442e A |
1178 | */ |
1179 | UnicodeString fSkeleton; | |
1180 | PatternInfo fIntervalPatterns[DateIntervalInfo::kIPI_MAX_INDEX]; | |
4388f060 | 1181 | |
b331163b A |
1182 | /** |
1183 | * Patterns for fallback formatting. | |
1184 | */ | |
1185 | UnicodeString* fDatePattern; | |
1186 | UnicodeString* fTimePattern; | |
1187 | UnicodeString* fDateTimeFormat; | |
1188 | ||
4388f060 A |
1189 | /** |
1190 | * Atttributes | |
1191 | */ | |
1192 | int32_t fMinimizeType; | |
3d1f044b A |
1193 | |
1194 | UDisplayContext fCapitalizationContext; | |
46f4442e | 1195 | }; |
46f4442e | 1196 | |
729e4ab9 | 1197 | inline UBool |
46f4442e | 1198 | DateIntervalFormat::operator!=(const Format& other) const { |
729e4ab9 | 1199 | return !operator==(other); |
46f4442e | 1200 | } |
729e4ab9 | 1201 | |
46f4442e A |
1202 | U_NAMESPACE_END |
1203 | ||
1204 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
1205 | ||
340931cb A |
1206 | #endif /* U_SHOW_CPLUSPLUS_API */ |
1207 | ||
46f4442e A |
1208 | #endif // _DTITVFMT_H__ |
1209 | //eof |