1 /********************************************************************************
2 * Copyright (C) 2008-2012, International Business Machines Corporation and
3 * others. All Rights Reserved.
4 *******************************************************************************
8 *******************************************************************************
11 #ifndef __DTITVFMT_H__
12 #define __DTITVFMT_H__
15 #include "unicode/utypes.h"
19 * \brief C++ API: Format and parse date interval in a language-independent manner.
22 #if !UCONFIG_NO_FORMATTING
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"
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.
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.
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.
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.
67 * Logically, the interval patterns are mappings
68 * from (skeleton, the_largest_different_calendar_field)
69 * to (date_interval_pattern).
75 * only keeps the field pattern letter and ignores all other parts
76 * in a pattern, such as space, punctuations, and string literals.
79 * hides the order of fields.
82 * might hide a field's pattern letter length.
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.
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.
96 * The calendar fields we support for interval formatting are:
97 * year, month, date, day-of-week, am-pm, hour, hour-of-day, and minute.
98 * Those calendar fields can be defined in the following order:
99 * year > month > date > hour (in day) > minute
101 * The largest different calendar fields between 2 calendars is the
102 * first different calendar field in above order.
104 * For example: the largest different calendar fields between "Jan 10, 2007"
105 * and "Feb 20, 2008" is year.
108 * For other calendar fields, the compact interval formatting is not
109 * supported. And the interval format will be fall back to fall-back
110 * patterns, which is mostly "{date0} - {date1}".
113 * There is a set of pre-defined static skeleton strings.
114 * There are pre-defined interval patterns for those pre-defined skeletons
115 * in locales' resource files.
116 * For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is "yMMMd",
117 * in en_US, if the largest different calendar field between date1 and date2
118 * is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy",
119 * such as "Jan 10, 2007 - Jan 10, 2008".
120 * If the largest different calendar field between date1 and date2 is "month",
121 * the date interval pattern is "MMM d - MMM d, yyyy",
122 * such as "Jan 10 - Feb 10, 2007".
123 * If the largest different calendar field between date1 and date2 is "day",
124 * the date interval pattern is "MMM d-d, yyyy", such as "Jan 10-20, 2007".
126 * For date skeleton, the interval patterns when year, or month, or date is
127 * different are defined in resource files.
128 * For time skeleton, the interval patterns when am/pm, or hour, or minute is
129 * different are defined in resource files.
132 * If a skeleton is not found in a locale's DateIntervalInfo, which means
133 * the interval patterns for the skeleton is not defined in resource file,
134 * the interval pattern will falls back to the interval "fallback" pattern
135 * defined in resource file.
136 * If the interval "fallback" pattern is not defined, the default fall-back
137 * is "{date0} - {data1}".
140 * For the combination of date and time,
141 * The rule to generate interval patterns are:
144 * when the year, month, or day differs, falls back to fall-back
145 * interval pattern, which mostly is the concatenate the two original
146 * expressions with a separator between,
147 * For example, interval pattern from "Jan 10, 2007 10:10 am"
148 * to "Jan 11, 2007 10:10am" is
149 * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
152 * otherwise, present the date followed by the range expression
154 * For example, interval pattern from "Jan 10, 2007 10:10 am"
155 * to "Jan 10, 2007 11:10am" is "Jan 10, 2007 10:10 am - 11:10am"
161 * If two dates are the same, the interval pattern is the single date pattern.
162 * For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is
165 * Or if the presenting fields between 2 dates have the exact same values,
166 * the interval pattern is the single date pattern.
167 * For example, if user only requests year and month,
168 * the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007".
171 * DateIntervalFormat needs the following information for correct
172 * formatting: time zone, calendar type, pattern, date format symbols,
173 * and date interval patterns.
174 * It can be instantiated in 2 ways:
177 * create an instance using default or given locale plus given skeleton.
178 * Users are encouraged to created date interval formatter this way and
179 * to use the pre-defined skeleton macros, such as
180 * UDAT_YEAR_NUM_MONTH, which consists the calendar fields and
184 * create an instance using default or given locale plus given skeleton
185 * plus a given DateIntervalInfo.
186 * This factory method is for powerful users who want to provide their own
188 * Locale provides the timezone, calendar, and format symbols information.
189 * Local plus skeleton provides full pattern information.
190 * DateIntervalInfo provides the date interval patterns.
195 * For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc.
196 * DateIntervalFormat uses the same syntax as that of
200 * Code Sample: general usage
203 * // the date interval object which the DateIntervalFormat formats on
205 * DateInterval* dtInterval = new DateInterval(1000*3600*24, 1000*3600*24*2);
206 * UErrorCode status = U_ZERO_ERROR;
207 * DateIntervalFormat* dtIntervalFmt = DateIntervalFormat::createInstance(
208 * UDAT_YEAR_MONTH_DAY,
209 * Locale("en", "GB", ""), status);
210 * UnicodeUnicodeString dateIntervalString;
211 * FieldPosition pos = 0;
213 * dtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status);
214 * delete dtIntervalFmt;
219 class U_I18N_API DateIntervalFormat
: public Format
{
223 * Construct a DateIntervalFormat from skeleton and the default locale.
225 * This is a convenient override of
226 * createInstance(const UnicodeString& skeleton, const Locale& locale,
228 * with the value of locale as default locale.
230 * @param skeleton the skeleton on which interval format based.
231 * @param status output param set to success/failure code on exit
232 * @return a date time interval formatter which the caller owns.
235 static DateIntervalFormat
* U_EXPORT2
createInstance(
236 const UnicodeString
& skeleton
,
240 * Construct a DateIntervalFormat from skeleton and a given locale.
242 * In this factory method,
243 * the date interval pattern information is load from resource files.
244 * Users are encouraged to created date interval formatter this way and
245 * to use the pre-defined skeleton macros.
248 * There are pre-defined skeletons (defined in udate.h) having predefined
249 * interval patterns in resource files.
250 * Users are encouraged to use those macros.
252 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
254 * The given Locale provides the interval patterns.
255 * For example, for en_GB, if skeleton is UDAT_YEAR_ABBR_MONTH_WEEKDAY_DAY,
256 * which is "yMMMEEEd",
257 * the interval patterns defined in resource file to above skeleton are:
258 * "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs,
259 * "EEE, d MMM - EEE, d MMM, yyyy" for month differs,
260 * "EEE, d - EEE, d MMM, yyyy" for day differs,
261 * @param skeleton the skeleton on which the interval format is based.
262 * @param locale the given locale
263 * @param status output param set to success/failure code on exit
264 * @return a date time interval formatter which the caller owns.
268 static DateIntervalFormat
* U_EXPORT2
createInstance(
269 const UnicodeString
& skeleton
,
270 const Locale
& locale
,
274 * Construct a DateIntervalFormat from skeleton
275 * DateIntervalInfo, and default locale.
277 * This is a convenient override of
278 * createInstance(const UnicodeString& skeleton, const Locale& locale,
279 * const DateIntervalInfo& dtitvinf, UErrorCode&)
280 * with the locale value as default locale.
282 * @param skeleton the skeleton on which interval format based.
283 * @param dtitvinf the DateIntervalInfo object.
284 * @param status output param set to success/failure code on exit
285 * @return a date time interval formatter which the caller owns.
288 static DateIntervalFormat
* U_EXPORT2
createInstance(
289 const UnicodeString
& skeleton
,
290 const DateIntervalInfo
& dtitvinf
,
294 * Construct a DateIntervalFormat from skeleton
295 * a DateIntervalInfo, and the given locale.
298 * In this factory method, user provides its own date interval pattern
299 * information, instead of using those pre-defined data in resource file.
300 * This factory method is for powerful users who want to provide their own
303 * There are pre-defined skeletons (defined in udate.h) having predefined
304 * interval patterns in resource files.
305 * Users are encouraged to use those macros.
307 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
309 * The DateIntervalInfo provides the interval patterns.
310 * and the DateIntervalInfo ownership remains to the caller.
312 * User are encouraged to set default interval pattern in DateIntervalInfo
313 * as well, if they want to set other interval patterns ( instead of
314 * reading the interval patterns from resource files).
315 * When the corresponding interval pattern for a largest calendar different
316 * field is not found ( if user not set it ), interval format fallback to
317 * the default interval pattern.
318 * If user does not provide default interval pattern, it fallback to
319 * "{date0} - {date1}"
321 * @param skeleton the skeleton on which interval format based.
322 * @param locale the given locale
323 * @param dtitvinf the DateIntervalInfo object.
324 * @param status output param set to success/failure code on exit
325 * @return a date time interval formatter which the caller owns.
328 static DateIntervalFormat
* U_EXPORT2
createInstance(
329 const UnicodeString
& skeleton
,
330 const Locale
& locale
,
331 const DateIntervalInfo
& dtitvinf
,
338 virtual ~DateIntervalFormat();
341 * Clone this Format object polymorphically. The caller owns the result and
342 * should delete it when done.
343 * @return A copy of the object.
346 virtual Format
* clone(void) const;
349 * Return true if the given Format objects are semantically equal. Objects
350 * of different subclasses are considered unequal.
351 * @param other the object to be compared with.
352 * @return true if the given Format objects are semantically equal.
355 virtual UBool
operator==(const Format
& other
) const;
358 * Return true if the given Format objects are not semantically equal.
359 * Objects of different subclasses are considered unequal.
360 * @param other the object to be compared with.
361 * @return true if the given Format objects are not semantically equal.
364 UBool
operator!=(const Format
& other
) const;
367 using Format::format
;
370 * Format an object to produce a string. This method handles Formattable
371 * objects with a DateInterval type.
372 * If a the Formattable object type is not a DateInterval,
373 * then it returns a failing UErrorCode.
375 * @param obj The object to format.
376 * Must be a DateInterval.
377 * @param appendTo Output parameter to receive result.
378 * Result is appended to existing contents.
379 * @param fieldPosition On input: an alignment field, if desired.
380 * On output: the offsets of the alignment field.
381 * @param status Output param filled with success/failure status.
382 * @return Reference to 'appendTo' parameter.
385 virtual UnicodeString
& format(const Formattable
& obj
,
386 UnicodeString
& appendTo
,
387 FieldPosition
& fieldPosition
,
388 UErrorCode
& status
) const ;
393 * Format a DateInterval to produce a string.
395 * @param dtInterval DateInterval to be formatted.
396 * @param appendTo Output parameter to receive result.
397 * Result is appended to existing contents.
398 * @param fieldPosition On input: an alignment field, if desired.
399 * On output: the offsets of the alignment field.
400 * @param status Output param filled with success/failure status.
401 * @return Reference to 'appendTo' parameter.
404 UnicodeString
& format(const DateInterval
* dtInterval
,
405 UnicodeString
& appendTo
,
406 FieldPosition
& fieldPosition
,
407 UErrorCode
& status
) const ;
411 * Format 2 Calendars to produce a string.
413 * Note: "fromCalendar" and "toCalendar" are not const,
414 * since calendar is not const in SimpleDateFormat::format(Calendar&),
416 * @param fromCalendar calendar set to the from date in date interval
417 * to be formatted into date interval string
418 * @param toCalendar calendar set to the to date in date interval
419 * to be formatted into date interval string
420 * @param appendTo Output parameter to receive result.
421 * Result is appended to existing contents.
422 * @param fieldPosition On input: an alignment field, if desired.
423 * On output: the offsets of the alignment field.
424 * @param status Output param filled with success/failure status.
425 * Caller needs to make sure it is SUCCESS
426 * at the function entrance
427 * @return Reference to 'appendTo' parameter.
430 UnicodeString
& format(Calendar
& fromCalendar
,
431 Calendar
& toCalendar
,
432 UnicodeString
& appendTo
,
433 FieldPosition
& fieldPosition
,
434 UErrorCode
& status
) const ;
437 * Date interval parsing is not supported. Please do not use.
439 * This method should handle parsing of
440 * date time interval strings into Formattable objects with
441 * DateInterval type, which is a pair of UDate.
443 * Before calling, set parse_pos.index to the offset you want to start
444 * parsing at in the source. After calling, parse_pos.index is the end of
445 * the text you parsed. If error occurs, index is unchanged.
447 * When parsing, leading whitespace is discarded (with a successful parse),
448 * while trailing whitespace is left as is.
450 * See Format::parseObject() for more.
452 * @param source The string to be parsed into an object.
453 * @param result Formattable to be set to the parse result.
454 * If parse fails, return contents are undefined.
455 * @param parse_pos The position to start parsing at. Since no parsing
456 * is supported, upon return this param is unchanged.
457 * @return A newly created Formattable* object, or NULL
458 * on failure. The caller owns this and should
459 * delete it when done.
462 virtual void parseObject(const UnicodeString
& source
,
464 ParsePosition
& parse_pos
) const;
468 * Gets the date time interval patterns.
469 * @return the date time interval patterns associated with
470 * this date interval formatter.
473 const DateIntervalInfo
* getDateIntervalInfo(void) const;
477 * Set the date time interval patterns.
478 * @param newIntervalPatterns the given interval patterns to copy.
479 * @param status output param set to success/failure code on exit
482 void setDateIntervalInfo(const DateIntervalInfo
& newIntervalPatterns
,
487 * Gets the date formatter
488 * @return the date formatter associated with this date interval formatter.
491 const DateFormat
* getDateFormat(void) const;
494 * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar.
495 * @return the time zone associated with the calendar of DateIntervalFormat.
498 virtual const TimeZone
& getTimeZone(void) const;
501 * Sets the time zone for the calendar used by this DateIntervalFormat object. The
502 * caller no longer owns the TimeZone object and should not delete it after this call.
503 * @param zoneToAdopt the TimeZone to be adopted.
506 virtual void adoptTimeZone(TimeZone
* zoneToAdopt
);
509 * Sets the time zone for the calendar used by this DateIntervalFormat object.
510 * @param zone the new time zone.
513 virtual void setTimeZone(const TimeZone
& zone
);
516 * Change attributes for the DateIntervalFormat object.
518 * The attribute to change.
520 * The new value for the attribute.
522 * A UErrorCode to receive any errors.
525 virtual void setAttribute(UDateIntervalFormatAttribute attr
,
526 UDateIntervalFormatAttributeValue value
,
530 * Return the class ID for this class. This is useful only for comparing to
531 * a return value from getDynamicClassID(). For example:
533 * . Base* polymorphic_pointer = createPolymorphicObject();
534 * . if (polymorphic_pointer->getDynamicClassID() ==
535 * . erived::getStaticClassID()) ...
537 * @return The class ID for all objects of this class.
540 static UClassID U_EXPORT2
getStaticClassID(void);
543 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
544 * method is to implement a simple version of RTTI, since not all C++
545 * compilers support genuine RTTI. Polymorphic operator==() and clone()
546 * methods call this method.
548 * @return The class ID for this object. All objects of a
549 * given class have the same class ID. Objects of
550 * other classes have different class IDs.
553 virtual UClassID
getDynamicClassID(void) const;
561 DateIntervalFormat(const DateIntervalFormat
&);
564 * Assignment operator.
567 DateIntervalFormat
& operator=(const DateIntervalFormat
&);
572 * This is for ICU internal use only. Please do not use.
573 * Save the interval pattern information.
574 * Interval pattern consists of 2 single date patterns and the separator.
575 * For example, interval pattern "MMM d - MMM d, yyyy" consists
576 * a single date pattern "MMM d", another single date pattern "MMM d, yyyy",
577 * and a separator "-".
578 * The pattern is divided into 2 parts. For above example,
579 * the first part is "MMM d - ", and the second part is "MMM d, yyyy".
580 * Also, the first date appears in an interval pattern could be
581 * the earlier date or the later date.
582 * And such information is saved in the interval pattern as well.
586 UnicodeString firstPart
;
587 UnicodeString secondPart
;
589 * Whether the first date in interval pattern is later date or not.
590 * Fallback format set the default ordering.
591 * And for a particular interval pattern, the order can be
592 * overriden by prefixing the interval pattern with "latestFirst:" or
594 * For example, given 2 date, Jan 10, 2007 to Feb 10, 2007.
595 * if the fallback format is "{0} - {1}",
596 * and the pattern is "d MMM - d MMM yyyy", the interval format is
597 * "10 Jan - 10 Feb, 2007".
598 * If the pattern is "latestFirst:d MMM - d MMM yyyy",
599 * the interval format is "10 Feb - 10 Jan, 2007"
601 UBool laterDateFirst
;
606 * default constructor
609 DateIntervalFormat();
612 * Construct a DateIntervalFormat from DateFormat,
613 * a DateIntervalInfo, and skeleton.
614 * DateFormat provides the timezone, calendar,
615 * full pattern, and date format symbols information.
616 * It should be a SimpleDateFormat object which
617 * has a pattern in it.
618 * the DateIntervalInfo provides the interval patterns.
620 * Note: the DateIntervalFormat takes ownership of both
621 * DateFormat and DateIntervalInfo objects.
622 * Caller should not delete them.
624 * @param locale the locale of this date interval formatter.
625 * @param dtItvInfo the DateIntervalInfo object to be adopted.
626 * @param skeleton the skeleton of the date formatter
627 * @param status output param set to success/failure code on exit
630 DateIntervalFormat(const Locale
& locale
, DateIntervalInfo
* dtItvInfo
,
631 const UnicodeString
* skeleton
, UErrorCode
& status
);
635 * Construct a DateIntervalFormat from DateFormat
636 * and a DateIntervalInfo.
638 * It is a wrapper of the constructor.
640 * @param locale the locale of this date interval formatter.
641 * @param dtitvinf the DateIntervalInfo object to be adopted.
642 * @param skeleton the skeleton of this formatter.
643 * @param status Output param set to success/failure code.
644 * @return a date time interval formatter which the caller owns.
647 static DateIntervalFormat
* U_EXPORT2
create(const Locale
& locale
,
648 DateIntervalInfo
* dtitvinf
,
649 const UnicodeString
* skeleton
,
653 * Create a simple date/time formatter from skeleton, given locale,
654 * and date time pattern generator.
656 * @param skeleton the skeleton on which date format based.
657 * @param locale the given locale.
658 * @param dtpng the date time pattern generator.
659 * @param status Output param to be set to success/failure code.
660 * If it is failure, the returned date formatter will
662 * @return a simple date formatter which the caller owns.
665 static SimpleDateFormat
* U_EXPORT2
createSDFPatternInstance(
666 const UnicodeString
& skeleton
,
667 const Locale
& locale
,
668 DateTimePatternGenerator
* dtpng
,
673 * Below are for generating interval patterns local to the formatter
678 * Format 2 Calendars using fall-back interval pattern
680 * The full pattern used in this fall-back format is the
681 * full pattern of the date formatter.
683 * @param fromCalendar calendar set to the from date in date interval
684 * to be formatted into date interval string
685 * @param toCalendar calendar set to the to date in date interval
686 * to be formatted into date interval string
687 * @param appendTo Output parameter to receive result.
688 * Result is appended to existing contents.
689 * @param pos On input: an alignment field, if desired.
690 * On output: the offsets of the alignment field.
691 * @param status output param set to success/failure code on exit
692 * @return Reference to 'appendTo' parameter.
695 UnicodeString
& fallbackFormat(Calendar
& fromCalendar
,
696 Calendar
& toCalendar
,
697 UnicodeString
& appendTo
,
699 UErrorCode
& status
) const;
704 * Initialize interval patterns locale to this formatter
706 * This code is a bit complicated since
707 * 1. the interval patterns saved in resource bundle files are interval
708 * patterns based on date or time only.
709 * It does not have interval patterns based on both date and time.
710 * Interval patterns on both date and time are algorithm generated.
712 * For example, it has interval patterns on skeleton "dMy" and "hm",
713 * but it does not have interval patterns on skeleton "dMyhm".
715 * The rule to generate interval patterns for both date and time skeleton are
716 * 1) when the year, month, or day differs, concatenate the two original
717 * expressions with a separator between,
718 * For example, interval pattern from "Jan 10, 2007 10:10 am"
719 * to "Jan 11, 2007 10:10am" is
720 * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
722 * 2) otherwise, present the date followed by the range expression
724 * For example, interval pattern from "Jan 10, 2007 10:10 am"
725 * to "Jan 10, 2007 11:10am" is
726 * "Jan 10, 2007 10:10 am - 11:10am"
728 * 2. even a pattern does not request a certain calendar field,
729 * the interval pattern needs to include such field if such fields are
730 * different between 2 dates.
731 * For example, a pattern/skeleton is "hm", but the interval pattern
732 * includes year, month, and date when year, month, and date differs.
735 * @param status output param set to success/failure code on exit
738 void initializePattern(UErrorCode
& status
);
743 * Set fall back interval pattern given a calendar field,
744 * a skeleton, and a date time pattern generator.
745 * @param field the largest different calendar field
746 * @param skeleton a skeleton
747 * @param status output param set to success/failure code on exit
750 void setFallbackPattern(UCalendarDateFields field
,
751 const UnicodeString
& skeleton
,
757 * get separated date and time skeleton from a combined skeleton.
759 * The difference between date skeleton and normalizedDateSkeleton are:
760 * 1. both 'y' and 'd' are appeared only once in normalizeDateSkeleton
761 * 2. 'E' and 'EE' are normalized into 'EEE'
762 * 3. 'MM' is normalized into 'M'
764 ** the difference between time skeleton and normalizedTimeSkeleton are:
765 * 1. both 'H' and 'h' are normalized as 'h' in normalized time skeleton,
766 * 2. 'a' is omitted in normalized time skeleton.
767 * 3. there is only one appearance for 'h', 'm','v', 'z' in normalized time
771 * @param skeleton given combined skeleton.
772 * @param date Output parameter for date only skeleton.
773 * @param normalizedDate Output parameter for normalized date only
775 * @param time Output parameter for time only skeleton.
776 * @param normalizedTime Output parameter for normalized time only
781 static void U_EXPORT2
getDateTimeSkeleton(const UnicodeString
& skeleton
,
783 UnicodeString
& normalizedDate
,
785 UnicodeString
& normalizedTime
);
790 * Generate date or time interval pattern from resource,
791 * and set them into the interval pattern locale to this formatter.
793 * It needs to handle the following:
794 * 1. need to adjust field width.
795 * For example, the interval patterns saved in DateIntervalInfo
796 * includes "dMMMy", but not "dMMMMy".
797 * Need to get interval patterns for dMMMMy from dMMMy.
798 * Another example, the interval patterns saved in DateIntervalInfo
799 * includes "hmv", but not "hmz".
800 * Need to get interval patterns for "hmz' from 'hmv'
802 * 2. there might be no pattern for 'y' differ for skeleton "Md",
803 * in order to get interval patterns for 'y' differ,
804 * need to look for it from skeleton 'yMd'
806 * @param dateSkeleton normalized date skeleton
807 * @param timeSkeleton normalized time skeleton
808 * @return whether the resource is found for the skeleton.
809 * TRUE if interval pattern found for the skeleton,
813 UBool
setSeparateDateTimePtn(const UnicodeString
& dateSkeleton
,
814 const UnicodeString
& timeSkeleton
);
820 * Generate interval pattern from existing resource
822 * It not only save the interval patterns,
823 * but also return the extended skeleton and its best match skeleton.
825 * @param field largest different calendar field
826 * @param skeleton skeleton
827 * @param bestSkeleton the best match skeleton which has interval pattern
828 * defined in resource
829 * @param differenceInfo the difference between skeleton and best skeleton
830 * 0 means the best matched skeleton is the same as input skeleton
831 * 1 means the fields are the same, but field width are different
832 * 2 means the only difference between fields are v/z,
833 * -1 means there are other fields difference
835 * @param extendedSkeleton extended skeleton
836 * @param extendedBestSkeleton extended best match skeleton
837 * @return whether the interval pattern is found
838 * through extending skeleton or not.
839 * TRUE if interval pattern is found by
840 * extending skeleton, FALSE otherwise.
843 UBool
setIntervalPattern(UCalendarDateFields field
,
844 const UnicodeString
* skeleton
,
845 const UnicodeString
* bestSkeleton
,
846 int8_t differenceInfo
,
847 UnicodeString
* extendedSkeleton
= NULL
,
848 UnicodeString
* extendedBestSkeleton
= NULL
);
851 * Adjust field width in best match interval pattern to match
852 * the field width in input skeleton.
854 * TODO (xji) make a general solution
855 * The adjusting rule can be:
858 * 3. default adjust, which means adjust according to the following rules
859 * 3.1 always adjust string, such as MMM and MMMM
860 * 3.2 never adjust between string and numeric, such as MM and MMM
861 * 3.3 always adjust year
862 * 3.4 do not adjust 'd', 'h', or 'm' if h presents
863 * 3.5 do not adjust 'M' if it is numeric(?)
865 * Since date interval format is well-formed format,
866 * date and time skeletons are normalized previously,
867 * till this stage, the adjust here is only "adjust strings, such as MMM
868 * and MMMM, EEE and EEEE.
870 * @param inputSkeleton the input skeleton
871 * @param bestMatchSkeleton the best match skeleton
872 * @param bestMatchIntervalPattern the best match interval pattern
873 * @param differenceInfo the difference between 2 skeletons
874 * 1 means only field width differs
875 * 2 means v/z exchange
876 * @param adjustedIntervalPattern adjusted interval pattern
879 static void U_EXPORT2
adjustFieldWidth(
880 const UnicodeString
& inputSkeleton
,
881 const UnicodeString
& bestMatchSkeleton
,
882 const UnicodeString
& bestMatchIntervalPattern
,
883 int8_t differenceInfo
,
884 UnicodeString
& adjustedIntervalPattern
);
887 * Concat a single date pattern with a time interval pattern,
888 * set it into the intervalPatterns, while field is time field.
889 * This is used to handle time interval patterns on skeleton with
890 * both time and date. Present the date followed by
891 * the range expression for the time.
892 * @param format date and time format
893 * @param formatLen format string length
894 * @param datePattern date pattern
895 * @param field time calendar field: AM_PM, HOUR, MINUTE
896 * @param status output param set to success/failure code on exit
899 void concatSingleDate2TimeInterval(const UChar
* format
,
901 const UnicodeString
& datePattern
,
902 UCalendarDateFields field
,
906 * check whether a calendar field present in a skeleton.
907 * @param field calendar field need to check
908 * @param skeleton given skeleton on which to check the calendar field
909 * @return true if field present in a skeleton.
912 static UBool U_EXPORT2
fieldExistsInSkeleton(UCalendarDateFields field
,
913 const UnicodeString
& skeleton
);
917 * Split interval patterns into 2 part.
918 * @param intervalPattern interval pattern
919 * @return the index in interval pattern which split the pattern into 2 part
922 static int32_t U_EXPORT2
splitPatternInto2Part(const UnicodeString
& intervalPattern
);
926 * Break interval patterns as 2 part and save them into pattern info.
927 * @param field calendar field
928 * @param intervalPattern interval pattern
931 void setIntervalPattern(UCalendarDateFields field
,
932 const UnicodeString
& intervalPattern
);
936 * Break interval patterns as 2 part and save them into pattern info.
937 * @param field calendar field
938 * @param intervalPattern interval pattern
939 * @param laterDateFirst whether later date appear first in interval pattern
942 void setIntervalPattern(UCalendarDateFields field
,
943 const UnicodeString
& intervalPattern
,
944 UBool laterDateFirst
);
948 * Set pattern information.
950 * @param field calendar field
951 * @param firstPart the first part in interval pattern
952 * @param secondPart the second part in interval pattern
953 * @param laterDateFirst whether the first date in intervalPattern
954 * is earlier date or later date
957 void setPatternInfo(UCalendarDateFields field
,
958 const UnicodeString
* firstPart
,
959 const UnicodeString
* secondPart
,
960 UBool laterDateFirst
);
963 // from calendar field to pattern letter
964 static const UChar fgCalendarFieldToPatternLetter
[];
968 * The interval patterns for this locale.
970 DateIntervalInfo
* fInfo
;
973 * The DateFormat object used to format single pattern
975 SimpleDateFormat
* fDateFormat
;
978 * The 2 calendars with the from and to date.
979 * could re-use the calendar in fDateFormat,
980 * but keeping 2 calendars make it clear and clean.
982 Calendar
* fFromCalendar
;
983 Calendar
* fToCalendar
;
986 * Date time pattern generator
988 DateTimePatternGenerator
* fDtpng
;
991 * Following are interval information relevant (locale) to this formatter.
993 UnicodeString fSkeleton
;
994 PatternInfo fIntervalPatterns
[DateIntervalInfo::kIPI_MAX_INDEX
];
999 int32_t fMinimizeType
;
1003 DateIntervalFormat::operator!=(const Format
& other
) const {
1004 return !operator==(other
);
1009 #endif /* #if !UCONFIG_NO_FORMATTING */
1011 #endif // _DTITVFMT_H__