1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************************
4 * Copyright (C) 2008-2016, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 *******************************************************************************
10 *******************************************************************************
13 #ifndef __DTITVFMT_H__
14 #define __DTITVFMT_H__
17 #include "unicode/utypes.h"
19 #if U_SHOW_CPLUSPLUS_API
23 * \brief C++ API: Format and parse date interval in a language-independent manner.
26 #if !UCONFIG_NO_FORMATTING
28 #include "unicode/ucal.h"
29 #include "unicode/smpdtfmt.h"
30 #include "unicode/dtintrv.h"
31 #include "unicode/dtitvinf.h"
32 #include "unicode/dtptngen.h"
33 #include "unicode/formattedvalue.h"
34 #include "unicode/udisplaycontext.h"
36 #include "unicode/udateintervalformat.h"
41 class FormattedDateIntervalData
;
42 class DateIntervalFormat
;
44 #ifndef U_HIDE_DRAFT_API
46 * An immutable class containing the result of a date interval formatting operation.
48 * Instances of this class are immutable and thread-safe.
50 * When calling nextPosition():
51 * The fields are returned from left to right. The special field category
52 * UFIELD_CATEGORY_DATE_INTERVAL_SPAN is used to indicate which datetime
53 * primitives came from which arguments: 0 means fromCalendar, and 1 means
54 * toCalendar. The span category will always occur before the
55 * corresponding fields in UFIELD_CATEGORY_DATE
56 * in the nextPosition() iterator.
58 * Not intended for public subclassing.
62 class U_I18N_API FormattedDateInterval
: public UMemory
, public FormattedValue
{
65 * Default constructor; makes an empty FormattedDateInterval.
68 FormattedDateInterval() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR
) {}
71 * Move constructor: Leaves the source FormattedDateInterval in an undefined state.
74 FormattedDateInterval(FormattedDateInterval
&& src
) U_NOEXCEPT
;
77 * Destruct an instance of FormattedDateInterval.
80 virtual ~FormattedDateInterval() U_OVERRIDE
;
82 /** Copying not supported; use move constructor instead. */
83 FormattedDateInterval(const FormattedDateInterval
&) = delete;
85 /** Copying not supported; use move assignment instead. */
86 FormattedDateInterval
& operator=(const FormattedDateInterval
&) = delete;
89 * Move assignment: Leaves the source FormattedDateInterval in an undefined state.
92 FormattedDateInterval
& operator=(FormattedDateInterval
&& src
) U_NOEXCEPT
;
94 /** @copydoc FormattedValue::toString() */
95 UnicodeString
toString(UErrorCode
& status
) const U_OVERRIDE
;
97 /** @copydoc FormattedValue::toTempString() */
98 UnicodeString
toTempString(UErrorCode
& status
) const U_OVERRIDE
;
100 /** @copydoc FormattedValue::appendTo() */
101 Appendable
&appendTo(Appendable
& appendable
, UErrorCode
& status
) const U_OVERRIDE
;
103 /** @copydoc FormattedValue::nextPosition() */
104 UBool
nextPosition(ConstrainedFieldPosition
& cfpos
, UErrorCode
& status
) const U_OVERRIDE
;
107 FormattedDateIntervalData
*fData
;
108 UErrorCode fErrorCode
;
109 explicit FormattedDateInterval(FormattedDateIntervalData
*results
)
110 : fData(results
), fErrorCode(U_ZERO_ERROR
) {}
111 explicit FormattedDateInterval(UErrorCode errorCode
)
112 : fData(nullptr), fErrorCode(errorCode
) {}
113 friend class DateIntervalFormat
;
115 #endif /* U_HIDE_DRAFT_API */
119 * DateIntervalFormat is a class for formatting and parsing date
120 * intervals in a language-independent manner.
121 * Only formatting is supported, parsing is not supported.
124 * Date interval means from one date to another date,
125 * for example, from "Jan 11, 2008" to "Jan 18, 2008".
126 * We introduced class DateInterval to represent it.
127 * DateInterval is a pair of UDate, which is
128 * the standard milliseconds since 24:00 GMT, Jan 1, 1970.
131 * DateIntervalFormat formats a DateInterval into
132 * text as compactly as possible.
133 * For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008"
134 * is "Jan 11-18, 2008" for English.
135 * And it parses text into DateInterval,
136 * although initially, parsing is not supported.
139 * There is no structural information in date time patterns.
140 * For any punctuations and string literals inside a date time pattern,
141 * we do not know whether it is just a separator, or a prefix, or a suffix.
142 * Without such information, so, it is difficult to generate a sub-pattern
143 * (or super-pattern) by algorithm.
144 * So, formatting a DateInterval is pattern-driven. It is very
145 * similar to formatting in SimpleDateFormat.
146 * We introduce class DateIntervalInfo to save date interval
147 * patterns, similar to date time pattern in SimpleDateFormat.
150 * Logically, the interval patterns are mappings
151 * from (skeleton, the_largest_different_calendar_field)
152 * to (date_interval_pattern).
158 * only keeps the field pattern letter and ignores all other parts
159 * in a pattern, such as space, punctuations, and string literals.
162 * hides the order of fields.
165 * might hide a field's pattern letter length.
169 * For those non-digit calendar fields, the pattern letter length is
170 * important, such as MMM, MMMM, and MMMMM; EEE and EEEE,
171 * and the field's pattern letter length is honored.
173 * For the digit calendar fields, such as M or MM, d or dd, yy or yyyy,
174 * the field pattern length is ignored and the best match, which is defined
175 * in date time patterns, will be returned without honor the field pattern
176 * letter length in skeleton.
179 * The calendar fields we support for interval formatting are:
180 * year, month, date, day-of-week, am-pm, hour, hour-of-day, minute, and second
181 * (though we do not currently have specific intervalFormat date for skeletons
183 * Those calendar fields can be defined in the following order:
184 * year > month > date > hour (in day) > minute > second
186 * The largest different calendar fields between 2 calendars is the
187 * first different calendar field in above order.
189 * For example: the largest different calendar fields between "Jan 10, 2007"
190 * and "Feb 20, 2008" is year.
193 * For other calendar fields, the compact interval formatting is not
194 * supported. And the interval format will be fall back to fall-back
195 * patterns, which is mostly "{date0} - {date1}".
198 * There is a set of pre-defined static skeleton strings.
199 * There are pre-defined interval patterns for those pre-defined skeletons
200 * in locales' resource files.
201 * For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is "yMMMd",
202 * in en_US, if the largest different calendar field between date1 and date2
203 * is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy",
204 * such as "Jan 10, 2007 - Jan 10, 2008".
205 * If the largest different calendar field between date1 and date2 is "month",
206 * the date interval pattern is "MMM d - MMM d, yyyy",
207 * such as "Jan 10 - Feb 10, 2007".
208 * If the largest different calendar field between date1 and date2 is "day",
209 * the date interval pattern is "MMM d-d, yyyy", such as "Jan 10-20, 2007".
211 * For date skeleton, the interval patterns when year, or month, or date is
212 * different are defined in resource files.
213 * For time skeleton, the interval patterns when am/pm, or hour, or minute is
214 * different are defined in resource files.
217 * If a skeleton is not found in a locale's DateIntervalInfo, which means
218 * the interval patterns for the skeleton is not defined in resource file,
219 * the interval pattern will falls back to the interval "fallback" pattern
220 * defined in resource file.
221 * If the interval "fallback" pattern is not defined, the default fall-back
222 * is "{date0} - {data1}".
225 * For the combination of date and time,
226 * The rule to generate interval patterns are:
229 * when the year, month, or day differs, falls back to fall-back
230 * interval pattern, which mostly is the concatenate the two original
231 * expressions with a separator between,
232 * For example, interval pattern from "Jan 10, 2007 10:10 am"
233 * to "Jan 11, 2007 10:10am" is
234 * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
237 * otherwise, present the date followed by the range expression
239 * For example, interval pattern from "Jan 10, 2007 10:10 am"
240 * to "Jan 10, 2007 11:10am" is "Jan 10, 2007 10:10 am - 11:10am"
246 * If two dates are the same, the interval pattern is the single date pattern.
247 * For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is
250 * Or if the presenting fields between 2 dates have the exact same values,
251 * the interval pattern is the single date pattern.
252 * For example, if user only requests year and month,
253 * the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007".
256 * DateIntervalFormat needs the following information for correct
257 * formatting: time zone, calendar type, pattern, date format symbols,
258 * and date interval patterns.
259 * It can be instantiated in 2 ways:
262 * create an instance using default or given locale plus given skeleton.
263 * Users are encouraged to created date interval formatter this way and
264 * to use the pre-defined skeleton macros, such as
265 * UDAT_YEAR_NUM_MONTH, which consists the calendar fields and
269 * create an instance using default or given locale plus given skeleton
270 * plus a given DateIntervalInfo.
271 * This factory method is for powerful users who want to provide their own
273 * Locale provides the timezone, calendar, and format symbols information.
274 * Local plus skeleton provides full pattern information.
275 * DateIntervalInfo provides the date interval patterns.
280 * For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc.
281 * DateIntervalFormat uses the same syntax as that of
285 * Code Sample: general usage
288 * // the date interval object which the DateIntervalFormat formats on
290 * DateInterval* dtInterval = new DateInterval(1000*3600*24, 1000*3600*24*2);
291 * UErrorCode status = U_ZERO_ERROR;
292 * DateIntervalFormat* dtIntervalFmt = DateIntervalFormat::createInstance(
293 * UDAT_YEAR_MONTH_DAY,
294 * Locale("en", "GB", ""), status);
295 * UnicodeUnicodeString dateIntervalString;
296 * FieldPosition pos = 0;
298 * dtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status);
299 * delete dtIntervalFmt;
303 class U_I18N_API DateIntervalFormat
: public Format
{
307 * Construct a DateIntervalFormat from skeleton and the default locale.
309 * This is a convenient override of
310 * createInstance(const UnicodeString& skeleton, const Locale& locale,
312 * with the value of locale as default locale.
314 * @param skeleton the skeleton on which interval format based.
315 * @param status output param set to success/failure code on exit
316 * @return a date time interval formatter which the caller owns.
319 static DateIntervalFormat
* U_EXPORT2
createInstance(
320 const UnicodeString
& skeleton
,
324 * Construct a DateIntervalFormat from skeleton and a given locale.
326 * In this factory method,
327 * the date interval pattern information is load from resource files.
328 * Users are encouraged to created date interval formatter this way and
329 * to use the pre-defined skeleton macros.
332 * There are pre-defined skeletons (defined in udate.h) having predefined
333 * interval patterns in resource files.
334 * Users are encouraged to use those macros.
336 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
338 * The given Locale provides the interval patterns.
339 * For example, for en_GB, if skeleton is UDAT_YEAR_ABBR_MONTH_WEEKDAY_DAY,
340 * which is "yMMMEEEd",
341 * the interval patterns defined in resource file to above skeleton are:
342 * "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs,
343 * "EEE, d MMM - EEE, d MMM, yyyy" for month differs,
344 * "EEE, d - EEE, d MMM, yyyy" for day differs,
345 * @param skeleton the skeleton on which the interval format is based.
346 * @param locale the given locale
347 * @param status output param set to success/failure code on exit
348 * @return a date time interval formatter which the caller owns.
351 * <h4>Sample code</h4>
352 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
353 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined
357 static DateIntervalFormat
* U_EXPORT2
createInstance(
358 const UnicodeString
& skeleton
,
359 const Locale
& locale
,
363 * Construct a DateIntervalFormat from skeleton
364 * DateIntervalInfo, and default locale.
366 * This is a convenient override of
367 * createInstance(const UnicodeString& skeleton, const Locale& locale,
368 * const DateIntervalInfo& dtitvinf, UErrorCode&)
369 * with the locale value as default locale.
371 * @param skeleton the skeleton on which interval format based.
372 * @param dtitvinf the DateIntervalInfo object.
373 * @param status output param set to success/failure code on exit
374 * @return a date time interval formatter which the caller owns.
377 static DateIntervalFormat
* U_EXPORT2
createInstance(
378 const UnicodeString
& skeleton
,
379 const DateIntervalInfo
& dtitvinf
,
383 * Construct a DateIntervalFormat from skeleton
384 * a DateIntervalInfo, and the given locale.
387 * In this factory method, user provides its own date interval pattern
388 * information, instead of using those pre-defined data in resource file.
389 * This factory method is for powerful users who want to provide their own
392 * There are pre-defined skeletons (defined in udate.h) having predefined
393 * interval patterns in resource files.
394 * Users are encouraged to use those macros.
396 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
398 * The DateIntervalInfo provides the interval patterns.
399 * and the DateIntervalInfo ownership remains to the caller.
401 * User are encouraged to set default interval pattern in DateIntervalInfo
402 * as well, if they want to set other interval patterns ( instead of
403 * reading the interval patterns from resource files).
404 * When the corresponding interval pattern for a largest calendar different
405 * field is not found ( if user not set it ), interval format fallback to
406 * the default interval pattern.
407 * If user does not provide default interval pattern, it fallback to
408 * "{date0} - {date1}"
410 * @param skeleton the skeleton on which interval format based.
411 * @param locale the given locale
412 * @param dtitvinf the DateIntervalInfo object.
413 * @param status output param set to success/failure code on exit
414 * @return a date time interval formatter which the caller owns.
417 * <h4>Sample code</h4>
418 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
419 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtCustomized
422 static DateIntervalFormat
* U_EXPORT2
createInstance(
423 const UnicodeString
& skeleton
,
424 const Locale
& locale
,
425 const DateIntervalInfo
& dtitvinf
,
432 virtual ~DateIntervalFormat();
435 * Clone this Format object polymorphically. The caller owns the result and
436 * should delete it when done.
437 * @return A copy of the object.
440 virtual DateIntervalFormat
* clone() const;
443 * Return true if the given Format objects are semantically equal. Objects
444 * of different subclasses are considered unequal.
445 * @param other the object to be compared with.
446 * @return true if the given Format objects are semantically equal.
449 virtual UBool
operator==(const Format
& other
) const;
452 * Return true if the given Format objects are not semantically equal.
453 * Objects of different subclasses are considered unequal.
454 * @param other the object to be compared with.
455 * @return true if the given Format objects are not semantically equal.
458 UBool
operator!=(const Format
& other
) const;
461 using Format::format
;
464 * Format an object to produce a string. This method handles Formattable
465 * objects with a DateInterval type.
466 * If a the Formattable object type is not a DateInterval,
467 * then it returns a failing UErrorCode.
469 * @param obj The object to format.
470 * Must be a DateInterval.
471 * @param appendTo Output parameter to receive result.
472 * Result is appended to existing contents.
473 * @param fieldPosition On input: an alignment field, if desired.
474 * On output: the offsets of the alignment field.
475 * There may be multiple instances of a given field type
476 * in an interval format; in this case the fieldPosition
477 * offsets refer to the first instance.
478 * @param status Output param filled with success/failure status.
479 * @return Reference to 'appendTo' parameter.
482 virtual UnicodeString
& format(const Formattable
& obj
,
483 UnicodeString
& appendTo
,
484 FieldPosition
& fieldPosition
,
485 UErrorCode
& status
) const ;
490 * Format a DateInterval to produce a string.
492 * @param dtInterval DateInterval to be formatted.
493 * @param appendTo Output parameter to receive result.
494 * Result is appended to existing contents.
495 * @param fieldPosition On input: an alignment field, if desired.
496 * On output: the offsets of the alignment field.
497 * There may be multiple instances of a given field type
498 * in an interval format; in this case the fieldPosition
499 * offsets refer to the first instance.
500 * @param status Output param filled with success/failure status.
501 * @return Reference to 'appendTo' parameter.
504 UnicodeString
& format(const DateInterval
* dtInterval
,
505 UnicodeString
& appendTo
,
506 FieldPosition
& fieldPosition
,
507 UErrorCode
& status
) const ;
509 #ifndef U_HIDE_DRAFT_API
511 * Format a DateInterval to produce a FormattedDateInterval.
513 * The FormattedDateInterval exposes field information about the formatted string.
515 * @param dtInterval DateInterval to be formatted.
516 * @param status Set if an error occurs.
517 * @return A FormattedDateInterval containing the format result.
520 FormattedDateInterval
formatToValue(
521 const DateInterval
& dtInterval
,
522 UErrorCode
& status
) const;
523 #endif /* U_HIDE_DRAFT_API */
526 * Format 2 Calendars to produce a string.
528 * Note: "fromCalendar" and "toCalendar" are not const,
529 * since calendar is not const in SimpleDateFormat::format(Calendar&),
531 * @param fromCalendar calendar set to the from date in date interval
532 * to be formatted into date interval string
533 * @param toCalendar calendar set to the to date in date interval
534 * to be formatted into date interval string
535 * @param appendTo Output parameter to receive result.
536 * Result is appended to existing contents.
537 * @param fieldPosition On input: an alignment field, if desired.
538 * On output: the offsets of the alignment field.
539 * There may be multiple instances of a given field type
540 * in an interval format; in this case the fieldPosition
541 * offsets refer to the first instance.
542 * @param status Output param filled with success/failure status.
543 * Caller needs to make sure it is SUCCESS
544 * at the function entrance
545 * @return Reference to 'appendTo' parameter.
548 UnicodeString
& format(Calendar
& fromCalendar
,
549 Calendar
& toCalendar
,
550 UnicodeString
& appendTo
,
551 FieldPosition
& fieldPosition
,
552 UErrorCode
& status
) const ;
554 #ifndef U_HIDE_DRAFT_API
556 * Format 2 Calendars to produce a FormattedDateInterval.
558 * The FormattedDateInterval exposes field information about the formatted string.
560 * Note: "fromCalendar" and "toCalendar" are not const,
561 * since calendar is not const in SimpleDateFormat::format(Calendar&),
563 * @param fromCalendar calendar set to the from date in date interval
564 * to be formatted into date interval string
565 * @param toCalendar calendar set to the to date in date interval
566 * to be formatted into date interval string
567 * @param status Set if an error occurs.
568 * @return A FormattedDateInterval containing the format result.
571 FormattedDateInterval
formatToValue(
572 Calendar
& fromCalendar
,
573 Calendar
& toCalendar
,
574 UErrorCode
& status
) const;
575 #endif /* U_HIDE_DRAFT_API */
578 * Date interval parsing is not supported. Please do not use.
580 * This method should handle parsing of
581 * date time interval strings into Formattable objects with
582 * DateInterval type, which is a pair of UDate.
584 * Before calling, set parse_pos.index to the offset you want to start
585 * parsing at in the source. After calling, parse_pos.index is the end of
586 * the text you parsed. If error occurs, index is unchanged.
588 * When parsing, leading whitespace is discarded (with a successful parse),
589 * while trailing whitespace is left as is.
591 * See Format::parseObject() for more.
593 * @param source The string to be parsed into an object.
594 * @param result Formattable to be set to the parse result.
595 * If parse fails, return contents are undefined.
596 * @param parse_pos The position to start parsing at. Since no parsing
597 * is supported, upon return this param is unchanged.
598 * @return A newly created Formattable* object, or NULL
599 * on failure. The caller owns this and should
600 * delete it when done.
603 virtual void parseObject(const UnicodeString
& source
,
605 ParsePosition
& parse_pos
) const;
609 * Gets the date time interval patterns.
610 * @return the date time interval patterns associated with
611 * this date interval formatter.
614 const DateIntervalInfo
* getDateIntervalInfo(void) const;
618 * Set the date time interval patterns.
619 * @param newIntervalPatterns the given interval patterns to copy.
620 * @param status output param set to success/failure code on exit
623 void setDateIntervalInfo(const DateIntervalInfo
& newIntervalPatterns
,
628 * Gets the date formatter. The DateIntervalFormat instance continues to own
629 * the returned DateFormatter object, and will use and possibly modify it
630 * during format operations. In a multi-threaded environment, the returned
631 * DateFormat can only be used if it is certain that no other threads are
632 * concurrently using this DateIntervalFormatter, even for nominally const
635 * @return the date formatter associated with this date interval formatter.
638 const DateFormat
* getDateFormat(void) const;
641 * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar.
642 * @return the time zone associated with the calendar of DateIntervalFormat.
645 virtual const TimeZone
& getTimeZone(void) const;
648 * Sets the time zone for the calendar used by this DateIntervalFormat object. The
649 * caller no longer owns the TimeZone object and should not delete it after this call.
650 * @param zoneToAdopt the TimeZone to be adopted.
653 virtual void adoptTimeZone(TimeZone
* zoneToAdopt
);
656 * Sets the time zone for the calendar used by this DateIntervalFormat object.
657 * @param zone the new time zone.
660 virtual void setTimeZone(const TimeZone
& zone
);
663 * Change attributes for the DateIntervalFormat object.
665 * The attribute to change.
667 * The new value for the attribute.
669 * A UErrorCode to receive any errors.
672 virtual void setAttribute(UDateIntervalFormatAttribute attr
,
673 UDateIntervalFormatAttributeValue value
,
677 * Set a particular UDisplayContext value in the formatter, such as
678 * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. This causes the formatted
679 * result to be capitalized appropriately for the context in which
680 * it is intended to be used, considering both the locale and the
681 * type of field at the beginning of the formatted result.
682 * @param value The UDisplayContext value to set.
683 * @param status Input/output status. If at entry this indicates a failure
684 * status, the function will do nothing; otherwise this will be
685 * updated with any new status from the function.
688 virtual void setContext(UDisplayContext value
, UErrorCode
& status
);
691 * Get the formatter's UDisplayContext value for the specified UDisplayContextType,
692 * such as UDISPCTX_TYPE_CAPITALIZATION.
693 * @param type The UDisplayContextType whose value to return
694 * @param status Input/output status. If at entry this indicates a failure
695 * status, the function will do nothing; otherwise this will be
696 * updated with any new status from the function.
697 * @return The UDisplayContextValue for the specified type.
700 virtual UDisplayContext
getContext(UDisplayContextType type
, UErrorCode
& status
) const;
703 * Return the class ID for this class. This is useful only for comparing to
704 * a return value from getDynamicClassID(). For example:
706 * . Base* polymorphic_pointer = createPolymorphicObject();
707 * . if (polymorphic_pointer->getDynamicClassID() ==
708 * . erived::getStaticClassID()) ...
710 * @return The class ID for all objects of this class.
713 static UClassID U_EXPORT2
getStaticClassID(void);
716 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
717 * method is to implement a simple version of RTTI, since not all C++
718 * compilers support genuine RTTI. Polymorphic operator==() and clone()
719 * methods call this method.
721 * @return The class ID for this object. All objects of a
722 * given class have the same class ID. Objects of
723 * other classes have different class IDs.
726 virtual UClassID
getDynamicClassID(void) const;
734 DateIntervalFormat(const DateIntervalFormat
&);
737 * Assignment operator.
740 DateIntervalFormat
& operator=(const DateIntervalFormat
&);
745 * This is for ICU internal use only. Please do not use.
746 * Save the interval pattern information.
747 * Interval pattern consists of 2 single date patterns and the separator.
748 * For example, interval pattern "MMM d - MMM d, yyyy" consists
749 * a single date pattern "MMM d", another single date pattern "MMM d, yyyy",
750 * and a separator "-".
751 * The pattern is divided into 2 parts. For above example,
752 * the first part is "MMM d - ", and the second part is "MMM d, yyyy".
753 * Also, the first date appears in an interval pattern could be
754 * the earlier date or the later date.
755 * And such information is saved in the interval pattern as well.
758 UnicodeString firstPart
;
759 UnicodeString secondPart
;
761 * Whether the first date in interval pattern is later date or not.
762 * Fallback format set the default ordering.
763 * And for a particular interval pattern, the order can be
764 * overriden by prefixing the interval pattern with "latestFirst:" or
766 * For example, given 2 date, Jan 10, 2007 to Feb 10, 2007.
767 * if the fallback format is "{0} - {1}",
768 * and the pattern is "d MMM - d MMM yyyy", the interval format is
769 * "10 Jan - 10 Feb, 2007".
770 * If the pattern is "latestFirst:d MMM - d MMM yyyy",
771 * the interval format is "10 Feb - 10 Jan, 2007"
773 UBool laterDateFirst
;
778 * default constructor
779 * @internal (private)
781 DateIntervalFormat();
784 * Construct a DateIntervalFormat from DateFormat,
785 * a DateIntervalInfo, and skeleton.
786 * DateFormat provides the timezone, calendar,
787 * full pattern, and date format symbols information.
788 * It should be a SimpleDateFormat object which
789 * has a pattern in it.
790 * the DateIntervalInfo provides the interval patterns.
792 * Note: the DateIntervalFormat takes ownership of both
793 * DateFormat and DateIntervalInfo objects.
794 * Caller should not delete them.
796 * @param locale the locale of this date interval formatter.
797 * @param dtItvInfo the DateIntervalInfo object to be adopted.
798 * @param skeleton the skeleton of the date formatter
799 * @param status output param set to success/failure code on exit
801 DateIntervalFormat(const Locale
& locale
, DateIntervalInfo
* dtItvInfo
,
802 const UnicodeString
* skeleton
, UErrorCode
& status
);
806 * Construct a DateIntervalFormat from DateFormat
807 * and a DateIntervalInfo.
809 * It is a wrapper of the constructor.
811 * @param locale the locale of this date interval formatter.
812 * @param dtitvinf the DateIntervalInfo object to be adopted.
813 * @param skeleton the skeleton of this formatter.
814 * @param status Output param set to success/failure code.
815 * @return a date time interval formatter which the caller owns.
817 static DateIntervalFormat
* U_EXPORT2
create(const Locale
& locale
,
818 DateIntervalInfo
* dtitvinf
,
819 const UnicodeString
* skeleton
,
823 * Below are for generating interval patterns local to the formatter
826 /** Like fallbackFormat, but only formats the range part of the fallback. */
827 void fallbackFormatRange(
828 Calendar
& fromCalendar
,
829 Calendar
& toCalendar
,
830 UnicodeString
& appendTo
,
832 FieldPositionHandler
& fphandler
,
833 UErrorCode
& status
) const;
836 * Format 2 Calendars using fall-back interval pattern
838 * The full pattern used in this fall-back format is the
839 * full pattern of the date formatter.
841 * gFormatterMutex must already be locked when calling this function.
843 * @param fromCalendar calendar set to the from date in date interval
844 * to be formatted into date interval string
845 * @param toCalendar calendar set to the to date in date interval
846 * to be formatted into date interval string
847 * @param fromToOnSameDay TRUE iff from and to dates are on the same day
848 * (any difference is in ampm/hours or below)
849 * @param appendTo Output parameter to receive result.
850 * Result is appended to existing contents.
851 * @param firstIndex See formatImpl for more information.
852 * @param fphandler See formatImpl for more information.
853 * @param status output param set to success/failure code on exit
854 * @return Reference to 'appendTo' parameter.
855 * @internal (private)
857 UnicodeString
& fallbackFormat(Calendar
& fromCalendar
,
858 Calendar
& toCalendar
,
859 UBool fromToOnSameDay
,
860 UnicodeString
& appendTo
,
862 FieldPositionHandler
& fphandler
,
863 UErrorCode
& status
) const;
868 * Initialize interval patterns locale to this formatter
870 * This code is a bit complicated since
871 * 1. the interval patterns saved in resource bundle files are interval
872 * patterns based on date or time only.
873 * It does not have interval patterns based on both date and time.
874 * Interval patterns on both date and time are algorithm generated.
876 * For example, it has interval patterns on skeleton "dMy" and "hm",
877 * but it does not have interval patterns on skeleton "dMyhm".
879 * The rule to generate interval patterns for both date and time skeleton are
880 * 1) when the year, month, or day differs, concatenate the two original
881 * expressions with a separator between,
882 * For example, interval pattern from "Jan 10, 2007 10:10 am"
883 * to "Jan 11, 2007 10:10am" is
884 * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
886 * 2) otherwise, present the date followed by the range expression
888 * For example, interval pattern from "Jan 10, 2007 10:10 am"
889 * to "Jan 10, 2007 11:10am" is
890 * "Jan 10, 2007 10:10 am - 11:10am"
892 * 2. even a pattern does not request a certain calendar field,
893 * the interval pattern needs to include such field if such fields are
894 * different between 2 dates.
895 * For example, a pattern/skeleton is "hm", but the interval pattern
896 * includes year, month, and date when year, month, and date differs.
899 * @param status output param set to success/failure code on exit
901 void initializePattern(UErrorCode
& status
);
906 * Set fall back interval pattern given a calendar field,
907 * a skeleton, and a date time pattern generator.
908 * @param field the largest different calendar field
909 * @param skeleton a skeleton
910 * @param status output param set to success/failure code on exit
912 void setFallbackPattern(UCalendarDateFields field
,
913 const UnicodeString
& skeleton
,
919 * get separated date and time skeleton from a combined skeleton.
921 * The difference between date skeleton and normalizedDateSkeleton are:
922 * 1. both 'y' and 'd' are appeared only once in normalizeDateSkeleton
923 * 2. 'E' and 'EE' are normalized into 'EEE'
924 * 3. 'MM' is normalized into 'M'
926 ** the difference between time skeleton and normalizedTimeSkeleton are:
927 * 1. both 'H' and 'h' are normalized as 'h' in normalized time skeleton,
928 * 2. 'a' is omitted in normalized time skeleton.
929 * 3. there is only one appearance for 'h', 'm','v', 'z' in normalized time
933 * @param skeleton given combined skeleton.
934 * @param date Output parameter for date only skeleton.
935 * @param normalizedDate Output parameter for normalized date only
937 * @param time Output parameter for time only skeleton.
938 * @param normalizedTime Output parameter for normalized time only
942 static void U_EXPORT2
getDateTimeSkeleton(const UnicodeString
& skeleton
,
944 UnicodeString
& normalizedDate
,
946 UnicodeString
& normalizedTime
);
951 * Generate date or time interval pattern from resource,
952 * and set them into the interval pattern locale to this formatter.
954 * It needs to handle the following:
955 * 1. need to adjust field width.
956 * For example, the interval patterns saved in DateIntervalInfo
957 * includes "dMMMy", but not "dMMMMy".
958 * Need to get interval patterns for dMMMMy from dMMMy.
959 * Another example, the interval patterns saved in DateIntervalInfo
960 * includes "hmv", but not "hmz".
961 * Need to get interval patterns for "hmz' from 'hmv'
963 * 2. there might be no pattern for 'y' differ for skeleton "Md",
964 * in order to get interval patterns for 'y' differ,
965 * need to look for it from skeleton 'yMd'
967 * @param dateSkeleton normalized date skeleton
968 * @param timeSkeleton normalized time skeleton
969 * @return whether the resource is found for the skeleton.
970 * TRUE if interval pattern found for the skeleton,
973 UBool
setSeparateDateTimePtn(const UnicodeString
& dateSkeleton
,
974 const UnicodeString
& timeSkeleton
);
980 * Generate interval pattern from existing resource
982 * It not only save the interval patterns,
983 * but also return the extended skeleton and its best match skeleton.
985 * @param field largest different calendar field
986 * @param skeleton skeleton
987 * @param bestSkeleton the best match skeleton which has interval pattern
988 * defined in resource
989 * @param differenceInfo the difference between skeleton and best skeleton
990 * 0 means the best matched skeleton is the same as input skeleton
991 * 1 means the fields are the same, but field width are different
992 * 2 means the only difference between fields are v/z,
993 * -1 means there are other fields difference
995 * @param extendedSkeleton extended skeleton
996 * @param extendedBestSkeleton extended best match skeleton
997 * @return whether the interval pattern is found
998 * through extending skeleton or not.
999 * TRUE if interval pattern is found by
1000 * extending skeleton, FALSE otherwise.
1002 UBool
setIntervalPattern(UCalendarDateFields field
,
1003 const UnicodeString
* skeleton
,
1004 const UnicodeString
* bestSkeleton
,
1005 int8_t differenceInfo
,
1006 UnicodeString
* extendedSkeleton
= NULL
,
1007 UnicodeString
* extendedBestSkeleton
= NULL
);
1010 * Adjust field width in best match interval pattern to match
1011 * the field width in input skeleton.
1013 * TODO (xji) make a general solution
1014 * The adjusting rule can be:
1017 * 3. default adjust, which means adjust according to the following rules
1018 * 3.1 always adjust string, such as MMM and MMMM
1019 * 3.2 never adjust between string and numeric, such as MM and MMM
1020 * 3.3 always adjust year
1021 * 3.4 do not adjust 'd', 'h', or 'm' if h presents
1022 * 3.5 do not adjust 'M' if it is numeric(?)
1024 * Since date interval format is well-formed format,
1025 * date and time skeletons are normalized previously,
1026 * till this stage, the adjust here is only "adjust strings, such as MMM
1027 * and MMMM, EEE and EEEE.
1029 * @param inputSkeleton the input skeleton
1030 * @param bestMatchSkeleton the best match skeleton
1031 * @param bestMatchIntervalPattern the best match interval pattern
1032 * @param differenceInfo the difference between 2 skeletons
1033 * 1 means only field width differs
1034 * 2 means v/z exchange
1035 * @param adjustedIntervalPattern adjusted interval pattern
1037 static void U_EXPORT2
adjustFieldWidth(
1038 const UnicodeString
& inputSkeleton
,
1039 const UnicodeString
& bestMatchSkeleton
,
1040 const UnicodeString
& bestMatchIntervalPattern
,
1041 int8_t differenceInfo
,
1042 UnicodeString
& adjustedIntervalPattern
);
1045 * Concat a single date pattern with a time interval pattern,
1046 * set it into the intervalPatterns, while field is time field.
1047 * This is used to handle time interval patterns on skeleton with
1048 * both time and date. Present the date followed by
1049 * the range expression for the time.
1050 * @param format date and time format
1051 * @param datePattern date pattern
1052 * @param field time calendar field: AM_PM, HOUR, MINUTE
1053 * @param status output param set to success/failure code on exit
1055 void concatSingleDate2TimeInterval(UnicodeString
& format
,
1056 const UnicodeString
& datePattern
,
1057 UCalendarDateFields field
,
1058 UErrorCode
& status
);
1061 * check whether a calendar field present in a skeleton.
1062 * @param field calendar field need to check
1063 * @param skeleton given skeleton on which to check the calendar field
1064 * @return true if field present in a skeleton.
1066 static UBool U_EXPORT2
fieldExistsInSkeleton(UCalendarDateFields field
,
1067 const UnicodeString
& skeleton
);
1071 * Split interval patterns into 2 part.
1072 * @param intervalPattern interval pattern
1073 * @return the index in interval pattern which split the pattern into 2 part
1075 static int32_t U_EXPORT2
splitPatternInto2Part(const UnicodeString
& intervalPattern
);
1079 * Break interval patterns as 2 part and save them into pattern info.
1080 * @param field calendar field
1081 * @param intervalPattern interval pattern
1083 void setIntervalPattern(UCalendarDateFields field
,
1084 const UnicodeString
& intervalPattern
);
1088 * Break interval patterns as 2 part and save them into pattern info.
1089 * @param field calendar field
1090 * @param intervalPattern interval pattern
1091 * @param laterDateFirst whether later date appear first in interval pattern
1093 void setIntervalPattern(UCalendarDateFields field
,
1094 const UnicodeString
& intervalPattern
,
1095 UBool laterDateFirst
);
1099 * Set pattern information.
1101 * @param field calendar field
1102 * @param firstPart the first part in interval pattern
1103 * @param secondPart the second part in interval pattern
1104 * @param laterDateFirst whether the first date in intervalPattern
1105 * is earlier date or later date
1107 void setPatternInfo(UCalendarDateFields field
,
1108 const UnicodeString
* firstPart
,
1109 const UnicodeString
* secondPart
,
1110 UBool laterDateFirst
);
1113 * Format 2 Calendars to produce a string.
1114 * Implementation of the similar public format function.
1115 * Must be called with gFormatterMutex already locked.
1117 * Note: "fromCalendar" and "toCalendar" are not const,
1118 * since calendar is not const in SimpleDateFormat::format(Calendar&),
1120 * @param fromCalendar calendar set to the from date in date interval
1121 * to be formatted into date interval string
1122 * @param toCalendar calendar set to the to date in date interval
1123 * to be formatted into date interval string
1124 * @param appendTo Output parameter to receive result.
1125 * Result is appended to existing contents.
1126 * @param firstIndex 0 if the first output date is fromCalendar;
1127 * 1 if it corresponds to toCalendar;
1128 * -1 if there is only one date printed.
1129 * @param fphandler Handler for field position information.
1130 * The fields will be from the UDateFormatField enum.
1131 * @param status Output param filled with success/failure status.
1132 * Caller needs to make sure it is SUCCESS
1133 * at the function entrance
1134 * @return Reference to 'appendTo' parameter.
1135 * @internal (private)
1137 UnicodeString
& formatImpl(Calendar
& fromCalendar
,
1138 Calendar
& toCalendar
,
1139 UnicodeString
& appendTo
,
1141 FieldPositionHandler
& fphandler
,
1142 UErrorCode
& status
) const ;
1144 /** Version of formatImpl for DateInterval. */
1145 UnicodeString
& formatIntervalImpl(const DateInterval
& dtInterval
,
1146 UnicodeString
& appendTo
,
1148 FieldPositionHandler
& fphandler
,
1149 UErrorCode
& status
) const;
1152 // from calendar field to pattern letter
1153 static const char16_t fgCalendarFieldToPatternLetter
[];
1157 * The interval patterns for this locale.
1159 DateIntervalInfo
* fInfo
;
1162 * The DateFormat object used to format single pattern
1164 SimpleDateFormat
* fDateFormat
;
1167 * The 2 calendars with the from and to date.
1168 * could re-use the calendar in fDateFormat,
1169 * but keeping 2 calendars make it clear and clean.
1171 Calendar
* fFromCalendar
;
1172 Calendar
* fToCalendar
;
1177 * Following are interval information relevant (locale) to this formatter.
1179 UnicodeString fSkeleton
;
1180 PatternInfo fIntervalPatterns
[DateIntervalInfo::kIPI_MAX_INDEX
];
1183 * Patterns for fallback formatting.
1185 UnicodeString
* fDatePattern
;
1186 UnicodeString
* fTimePattern
;
1187 UnicodeString
* fDateTimeFormat
;
1192 int32_t fMinimizeType
;
1194 UDisplayContext fCapitalizationContext
;
1198 DateIntervalFormat::operator!=(const Format
& other
) const {
1199 return !operator==(other
);
1204 #endif /* #if !UCONFIG_NO_FORMATTING */
1206 #endif /* U_SHOW_CPLUSPLUS_API */
1208 #endif // _DTITVFMT_H__