Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-03 10:23:58

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /********************************************************************************
0004 * Copyright (C) 2008-2016, International Business Machines Corporation and
0005 * others. All Rights Reserved.
0006 *******************************************************************************
0007 *
0008 * File DTITVFMT.H
0009 *
0010 *******************************************************************************
0011 */
0012 
0013 #ifndef __DTITVFMT_H__
0014 #define __DTITVFMT_H__
0015 
0016 
0017 #include "unicode/utypes.h"
0018 
0019 #if U_SHOW_CPLUSPLUS_API
0020 
0021 /**
0022  * \file
0023  * \brief C++ API: Format and parse date interval in a language-independent manner.
0024  */
0025 
0026 #if !UCONFIG_NO_FORMATTING
0027 
0028 #include "unicode/ucal.h"
0029 #include "unicode/smpdtfmt.h"
0030 #include "unicode/dtintrv.h"
0031 #include "unicode/dtitvinf.h"
0032 #include "unicode/dtptngen.h"
0033 #include "unicode/formattedvalue.h"
0034 #include "unicode/udisplaycontext.h"
0035 
0036 U_NAMESPACE_BEGIN
0037 
0038 
0039 class FormattedDateIntervalData;
0040 class DateIntervalFormat;
0041 
0042 /**
0043  * An immutable class containing the result of a date interval formatting operation.
0044  *
0045  * Instances of this class are immutable and thread-safe.
0046  *
0047  * When calling nextPosition():
0048  * The fields are returned from left to right. The special field category
0049  * UFIELD_CATEGORY_DATE_INTERVAL_SPAN is used to indicate which datetime
0050  * primitives came from which arguments: 0 means fromCalendar, and 1 means
0051  * toCalendar. The span category will always occur before the
0052  * corresponding fields in UFIELD_CATEGORY_DATE
0053  * in the nextPosition() iterator.
0054  *
0055  * Not intended for public subclassing.
0056  *
0057  * @stable ICU 64
0058  */
0059 class U_I18N_API FormattedDateInterval : public UMemory, public FormattedValue {
0060   public:
0061     /**
0062      * Default constructor; makes an empty FormattedDateInterval.
0063      * @stable ICU 64
0064      */
0065     FormattedDateInterval() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
0066 
0067     /**
0068      * Move constructor: Leaves the source FormattedDateInterval in an undefined state.
0069      * @stable ICU 64
0070      */
0071     FormattedDateInterval(FormattedDateInterval&& src) noexcept;
0072 
0073     /**
0074      * Destruct an instance of FormattedDateInterval.
0075      * @stable ICU 64
0076      */
0077     virtual ~FormattedDateInterval() override;
0078 
0079     /** Copying not supported; use move constructor instead. */
0080     FormattedDateInterval(const FormattedDateInterval&) = delete;
0081 
0082     /** Copying not supported; use move assignment instead. */
0083     FormattedDateInterval& operator=(const FormattedDateInterval&) = delete;
0084 
0085     /**
0086      * Move assignment: Leaves the source FormattedDateInterval in an undefined state.
0087      * @stable ICU 64
0088      */
0089     FormattedDateInterval& operator=(FormattedDateInterval&& src) noexcept;
0090 
0091     /** @copydoc FormattedValue::toString() */
0092     UnicodeString toString(UErrorCode& status) const override;
0093 
0094     /** @copydoc FormattedValue::toTempString() */
0095     UnicodeString toTempString(UErrorCode& status) const override;
0096 
0097     /** @copydoc FormattedValue::appendTo() */
0098     Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override;
0099 
0100     /** @copydoc FormattedValue::nextPosition() */
0101     UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
0102 
0103   private:
0104     FormattedDateIntervalData *fData;
0105     UErrorCode fErrorCode;
0106     explicit FormattedDateInterval(FormattedDateIntervalData *results)
0107         : fData(results), fErrorCode(U_ZERO_ERROR) {}
0108     explicit FormattedDateInterval(UErrorCode errorCode)
0109         : fData(nullptr), fErrorCode(errorCode) {}
0110     friend class DateIntervalFormat;
0111 };
0112 
0113 
0114 /**
0115  * DateIntervalFormat is a class for formatting and parsing date
0116  * intervals in a language-independent manner.
0117  * Only formatting is supported, parsing is not supported.
0118  *
0119  * <P>
0120  * Date interval means from one date to another date,
0121  * for example, from "Jan 11, 2008" to "Jan 18, 2008".
0122  * We introduced class DateInterval to represent it.
0123  * DateInterval is a pair of UDate, which is
0124  * the standard milliseconds since 24:00 GMT, Jan 1, 1970.
0125  *
0126  * <P>
0127  * DateIntervalFormat formats a DateInterval into
0128  * text as compactly as possible.
0129  * For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008"
0130  * is "Jan 11-18, 2008" for English.
0131  * And it parses text into DateInterval,
0132  * although initially, parsing is not supported.
0133  *
0134  * <P>
0135  * There is no structural information in date time patterns.
0136  * For any punctuations and string literals inside a date time pattern,
0137  * we do not know whether it is just a separator, or a prefix, or a suffix.
0138  * Without such information, so, it is difficult to generate a sub-pattern
0139  * (or super-pattern) by algorithm.
0140  * So, formatting a DateInterval is pattern-driven. It is very
0141  * similar to formatting in SimpleDateFormat.
0142  * We introduce class DateIntervalInfo to save date interval
0143  * patterns, similar to date time pattern in SimpleDateFormat.
0144  *
0145  * <P>
0146  * Logically, the interval patterns are mappings
0147  * from (skeleton, the_largest_different_calendar_field)
0148  * to (date_interval_pattern).
0149  *
0150  * <P>
0151  * A skeleton
0152  * <ol>
0153  * <li>
0154  * only keeps the field pattern letter and ignores all other parts
0155  * in a pattern, such as space, punctuations, and string literals.
0156  * </li>
0157  * <li>
0158  * hides the order of fields.
0159  * </li>
0160  * <li>
0161  * might hide a field's pattern letter length.
0162  * </li>
0163  * </ol>
0164  *
0165  * For those non-digit calendar fields, the pattern letter length is
0166  * important, such as MMM, MMMM, and MMMMM; EEE and EEEE,
0167  * and the field's pattern letter length is honored.
0168  *
0169  * For the digit calendar fields,  such as M or MM, d or dd, yy or yyyy,
0170  * the field pattern length is ignored and the best match, which is defined
0171  * in date time patterns, will be returned without honor the field pattern
0172  * letter length in skeleton.
0173  *
0174  * <P>
0175  * The calendar fields we support for interval formatting are:
0176  * year, month, date, day-of-week, am-pm, hour, hour-of-day, minute, second,
0177  * and millisecond.
0178  * (though we do not currently have specific intervalFormat date for skeletons
0179  * with seconds and millisecond).
0180  * Those calendar fields can be defined in the following order:
0181  * year >  month > date > hour (in day) >  minute > second > millisecond
0182  *
0183  * The largest different calendar fields between 2 calendars is the
0184  * first different calendar field in above order.
0185  *
0186  * For example: the largest different calendar fields between "Jan 10, 2007"
0187  * and "Feb 20, 2008" is year.
0188  *
0189  * <P>
0190  * For other calendar fields, the compact interval formatting is not
0191  * supported. And the interval format will be fall back to fall-back
0192  * patterns, which is mostly "{date0} - {date1}".
0193  *
0194  * <P>
0195  * There is a set of pre-defined static skeleton strings.
0196  * There are pre-defined interval patterns for those pre-defined skeletons
0197  * in locales' resource files.
0198  * For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is  &quot;yMMMd&quot;,
0199  * in  en_US, if the largest different calendar field between date1 and date2
0200  * is &quot;year&quot;, the date interval pattern  is &quot;MMM d, yyyy - MMM d, yyyy&quot;,
0201  * such as &quot;Jan 10, 2007 - Jan 10, 2008&quot;.
0202  * If the largest different calendar field between date1 and date2 is &quot;month&quot;,
0203  * the date interval pattern is &quot;MMM d - MMM d, yyyy&quot;,
0204  * such as &quot;Jan 10 - Feb 10, 2007&quot;.
0205  * If the largest different calendar field between date1 and date2 is &quot;day&quot;,
0206  * the date interval pattern is &quot;MMM d-d, yyyy&quot;, such as &quot;Jan 10-20, 2007&quot;.
0207  *
0208  * For date skeleton, the interval patterns when year, or month, or date is
0209  * different are defined in resource files.
0210  * For time skeleton, the interval patterns when am/pm, or hour, or minute is
0211  * different are defined in resource files.
0212  *
0213  * <P>
0214  * If a skeleton is not found in a locale's DateIntervalInfo, which means
0215  * the interval patterns for the skeleton is not defined in resource file,
0216  * the interval pattern will falls back to the interval "fallback" pattern
0217  * defined in resource file.
0218  * If the interval "fallback" pattern is not defined, the default fall-back
0219  * is "{date0} - {data1}".
0220  *
0221  * <P>
0222  * For the combination of date and time,
0223  * The rule to generate interval patterns are:
0224  * <ol>
0225  * <li>
0226  *    when the year, month, or day differs, falls back to fall-back
0227  *    interval pattern, which mostly is the concatenate the two original
0228  *    expressions with a separator between,
0229  *    For example, interval pattern from "Jan 10, 2007 10:10 am"
0230  *    to "Jan 11, 2007 10:10am" is
0231  *    "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
0232  * </li>
0233  * <li>
0234  *    otherwise, present the date followed by the range expression
0235  *    for the time.
0236  *    For example, interval pattern from "Jan 10, 2007 10:10 am"
0237  *    to "Jan 10, 2007 11:10am" is "Jan 10, 2007 10:10 am - 11:10am"
0238  * </li>
0239  * </ol>
0240  *
0241  *
0242  * <P>
0243  * If two dates are the same, the interval pattern is the single date pattern.
0244  * For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is
0245  * "Jan 10, 2007".
0246  *
0247  * Or if the presenting fields between 2 dates have the exact same values,
0248  * the interval pattern is the  single date pattern.
0249  * For example, if user only requests year and month,
0250  * the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007".
0251  *
0252  * <P>
0253  * DateIntervalFormat needs the following information for correct
0254  * formatting: time zone, calendar type, pattern, date format symbols,
0255  * and date interval patterns.
0256  * It can be instantiated in 2 ways:
0257  * <ol>
0258  * <li>
0259  *    create an instance using default or given locale plus given skeleton.
0260  *    Users are encouraged to created date interval formatter this way and
0261  *    to use the pre-defined skeleton macros, such as
0262  *    UDAT_YEAR_NUM_MONTH, which consists the calendar fields and
0263  *    the format style.
0264  * </li>
0265  * <li>
0266  *    create an instance using default or given locale plus given skeleton
0267  *    plus a given DateIntervalInfo.
0268  *    This factory method is for powerful users who want to provide their own
0269  *    interval patterns.
0270  *    Locale provides the timezone, calendar, and format symbols information.
0271  *    Local plus skeleton provides full pattern information.
0272  *    DateIntervalInfo provides the date interval patterns.
0273  * </li>
0274  * </ol>
0275  *
0276  * <P>
0277  * For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc.
0278  * DateIntervalFormat uses the same syntax as that of
0279  * DateTime format.
0280  *
0281  * <P>
0282  * Code Sample: general usage
0283  * <pre>
0284  * \code
0285  *   // the date interval object which the DateIntervalFormat formats on
0286  *   // and parses into
0287  *   DateInterval*  dtInterval = new DateInterval(1000*3600*24, 1000*3600*24*2);
0288  *   UErrorCode status = U_ZERO_ERROR;
0289  *   DateIntervalFormat* dtIntervalFmt = DateIntervalFormat::createInstance(
0290  *                           UDAT_YEAR_MONTH_DAY,
0291  *                           Locale("en", "GB", ""), status);
0292  *   UnicodeUnicodeString dateIntervalString;
0293  *   FieldPosition pos = 0;
0294  *   // formatting
0295  *   dtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status);
0296  *   delete dtIntervalFmt;
0297  * \endcode
0298  * </pre>
0299  */
0300 class U_I18N_API DateIntervalFormat : public Format {
0301 public:
0302 
0303     /**
0304      * Construct a DateIntervalFormat from skeleton and  the default locale.
0305      *
0306      * This is a convenient override of
0307      * createInstance(const UnicodeString& skeleton, const Locale& locale,
0308      *                UErrorCode&)
0309      * with the value of locale as default locale.
0310      *
0311      * @param skeleton  the skeleton on which interval format based.
0312      * @param status    output param set to success/failure code on exit
0313      * @return          a date time interval formatter which the caller owns.
0314      * @stable ICU 4.0
0315      */
0316     static DateIntervalFormat* U_EXPORT2 createInstance(
0317                                                const UnicodeString& skeleton,
0318                                                UErrorCode& status);
0319 
0320     /**
0321      * Construct a DateIntervalFormat from skeleton and a given locale.
0322      * <P>
0323      * In this factory method,
0324      * the date interval pattern information is load from resource files.
0325      * Users are encouraged to created date interval formatter this way and
0326      * to use the pre-defined skeleton macros.
0327      *
0328      * <P>
0329      * There are pre-defined skeletons (defined in udate.h) having predefined
0330      * interval patterns in resource files.
0331      * Users are encouraged to use those macros.
0332      * For example:
0333      * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
0334      *
0335      * The given Locale provides the interval patterns.
0336      * For example, for en_GB, if skeleton is UDAT_YEAR_ABBR_MONTH_WEEKDAY_DAY,
0337      * which is "yMMMEEEd",
0338      * the interval patterns defined in resource file to above skeleton are:
0339      * "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs,
0340      * "EEE, d MMM - EEE, d MMM, yyyy" for month differs,
0341      * "EEE, d - EEE, d MMM, yyyy" for day differs,
0342      * @param skeleton  the skeleton on which the interval format is based.
0343      * @param locale    the given locale
0344      * @param status    output param set to success/failure code on exit
0345      * @return          a date time interval formatter which the caller owns.
0346      * @stable ICU 4.0
0347      */
0348 
0349     static DateIntervalFormat* U_EXPORT2 createInstance(
0350                                                const UnicodeString& skeleton,
0351                                                const Locale& locale,
0352                                                UErrorCode& status);
0353 
0354     /**
0355      * Construct a DateIntervalFormat from skeleton
0356      *  DateIntervalInfo, and default locale.
0357      *
0358      * This is a convenient override of
0359      * createInstance(const UnicodeString& skeleton, const Locale& locale,
0360      *                const DateIntervalInfo& dtitvinf, UErrorCode&)
0361      * with the locale value as default locale.
0362      *
0363      * @param skeleton  the skeleton on which interval format based.
0364      * @param dtitvinf  the DateIntervalInfo object.
0365      * @param status    output param set to success/failure code on exit
0366      * @return          a date time interval formatter which the caller owns.
0367      * @stable ICU 4.0
0368      */
0369     static DateIntervalFormat* U_EXPORT2 createInstance(
0370                                               const UnicodeString& skeleton,
0371                                               const DateIntervalInfo& dtitvinf,
0372                                               UErrorCode& status);
0373 
0374     /**
0375      * Construct a DateIntervalFormat from skeleton
0376      * a DateIntervalInfo, and the given locale.
0377      *
0378      * <P>
0379      * In this factory method, user provides its own date interval pattern
0380      * information, instead of using those pre-defined data in resource file.
0381      * This factory method is for powerful users who want to provide their own
0382      * interval patterns.
0383      * <P>
0384      * There are pre-defined skeletons (defined in udate.h) having predefined
0385      * interval patterns in resource files.
0386      * Users are encouraged to use those macros.
0387      * For example:
0388      * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
0389      *
0390      * The DateIntervalInfo provides the interval patterns.
0391      * and the DateIntervalInfo ownership remains to the caller.
0392      *
0393      * User are encouraged to set default interval pattern in DateIntervalInfo
0394      * as well, if they want to set other interval patterns ( instead of
0395      * reading the interval patterns from resource files).
0396      * When the corresponding interval pattern for a largest calendar different
0397      * field is not found ( if user not set it ), interval format fallback to
0398      * the default interval pattern.
0399      * If user does not provide default interval pattern, it fallback to
0400      * "{date0} - {date1}"
0401      *
0402      * @param skeleton  the skeleton on which interval format based.
0403      * @param locale    the given locale
0404      * @param dtitvinf  the DateIntervalInfo object.
0405      * @param status    output param set to success/failure code on exit
0406      * @return          a date time interval formatter which the caller owns.
0407      * @stable ICU 4.0
0408      */
0409     static DateIntervalFormat* U_EXPORT2 createInstance(
0410                                               const UnicodeString& skeleton,
0411                                               const Locale& locale,
0412                                               const DateIntervalInfo& dtitvinf,
0413                                               UErrorCode& status);
0414 
0415     /**
0416      * Destructor.
0417      * @stable ICU 4.0
0418      */
0419     virtual ~DateIntervalFormat();
0420 
0421     /**
0422      * Clone this Format object polymorphically. The caller owns the result and
0423      * should delete it when done.
0424      * @return    A copy of the object.
0425      * @stable ICU 4.0
0426      */
0427     virtual DateIntervalFormat* clone() const override;
0428 
0429     /**
0430      * Return true if the given Format objects are semantically equal. Objects
0431      * of different subclasses are considered unequal.
0432      * @param other    the object to be compared with.
0433      * @return         true if the given Format objects are semantically equal.
0434      * @stable ICU 4.0
0435      */
0436     virtual bool operator==(const Format& other) const override;
0437 
0438     /**
0439      * Return true if the given Format objects are not semantically equal.
0440      * Objects of different subclasses are considered unequal.
0441      * @param other the object to be compared with.
0442      * @return      true if the given Format objects are not semantically equal.
0443      * @stable ICU 4.0
0444      */
0445     bool operator!=(const Format& other) const;
0446 
0447 
0448     using Format::format;
0449 
0450     /**
0451      * Format an object to produce a string. This method handles Formattable
0452      * objects with a DateInterval type.
0453      * If a the Formattable object type is not a DateInterval,
0454      * then it returns a failing UErrorCode.
0455      *
0456      * @param obj               The object to format.
0457      *                          Must be a DateInterval.
0458      * @param appendTo          Output parameter to receive result.
0459      *                          Result is appended to existing contents.
0460      * @param fieldPosition     On input: an alignment field, if desired.
0461      *                          On output: the offsets of the alignment field.
0462      *                          There may be multiple instances of a given field type
0463      *                          in an interval format; in this case the fieldPosition
0464      *                          offsets refer to the first instance.
0465      * @param status            Output param filled with success/failure status.
0466      * @return                  Reference to 'appendTo' parameter.
0467      * @stable ICU 4.0
0468      */
0469     virtual UnicodeString& format(const Formattable& obj,
0470                                   UnicodeString& appendTo,
0471                                   FieldPosition& fieldPosition,
0472                                   UErrorCode& status) const override;
0473 
0474 
0475 
0476     /**
0477      * Format a DateInterval to produce a string.
0478      *
0479      * @param dtInterval        DateInterval to be formatted.
0480      * @param appendTo          Output parameter to receive result.
0481      *                          Result is appended to existing contents.
0482      * @param fieldPosition     On input: an alignment field, if desired.
0483      *                          On output: the offsets of the alignment field.
0484      *                          There may be multiple instances of a given field type
0485      *                          in an interval format; in this case the fieldPosition
0486      *                          offsets refer to the first instance.
0487      * @param status            Output param filled with success/failure status.
0488      * @return                  Reference to 'appendTo' parameter.
0489      * @stable ICU 4.0
0490      */
0491     UnicodeString& format(const DateInterval* dtInterval,
0492                           UnicodeString& appendTo,
0493                           FieldPosition& fieldPosition,
0494                           UErrorCode& status) const ;
0495 
0496     /**
0497      * Format a DateInterval to produce a FormattedDateInterval.
0498      *
0499      * The FormattedDateInterval exposes field information about the formatted string.
0500      *
0501      * @param dtInterval        DateInterval to be formatted.
0502      * @param status            Set if an error occurs.
0503      * @return                  A FormattedDateInterval containing the format result.
0504      * @stable ICU 64
0505      */
0506     FormattedDateInterval formatToValue(
0507         const DateInterval& dtInterval,
0508         UErrorCode& status) const;
0509 
0510     /**
0511      * Format 2 Calendars to produce a string.
0512      *
0513      * Note: "fromCalendar" and "toCalendar" are not const,
0514      * since calendar is not const in  SimpleDateFormat::format(Calendar&),
0515      *
0516      * @param fromCalendar      calendar set to the from date in date interval
0517      *                          to be formatted into date interval string
0518      * @param toCalendar        calendar set to the to date in date interval
0519      *                          to be formatted into date interval string
0520      * @param appendTo          Output parameter to receive result.
0521      *                          Result is appended to existing contents.
0522      * @param fieldPosition     On input: an alignment field, if desired.
0523      *                          On output: the offsets of the alignment field.
0524      *                          There may be multiple instances of a given field type
0525      *                          in an interval format; in this case the fieldPosition
0526      *                          offsets refer to the first instance.
0527      * @param status            Output param filled with success/failure status.
0528      *                          Caller needs to make sure it is SUCCESS
0529      *                          at the function entrance
0530      * @return                  Reference to 'appendTo' parameter.
0531      * @stable ICU 4.0
0532      */
0533     UnicodeString& format(Calendar& fromCalendar,
0534                           Calendar& toCalendar,
0535                           UnicodeString& appendTo,
0536                           FieldPosition& fieldPosition,
0537                           UErrorCode& status) const ;
0538 
0539     /**
0540      * Format 2 Calendars to produce a FormattedDateInterval.
0541      *
0542      * The FormattedDateInterval exposes field information about the formatted string.
0543      *
0544      * Note: "fromCalendar" and "toCalendar" are not const,
0545      * since calendar is not const in  SimpleDateFormat::format(Calendar&),
0546      *
0547      * @param fromCalendar      calendar set to the from date in date interval
0548      *                          to be formatted into date interval string
0549      * @param toCalendar        calendar set to the to date in date interval
0550      *                          to be formatted into date interval string
0551      * @param status            Set if an error occurs.
0552      * @return                  A FormattedDateInterval containing the format result.
0553      * @stable ICU 64
0554      */
0555     FormattedDateInterval formatToValue(
0556         Calendar& fromCalendar,
0557         Calendar& toCalendar,
0558         UErrorCode& status) const;
0559 
0560     /**
0561      * Date interval parsing is not supported. Please do not use.
0562      * <P>
0563      * This method should handle parsing of
0564      * date time interval strings into Formattable objects with
0565      * DateInterval type, which is a pair of UDate.
0566      * <P>
0567      * Before calling, set parse_pos.index to the offset you want to start
0568      * parsing at in the source. After calling, parse_pos.index is the end of
0569      * the text you parsed. If error occurs, index is unchanged.
0570      * <P>
0571      * When parsing, leading whitespace is discarded (with a successful parse),
0572      * while trailing whitespace is left as is.
0573      * <P>
0574      * See Format::parseObject() for more.
0575      *
0576      * @param source    The string to be parsed into an object.
0577      * @param result    Formattable to be set to the parse result.
0578      *                  If parse fails, return contents are undefined.
0579      * @param parse_pos The position to start parsing at. Since no parsing
0580      *                  is supported, upon return this param is unchanged.
0581      * @return          A newly created Formattable* object, or nullptr
0582      *                  on failure.  The caller owns this and should
0583      *                  delete it when done.
0584      * @internal ICU 4.0
0585      */
0586     virtual void parseObject(const UnicodeString& source,
0587                              Formattable& result,
0588                              ParsePosition& parse_pos) const override;
0589 
0590 
0591     /**
0592      * Gets the date time interval patterns.
0593      * @return the date time interval patterns associated with
0594      * this date interval formatter.
0595      * @stable ICU 4.0
0596      */
0597     const DateIntervalInfo* getDateIntervalInfo() const;
0598 
0599     /**
0600      * Set the date time interval patterns.
0601      * @param newIntervalPatterns   the given interval patterns to copy.
0602      * @param status          output param set to success/failure code on exit
0603      * @stable ICU 4.0
0604      */
0605     void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns,
0606                              UErrorCode& status);
0607 
0608 
0609     /**
0610      * Gets the date formatter. The DateIntervalFormat instance continues to own
0611      * the returned DateFormatter object, and will use and possibly modify it
0612      * during format operations. In a multi-threaded environment, the returned
0613      * DateFormat can only be used if it is certain that no other threads are
0614      * concurrently using this DateIntervalFormatter, even for nominally const
0615      * functions.
0616      *
0617      * @return the date formatter associated with this date interval formatter.
0618      * @stable ICU 4.0
0619      */
0620     const DateFormat* getDateFormat() const;
0621 
0622     /**
0623      * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar.
0624      * @return the time zone associated with the calendar of DateIntervalFormat.
0625      * @stable ICU 4.8
0626      */
0627     virtual const TimeZone& getTimeZone() const;
0628 
0629     /**
0630      * Sets the time zone for the calendar used by this DateIntervalFormat object. The
0631      * caller no longer owns the TimeZone object and should not delete it after this call.
0632      * @param zoneToAdopt the TimeZone to be adopted.
0633      * @stable ICU 4.8
0634      */
0635     virtual void adoptTimeZone(TimeZone* zoneToAdopt);
0636 
0637     /**
0638      * Sets the time zone for the calendar used by this DateIntervalFormat object.
0639      * @param zone the new time zone.
0640      * @stable ICU 4.8
0641      */
0642     virtual void setTimeZone(const TimeZone& zone);
0643 
0644     /**
0645      * Set a particular UDisplayContext value in the formatter, such as
0646      * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. This causes the formatted
0647      * result to be capitalized appropriately for the context in which
0648      * it is intended to be used, considering both the locale and the
0649      * type of field at the beginning of the formatted result.
0650      * @param value The UDisplayContext value to set.
0651      * @param status Input/output status. If at entry this indicates a failure
0652      *               status, the function will do nothing; otherwise this will be
0653      *               updated with any new status from the function.
0654      * @stable ICU 68
0655      */
0656     virtual void setContext(UDisplayContext value, UErrorCode& status);
0657 
0658     /**
0659      * Get the formatter's UDisplayContext value for the specified UDisplayContextType,
0660      * such as UDISPCTX_TYPE_CAPITALIZATION.
0661      * @param type The UDisplayContextType whose value to return
0662      * @param status Input/output status. If at entry this indicates a failure
0663      *               status, the function will do nothing; otherwise this will be
0664      *               updated with any new status from the function.
0665      * @return The UDisplayContextValue for the specified type.
0666      * @stable ICU 68
0667      */
0668     virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
0669 
0670     /**
0671      * Return the class ID for this class. This is useful only for comparing to
0672      * a return value from getDynamicClassID(). For example:
0673      * <pre>
0674      * .   Base* polymorphic_pointer = createPolymorphicObject();
0675      * .   if (polymorphic_pointer->getDynamicClassID() ==
0676      * .       erived::getStaticClassID()) ...
0677      * </pre>
0678      * @return          The class ID for all objects of this class.
0679      * @stable ICU 4.0
0680      */
0681     static UClassID U_EXPORT2 getStaticClassID();
0682 
0683     /**
0684      * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
0685      * method is to implement a simple version of RTTI, since not all C++
0686      * compilers support genuine RTTI. Polymorphic operator==() and clone()
0687      * methods call this method.
0688      *
0689      * @return          The class ID for this object. All objects of a
0690      *                  given class have the same class ID.  Objects of
0691      *                  other classes have different class IDs.
0692      * @stable ICU 4.0
0693      */
0694     virtual UClassID getDynamicClassID() const override;
0695 
0696 protected:
0697 
0698     /**
0699      * Copy constructor.
0700      * @stable ICU 4.0
0701      */
0702     DateIntervalFormat(const DateIntervalFormat&);
0703 
0704     /**
0705      * Assignment operator.
0706      * @stable ICU 4.0
0707      */
0708     DateIntervalFormat& operator=(const DateIntervalFormat&);
0709 
0710 private:
0711 
0712     /*
0713      * This is for ICU internal use only. Please do not use.
0714      * Save the interval pattern information.
0715      * Interval pattern consists of 2 single date patterns and the separator.
0716      * For example, interval pattern "MMM d - MMM d, yyyy" consists
0717      * a single date pattern "MMM d", another single date pattern "MMM d, yyyy",
0718      * and a separator "-".
0719      * The pattern is divided into 2 parts. For above example,
0720      * the first part is "MMM d - ", and the second part is "MMM d, yyyy".
0721      * Also, the first date appears in an interval pattern could be
0722      * the earlier date or the later date.
0723      * And such information is saved in the interval pattern as well.
0724      */
0725     struct PatternInfo {
0726         UnicodeString firstPart;
0727         UnicodeString secondPart;
0728         /**
0729          * Whether the first date in interval pattern is later date or not.
0730          * Fallback format set the default ordering.
0731          * And for a particular interval pattern, the order can be
0732          * overridden by prefixing the interval pattern with "latestFirst:" or
0733          * "earliestFirst:"
0734          * For example, given 2 date, Jan 10, 2007 to Feb 10, 2007.
0735          * if the fallback format is "{0} - {1}",
0736          * and the pattern is "d MMM - d MMM yyyy", the interval format is
0737          * "10 Jan - 10 Feb, 2007".
0738          * If the pattern is "latestFirst:d MMM - d MMM yyyy",
0739          * the interval format is "10 Feb - 10 Jan, 2007"
0740          */
0741         UBool         laterDateFirst;
0742     };
0743 
0744 
0745     /**
0746      * default constructor
0747      * @internal (private)
0748      */
0749     DateIntervalFormat();
0750 
0751     /**
0752      * Construct a DateIntervalFormat from DateFormat,
0753      * a DateIntervalInfo, and skeleton.
0754      * DateFormat provides the timezone, calendar,
0755      * full pattern, and date format symbols information.
0756      * It should be a SimpleDateFormat object which
0757      * has a pattern in it.
0758      * the DateIntervalInfo provides the interval patterns.
0759      *
0760      * Note: the DateIntervalFormat takes ownership of both
0761      * DateFormat and DateIntervalInfo objects.
0762      * Caller should not delete them.
0763      *
0764      * @param locale    the locale of this date interval formatter.
0765      * @param dtItvInfo the DateIntervalInfo object to be adopted.
0766      * @param skeleton  the skeleton of the date formatter
0767      * @param status    output param set to success/failure code on exit
0768      */
0769     DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo,
0770                        const UnicodeString* skeleton, UErrorCode& status);
0771 
0772 
0773     /**
0774      * Construct a DateIntervalFormat from DateFormat
0775      * and a DateIntervalInfo.
0776      *
0777      * It is a wrapper of the constructor.
0778      *
0779      * @param locale    the locale of this date interval formatter.
0780      * @param dtitvinf  the DateIntervalInfo object to be adopted.
0781      * @param skeleton  the skeleton of this formatter.
0782      * @param status    Output param set to success/failure code.
0783      * @return          a date time interval formatter which the caller owns.
0784      */
0785     static DateIntervalFormat* U_EXPORT2 create(const Locale& locale,
0786                                                 DateIntervalInfo* dtitvinf,
0787                                                 const UnicodeString* skeleton,
0788                                                 UErrorCode& status);
0789 
0790     /**
0791      *  Below are for generating interval patterns local to the formatter
0792      */
0793 
0794     /** Like fallbackFormat, but only formats the range part of the fallback. */
0795     void fallbackFormatRange(
0796         Calendar& fromCalendar,
0797         Calendar& toCalendar,
0798         UnicodeString& appendTo,
0799         int8_t& firstIndex,
0800         FieldPositionHandler& fphandler,
0801         UErrorCode& status) const;
0802 
0803     /**
0804      * Format 2 Calendars using fall-back interval pattern
0805      *
0806      * The full pattern used in this fall-back format is the
0807      * full pattern of the date formatter.
0808      *
0809      * gFormatterMutex must already be locked when calling this function.
0810      *
0811      * @param fromCalendar      calendar set to the from date in date interval
0812      *                          to be formatted into date interval string
0813      * @param toCalendar        calendar set to the to date in date interval
0814      *                          to be formatted into date interval string
0815      * @param fromToOnSameDay   true iff from and to dates are on the same day
0816      *                          (any difference is in ampm/hours or below)
0817      * @param appendTo          Output parameter to receive result.
0818      *                          Result is appended to existing contents.
0819      * @param firstIndex        See formatImpl for more information.
0820      * @param fphandler         See formatImpl for more information.
0821      * @param status            output param set to success/failure code on exit
0822      * @return                  Reference to 'appendTo' parameter.
0823      * @internal (private)
0824      */
0825     UnicodeString& fallbackFormat(Calendar& fromCalendar,
0826                                   Calendar& toCalendar,
0827                                   UBool fromToOnSameDay,
0828                                   UnicodeString& appendTo,
0829                                   int8_t& firstIndex,
0830                                   FieldPositionHandler& fphandler,
0831                                   UErrorCode& status) const;
0832 
0833 
0834 
0835     /**
0836      * Initialize interval patterns locale to this formatter
0837      *
0838      * This code is a bit complicated since
0839      * 1. the interval patterns saved in resource bundle files are interval
0840      *    patterns based on date or time only.
0841      *    It does not have interval patterns based on both date and time.
0842      *    Interval patterns on both date and time are algorithm generated.
0843      *
0844      *    For example, it has interval patterns on skeleton "dMy" and "hm",
0845      *    but it does not have interval patterns on skeleton "dMyhm".
0846      *
0847      *    The rule to generate interval patterns for both date and time skeleton are
0848      *    1) when the year, month, or day differs, concatenate the two original
0849      *    expressions with a separator between,
0850      *    For example, interval pattern from "Jan 10, 2007 10:10 am"
0851      *    to "Jan 11, 2007 10:10am" is
0852      *    "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
0853      *
0854      *    2) otherwise, present the date followed by the range expression
0855      *    for the time.
0856      *    For example, interval pattern from "Jan 10, 2007 10:10 am"
0857      *    to "Jan 10, 2007 11:10am" is
0858      *    "Jan 10, 2007 10:10 am - 11:10am"
0859      *
0860      * 2. even a pattern does not request a certain calendar field,
0861      *    the interval pattern needs to include such field if such fields are
0862      *    different between 2 dates.
0863      *    For example, a pattern/skeleton is "hm", but the interval pattern
0864      *    includes year, month, and date when year, month, and date differs.
0865      *
0866      *
0867      * @param status    output param set to success/failure code on exit
0868      */
0869     void initializePattern(UErrorCode& status);
0870 
0871 
0872 
0873     /**
0874      * Set fall back interval pattern given a calendar field,
0875      * a skeleton, and a date time pattern generator.
0876      * @param field      the largest different calendar field
0877      * @param skeleton   a skeleton
0878      * @param status     output param set to success/failure code on exit
0879      */
0880     void setFallbackPattern(UCalendarDateFields field,
0881                             const UnicodeString& skeleton,
0882                             UErrorCode& status);
0883     
0884 
0885 
0886     /**
0887      * Converts special hour metacharacters (such as 'j') in the skeleton into locale-appropriate
0888      * pattern characters.
0889      *
0890      *
0891      *  @param skeleton               The skeleton to convert
0892      *  @return A copy of the skeleton, which "j" and any other special hour metacharacters converted to the regular ones.
0893      *
0894      */
0895     UnicodeString normalizeHourMetacharacters(const UnicodeString& skeleton) const;
0896 
0897 
0898 
0899     /**
0900      * get separated date and time skeleton from a combined skeleton.
0901      *
0902      * The difference between date skeleton and normalizedDateSkeleton are:
0903      * 1. both 'y' and 'd' are appeared only once in normalizeDateSkeleton
0904      * 2. 'E' and 'EE' are normalized into 'EEE'
0905      * 3. 'MM' is normalized into 'M'
0906      *
0907      ** the difference between time skeleton and normalizedTimeSkeleton are:
0908      * 1. both 'H' and 'h' are normalized as 'h' in normalized time skeleton,
0909      * 2. 'a' is omitted in normalized time skeleton.
0910      * 3. there is only one appearance for 'h', 'm','v', 'z' in normalized time
0911      *    skeleton
0912      *
0913      *
0914      *  @param skeleton               given combined skeleton.
0915      *  @param date                   Output parameter for date only skeleton.
0916      *  @param normalizedDate         Output parameter for normalized date only
0917      *
0918      *  @param time                   Output parameter for time only skeleton.
0919      *  @param normalizedTime         Output parameter for normalized time only
0920      *                                skeleton.
0921      *
0922      */
0923     static void  U_EXPORT2 getDateTimeSkeleton(const UnicodeString& skeleton,
0924                                     UnicodeString& date,
0925                                     UnicodeString& normalizedDate,
0926                                     UnicodeString& time,
0927                                     UnicodeString& normalizedTime);
0928 
0929 
0930 
0931     /**
0932      * Generate date or time interval pattern from resource,
0933      * and set them into the interval pattern locale to this formatter.
0934      *
0935      * It needs to handle the following:
0936      * 1. need to adjust field width.
0937      *    For example, the interval patterns saved in DateIntervalInfo
0938      *    includes "dMMMy", but not "dMMMMy".
0939      *    Need to get interval patterns for dMMMMy from dMMMy.
0940      *    Another example, the interval patterns saved in DateIntervalInfo
0941      *    includes "hmv", but not "hmz".
0942      *    Need to get interval patterns for "hmz' from 'hmv'
0943      *
0944      * 2. there might be no pattern for 'y' differ for skeleton "Md",
0945      *    in order to get interval patterns for 'y' differ,
0946      *    need to look for it from skeleton 'yMd'
0947      *
0948      * @param dateSkeleton   normalized date skeleton
0949      * @param timeSkeleton   normalized time skeleton
0950      * @return               whether the resource is found for the skeleton.
0951      *                       true if interval pattern found for the skeleton,
0952      *                       false otherwise.
0953      */
0954     UBool setSeparateDateTimePtn(const UnicodeString& dateSkeleton,
0955                                  const UnicodeString& timeSkeleton);
0956 
0957 
0958 
0959 
0960     /**
0961      * Generate interval pattern from existing resource
0962      *
0963      * It not only save the interval patterns,
0964      * but also return the extended skeleton and its best match skeleton.
0965      *
0966      * @param field           largest different calendar field
0967      * @param skeleton        skeleton
0968      * @param bestSkeleton    the best match skeleton which has interval pattern
0969      *                        defined in resource
0970      * @param differenceInfo  the difference between skeleton and best skeleton
0971      *         0 means the best matched skeleton is the same as input skeleton
0972      *         1 means the fields are the same, but field width are different
0973      *         2 means the only difference between fields are v/z,
0974      *        -1 means there are other fields difference
0975      *
0976      * @param extendedSkeleton      extended skeleton
0977      * @param extendedBestSkeleton  extended best match skeleton
0978      * @return                      whether the interval pattern is found
0979      *                              through extending skeleton or not.
0980      *                              true if interval pattern is found by
0981      *                              extending skeleton, false otherwise.
0982      */
0983     UBool setIntervalPattern(UCalendarDateFields field,
0984                              const UnicodeString* skeleton,
0985                              const UnicodeString* bestSkeleton,
0986                              int8_t differenceInfo,
0987                              UnicodeString* extendedSkeleton = nullptr,
0988                              UnicodeString* extendedBestSkeleton = nullptr);
0989 
0990     /**
0991      * Adjust field width in best match interval pattern to match
0992      * the field width in input skeleton.
0993      *
0994      * TODO (xji) make a general solution
0995      * The adjusting rule can be:
0996      * 1. always adjust
0997      * 2. never adjust
0998      * 3. default adjust, which means adjust according to the following rules
0999      * 3.1 always adjust string, such as MMM and MMMM
1000      * 3.2 never adjust between string and numeric, such as MM and MMM
1001      * 3.3 always adjust year
1002      * 3.4 do not adjust 'd', 'h', or 'm' if h presents
1003      * 3.5 do not adjust 'M' if it is numeric(?)
1004      *
1005      * Since date interval format is well-formed format,
1006      * date and time skeletons are normalized previously,
1007      * till this stage, the adjust here is only "adjust strings, such as MMM
1008      * and MMMM, EEE and EEEE.
1009      *
1010      * @param inputSkeleton            the input skeleton
1011      * @param bestMatchSkeleton        the best match skeleton
1012      * @param bestMatchIntervalPattern the best match interval pattern
1013      * @param differenceInfo           the difference between 2 skeletons
1014      *                                 1 means only field width differs
1015      *                                 2 means v/z exchange
1016      * @param suppressDayPeriodField if true, remove the day period field from the pattern, if there is one
1017      * @param adjustedIntervalPattern  adjusted interval pattern
1018      */
1019     static void U_EXPORT2 adjustFieldWidth(
1020                             const UnicodeString& inputSkeleton,
1021                             const UnicodeString& bestMatchSkeleton,
1022                             const UnicodeString& bestMatchIntervalPattern,
1023                             int8_t differenceInfo,
1024                             UBool suppressDayPeriodField,
1025                             UnicodeString& adjustedIntervalPattern);
1026 
1027     /**
1028      * Does the same thing as UnicodeString::findAndReplace(), except that it won't perform
1029      * the substitution inside quoted literal text.
1030      * @param targetString The string to perform the find-replace operation on.
1031      * @param strToReplace The string to search for and replace in the target string.
1032      * @param strToReplaceWith The string to substitute in wherever `stringToReplace` was found.
1033      */
1034     static void U_EXPORT2 findReplaceInPattern(UnicodeString& targetString,
1035                                                const UnicodeString& strToReplace,
1036                                                const UnicodeString& strToReplaceWith);
1037 
1038     /**
1039      * Concat a single date pattern with a time interval pattern,
1040      * set it into the intervalPatterns, while field is time field.
1041      * This is used to handle time interval patterns on skeleton with
1042      * both time and date. Present the date followed by
1043      * the range expression for the time.
1044      * @param format         date and time format
1045      * @param datePattern    date pattern
1046      * @param field          time calendar field: AM_PM, HOUR, MINUTE
1047      * @param status         output param set to success/failure code on exit
1048      */
1049     void concatSingleDate2TimeInterval(UnicodeString& format,
1050                                        const UnicodeString& datePattern,
1051                                        UCalendarDateFields field,
1052                                        UErrorCode& status);
1053 
1054     /**
1055      * check whether a calendar field present in a skeleton.
1056      * @param field      calendar field need to check
1057      * @param skeleton   given skeleton on which to check the calendar field
1058      * @return           true if field present in a skeleton.
1059      */
1060     static UBool U_EXPORT2 fieldExistsInSkeleton(UCalendarDateFields field,
1061                                                  const UnicodeString& skeleton);
1062 
1063 
1064     /**
1065      * Split interval patterns into 2 part.
1066      * @param intervalPattern  interval pattern
1067      * @return the index in interval pattern which split the pattern into 2 part
1068      */
1069     static int32_t  U_EXPORT2 splitPatternInto2Part(const UnicodeString& intervalPattern);
1070 
1071 
1072     /**
1073      * Break interval patterns as 2 part and save them into pattern info.
1074      * @param field            calendar field
1075      * @param intervalPattern  interval pattern
1076      */
1077     void setIntervalPattern(UCalendarDateFields field,
1078                             const UnicodeString& intervalPattern);
1079 
1080 
1081     /**
1082      * Break interval patterns as 2 part and save them into pattern info.
1083      * @param field            calendar field
1084      * @param intervalPattern  interval pattern
1085      * @param laterDateFirst   whether later date appear first in interval pattern
1086      */
1087     void setIntervalPattern(UCalendarDateFields field,
1088                             const UnicodeString& intervalPattern,
1089                             UBool laterDateFirst);
1090 
1091 
1092     /**
1093      * Set pattern information.
1094      *
1095      * @param field            calendar field
1096      * @param firstPart        the first part in interval pattern
1097      * @param secondPart       the second part in interval pattern
1098      * @param laterDateFirst   whether the first date in intervalPattern
1099      *                         is earlier date or later date
1100      */
1101     void setPatternInfo(UCalendarDateFields field,
1102                         const UnicodeString* firstPart,
1103                         const UnicodeString* secondPart,
1104                         UBool laterDateFirst);
1105 
1106     /**
1107      * Format 2 Calendars to produce a string.
1108      * Implementation of the similar public format function.
1109      * Must be called with gFormatterMutex already locked.
1110      *
1111      * Note: "fromCalendar" and "toCalendar" are not const,
1112      * since calendar is not const in  SimpleDateFormat::format(Calendar&),
1113      *
1114      * @param fromCalendar      calendar set to the from date in date interval
1115      *                          to be formatted into date interval string
1116      * @param toCalendar        calendar set to the to date in date interval
1117      *                          to be formatted into date interval string
1118      * @param appendTo          Output parameter to receive result.
1119      *                          Result is appended to existing contents.
1120      * @param firstIndex        0 if the first output date is fromCalendar;
1121      *                          1 if it corresponds to toCalendar;
1122      *                          -1 if there is only one date printed.
1123      * @param fphandler         Handler for field position information.
1124      *                          The fields will be from the UDateFormatField enum.
1125      * @param status            Output param filled with success/failure status.
1126      *                          Caller needs to make sure it is SUCCESS
1127      *                          at the function entrance
1128      * @return                  Reference to 'appendTo' parameter.
1129      * @internal (private)
1130      */
1131     UnicodeString& formatImpl(Calendar& fromCalendar,
1132                               Calendar& toCalendar,
1133                               UnicodeString& appendTo,
1134                               int8_t& firstIndex,
1135                               FieldPositionHandler& fphandler,
1136                               UErrorCode& status) const ;
1137 
1138     /** Version of formatImpl for DateInterval. */
1139     UnicodeString& formatIntervalImpl(const DateInterval& dtInterval,
1140                               UnicodeString& appendTo,
1141                               int8_t& firstIndex,
1142                               FieldPositionHandler& fphandler,
1143                               UErrorCode& status) const;
1144 
1145 
1146     // from calendar field to pattern letter
1147     static const char16_t fgCalendarFieldToPatternLetter[];
1148 
1149 
1150     /**
1151      * The interval patterns for this locale.
1152      */
1153     DateIntervalInfo*     fInfo;
1154 
1155     /**
1156      * The DateFormat object used to format single pattern
1157      */
1158     SimpleDateFormat*     fDateFormat;
1159 
1160     /**
1161      * The 2 calendars with the from and to date.
1162      * could re-use the calendar in fDateFormat,
1163      * but keeping 2 calendars make it clear and clean.
1164      */
1165     Calendar* fFromCalendar;
1166     Calendar* fToCalendar;
1167 
1168     Locale fLocale;
1169 
1170     /**
1171      * Following are interval information relevant (locale) to this formatter.
1172      */
1173     UnicodeString fSkeleton;
1174     PatternInfo fIntervalPatterns[DateIntervalInfo::kIPI_MAX_INDEX];
1175 
1176     /**
1177      * Patterns for fallback formatting.
1178      */
1179     UnicodeString* fDatePattern;
1180     UnicodeString* fTimePattern;
1181     UnicodeString* fDateTimeFormat;
1182 
1183     /**
1184      * Other formatting information
1185      */
1186     UDisplayContext fCapitalizationContext;
1187 };
1188 
1189 inline bool
1190 DateIntervalFormat::operator!=(const Format& other) const  {
1191     return !operator==(other);
1192 }
1193 
1194 U_NAMESPACE_END
1195 
1196 #endif /* #if !UCONFIG_NO_FORMATTING */
1197 
1198 #endif /* U_SHOW_CPLUSPLUS_API */
1199 
1200 #endif // _DTITVFMT_H__
1201 //eof