1 /********************************************************************************
2 * Copyright (C) 2008-2013,2015, 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, minute, and second
98 * (though we do not currently have specific intervalFormat date for skeletons
100 * Those calendar fields can be defined in the following order:
101 * year > month > date > hour (in day) > minute > second
103 * The largest different calendar fields between 2 calendars is the
104 * first different calendar field in above order.
106 * For example: the largest different calendar fields between "Jan 10, 2007"
107 * and "Feb 20, 2008" is year.
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}".
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 "yMMMd",
119 * in en_US, if the largest different calendar field between date1 and date2
120 * is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy",
121 * such as "Jan 10, 2007 - Jan 10, 2008".
122 * If the largest different calendar field between date1 and date2 is "month",
123 * the date interval pattern is "MMM d - MMM d, yyyy",
124 * such as "Jan 10 - Feb 10, 2007".
125 * If the largest different calendar field between date1 and date2 is "day",
126 * the date interval pattern is "MMM d-d, yyyy", such as "Jan 10-20, 2007".
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.
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}".
142 * For the combination of date and time,
143 * The rule to generate interval patterns are:
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"
154 * otherwise, present the date followed by the range expression
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"
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
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".
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:
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
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
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.
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
202 * Code Sample: general usage
205 * // the date interval object which the DateIntervalFormat formats on
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;
215 * dtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status);
216 * delete dtIntervalFmt;
221 class U_I18N_API DateIntervalFormat
: public Format
{
225 * Construct a DateIntervalFormat from skeleton and the default locale.
227 * This is a convenient override of
228 * createInstance(const UnicodeString& skeleton, const Locale& locale,
230 * with the value of locale as default locale.
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.
237 static DateIntervalFormat
* U_EXPORT2
createInstance(
238 const UnicodeString
& skeleton
,
242 * Construct a DateIntervalFormat from skeleton and a given locale.
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.
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.
254 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
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.
269 * <h4>Sample code</h4>
270 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
271 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined
275 static DateIntervalFormat
* U_EXPORT2
createInstance(
276 const UnicodeString
& skeleton
,
277 const Locale
& locale
,
281 * Construct a DateIntervalFormat from skeleton
282 * DateIntervalInfo, and default locale.
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.
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.
295 static DateIntervalFormat
* U_EXPORT2
createInstance(
296 const UnicodeString
& skeleton
,
297 const DateIntervalInfo
& dtitvinf
,
301 * Construct a DateIntervalFormat from skeleton
302 * a DateIntervalInfo, and the given locale.
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
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.
314 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
316 * The DateIntervalInfo provides the interval patterns.
317 * and the DateIntervalInfo ownership remains to the caller.
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}"
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.
335 * <h4>Sample code</h4>
336 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
337 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtCustomized
340 static DateIntervalFormat
* U_EXPORT2
createInstance(
341 const UnicodeString
& skeleton
,
342 const Locale
& locale
,
343 const DateIntervalInfo
& dtitvinf
,
350 virtual ~DateIntervalFormat();
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.
358 virtual Format
* clone(void) const;
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.
367 virtual UBool
operator==(const Format
& other
) const;
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.
376 UBool
operator!=(const Format
& other
) const;
379 using Format::format
;
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.
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.
397 virtual UnicodeString
& format(const Formattable
& obj
,
398 UnicodeString
& appendTo
,
399 FieldPosition
& fieldPosition
,
400 UErrorCode
& status
) const ;
405 * Format a DateInterval to produce a string.
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.
416 UnicodeString
& format(const DateInterval
* dtInterval
,
417 UnicodeString
& appendTo
,
418 FieldPosition
& fieldPosition
,
419 UErrorCode
& status
) const ;
423 * Format 2 Calendars to produce a string.
425 * Note: "fromCalendar" and "toCalendar" are not const,
426 * since calendar is not const in SimpleDateFormat::format(Calendar&),
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.
442 UnicodeString
& format(Calendar
& fromCalendar
,
443 Calendar
& toCalendar
,
444 UnicodeString
& appendTo
,
445 FieldPosition
& fieldPosition
,
446 UErrorCode
& status
) const ;
449 * Date interval parsing is not supported. Please do not use.
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.
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.
459 * When parsing, leading whitespace is discarded (with a successful parse),
460 * while trailing whitespace is left as is.
462 * See Format::parseObject() for more.
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.
474 virtual void parseObject(const UnicodeString
& source
,
476 ParsePosition
& parse_pos
) const;
480 * Gets the date time interval patterns.
481 * @return the date time interval patterns associated with
482 * this date interval formatter.
485 const DateIntervalInfo
* getDateIntervalInfo(void) const;
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
494 void setDateIntervalInfo(const DateIntervalInfo
& newIntervalPatterns
,
499 * Gets the date formatter
500 * @return the date formatter associated with this date interval formatter.
503 const DateFormat
* getDateFormat(void) const;
506 * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar.
507 * @return the time zone associated with the calendar of DateIntervalFormat.
510 virtual const TimeZone
& getTimeZone(void) const;
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.
518 virtual void adoptTimeZone(TimeZone
* zoneToAdopt
);
521 * Sets the time zone for the calendar used by this DateIntervalFormat object.
522 * @param zone the new time zone.
525 virtual void setTimeZone(const TimeZone
& zone
);
528 * Change attributes for the DateIntervalFormat object.
530 * The attribute to change.
532 * The new value for the attribute.
534 * A UErrorCode to receive any errors.
537 virtual void setAttribute(UDateIntervalFormatAttribute attr
,
538 UDateIntervalFormatAttributeValue value
,
542 * Return the class ID for this class. This is useful only for comparing to
543 * a return value from getDynamicClassID(). For example:
545 * . Base* polymorphic_pointer = createPolymorphicObject();
546 * . if (polymorphic_pointer->getDynamicClassID() ==
547 * . erived::getStaticClassID()) ...
549 * @return The class ID for all objects of this class.
552 static UClassID U_EXPORT2
getStaticClassID(void);
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.
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.
565 virtual UClassID
getDynamicClassID(void) const;
573 DateIntervalFormat(const DateIntervalFormat
&);
576 * Assignment operator.
579 DateIntervalFormat
& operator=(const DateIntervalFormat
&);
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.
597 UnicodeString firstPart
;
598 UnicodeString secondPart
;
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
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"
612 UBool laterDateFirst
;
617 * default constructor
620 DateIntervalFormat();
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.
631 * Note: the DateIntervalFormat takes ownership of both
632 * DateFormat and DateIntervalInfo objects.
633 * Caller should not delete them.
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
640 DateIntervalFormat(const Locale
& locale
, DateIntervalInfo
* dtItvInfo
,
641 const UnicodeString
* skeleton
, UErrorCode
& status
);
645 * Construct a DateIntervalFormat from DateFormat
646 * and a DateIntervalInfo.
648 * It is a wrapper of the constructor.
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.
656 static DateIntervalFormat
* U_EXPORT2
create(const Locale
& locale
,
657 DateIntervalInfo
* dtitvinf
,
658 const UnicodeString
* skeleton
,
662 * Create a simple date/time formatter from skeleton, given locale,
663 * and date time pattern generator.
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
671 * @return a simple date formatter which the caller owns.
673 static SimpleDateFormat
* U_EXPORT2
createSDFPatternInstance(
674 const UnicodeString
& skeleton
,
675 const Locale
& locale
,
676 DateTimePatternGenerator
* dtpng
,
681 * Below are for generating interval patterns local to the formatter
685 * @param combiningPattern xxx
690 * @param posResult xxx
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
);
701 * Format 2 Calendars using fall-back interval pattern
703 * The full pattern used in this fall-back format is the
704 * full pattern of the date formatter.
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.
719 UnicodeString
& fallbackFormat(Calendar
& fromCalendar
,
720 Calendar
& toCalendar
,
721 UBool fromToOnSameDay
,
722 UnicodeString
& appendTo
,
724 UErrorCode
& status
) const;
729 * Initialize interval patterns locale to this formatter
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.
737 * For example, it has interval patterns on skeleton "dMy" and "hm",
738 * but it does not have interval patterns on skeleton "dMyhm".
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"
747 * 2) otherwise, present the date followed by the range expression
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"
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.
760 * @param status output param set to success/failure code on exit
762 void initializePattern(UErrorCode
& status
);
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
773 void setFallbackPattern(UCalendarDateFields field
,
774 const UnicodeString
& skeleton
,
780 * get separated date and time skeleton from a combined skeleton.
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'
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
794 * @param skeleton given combined skeleton.
795 * @param date Output parameter for date only skeleton.
796 * @param normalizedDate Output parameter for normalized date only
798 * @param time Output parameter for time only skeleton.
799 * @param normalizedTime Output parameter for normalized time only
803 static void U_EXPORT2
getDateTimeSkeleton(const UnicodeString
& skeleton
,
805 UnicodeString
& normalizedDate
,
807 UnicodeString
& normalizedTime
);
812 * Generate date or time interval pattern from resource,
813 * and set them into the interval pattern locale to this formatter.
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'
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'
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,
834 UBool
setSeparateDateTimePtn(const UnicodeString
& dateSkeleton
,
835 const UnicodeString
& timeSkeleton
);
841 * Generate interval pattern from existing resource
843 * It not only save the interval patterns,
844 * but also return the extended skeleton and its best match skeleton.
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
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.
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
);
871 * Adjust field width in best match interval pattern to match
872 * the field width in input skeleton.
874 * TODO (xji) make a general solution
875 * The adjusting rule can be:
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(?)
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.
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
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
);
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
916 void concatSingleDate2TimeInterval(UnicodeString
& format
,
917 const UnicodeString
& datePattern
,
918 UCalendarDateFields field
,
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.
927 static UBool U_EXPORT2
fieldExistsInSkeleton(UCalendarDateFields field
,
928 const UnicodeString
& skeleton
);
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
936 static int32_t U_EXPORT2
splitPatternInto2Part(const UnicodeString
& intervalPattern
);
940 * Break interval patterns as 2 part and save them into pattern info.
941 * @param field calendar field
942 * @param intervalPattern interval pattern
944 void setIntervalPattern(UCalendarDateFields field
,
945 const UnicodeString
& intervalPattern
);
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
954 void setIntervalPattern(UCalendarDateFields field
,
955 const UnicodeString
& intervalPattern
,
956 UBool laterDateFirst
);
960 * Set pattern information.
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
968 void setPatternInfo(UCalendarDateFields field
,
969 const UnicodeString
* firstPart
,
970 const UnicodeString
* secondPart
,
971 UBool laterDateFirst
);
974 // from calendar field to pattern letter
975 static const UChar fgCalendarFieldToPatternLetter
[];
979 * The interval patterns for this locale.
981 DateIntervalInfo
* fInfo
;
984 * The DateFormat object used to format single pattern
986 SimpleDateFormat
* fDateFormat
;
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.
993 Calendar
* fFromCalendar
;
994 Calendar
* fToCalendar
;
997 * Date time pattern generator
999 DateTimePatternGenerator
* fDtpng
;
1002 * Following are interval information relevant (locale) to this formatter.
1004 UnicodeString fSkeleton
;
1005 PatternInfo fIntervalPatterns
[DateIntervalInfo::kIPI_MAX_INDEX
];
1008 * Patterns for fallback formatting.
1010 UnicodeString
* fDatePattern
;
1011 UnicodeString
* fTimePattern
;
1012 UnicodeString
* fDateTimeFormat
;
1017 int32_t fMinimizeType
;
1021 DateIntervalFormat::operator!=(const Format
& other
) const {
1022 return !operator==(other
);
1027 #endif /* #if !UCONFIG_NO_FORMATTING */
1029 #endif // _DTITVFMT_H__