]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/dtitvfmt.h
ICU-551.24.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / dtitvfmt.h
1 /********************************************************************************
2 * Copyright (C) 2008-2013,2015, International Business Machines Corporation and
3 * others. All Rights Reserved.
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 /**
18 * \file
19 * \brief C++ API: Format and parse date interval in a language-independent manner.
20 */
21
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"
28 #include "unicode/dtptngen.h"
29 #include "unicode/udateintervalformat.h"
30
31 U_NAMESPACE_BEGIN
32
33
34
35 /**
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.
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.
44 * DateInterval is a pair of UDate, which is
45 * the standard milliseconds since 24:00 GMT, Jan 1, 1970.
46 *
47 * <P>
48 * DateIntervalFormat formats a DateInterval into
49 * text as compactly as possible.
50 * For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008"
51 * is "Jan 11-18, 2008" for English.
52 * And it parses text into DateInterval,
53 * although initially, parsing is not supported.
54 *
55 * <P>
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
60 * (or super-pattern) by algorithm.
61 * So, formatting a DateInterval is pattern-driven. It is very
62 * similar to formatting in SimpleDateFormat.
63 * We introduce class DateIntervalInfo to save date interval
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>
72 * A skeleton
73 * <ol>
74 * <li>
75 * only keeps the field pattern letter and ignores all other parts
76 * in a pattern, such as space, punctuations, and string literals.
77 * </li>
78 * <li>
79 * hides the order of fields.
80 * </li>
81 * <li>
82 * might hide a field's pattern letter length.
83 * </li>
84 * </ol>
85 *
86 * For those non-digit calendar fields, the pattern letter length is
87 * important, such as MMM, MMMM, and MMMMM; EEE and EEEE,
88 * and the field's pattern letter length is honored.
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
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:
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).
100 * Those calendar fields can be defined in the following order:
101 * year > month > date > hour (in day) > minute > second
102 *
103 * The largest different calendar fields between 2 calendars is the
104 * first different calendar field in above order.
105 *
106 * For example: the largest different calendar fields between "Jan 10, 2007"
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}".
113 *
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.
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;.
127 *
128 * For date skeleton, the interval patterns when year, or month, or date is
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,
136 * the interval pattern will falls back to the interval "fallback" pattern
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>
142 * For the combination of date and time,
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
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"
152 * </li>
153 * <li>
154 * otherwise, present the date followed by the range expression
155 * for the time.
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"
158 * </li>
159 * </ol>
160 *
161 *
162 * <P>
163 * If two dates are the same, the interval pattern is the single date pattern.
164 * For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is
165 * "Jan 10, 2007".
166 *
167 * Or if the presenting fields between 2 dates have the exact same values,
168 * the interval pattern is the single date pattern.
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>
173 * DateIntervalFormat needs the following information for correct
174 * formatting: time zone, calendar type, pattern, date format symbols,
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.
180 * Users are encouraged to created date interval formatter this way and
181 * to use the pre-defined skeleton macros, such as
182 * UDAT_YEAR_NUM_MONTH, which consists the calendar fields and
183 * the format style.
184 * </li>
185 * <li>
186 * create an instance using default or given locale plus given skeleton
187 * plus a given DateIntervalInfo.
188 * This factory method is for powerful users who want to provide their own
189 * interval patterns.
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.
200 *
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(
210 * UDAT_YEAR_MONTH_DAY,
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
221 class U_I18N_API DateIntervalFormat : public Format {
222 public:
223
224 /**
225 * Construct a DateIntervalFormat from skeleton and the default locale.
226 *
227 * This is a convenient override of
228 * createInstance(const UnicodeString& skeleton, const Locale& locale,
229 * UErrorCode&)
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.
235 * @stable ICU 4.0
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>
244 * In this factory method,
245 * the date interval pattern information is load from resource files.
246 * Users are encouraged to created date interval formatter this way and
247 * to use the pre-defined skeleton macros.
248 *
249 * <P>
250 * There are pre-defined skeletons (defined in udate.h) having predefined
251 * interval patterns in resource files.
252 * Users are encouraged to use those macros.
253 * For example:
254 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
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,
263 * @param skeleton the skeleton on which the interval format is based.
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.
267 * @stable ICU 4.0
268 * <p>
269 * <h4>Sample code</h4>
270 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
271 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined
272 * <p>
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
285 * createInstance(const UnicodeString& skeleton, const Locale& locale,
286 * const DateIntervalInfo& dtitvinf, UErrorCode&)
287 * with the locale value as default locale.
288 *
289 * @param skeleton the skeleton on which interval format based.
290 * @param dtitvinf the DateIntervalInfo object.
291 * @param status output param set to success/failure code on exit
292 * @return a date time interval formatter which the caller owns.
293 * @stable ICU 4.0
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
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.
309 * <P>
310 * There are pre-defined skeletons (defined in udate.h) having predefined
311 * interval patterns in resource files.
312 * Users are encouraged to use those macros.
313 * For example:
314 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
315 *
316 * The DateIntervalInfo provides the interval patterns.
317 * and the DateIntervalInfo ownership remains to the caller.
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
326 * "{date0} - {date1}"
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.
333 * @stable ICU 4.0
334 * <p>
335 * <h4>Sample code</h4>
336 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
337 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtCustomized
338 * <p>
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.
348 * @stable ICU 4.0
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.
356 * @stable ICU 4.0
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.
365 * @stable ICU 4.0
366 */
367 virtual UBool operator==(const Format& other) const;
368
369 /**
370 * Return true if the given Format objects are not semantically equal.
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.
374 * @stable ICU 4.0
375 */
376 UBool operator!=(const Format& other) const;
377
378
379 using Format::format;
380
381 /**
382 * Format an object to produce a string. This method handles Formattable
383 * objects with a DateInterval type.
384 * If a the Formattable object type is not a DateInterval,
385 * then it returns a failing UErrorCode.
386 *
387 * @param obj The object to format.
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.
393 * @param status Output param filled with success/failure status.
394 * @return Reference to 'appendTo' parameter.
395 * @stable ICU 4.0
396 */
397 virtual UnicodeString& format(const Formattable& obj,
398 UnicodeString& appendTo,
399 FieldPosition& fieldPosition,
400 UErrorCode& status) const ;
401
402
403
404 /**
405 * Format a DateInterval to produce a string.
406 *
407 * @param dtInterval DateInterval to be formatted.
408 * @param appendTo Output parameter to receive result.
409 * Result is appended to existing contents.
410 * @param fieldPosition On input: an alignment field, if desired.
411 * On output: the offsets of the alignment field.
412 * @param status Output param filled with success/failure status.
413 * @return Reference to 'appendTo' parameter.
414 * @stable ICU 4.0
415 */
416 UnicodeString& format(const DateInterval* dtInterval,
417 UnicodeString& appendTo,
418 FieldPosition& fieldPosition,
419 UErrorCode& status) const ;
420
421
422 /**
423 * Format 2 Calendars to produce a string.
424 *
425 * Note: "fromCalendar" and "toCalendar" are not const,
426 * since calendar is not const in SimpleDateFormat::format(Calendar&),
427 *
428 * @param fromCalendar calendar set to the from date in date interval
429 * to be formatted into date interval string
430 * @param toCalendar calendar set to the to date in date interval
431 * to be formatted into date interval string
432 * @param appendTo Output parameter to receive result.
433 * Result is appended to existing contents.
434 * @param fieldPosition On input: an alignment field, if desired.
435 * On output: the offsets of the alignment field.
436 * @param status Output param filled with success/failure status.
437 * Caller needs to make sure it is SUCCESS
438 * at the function entrance
439 * @return Reference to 'appendTo' parameter.
440 * @stable ICU 4.0
441 */
442 UnicodeString& format(Calendar& fromCalendar,
443 Calendar& toCalendar,
444 UnicodeString& appendTo,
445 FieldPosition& fieldPosition,
446 UErrorCode& status) const ;
447
448 /**
449 * Date interval parsing is not supported. Please do not use.
450 * <P>
451 * This method should handle parsing of
452 * date time interval strings into Formattable objects with
453 * DateInterval type, which is a pair of UDate.
454 * <P>
455 * Before calling, set parse_pos.index to the offset you want to start
456 * parsing at in the source. After calling, parse_pos.index is the end of
457 * the text you parsed. If error occurs, index is unchanged.
458 * <P>
459 * When parsing, leading whitespace is discarded (with a successful parse),
460 * while trailing whitespace is left as is.
461 * <P>
462 * See Format::parseObject() for more.
463 *
464 * @param source The string to be parsed into an object.
465 * @param result Formattable to be set to the parse result.
466 * If parse fails, return contents are undefined.
467 * @param parse_pos The position to start parsing at. Since no parsing
468 * is supported, upon return this param is unchanged.
469 * @return A newly created Formattable* object, or NULL
470 * on failure. The caller owns this and should
471 * delete it when done.
472 * @internal ICU 4.0
473 */
474 virtual void parseObject(const UnicodeString& source,
475 Formattable& result,
476 ParsePosition& parse_pos) const;
477
478
479 /**
480 * Gets the date time interval patterns.
481 * @return the date time interval patterns associated with
482 * this date interval formatter.
483 * @stable ICU 4.0
484 */
485 const DateIntervalInfo* getDateIntervalInfo(void) const;
486
487
488 /**
489 * Set the date time interval patterns.
490 * @param newIntervalPatterns the given interval patterns to copy.
491 * @param status output param set to success/failure code on exit
492 * @stable ICU 4.0
493 */
494 void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns,
495 UErrorCode& status);
496
497
498 /**
499 * Gets the date formatter
500 * @return the date formatter associated with this date interval formatter.
501 * @stable ICU 4.0
502 */
503 const DateFormat* getDateFormat(void) const;
504
505 /**
506 * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar.
507 * @return the time zone associated with the calendar of DateIntervalFormat.
508 * @stable ICU 4.8
509 */
510 virtual const TimeZone& getTimeZone(void) const;
511
512 /**
513 * Sets the time zone for the calendar used by this DateIntervalFormat object. The
514 * caller no longer owns the TimeZone object and should not delete it after this call.
515 * @param zoneToAdopt the TimeZone to be adopted.
516 * @stable ICU 4.8
517 */
518 virtual void adoptTimeZone(TimeZone* zoneToAdopt);
519
520 /**
521 * Sets the time zone for the calendar used by this DateIntervalFormat object.
522 * @param zone the new time zone.
523 * @stable ICU 4.8
524 */
525 virtual void setTimeZone(const TimeZone& zone);
526
527 /**
528 * Change attributes for the DateIntervalFormat object.
529 * @param attr
530 * The attribute to change.
531 * @param value
532 * The new value for the attribute.
533 * @param status
534 * A UErrorCode to receive any errors.
535 * @internal
536 */
537 virtual void setAttribute(UDateIntervalFormatAttribute attr,
538 UDateIntervalFormatAttributeValue value,
539 UErrorCode &status);
540
541 /**
542 * Return the class ID for this class. This is useful only for comparing to
543 * a return value from getDynamicClassID(). For example:
544 * <pre>
545 * . Base* polymorphic_pointer = createPolymorphicObject();
546 * . if (polymorphic_pointer->getDynamicClassID() ==
547 * . erived::getStaticClassID()) ...
548 * </pre>
549 * @return The class ID for all objects of this class.
550 * @stable ICU 4.0
551 */
552 static UClassID U_EXPORT2 getStaticClassID(void);
553
554 /**
555 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
556 * method is to implement a simple version of RTTI, since not all C++
557 * compilers support genuine RTTI. Polymorphic operator==() and clone()
558 * methods call this method.
559 *
560 * @return The class ID for this object. All objects of a
561 * given class have the same class ID. Objects of
562 * other classes have different class IDs.
563 * @stable ICU 4.0
564 */
565 virtual UClassID getDynamicClassID(void) const;
566
567 protected:
568
569 /**
570 * Copy constructor.
571 * @stable ICU 4.0
572 */
573 DateIntervalFormat(const DateIntervalFormat&);
574
575 /**
576 * Assignment operator.
577 * @stable ICU 4.0
578 */
579 DateIntervalFormat& operator=(const DateIntervalFormat&);
580
581 private:
582
583 /*
584 * This is for ICU internal use only. Please do not use.
585 * Save the interval pattern information.
586 * Interval pattern consists of 2 single date patterns and the separator.
587 * For example, interval pattern "MMM d - MMM d, yyyy" consists
588 * a single date pattern "MMM d", another single date pattern "MMM d, yyyy",
589 * and a separator "-".
590 * The pattern is divided into 2 parts. For above example,
591 * the first part is "MMM d - ", and the second part is "MMM d, yyyy".
592 * Also, the first date appears in an interval pattern could be
593 * the earlier date or the later date.
594 * And such information is saved in the interval pattern as well.
595 */
596 struct PatternInfo {
597 UnicodeString firstPart;
598 UnicodeString secondPart;
599 /**
600 * Whether the first date in interval pattern is later date or not.
601 * Fallback format set the default ordering.
602 * And for a particular interval pattern, the order can be
603 * overriden by prefixing the interval pattern with "latestFirst:" or
604 * "earliestFirst:"
605 * For example, given 2 date, Jan 10, 2007 to Feb 10, 2007.
606 * if the fallback format is "{0} - {1}",
607 * and the pattern is "d MMM - d MMM yyyy", the interval format is
608 * "10 Jan - 10 Feb, 2007".
609 * If the pattern is "latestFirst:d MMM - d MMM yyyy",
610 * the interval format is "10 Feb - 10 Jan, 2007"
611 */
612 UBool laterDateFirst;
613 };
614
615
616 /**
617 * default constructor
618 * @internal ICU 4.0
619 */
620 DateIntervalFormat();
621
622 /**
623 * Construct a DateIntervalFormat from DateFormat,
624 * a DateIntervalInfo, and skeleton.
625 * DateFormat provides the timezone, calendar,
626 * full pattern, and date format symbols information.
627 * It should be a SimpleDateFormat object which
628 * has a pattern in it.
629 * the DateIntervalInfo provides the interval patterns.
630 *
631 * Note: the DateIntervalFormat takes ownership of both
632 * DateFormat and DateIntervalInfo objects.
633 * Caller should not delete them.
634 *
635 * @param locale the locale of this date interval formatter.
636 * @param dtItvInfo the DateIntervalInfo object to be adopted.
637 * @param skeleton the skeleton of the date formatter
638 * @param status output param set to success/failure code on exit
639 */
640 DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo,
641 const UnicodeString* skeleton, UErrorCode& status);
642
643
644 /**
645 * Construct a DateIntervalFormat from DateFormat
646 * and a DateIntervalInfo.
647 *
648 * It is a wrapper of the constructor.
649 *
650 * @param locale the locale of this date interval formatter.
651 * @param dtitvinf the DateIntervalInfo object to be adopted.
652 * @param skeleton the skeleton of this formatter.
653 * @param status Output param set to success/failure code.
654 * @return a date time interval formatter which the caller owns.
655 */
656 static DateIntervalFormat* U_EXPORT2 create(const Locale& locale,
657 DateIntervalInfo* dtitvinf,
658 const UnicodeString* skeleton,
659 UErrorCode& status);
660
661 /**
662 * Create a simple date/time formatter from skeleton, given locale,
663 * and date time pattern generator.
664 *
665 * @param skeleton the skeleton on which date format based.
666 * @param locale the given locale.
667 * @param dtpng the date time pattern generator.
668 * @param status Output param to be set to success/failure code.
669 * If it is failure, the returned date formatter will
670 * be NULL.
671 * @return a simple date formatter which the caller owns.
672 */
673 static SimpleDateFormat* U_EXPORT2 createSDFPatternInstance(
674 const UnicodeString& skeleton,
675 const Locale& locale,
676 DateTimePatternGenerator* dtpng,
677 UErrorCode& status);
678
679
680 /**
681 * Below are for generating interval patterns local to the formatter
682 */
683
684 /**
685 * @param combiningPattern xxx
686 * @param pat0 xxx
687 * @param pos0 xxx
688 * @param pat1 xxx
689 * @param pos1 xxx
690 * @param posResult xxx
691 */
692 static void
693 adjustPosition(UnicodeString& combiningPattern, // has {0} and {1} in it
694 UnicodeString& pat0, FieldPosition& pos0, // pattern and pos corresponding to {0}
695 UnicodeString& pat1, FieldPosition& pos1, // pattern and pos corresponding to {1}
696 FieldPosition& posResult);
697
698
699
700 /**
701 * Format 2 Calendars using fall-back interval pattern
702 *
703 * The full pattern used in this fall-back format is the
704 * full pattern of the date formatter.
705 *
706 * @param fromCalendar calendar set to the from date in date interval
707 * to be formatted into date interval string
708 * @param toCalendar calendar set to the to date in date interval
709 * to be formatted into date interval string
710 * @param fromToOnSameDay TRUE iff from and to dates are on the same day
711 * (any difference is in ampm/hours or below)
712 * @param appendTo Output parameter to receive result.
713 * Result is appended to existing contents.
714 * @param pos On input: an alignment field, if desired.
715 * On output: the offsets of the alignment field.
716 * @param status output param set to success/failure code on exit
717 * @return Reference to 'appendTo' parameter.
718 */
719 UnicodeString& fallbackFormat(Calendar& fromCalendar,
720 Calendar& toCalendar,
721 UBool fromToOnSameDay,
722 UnicodeString& appendTo,
723 FieldPosition& pos,
724 UErrorCode& status) const;
725
726
727
728 /**
729 * Initialize interval patterns locale to this formatter
730 *
731 * This code is a bit complicated since
732 * 1. the interval patterns saved in resource bundle files are interval
733 * patterns based on date or time only.
734 * It does not have interval patterns based on both date and time.
735 * Interval patterns on both date and time are algorithm generated.
736 *
737 * For example, it has interval patterns on skeleton "dMy" and "hm",
738 * but it does not have interval patterns on skeleton "dMyhm".
739 *
740 * The rule to generate interval patterns for both date and time skeleton are
741 * 1) when the year, month, or day differs, concatenate the two original
742 * expressions with a separator between,
743 * For example, interval pattern from "Jan 10, 2007 10:10 am"
744 * to "Jan 11, 2007 10:10am" is
745 * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
746 *
747 * 2) otherwise, present the date followed by the range expression
748 * for the time.
749 * For example, interval pattern from "Jan 10, 2007 10:10 am"
750 * to "Jan 10, 2007 11:10am" is
751 * "Jan 10, 2007 10:10 am - 11:10am"
752 *
753 * 2. even a pattern does not request a certain calendar field,
754 * the interval pattern needs to include such field if such fields are
755 * different between 2 dates.
756 * For example, a pattern/skeleton is "hm", but the interval pattern
757 * includes year, month, and date when year, month, and date differs.
758 *
759 *
760 * @param status output param set to success/failure code on exit
761 */
762 void initializePattern(UErrorCode& status);
763
764
765
766 /**
767 * Set fall back interval pattern given a calendar field,
768 * a skeleton, and a date time pattern generator.
769 * @param field the largest different calendar field
770 * @param skeleton a skeleton
771 * @param status output param set to success/failure code on exit
772 */
773 void setFallbackPattern(UCalendarDateFields field,
774 const UnicodeString& skeleton,
775 UErrorCode& status);
776
777
778
779 /**
780 * get separated date and time skeleton from a combined skeleton.
781 *
782 * The difference between date skeleton and normalizedDateSkeleton are:
783 * 1. both 'y' and 'd' are appeared only once in normalizeDateSkeleton
784 * 2. 'E' and 'EE' are normalized into 'EEE'
785 * 3. 'MM' is normalized into 'M'
786 *
787 ** the difference between time skeleton and normalizedTimeSkeleton are:
788 * 1. both 'H' and 'h' are normalized as 'h' in normalized time skeleton,
789 * 2. 'a' is omitted in normalized time skeleton.
790 * 3. there is only one appearance for 'h', 'm','v', 'z' in normalized time
791 * skeleton
792 *
793 *
794 * @param skeleton given combined skeleton.
795 * @param date Output parameter for date only skeleton.
796 * @param normalizedDate Output parameter for normalized date only
797 *
798 * @param time Output parameter for time only skeleton.
799 * @param normalizedTime Output parameter for normalized time only
800 * skeleton.
801 *
802 */
803 static void U_EXPORT2 getDateTimeSkeleton(const UnicodeString& skeleton,
804 UnicodeString& date,
805 UnicodeString& normalizedDate,
806 UnicodeString& time,
807 UnicodeString& normalizedTime);
808
809
810
811 /**
812 * Generate date or time interval pattern from resource,
813 * and set them into the interval pattern locale to this formatter.
814 *
815 * It needs to handle the following:
816 * 1. need to adjust field width.
817 * For example, the interval patterns saved in DateIntervalInfo
818 * includes "dMMMy", but not "dMMMMy".
819 * Need to get interval patterns for dMMMMy from dMMMy.
820 * Another example, the interval patterns saved in DateIntervalInfo
821 * includes "hmv", but not "hmz".
822 * Need to get interval patterns for "hmz' from 'hmv'
823 *
824 * 2. there might be no pattern for 'y' differ for skeleton "Md",
825 * in order to get interval patterns for 'y' differ,
826 * need to look for it from skeleton 'yMd'
827 *
828 * @param dateSkeleton normalized date skeleton
829 * @param timeSkeleton normalized time skeleton
830 * @return whether the resource is found for the skeleton.
831 * TRUE if interval pattern found for the skeleton,
832 * FALSE otherwise.
833 */
834 UBool setSeparateDateTimePtn(const UnicodeString& dateSkeleton,
835 const UnicodeString& timeSkeleton);
836
837
838
839
840 /**
841 * Generate interval pattern from existing resource
842 *
843 * It not only save the interval patterns,
844 * but also return the extended skeleton and its best match skeleton.
845 *
846 * @param field largest different calendar field
847 * @param skeleton skeleton
848 * @param bestSkeleton the best match skeleton which has interval pattern
849 * defined in resource
850 * @param differenceInfo the difference between skeleton and best skeleton
851 * 0 means the best matched skeleton is the same as input skeleton
852 * 1 means the fields are the same, but field width are different
853 * 2 means the only difference between fields are v/z,
854 * -1 means there are other fields difference
855 *
856 * @param extendedSkeleton extended skeleton
857 * @param extendedBestSkeleton extended best match skeleton
858 * @return whether the interval pattern is found
859 * through extending skeleton or not.
860 * TRUE if interval pattern is found by
861 * extending skeleton, FALSE otherwise.
862 */
863 UBool setIntervalPattern(UCalendarDateFields field,
864 const UnicodeString* skeleton,
865 const UnicodeString* bestSkeleton,
866 int8_t differenceInfo,
867 UnicodeString* extendedSkeleton = NULL,
868 UnicodeString* extendedBestSkeleton = NULL);
869
870 /**
871 * Adjust field width in best match interval pattern to match
872 * the field width in input skeleton.
873 *
874 * TODO (xji) make a general solution
875 * The adjusting rule can be:
876 * 1. always adjust
877 * 2. never adjust
878 * 3. default adjust, which means adjust according to the following rules
879 * 3.1 always adjust string, such as MMM and MMMM
880 * 3.2 never adjust between string and numeric, such as MM and MMM
881 * 3.3 always adjust year
882 * 3.4 do not adjust 'd', 'h', or 'm' if h presents
883 * 3.5 do not adjust 'M' if it is numeric(?)
884 *
885 * Since date interval format is well-formed format,
886 * date and time skeletons are normalized previously,
887 * till this stage, the adjust here is only "adjust strings, such as MMM
888 * and MMMM, EEE and EEEE.
889 *
890 * @param inputSkeleton the input skeleton
891 * @param bestMatchSkeleton the best match skeleton
892 * @param bestMatchIntervalPattern the best match interval pattern
893 * @param differenceInfo the difference between 2 skeletons
894 * 1 means only field width differs
895 * 2 means v/z exchange
896 * @param adjustedIntervalPattern adjusted interval pattern
897 */
898 static void U_EXPORT2 adjustFieldWidth(
899 const UnicodeString& inputSkeleton,
900 const UnicodeString& bestMatchSkeleton,
901 const UnicodeString& bestMatchIntervalPattern,
902 int8_t differenceInfo,
903 UnicodeString& adjustedIntervalPattern);
904
905 /**
906 * Concat a single date pattern with a time interval pattern,
907 * set it into the intervalPatterns, while field is time field.
908 * This is used to handle time interval patterns on skeleton with
909 * both time and date. Present the date followed by
910 * the range expression for the time.
911 * @param format date and time format
912 * @param datePattern date pattern
913 * @param field time calendar field: AM_PM, HOUR, MINUTE
914 * @param status output param set to success/failure code on exit
915 */
916 void concatSingleDate2TimeInterval(UnicodeString& format,
917 const UnicodeString& datePattern,
918 UCalendarDateFields field,
919 UErrorCode& status);
920
921 /**
922 * check whether a calendar field present in a skeleton.
923 * @param field calendar field need to check
924 * @param skeleton given skeleton on which to check the calendar field
925 * @return true if field present in a skeleton.
926 */
927 static UBool U_EXPORT2 fieldExistsInSkeleton(UCalendarDateFields field,
928 const UnicodeString& skeleton);
929
930
931 /**
932 * Split interval patterns into 2 part.
933 * @param intervalPattern interval pattern
934 * @return the index in interval pattern which split the pattern into 2 part
935 */
936 static int32_t U_EXPORT2 splitPatternInto2Part(const UnicodeString& intervalPattern);
937
938
939 /**
940 * Break interval patterns as 2 part and save them into pattern info.
941 * @param field calendar field
942 * @param intervalPattern interval pattern
943 */
944 void setIntervalPattern(UCalendarDateFields field,
945 const UnicodeString& intervalPattern);
946
947
948 /**
949 * Break interval patterns as 2 part and save them into pattern info.
950 * @param field calendar field
951 * @param intervalPattern interval pattern
952 * @param laterDateFirst whether later date appear first in interval pattern
953 */
954 void setIntervalPattern(UCalendarDateFields field,
955 const UnicodeString& intervalPattern,
956 UBool laterDateFirst);
957
958
959 /**
960 * Set pattern information.
961 *
962 * @param field calendar field
963 * @param firstPart the first part in interval pattern
964 * @param secondPart the second part in interval pattern
965 * @param laterDateFirst whether the first date in intervalPattern
966 * is earlier date or later date
967 */
968 void setPatternInfo(UCalendarDateFields field,
969 const UnicodeString* firstPart,
970 const UnicodeString* secondPart,
971 UBool laterDateFirst);
972
973
974 // from calendar field to pattern letter
975 static const UChar fgCalendarFieldToPatternLetter[];
976
977
978 /**
979 * The interval patterns for this locale.
980 */
981 DateIntervalInfo* fInfo;
982
983 /**
984 * The DateFormat object used to format single pattern
985 */
986 SimpleDateFormat* fDateFormat;
987
988 /**
989 * The 2 calendars with the from and to date.
990 * could re-use the calendar in fDateFormat,
991 * but keeping 2 calendars make it clear and clean.
992 */
993 Calendar* fFromCalendar;
994 Calendar* fToCalendar;
995
996 /**
997 * Date time pattern generator
998 */
999 DateTimePatternGenerator* fDtpng;
1000
1001 /**
1002 * Following are interval information relevant (locale) to this formatter.
1003 */
1004 UnicodeString fSkeleton;
1005 PatternInfo fIntervalPatterns[DateIntervalInfo::kIPI_MAX_INDEX];
1006
1007 /**
1008 * Patterns for fallback formatting.
1009 */
1010 UnicodeString* fDatePattern;
1011 UnicodeString* fTimePattern;
1012 UnicodeString* fDateTimeFormat;
1013
1014 /**
1015 * Atttributes
1016 */
1017 int32_t fMinimizeType;
1018 };
1019
1020 inline UBool
1021 DateIntervalFormat::operator!=(const Format& other) const {
1022 return !operator==(other);
1023 }
1024
1025 U_NAMESPACE_END
1026
1027 #endif /* #if !UCONFIG_NO_FORMATTING */
1028
1029 #endif // _DTITVFMT_H__
1030 //eof