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