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"
21 * \brief C++ API: Format and parse date interval in a language-independent manner.
24 #if !UCONFIG_NO_FORMATTING
26 #include "unicode/ucal.h"
27 #include "unicode/smpdtfmt.h"
28 #include "unicode/dtintrv.h"
29 #include "unicode/dtitvinf.h"
30 #include "unicode/dtptngen.h"
31 #include "unicode/formattedvalue.h"
32 #include "unicode/udisplaycontext.h"
34 #include "unicode/udateintervalformat.h"
36 #if U_SHOW_CPLUSPLUS_API
40 class FormattedDateIntervalData
;
41 class DateIntervalFormat
;
43 #ifndef U_HIDE_DRAFT_API
45 * An immutable class containing the result of a date interval formatting operation.
47 * Instances of this class are immutable and thread-safe.
49 * When calling nextPosition():
50 * The fields are returned from left to right. The special field category
51 * UFIELD_CATEGORY_DATE_INTERVAL_SPAN is used to indicate which datetime
52 * primitives came from which arguments: 0 means fromCalendar, and 1 means
53 * toCalendar. The span category will always occur before the
54 * corresponding fields in UFIELD_CATEGORY_DATE
55 * in the nextPosition() iterator.
57 * Not intended for public subclassing.
61 class U_I18N_API FormattedDateInterval
: public UMemory
, public FormattedValue
{
64 * Default constructor; makes an empty FormattedDateInterval.
67 FormattedDateInterval() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR
) {}
70 * Move constructor: Leaves the source FormattedDateInterval in an undefined state.
73 FormattedDateInterval(FormattedDateInterval
&& src
) U_NOEXCEPT
;
76 * Destruct an instance of FormattedDateInterval.
79 virtual ~FormattedDateInterval() U_OVERRIDE
;
81 /** Copying not supported; use move constructor instead. */
82 FormattedDateInterval(const FormattedDateInterval
&) = delete;
84 /** Copying not supported; use move assignment instead. */
85 FormattedDateInterval
& operator=(const FormattedDateInterval
&) = delete;
88 * Move assignment: Leaves the source FormattedDateInterval in an undefined state.
91 FormattedDateInterval
& operator=(FormattedDateInterval
&& src
) U_NOEXCEPT
;
93 /** @copydoc FormattedValue::toString() */
94 UnicodeString
toString(UErrorCode
& status
) const U_OVERRIDE
;
96 /** @copydoc FormattedValue::toTempString() */
97 UnicodeString
toTempString(UErrorCode
& status
) const U_OVERRIDE
;
99 /** @copydoc FormattedValue::appendTo() */
100 Appendable
&appendTo(Appendable
& appendable
, UErrorCode
& status
) const U_OVERRIDE
;
102 /** @copydoc FormattedValue::nextPosition() */
103 UBool
nextPosition(ConstrainedFieldPosition
& cfpos
, UErrorCode
& status
) const U_OVERRIDE
;
106 FormattedDateIntervalData
*fData
;
107 UErrorCode fErrorCode
;
108 explicit FormattedDateInterval(FormattedDateIntervalData
*results
)
109 : fData(results
), fErrorCode(U_ZERO_ERROR
) {}
110 explicit FormattedDateInterval(UErrorCode errorCode
)
111 : fData(nullptr), fErrorCode(errorCode
) {}
112 friend class DateIntervalFormat
;
114 #endif /* U_HIDE_DRAFT_API */
118 * DateIntervalFormat is a class for formatting and parsing date
119 * intervals in a language-independent manner.
120 * Only formatting is supported, parsing is not supported.
123 * Date interval means from one date to another date,
124 * for example, from "Jan 11, 2008" to "Jan 18, 2008".
125 * We introduced class DateInterval to represent it.
126 * DateInterval is a pair of UDate, which is
127 * the standard milliseconds since 24:00 GMT, Jan 1, 1970.
130 * DateIntervalFormat formats a DateInterval into
131 * text as compactly as possible.
132 * For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008"
133 * is "Jan 11-18, 2008" for English.
134 * And it parses text into DateInterval,
135 * although initially, parsing is not supported.
138 * There is no structural information in date time patterns.
139 * For any punctuations and string literals inside a date time pattern,
140 * we do not know whether it is just a separator, or a prefix, or a suffix.
141 * Without such information, so, it is difficult to generate a sub-pattern
142 * (or super-pattern) by algorithm.
143 * So, formatting a DateInterval is pattern-driven. It is very
144 * similar to formatting in SimpleDateFormat.
145 * We introduce class DateIntervalInfo to save date interval
146 * patterns, similar to date time pattern in SimpleDateFormat.
149 * Logically, the interval patterns are mappings
150 * from (skeleton, the_largest_different_calendar_field)
151 * to (date_interval_pattern).
157 * only keeps the field pattern letter and ignores all other parts
158 * in a pattern, such as space, punctuations, and string literals.
161 * hides the order of fields.
164 * might hide a field's pattern letter length.
168 * For those non-digit calendar fields, the pattern letter length is
169 * important, such as MMM, MMMM, and MMMMM; EEE and EEEE,
170 * and the field's pattern letter length is honored.
172 * For the digit calendar fields, such as M or MM, d or dd, yy or yyyy,
173 * the field pattern length is ignored and the best match, which is defined
174 * in date time patterns, will be returned without honor the field pattern
175 * letter length in skeleton.
178 * The calendar fields we support for interval formatting are:
179 * year, month, date, day-of-week, am-pm, hour, hour-of-day, minute, and second
180 * (though we do not currently have specific intervalFormat date for skeletons
182 * Those calendar fields can be defined in the following order:
183 * year > month > date > hour (in day) > minute > second
185 * The largest different calendar fields between 2 calendars is the
186 * first different calendar field in above order.
188 * For example: the largest different calendar fields between "Jan 10, 2007"
189 * and "Feb 20, 2008" is year.
192 * For other calendar fields, the compact interval formatting is not
193 * supported. And the interval format will be fall back to fall-back
194 * patterns, which is mostly "{date0} - {date1}".
197 * There is a set of pre-defined static skeleton strings.
198 * There are pre-defined interval patterns for those pre-defined skeletons
199 * in locales' resource files.
200 * For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is "yMMMd",
201 * in en_US, if the largest different calendar field between date1 and date2
202 * is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy",
203 * such as "Jan 10, 2007 - Jan 10, 2008".
204 * If the largest different calendar field between date1 and date2 is "month",
205 * the date interval pattern is "MMM d - MMM d, yyyy",
206 * such as "Jan 10 - Feb 10, 2007".
207 * If the largest different calendar field between date1 and date2 is "day",
208 * the date interval pattern is "MMM d-d, yyyy", such as "Jan 10-20, 2007".
210 * For date skeleton, the interval patterns when year, or month, or date is
211 * different are defined in resource files.
212 * For time skeleton, the interval patterns when am/pm, or hour, or minute is
213 * different are defined in resource files.
216 * If a skeleton is not found in a locale's DateIntervalInfo, which means
217 * the interval patterns for the skeleton is not defined in resource file,
218 * the interval pattern will falls back to the interval "fallback" pattern
219 * defined in resource file.
220 * If the interval "fallback" pattern is not defined, the default fall-back
221 * is "{date0} - {data1}".
224 * For the combination of date and time,
225 * The rule to generate interval patterns are:
228 * when the year, month, or day differs, falls back to fall-back
229 * interval pattern, which mostly is the concatenate the two original
230 * expressions with a separator between,
231 * For example, interval pattern from "Jan 10, 2007 10:10 am"
232 * to "Jan 11, 2007 10:10am" is
233 * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
236 * otherwise, present the date followed by the range expression
238 * For example, interval pattern from "Jan 10, 2007 10:10 am"
239 * to "Jan 10, 2007 11:10am" is "Jan 10, 2007 10:10 am - 11:10am"
245 * If two dates are the same, the interval pattern is the single date pattern.
246 * For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is
249 * Or if the presenting fields between 2 dates have the exact same values,
250 * the interval pattern is the single date pattern.
251 * For example, if user only requests year and month,
252 * the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007".
255 * DateIntervalFormat needs the following information for correct
256 * formatting: time zone, calendar type, pattern, date format symbols,
257 * and date interval patterns.
258 * It can be instantiated in 2 ways:
261 * create an instance using default or given locale plus given skeleton.
262 * Users are encouraged to created date interval formatter this way and
263 * to use the pre-defined skeleton macros, such as
264 * UDAT_YEAR_NUM_MONTH, which consists the calendar fields and
268 * create an instance using default or given locale plus given skeleton
269 * plus a given DateIntervalInfo.
270 * This factory method is for powerful users who want to provide their own
272 * Locale provides the timezone, calendar, and format symbols information.
273 * Local plus skeleton provides full pattern information.
274 * DateIntervalInfo provides the date interval patterns.
279 * For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc.
280 * DateIntervalFormat uses the same syntax as that of
284 * Code Sample: general usage
287 * // the date interval object which the DateIntervalFormat formats on
289 * DateInterval* dtInterval = new DateInterval(1000*3600*24, 1000*3600*24*2);
290 * UErrorCode status = U_ZERO_ERROR;
291 * DateIntervalFormat* dtIntervalFmt = DateIntervalFormat::createInstance(
292 * UDAT_YEAR_MONTH_DAY,
293 * Locale("en", "GB", ""), status);
294 * UnicodeUnicodeString dateIntervalString;
295 * FieldPosition pos = 0;
297 * dtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status);
298 * delete dtIntervalFmt;
302 class U_I18N_API DateIntervalFormat
: public Format
{
306 * Construct a DateIntervalFormat from skeleton and the default locale.
308 * This is a convenient override of
309 * createInstance(const UnicodeString& skeleton, const Locale& locale,
311 * with the value of locale as default locale.
313 * @param skeleton the skeleton on which interval format based.
314 * @param status output param set to success/failure code on exit
315 * @return a date time interval formatter which the caller owns.
318 static DateIntervalFormat
* U_EXPORT2
createInstance(
319 const UnicodeString
& skeleton
,
323 * Construct a DateIntervalFormat from skeleton and a given locale.
325 * In this factory method,
326 * the date interval pattern information is load from resource files.
327 * Users are encouraged to created date interval formatter this way and
328 * to use the pre-defined skeleton macros.
331 * There are pre-defined skeletons (defined in udate.h) having predefined
332 * interval patterns in resource files.
333 * Users are encouraged to use those macros.
335 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
337 * The given Locale provides the interval patterns.
338 * For example, for en_GB, if skeleton is UDAT_YEAR_ABBR_MONTH_WEEKDAY_DAY,
339 * which is "yMMMEEEd",
340 * the interval patterns defined in resource file to above skeleton are:
341 * "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs,
342 * "EEE, d MMM - EEE, d MMM, yyyy" for month differs,
343 * "EEE, d - EEE, d MMM, yyyy" for day differs,
344 * @param skeleton the skeleton on which the interval format is based.
345 * @param locale the given locale
346 * @param status output param set to success/failure code on exit
347 * @return a date time interval formatter which the caller owns.
350 * <h4>Sample code</h4>
351 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
352 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined
356 static DateIntervalFormat
* U_EXPORT2
createInstance(
357 const UnicodeString
& skeleton
,
358 const Locale
& locale
,
362 * Construct a DateIntervalFormat from skeleton
363 * DateIntervalInfo, and default locale.
365 * This is a convenient override of
366 * createInstance(const UnicodeString& skeleton, const Locale& locale,
367 * const DateIntervalInfo& dtitvinf, UErrorCode&)
368 * with the locale value as default locale.
370 * @param skeleton the skeleton on which interval format based.
371 * @param dtitvinf the DateIntervalInfo object.
372 * @param status output param set to success/failure code on exit
373 * @return a date time interval formatter which the caller owns.
376 static DateIntervalFormat
* U_EXPORT2
createInstance(
377 const UnicodeString
& skeleton
,
378 const DateIntervalInfo
& dtitvinf
,
382 * Construct a DateIntervalFormat from skeleton
383 * a DateIntervalInfo, and the given locale.
386 * In this factory method, user provides its own date interval pattern
387 * information, instead of using those pre-defined data in resource file.
388 * This factory method is for powerful users who want to provide their own
391 * There are pre-defined skeletons (defined in udate.h) having predefined
392 * interval patterns in resource files.
393 * Users are encouraged to use those macros.
395 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
397 * The DateIntervalInfo provides the interval patterns.
398 * and the DateIntervalInfo ownership remains to the caller.
400 * User are encouraged to set default interval pattern in DateIntervalInfo
401 * as well, if they want to set other interval patterns ( instead of
402 * reading the interval patterns from resource files).
403 * When the corresponding interval pattern for a largest calendar different
404 * field is not found ( if user not set it ), interval format fallback to
405 * the default interval pattern.
406 * If user does not provide default interval pattern, it fallback to
407 * "{date0} - {date1}"
409 * @param skeleton the skeleton on which interval format based.
410 * @param locale the given locale
411 * @param dtitvinf the DateIntervalInfo object.
412 * @param status output param set to success/failure code on exit
413 * @return a date time interval formatter which the caller owns.
416 * <h4>Sample code</h4>
417 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
418 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtCustomized
421 static DateIntervalFormat
* U_EXPORT2
createInstance(
422 const UnicodeString
& skeleton
,
423 const Locale
& locale
,
424 const DateIntervalInfo
& dtitvinf
,
431 virtual ~DateIntervalFormat();
434 * Clone this Format object polymorphically. The caller owns the result and
435 * should delete it when done.
436 * @return A copy of the object.
439 virtual Format
* clone(void) const;
442 * Return true if the given Format objects are semantically equal. Objects
443 * of different subclasses are considered unequal.
444 * @param other the object to be compared with.
445 * @return true if the given Format objects are semantically equal.
448 virtual UBool
operator==(const Format
& other
) const;
451 * Return true if the given Format objects are not semantically equal.
452 * Objects of different subclasses are considered unequal.
453 * @param other the object to be compared with.
454 * @return true if the given Format objects are not semantically equal.
457 UBool
operator!=(const Format
& other
) const;
460 using Format::format
;
463 * Format an object to produce a string. This method handles Formattable
464 * objects with a DateInterval type.
465 * If a the Formattable object type is not a DateInterval,
466 * then it returns a failing UErrorCode.
468 * @param obj The object to format.
469 * Must be a DateInterval.
470 * @param appendTo Output parameter to receive result.
471 * Result is appended to existing contents.
472 * @param fieldPosition On input: an alignment field, if desired.
473 * On output: the offsets of the alignment field.
474 * There may be multiple instances of a given field type
475 * in an interval format; in this case the fieldPosition
476 * offsets refer to the first instance.
477 * @param status Output param filled with success/failure status.
478 * @return Reference to 'appendTo' parameter.
481 virtual UnicodeString
& format(const Formattable
& obj
,
482 UnicodeString
& appendTo
,
483 FieldPosition
& fieldPosition
,
484 UErrorCode
& status
) const ;
489 * Format a DateInterval to produce a string.
491 * @param dtInterval DateInterval to be formatted.
492 * @param appendTo Output parameter to receive result.
493 * Result is appended to existing contents.
494 * @param fieldPosition On input: an alignment field, if desired.
495 * On output: the offsets of the alignment field.
496 * There may be multiple instances of a given field type
497 * in an interval format; in this case the fieldPosition
498 * offsets refer to the first instance.
499 * @param status Output param filled with success/failure status.
500 * @return Reference to 'appendTo' parameter.
503 UnicodeString
& format(const DateInterval
* dtInterval
,
504 UnicodeString
& appendTo
,
505 FieldPosition
& fieldPosition
,
506 UErrorCode
& status
) const ;
508 #ifndef U_HIDE_DRAFT_API
510 * Format a DateInterval to produce a FormattedDateInterval.
512 * The FormattedDateInterval exposes field information about the formatted string.
514 * @param dtInterval DateInterval to be formatted.
515 * @param status Set if an error occurs.
516 * @return A FormattedDateInterval containing the format result.
519 FormattedDateInterval
formatToValue(
520 const DateInterval
& dtInterval
,
521 UErrorCode
& status
) const;
522 #endif /* U_HIDE_DRAFT_API */
525 * Format 2 Calendars to produce a string.
527 * Note: "fromCalendar" and "toCalendar" are not const,
528 * since calendar is not const in SimpleDateFormat::format(Calendar&),
530 * @param fromCalendar calendar set to the from date in date interval
531 * to be formatted into date interval string
532 * @param toCalendar calendar set to the to date in date interval
533 * to be formatted into date interval string
534 * @param appendTo Output parameter to receive result.
535 * Result is appended to existing contents.
536 * @param fieldPosition On input: an alignment field, if desired.
537 * On output: the offsets of the alignment field.
538 * There may be multiple instances of a given field type
539 * in an interval format; in this case the fieldPosition
540 * offsets refer to the first instance.
541 * @param status Output param filled with success/failure status.
542 * Caller needs to make sure it is SUCCESS
543 * at the function entrance
544 * @return Reference to 'appendTo' parameter.
547 UnicodeString
& format(Calendar
& fromCalendar
,
548 Calendar
& toCalendar
,
549 UnicodeString
& appendTo
,
550 FieldPosition
& fieldPosition
,
551 UErrorCode
& status
) const ;
553 #ifndef U_HIDE_DRAFT_API
555 * Format 2 Calendars to produce a FormattedDateInterval.
557 * The FormattedDateInterval exposes field information about the formatted string.
559 * Note: "fromCalendar" and "toCalendar" are not const,
560 * since calendar is not const in SimpleDateFormat::format(Calendar&),
562 * @param fromCalendar calendar set to the from date in date interval
563 * to be formatted into date interval string
564 * @param toCalendar calendar set to the to date in date interval
565 * to be formatted into date interval string
566 * @param status Set if an error occurs.
567 * @return A FormattedDateInterval containing the format result.
570 FormattedDateInterval
formatToValue(
571 Calendar
& fromCalendar
,
572 Calendar
& toCalendar
,
573 UErrorCode
& status
) const;
574 #endif /* U_HIDE_DRAFT_API */
577 * Date interval parsing is not supported. Please do not use.
579 * This method should handle parsing of
580 * date time interval strings into Formattable objects with
581 * DateInterval type, which is a pair of UDate.
583 * Before calling, set parse_pos.index to the offset you want to start
584 * parsing at in the source. After calling, parse_pos.index is the end of
585 * the text you parsed. If error occurs, index is unchanged.
587 * When parsing, leading whitespace is discarded (with a successful parse),
588 * while trailing whitespace is left as is.
590 * See Format::parseObject() for more.
592 * @param source The string to be parsed into an object.
593 * @param result Formattable to be set to the parse result.
594 * If parse fails, return contents are undefined.
595 * @param parse_pos The position to start parsing at. Since no parsing
596 * is supported, upon return this param is unchanged.
597 * @return A newly created Formattable* object, or NULL
598 * on failure. The caller owns this and should
599 * delete it when done.
602 virtual void parseObject(const UnicodeString
& source
,
604 ParsePosition
& parse_pos
) const;
608 * Gets the date time interval patterns.
609 * @return the date time interval patterns associated with
610 * this date interval formatter.
613 const DateIntervalInfo
* getDateIntervalInfo(void) const;
617 * Set the date time interval patterns.
618 * @param newIntervalPatterns the given interval patterns to copy.
619 * @param status output param set to success/failure code on exit
622 void setDateIntervalInfo(const DateIntervalInfo
& newIntervalPatterns
,
627 * Gets the date formatter. The DateIntervalFormat instance continues to own
628 * the returned DateFormatter object, and will use and possibly modify it
629 * during format operations. In a multi-threaded environment, the returned
630 * DateFormat can only be used if it is certain that no other threads are
631 * concurrently using this DateIntervalFormatter, even for nominally const
634 * @return the date formatter associated with this date interval formatter.
637 const DateFormat
* getDateFormat(void) const;
640 * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar.
641 * @return the time zone associated with the calendar of DateIntervalFormat.
644 virtual const TimeZone
& getTimeZone(void) const;
647 * Sets the time zone for the calendar used by this DateIntervalFormat object. The
648 * caller no longer owns the TimeZone object and should not delete it after this call.
649 * @param zoneToAdopt the TimeZone to be adopted.
652 virtual void adoptTimeZone(TimeZone
* zoneToAdopt
);
655 * Sets the time zone for the calendar used by this DateIntervalFormat object.
656 * @param zone the new time zone.
659 virtual void setTimeZone(const TimeZone
& zone
);
662 * Change attributes for the DateIntervalFormat object.
664 * The attribute to change.
666 * The new value for the attribute.
668 * A UErrorCode to receive any errors.
671 virtual void setAttribute(UDateIntervalFormatAttribute attr
,
672 UDateIntervalFormatAttributeValue value
,
676 * Set a particular UDisplayContext value in the formatter, such as
677 * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. This causes the formatted
678 * result to be capitalized appropriately for the context in which
679 * it is intended to be used, considering both the locale and the
680 * type of field at the beginning of the formatted result.
681 * @param value The UDisplayContext value to set.
682 * @param status Input/output status. If at entry this indicates a failure
683 * status, the function will do nothing; otherwise this will be
684 * updated with any new status from the function.
687 virtual void setContext(UDisplayContext value
, UErrorCode
& status
);
690 * Get the formatter's UDisplayContext value for the specified UDisplayContextType,
691 * such as UDISPCTX_TYPE_CAPITALIZATION.
692 * @param type The UDisplayContextType whose value to return
693 * @param status Input/output status. If at entry this indicates a failure
694 * status, the function will do nothing; otherwise this will be
695 * updated with any new status from the function.
696 * @return The UDisplayContextValue for the specified type.
699 virtual UDisplayContext
getContext(UDisplayContextType type
, UErrorCode
& status
) const;
702 * Return the class ID for this class. This is useful only for comparing to
703 * a return value from getDynamicClassID(). For example:
705 * . Base* polymorphic_pointer = createPolymorphicObject();
706 * . if (polymorphic_pointer->getDynamicClassID() ==
707 * . erived::getStaticClassID()) ...
709 * @return The class ID for all objects of this class.
712 static UClassID U_EXPORT2
getStaticClassID(void);
715 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
716 * method is to implement a simple version of RTTI, since not all C++
717 * compilers support genuine RTTI. Polymorphic operator==() and clone()
718 * methods call this method.
720 * @return The class ID for this object. All objects of a
721 * given class have the same class ID. Objects of
722 * other classes have different class IDs.
725 virtual UClassID
getDynamicClassID(void) const;
733 DateIntervalFormat(const DateIntervalFormat
&);
736 * Assignment operator.
739 DateIntervalFormat
& operator=(const DateIntervalFormat
&);
744 * This is for ICU internal use only. Please do not use.
745 * Save the interval pattern information.
746 * Interval pattern consists of 2 single date patterns and the separator.
747 * For example, interval pattern "MMM d - MMM d, yyyy" consists
748 * a single date pattern "MMM d", another single date pattern "MMM d, yyyy",
749 * and a separator "-".
750 * The pattern is divided into 2 parts. For above example,
751 * the first part is "MMM d - ", and the second part is "MMM d, yyyy".
752 * Also, the first date appears in an interval pattern could be
753 * the earlier date or the later date.
754 * And such information is saved in the interval pattern as well.
757 UnicodeString firstPart
;
758 UnicodeString secondPart
;
760 * Whether the first date in interval pattern is later date or not.
761 * Fallback format set the default ordering.
762 * And for a particular interval pattern, the order can be
763 * overriden by prefixing the interval pattern with "latestFirst:" or
765 * For example, given 2 date, Jan 10, 2007 to Feb 10, 2007.
766 * if the fallback format is "{0} - {1}",
767 * and the pattern is "d MMM - d MMM yyyy", the interval format is
768 * "10 Jan - 10 Feb, 2007".
769 * If the pattern is "latestFirst:d MMM - d MMM yyyy",
770 * the interval format is "10 Feb - 10 Jan, 2007"
772 UBool laterDateFirst
;
777 * default constructor
778 * @internal (private)
780 DateIntervalFormat();
783 * Construct a DateIntervalFormat from DateFormat,
784 * a DateIntervalInfo, and skeleton.
785 * DateFormat provides the timezone, calendar,
786 * full pattern, and date format symbols information.
787 * It should be a SimpleDateFormat object which
788 * has a pattern in it.
789 * the DateIntervalInfo provides the interval patterns.
791 * Note: the DateIntervalFormat takes ownership of both
792 * DateFormat and DateIntervalInfo objects.
793 * Caller should not delete them.
795 * @param locale the locale of this date interval formatter.
796 * @param dtItvInfo the DateIntervalInfo object to be adopted.
797 * @param skeleton the skeleton of the date formatter
798 * @param status output param set to success/failure code on exit
800 DateIntervalFormat(const Locale
& locale
, DateIntervalInfo
* dtItvInfo
,
801 const UnicodeString
* skeleton
, UErrorCode
& status
);
805 * Construct a DateIntervalFormat from DateFormat
806 * and a DateIntervalInfo.
808 * It is a wrapper of the constructor.
810 * @param locale the locale of this date interval formatter.
811 * @param dtitvinf the DateIntervalInfo object to be adopted.
812 * @param skeleton the skeleton of this formatter.
813 * @param status Output param set to success/failure code.
814 * @return a date time interval formatter which the caller owns.
816 static DateIntervalFormat
* U_EXPORT2
create(const Locale
& locale
,
817 DateIntervalInfo
* dtitvinf
,
818 const UnicodeString
* skeleton
,
822 * Below are for generating interval patterns local to the formatter
825 /** Like fallbackFormat, but only formats the range part of the fallback. */
826 void fallbackFormatRange(
827 Calendar
& fromCalendar
,
828 Calendar
& toCalendar
,
829 UnicodeString
& appendTo
,
831 FieldPositionHandler
& fphandler
,
832 UErrorCode
& status
) const;
835 * Format 2 Calendars using fall-back interval pattern
837 * The full pattern used in this fall-back format is the
838 * full pattern of the date formatter.
840 * gFormatterMutex must already be locked when calling this function.
842 * @param fromCalendar calendar set to the from date in date interval
843 * to be formatted into date interval string
844 * @param toCalendar calendar set to the to date in date interval
845 * to be formatted into date interval string
846 * @param fromToOnSameDay TRUE iff from and to dates are on the same day
847 * (any difference is in ampm/hours or below)
848 * @param appendTo Output parameter to receive result.
849 * Result is appended to existing contents.
850 * @param firstIndex See formatImpl for more information.
851 * @param fphandler See formatImpl for more information.
852 * @param status output param set to success/failure code on exit
853 * @return Reference to 'appendTo' parameter.
854 * @internal (private)
856 UnicodeString
& fallbackFormat(Calendar
& fromCalendar
,
857 Calendar
& toCalendar
,
858 UBool fromToOnSameDay
,
859 UnicodeString
& appendTo
,
861 FieldPositionHandler
& fphandler
,
862 UErrorCode
& status
) const;
867 * Initialize interval patterns locale to this formatter
869 * This code is a bit complicated since
870 * 1. the interval patterns saved in resource bundle files are interval
871 * patterns based on date or time only.
872 * It does not have interval patterns based on both date and time.
873 * Interval patterns on both date and time are algorithm generated.
875 * For example, it has interval patterns on skeleton "dMy" and "hm",
876 * but it does not have interval patterns on skeleton "dMyhm".
878 * The rule to generate interval patterns for both date and time skeleton are
879 * 1) when the year, month, or day differs, concatenate the two original
880 * expressions with a separator between,
881 * For example, interval pattern from "Jan 10, 2007 10:10 am"
882 * to "Jan 11, 2007 10:10am" is
883 * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
885 * 2) otherwise, present the date followed by the range expression
887 * For example, interval pattern from "Jan 10, 2007 10:10 am"
888 * to "Jan 10, 2007 11:10am" is
889 * "Jan 10, 2007 10:10 am - 11:10am"
891 * 2. even a pattern does not request a certain calendar field,
892 * the interval pattern needs to include such field if such fields are
893 * different between 2 dates.
894 * For example, a pattern/skeleton is "hm", but the interval pattern
895 * includes year, month, and date when year, month, and date differs.
898 * @param status output param set to success/failure code on exit
900 void initializePattern(UErrorCode
& status
);
905 * Set fall back interval pattern given a calendar field,
906 * a skeleton, and a date time pattern generator.
907 * @param field the largest different calendar field
908 * @param skeleton a skeleton
909 * @param status output param set to success/failure code on exit
911 void setFallbackPattern(UCalendarDateFields field
,
912 const UnicodeString
& skeleton
,
918 * get separated date and time skeleton from a combined skeleton.
920 * The difference between date skeleton and normalizedDateSkeleton are:
921 * 1. both 'y' and 'd' are appeared only once in normalizeDateSkeleton
922 * 2. 'E' and 'EE' are normalized into 'EEE'
923 * 3. 'MM' is normalized into 'M'
925 ** the difference between time skeleton and normalizedTimeSkeleton are:
926 * 1. both 'H' and 'h' are normalized as 'h' in normalized time skeleton,
927 * 2. 'a' is omitted in normalized time skeleton.
928 * 3. there is only one appearance for 'h', 'm','v', 'z' in normalized time
932 * @param skeleton given combined skeleton.
933 * @param date Output parameter for date only skeleton.
934 * @param normalizedDate Output parameter for normalized date only
936 * @param time Output parameter for time only skeleton.
937 * @param normalizedTime Output parameter for normalized time only
941 static void U_EXPORT2
getDateTimeSkeleton(const UnicodeString
& skeleton
,
943 UnicodeString
& normalizedDate
,
945 UnicodeString
& normalizedTime
);
950 * Generate date or time interval pattern from resource,
951 * and set them into the interval pattern locale to this formatter.
953 * It needs to handle the following:
954 * 1. need to adjust field width.
955 * For example, the interval patterns saved in DateIntervalInfo
956 * includes "dMMMy", but not "dMMMMy".
957 * Need to get interval patterns for dMMMMy from dMMMy.
958 * Another example, the interval patterns saved in DateIntervalInfo
959 * includes "hmv", but not "hmz".
960 * Need to get interval patterns for "hmz' from 'hmv'
962 * 2. there might be no pattern for 'y' differ for skeleton "Md",
963 * in order to get interval patterns for 'y' differ,
964 * need to look for it from skeleton 'yMd'
966 * @param dateSkeleton normalized date skeleton
967 * @param timeSkeleton normalized time skeleton
968 * @return whether the resource is found for the skeleton.
969 * TRUE if interval pattern found for the skeleton,
972 UBool
setSeparateDateTimePtn(const UnicodeString
& dateSkeleton
,
973 const UnicodeString
& timeSkeleton
);
979 * Generate interval pattern from existing resource
981 * It not only save the interval patterns,
982 * but also return the extended skeleton and its best match skeleton.
984 * @param field largest different calendar field
985 * @param skeleton skeleton
986 * @param bestSkeleton the best match skeleton which has interval pattern
987 * defined in resource
988 * @param differenceInfo the difference between skeleton and best skeleton
989 * 0 means the best matched skeleton is the same as input skeleton
990 * 1 means the fields are the same, but field width are different
991 * 2 means the only difference between fields are v/z,
992 * -1 means there are other fields difference
994 * @param extendedSkeleton extended skeleton
995 * @param extendedBestSkeleton extended best match skeleton
996 * @return whether the interval pattern is found
997 * through extending skeleton or not.
998 * TRUE if interval pattern is found by
999 * extending skeleton, FALSE otherwise.
1001 UBool
setIntervalPattern(UCalendarDateFields field
,
1002 const UnicodeString
* skeleton
,
1003 const UnicodeString
* bestSkeleton
,
1004 int8_t differenceInfo
,
1005 UnicodeString
* extendedSkeleton
= NULL
,
1006 UnicodeString
* extendedBestSkeleton
= NULL
);
1009 * Adjust field width in best match interval pattern to match
1010 * the field width in input skeleton.
1012 * TODO (xji) make a general solution
1013 * The adjusting rule can be:
1016 * 3. default adjust, which means adjust according to the following rules
1017 * 3.1 always adjust string, such as MMM and MMMM
1018 * 3.2 never adjust between string and numeric, such as MM and MMM
1019 * 3.3 always adjust year
1020 * 3.4 do not adjust 'd', 'h', or 'm' if h presents
1021 * 3.5 do not adjust 'M' if it is numeric(?)
1023 * Since date interval format is well-formed format,
1024 * date and time skeletons are normalized previously,
1025 * till this stage, the adjust here is only "adjust strings, such as MMM
1026 * and MMMM, EEE and EEEE.
1028 * @param inputSkeleton the input skeleton
1029 * @param bestMatchSkeleton the best match skeleton
1030 * @param bestMatchIntervalPattern the best match interval pattern
1031 * @param differenceInfo the difference between 2 skeletons
1032 * 1 means only field width differs
1033 * 2 means v/z exchange
1034 * @param adjustedIntervalPattern adjusted interval pattern
1036 static void U_EXPORT2
adjustFieldWidth(
1037 const UnicodeString
& inputSkeleton
,
1038 const UnicodeString
& bestMatchSkeleton
,
1039 const UnicodeString
& bestMatchIntervalPattern
,
1040 int8_t differenceInfo
,
1041 UnicodeString
& adjustedIntervalPattern
);
1044 * Concat a single date pattern with a time interval pattern,
1045 * set it into the intervalPatterns, while field is time field.
1046 * This is used to handle time interval patterns on skeleton with
1047 * both time and date. Present the date followed by
1048 * the range expression for the time.
1049 * @param format date and time format
1050 * @param datePattern date pattern
1051 * @param field time calendar field: AM_PM, HOUR, MINUTE
1052 * @param status output param set to success/failure code on exit
1054 void concatSingleDate2TimeInterval(UnicodeString
& format
,
1055 const UnicodeString
& datePattern
,
1056 UCalendarDateFields field
,
1057 UErrorCode
& status
);
1060 * check whether a calendar field present in a skeleton.
1061 * @param field calendar field need to check
1062 * @param skeleton given skeleton on which to check the calendar field
1063 * @return true if field present in a skeleton.
1065 static UBool U_EXPORT2
fieldExistsInSkeleton(UCalendarDateFields field
,
1066 const UnicodeString
& skeleton
);
1070 * Split interval patterns into 2 part.
1071 * @param intervalPattern interval pattern
1072 * @return the index in interval pattern which split the pattern into 2 part
1074 static int32_t U_EXPORT2
splitPatternInto2Part(const UnicodeString
& intervalPattern
);
1078 * Break interval patterns as 2 part and save them into pattern info.
1079 * @param field calendar field
1080 * @param intervalPattern interval pattern
1082 void setIntervalPattern(UCalendarDateFields field
,
1083 const UnicodeString
& intervalPattern
);
1087 * Break interval patterns as 2 part and save them into pattern info.
1088 * @param field calendar field
1089 * @param intervalPattern interval pattern
1090 * @param laterDateFirst whether later date appear first in interval pattern
1092 void setIntervalPattern(UCalendarDateFields field
,
1093 const UnicodeString
& intervalPattern
,
1094 UBool laterDateFirst
);
1098 * Set pattern information.
1100 * @param field calendar field
1101 * @param firstPart the first part in interval pattern
1102 * @param secondPart the second part in interval pattern
1103 * @param laterDateFirst whether the first date in intervalPattern
1104 * is earlier date or later date
1106 void setPatternInfo(UCalendarDateFields field
,
1107 const UnicodeString
* firstPart
,
1108 const UnicodeString
* secondPart
,
1109 UBool laterDateFirst
);
1112 * Format 2 Calendars to produce a string.
1113 * Implementation of the similar public format function.
1114 * Must be called with gFormatterMutex already locked.
1116 * Note: "fromCalendar" and "toCalendar" are not const,
1117 * since calendar is not const in SimpleDateFormat::format(Calendar&),
1119 * @param fromCalendar calendar set to the from date in date interval
1120 * to be formatted into date interval string
1121 * @param toCalendar calendar set to the to date in date interval
1122 * to be formatted into date interval string
1123 * @param appendTo Output parameter to receive result.
1124 * Result is appended to existing contents.
1125 * @param firstIndex 0 if the first output date is fromCalendar;
1126 * 1 if it corresponds to toCalendar;
1127 * -1 if there is only one date printed.
1128 * @param fphandler Handler for field position information.
1129 * The fields will be from the UDateFormatField enum.
1130 * @param status Output param filled with success/failure status.
1131 * Caller needs to make sure it is SUCCESS
1132 * at the function entrance
1133 * @return Reference to 'appendTo' parameter.
1134 * @internal (private)
1136 UnicodeString
& formatImpl(Calendar
& fromCalendar
,
1137 Calendar
& toCalendar
,
1138 UnicodeString
& appendTo
,
1140 FieldPositionHandler
& fphandler
,
1141 UErrorCode
& status
) const ;
1143 /** Version of formatImpl for DateInterval. */
1144 UnicodeString
& formatIntervalImpl(const DateInterval
& dtInterval
,
1145 UnicodeString
& appendTo
,
1147 FieldPositionHandler
& fphandler
,
1148 UErrorCode
& status
) const;
1151 // from calendar field to pattern letter
1152 static const char16_t fgCalendarFieldToPatternLetter
[];
1156 * The interval patterns for this locale.
1158 DateIntervalInfo
* fInfo
;
1161 * The DateFormat object used to format single pattern
1163 SimpleDateFormat
* fDateFormat
;
1166 * The 2 calendars with the from and to date.
1167 * could re-use the calendar in fDateFormat,
1168 * but keeping 2 calendars make it clear and clean.
1170 Calendar
* fFromCalendar
;
1171 Calendar
* fToCalendar
;
1176 * Following are interval information relevant (locale) to this formatter.
1178 UnicodeString fSkeleton
;
1179 PatternInfo fIntervalPatterns
[DateIntervalInfo::kIPI_MAX_INDEX
];
1182 * Patterns for fallback formatting.
1184 UnicodeString
* fDatePattern
;
1185 UnicodeString
* fTimePattern
;
1186 UnicodeString
* fDateTimeFormat
;
1191 int32_t fMinimizeType
;
1193 UDisplayContext fCapitalizationContext
;
1197 DateIntervalFormat::operator!=(const Format
& other
) const {
1198 return !operator==(other
);
1202 #endif // U_SHOW_CPLUSPLUS_API
1204 #endif /* #if !UCONFIG_NO_FORMATTING */
1206 #endif // _DTITVFMT_H__