|
||||
Warning, file /include/unicode/dtitvfmt.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
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 "yMMMd", 0199 * in en_US, if the largest different calendar field between date1 and date2 0200 * is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy", 0201 * such as "Jan 10, 2007 - Jan 10, 2008". 0202 * If the largest different calendar field between date1 and date2 is "month", 0203 * the date interval pattern is "MMM d - MMM d, yyyy", 0204 * such as "Jan 10 - Feb 10, 2007". 0205 * If the largest different calendar field between date1 and date2 is "day", 0206 * the date interval pattern is "MMM d-d, yyyy", such as "Jan 10-20, 2007". 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 * <p> 0348 * <h4>Sample code</h4> 0349 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1 0350 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined 0351 * <p> 0352 */ 0353 0354 static DateIntervalFormat* U_EXPORT2 createInstance( 0355 const UnicodeString& skeleton, 0356 const Locale& locale, 0357 UErrorCode& status); 0358 0359 /** 0360 * Construct a DateIntervalFormat from skeleton 0361 * DateIntervalInfo, and default locale. 0362 * 0363 * This is a convenient override of 0364 * createInstance(const UnicodeString& skeleton, const Locale& locale, 0365 * const DateIntervalInfo& dtitvinf, UErrorCode&) 0366 * with the locale value as default locale. 0367 * 0368 * @param skeleton the skeleton on which interval format based. 0369 * @param dtitvinf the DateIntervalInfo object. 0370 * @param status output param set to success/failure code on exit 0371 * @return a date time interval formatter which the caller owns. 0372 * @stable ICU 4.0 0373 */ 0374 static DateIntervalFormat* U_EXPORT2 createInstance( 0375 const UnicodeString& skeleton, 0376 const DateIntervalInfo& dtitvinf, 0377 UErrorCode& status); 0378 0379 /** 0380 * Construct a DateIntervalFormat from skeleton 0381 * a DateIntervalInfo, and the given locale. 0382 * 0383 * <P> 0384 * In this factory method, user provides its own date interval pattern 0385 * information, instead of using those pre-defined data in resource file. 0386 * This factory method is for powerful users who want to provide their own 0387 * interval patterns. 0388 * <P> 0389 * There are pre-defined skeletons (defined in udate.h) having predefined 0390 * interval patterns in resource files. 0391 * Users are encouraged to use those macros. 0392 * For example: 0393 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status) 0394 * 0395 * The DateIntervalInfo provides the interval patterns. 0396 * and the DateIntervalInfo ownership remains to the caller. 0397 * 0398 * User are encouraged to set default interval pattern in DateIntervalInfo 0399 * as well, if they want to set other interval patterns ( instead of 0400 * reading the interval patterns from resource files). 0401 * When the corresponding interval pattern for a largest calendar different 0402 * field is not found ( if user not set it ), interval format fallback to 0403 * the default interval pattern. 0404 * If user does not provide default interval pattern, it fallback to 0405 * "{date0} - {date1}" 0406 * 0407 * @param skeleton the skeleton on which interval format based. 0408 * @param locale the given locale 0409 * @param dtitvinf the DateIntervalInfo object. 0410 * @param status output param set to success/failure code on exit 0411 * @return a date time interval formatter which the caller owns. 0412 * @stable ICU 4.0 0413 * <p> 0414 * <h4>Sample code</h4> 0415 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1 0416 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtCustomized 0417 * <p> 0418 */ 0419 static DateIntervalFormat* U_EXPORT2 createInstance( 0420 const UnicodeString& skeleton, 0421 const Locale& locale, 0422 const DateIntervalInfo& dtitvinf, 0423 UErrorCode& status); 0424 0425 /** 0426 * Destructor. 0427 * @stable ICU 4.0 0428 */ 0429 virtual ~DateIntervalFormat(); 0430 0431 /** 0432 * Clone this Format object polymorphically. The caller owns the result and 0433 * should delete it when done. 0434 * @return A copy of the object. 0435 * @stable ICU 4.0 0436 */ 0437 virtual DateIntervalFormat* clone() const override; 0438 0439 /** 0440 * Return true if the given Format objects are semantically equal. Objects 0441 * of different subclasses are considered unequal. 0442 * @param other the object to be compared with. 0443 * @return true if the given Format objects are semantically equal. 0444 * @stable ICU 4.0 0445 */ 0446 virtual bool operator==(const Format& other) const override; 0447 0448 /** 0449 * Return true if the given Format objects are not semantically equal. 0450 * Objects of different subclasses are considered unequal. 0451 * @param other the object to be compared with. 0452 * @return true if the given Format objects are not semantically equal. 0453 * @stable ICU 4.0 0454 */ 0455 bool operator!=(const Format& other) const; 0456 0457 0458 using Format::format; 0459 0460 /** 0461 * Format an object to produce a string. This method handles Formattable 0462 * objects with a DateInterval type. 0463 * If a the Formattable object type is not a DateInterval, 0464 * then it returns a failing UErrorCode. 0465 * 0466 * @param obj The object to format. 0467 * Must be a DateInterval. 0468 * @param appendTo Output parameter to receive result. 0469 * Result is appended to existing contents. 0470 * @param fieldPosition On input: an alignment field, if desired. 0471 * On output: the offsets of the alignment field. 0472 * There may be multiple instances of a given field type 0473 * in an interval format; in this case the fieldPosition 0474 * offsets refer to the first instance. 0475 * @param status Output param filled with success/failure status. 0476 * @return Reference to 'appendTo' parameter. 0477 * @stable ICU 4.0 0478 */ 0479 virtual UnicodeString& format(const Formattable& obj, 0480 UnicodeString& appendTo, 0481 FieldPosition& fieldPosition, 0482 UErrorCode& status) const override; 0483 0484 0485 0486 /** 0487 * Format a DateInterval to produce a string. 0488 * 0489 * @param dtInterval DateInterval to be formatted. 0490 * @param appendTo Output parameter to receive result. 0491 * Result is appended to existing contents. 0492 * @param fieldPosition On input: an alignment field, if desired. 0493 * On output: the offsets of the alignment field. 0494 * There may be multiple instances of a given field type 0495 * in an interval format; in this case the fieldPosition 0496 * offsets refer to the first instance. 0497 * @param status Output param filled with success/failure status. 0498 * @return Reference to 'appendTo' parameter. 0499 * @stable ICU 4.0 0500 */ 0501 UnicodeString& format(const DateInterval* dtInterval, 0502 UnicodeString& appendTo, 0503 FieldPosition& fieldPosition, 0504 UErrorCode& status) const ; 0505 0506 /** 0507 * Format a DateInterval to produce a FormattedDateInterval. 0508 * 0509 * The FormattedDateInterval exposes field information about the formatted string. 0510 * 0511 * @param dtInterval DateInterval to be formatted. 0512 * @param status Set if an error occurs. 0513 * @return A FormattedDateInterval containing the format result. 0514 * @stable ICU 64 0515 */ 0516 FormattedDateInterval formatToValue( 0517 const DateInterval& dtInterval, 0518 UErrorCode& status) const; 0519 0520 /** 0521 * Format 2 Calendars to produce a string. 0522 * 0523 * Note: "fromCalendar" and "toCalendar" are not const, 0524 * since calendar is not const in SimpleDateFormat::format(Calendar&), 0525 * 0526 * @param fromCalendar calendar set to the from date in date interval 0527 * to be formatted into date interval string 0528 * @param toCalendar calendar set to the to date in date interval 0529 * to be formatted into date interval string 0530 * @param appendTo Output parameter to receive result. 0531 * Result is appended to existing contents. 0532 * @param fieldPosition On input: an alignment field, if desired. 0533 * On output: the offsets of the alignment field. 0534 * There may be multiple instances of a given field type 0535 * in an interval format; in this case the fieldPosition 0536 * offsets refer to the first instance. 0537 * @param status Output param filled with success/failure status. 0538 * Caller needs to make sure it is SUCCESS 0539 * at the function entrance 0540 * @return Reference to 'appendTo' parameter. 0541 * @stable ICU 4.0 0542 */ 0543 UnicodeString& format(Calendar& fromCalendar, 0544 Calendar& toCalendar, 0545 UnicodeString& appendTo, 0546 FieldPosition& fieldPosition, 0547 UErrorCode& status) const ; 0548 0549 /** 0550 * Format 2 Calendars to produce a FormattedDateInterval. 0551 * 0552 * The FormattedDateInterval exposes field information about the formatted string. 0553 * 0554 * Note: "fromCalendar" and "toCalendar" are not const, 0555 * since calendar is not const in SimpleDateFormat::format(Calendar&), 0556 * 0557 * @param fromCalendar calendar set to the from date in date interval 0558 * to be formatted into date interval string 0559 * @param toCalendar calendar set to the to date in date interval 0560 * to be formatted into date interval string 0561 * @param status Set if an error occurs. 0562 * @return A FormattedDateInterval containing the format result. 0563 * @stable ICU 64 0564 */ 0565 FormattedDateInterval formatToValue( 0566 Calendar& fromCalendar, 0567 Calendar& toCalendar, 0568 UErrorCode& status) const; 0569 0570 /** 0571 * Date interval parsing is not supported. Please do not use. 0572 * <P> 0573 * This method should handle parsing of 0574 * date time interval strings into Formattable objects with 0575 * DateInterval type, which is a pair of UDate. 0576 * <P> 0577 * Before calling, set parse_pos.index to the offset you want to start 0578 * parsing at in the source. After calling, parse_pos.index is the end of 0579 * the text you parsed. If error occurs, index is unchanged. 0580 * <P> 0581 * When parsing, leading whitespace is discarded (with a successful parse), 0582 * while trailing whitespace is left as is. 0583 * <P> 0584 * See Format::parseObject() for more. 0585 * 0586 * @param source The string to be parsed into an object. 0587 * @param result Formattable to be set to the parse result. 0588 * If parse fails, return contents are undefined. 0589 * @param parse_pos The position to start parsing at. Since no parsing 0590 * is supported, upon return this param is unchanged. 0591 * @return A newly created Formattable* object, or nullptr 0592 * on failure. The caller owns this and should 0593 * delete it when done. 0594 * @internal ICU 4.0 0595 */ 0596 virtual void parseObject(const UnicodeString& source, 0597 Formattable& result, 0598 ParsePosition& parse_pos) const override; 0599 0600 0601 /** 0602 * Gets the date time interval patterns. 0603 * @return the date time interval patterns associated with 0604 * this date interval formatter. 0605 * @stable ICU 4.0 0606 */ 0607 const DateIntervalInfo* getDateIntervalInfo(void) const; 0608 0609 0610 /** 0611 * Set the date time interval patterns. 0612 * @param newIntervalPatterns the given interval patterns to copy. 0613 * @param status output param set to success/failure code on exit 0614 * @stable ICU 4.0 0615 */ 0616 void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns, 0617 UErrorCode& status); 0618 0619 0620 /** 0621 * Gets the date formatter. The DateIntervalFormat instance continues to own 0622 * the returned DateFormatter object, and will use and possibly modify it 0623 * during format operations. In a multi-threaded environment, the returned 0624 * DateFormat can only be used if it is certain that no other threads are 0625 * concurrently using this DateIntervalFormatter, even for nominally const 0626 * functions. 0627 * 0628 * @return the date formatter associated with this date interval formatter. 0629 * @stable ICU 4.0 0630 */ 0631 const DateFormat* getDateFormat(void) const; 0632 0633 /** 0634 * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar. 0635 * @return the time zone associated with the calendar of DateIntervalFormat. 0636 * @stable ICU 4.8 0637 */ 0638 virtual const TimeZone& getTimeZone(void) const; 0639 0640 /** 0641 * Sets the time zone for the calendar used by this DateIntervalFormat object. The 0642 * caller no longer owns the TimeZone object and should not delete it after this call. 0643 * @param zoneToAdopt the TimeZone to be adopted. 0644 * @stable ICU 4.8 0645 */ 0646 virtual void adoptTimeZone(TimeZone* zoneToAdopt); 0647 0648 /** 0649 * Sets the time zone for the calendar used by this DateIntervalFormat object. 0650 * @param zone the new time zone. 0651 * @stable ICU 4.8 0652 */ 0653 virtual void setTimeZone(const TimeZone& zone); 0654 0655 /** 0656 * Set a particular UDisplayContext value in the formatter, such as 0657 * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. This causes the formatted 0658 * result to be capitalized appropriately for the context in which 0659 * it is intended to be used, considering both the locale and the 0660 * type of field at the beginning of the formatted result. 0661 * @param value The UDisplayContext value to set. 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 * @stable ICU 68 0666 */ 0667 virtual void setContext(UDisplayContext value, UErrorCode& status); 0668 0669 /** 0670 * Get the formatter's UDisplayContext value for the specified UDisplayContextType, 0671 * such as UDISPCTX_TYPE_CAPITALIZATION. 0672 * @param type The UDisplayContextType whose value to return 0673 * @param status Input/output status. If at entry this indicates a failure 0674 * status, the function will do nothing; otherwise this will be 0675 * updated with any new status from the function. 0676 * @return The UDisplayContextValue for the specified type. 0677 * @stable ICU 68 0678 */ 0679 virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const; 0680 0681 /** 0682 * Return the class ID for this class. This is useful only for comparing to 0683 * a return value from getDynamicClassID(). For example: 0684 * <pre> 0685 * . Base* polymorphic_pointer = createPolymorphicObject(); 0686 * . if (polymorphic_pointer->getDynamicClassID() == 0687 * . erived::getStaticClassID()) ... 0688 * </pre> 0689 * @return The class ID for all objects of this class. 0690 * @stable ICU 4.0 0691 */ 0692 static UClassID U_EXPORT2 getStaticClassID(void); 0693 0694 /** 0695 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This 0696 * method is to implement a simple version of RTTI, since not all C++ 0697 * compilers support genuine RTTI. Polymorphic operator==() and clone() 0698 * methods call this method. 0699 * 0700 * @return The class ID for this object. All objects of a 0701 * given class have the same class ID. Objects of 0702 * other classes have different class IDs. 0703 * @stable ICU 4.0 0704 */ 0705 virtual UClassID getDynamicClassID(void) const override; 0706 0707 protected: 0708 0709 /** 0710 * Copy constructor. 0711 * @stable ICU 4.0 0712 */ 0713 DateIntervalFormat(const DateIntervalFormat&); 0714 0715 /** 0716 * Assignment operator. 0717 * @stable ICU 4.0 0718 */ 0719 DateIntervalFormat& operator=(const DateIntervalFormat&); 0720 0721 private: 0722 0723 /* 0724 * This is for ICU internal use only. Please do not use. 0725 * Save the interval pattern information. 0726 * Interval pattern consists of 2 single date patterns and the separator. 0727 * For example, interval pattern "MMM d - MMM d, yyyy" consists 0728 * a single date pattern "MMM d", another single date pattern "MMM d, yyyy", 0729 * and a separator "-". 0730 * The pattern is divided into 2 parts. For above example, 0731 * the first part is "MMM d - ", and the second part is "MMM d, yyyy". 0732 * Also, the first date appears in an interval pattern could be 0733 * the earlier date or the later date. 0734 * And such information is saved in the interval pattern as well. 0735 */ 0736 struct PatternInfo { 0737 UnicodeString firstPart; 0738 UnicodeString secondPart; 0739 /** 0740 * Whether the first date in interval pattern is later date or not. 0741 * Fallback format set the default ordering. 0742 * And for a particular interval pattern, the order can be 0743 * overridden by prefixing the interval pattern with "latestFirst:" or 0744 * "earliestFirst:" 0745 * For example, given 2 date, Jan 10, 2007 to Feb 10, 2007. 0746 * if the fallback format is "{0} - {1}", 0747 * and the pattern is "d MMM - d MMM yyyy", the interval format is 0748 * "10 Jan - 10 Feb, 2007". 0749 * If the pattern is "latestFirst:d MMM - d MMM yyyy", 0750 * the interval format is "10 Feb - 10 Jan, 2007" 0751 */ 0752 UBool laterDateFirst; 0753 }; 0754 0755 0756 /** 0757 * default constructor 0758 * @internal (private) 0759 */ 0760 DateIntervalFormat(); 0761 0762 /** 0763 * Construct a DateIntervalFormat from DateFormat, 0764 * a DateIntervalInfo, and skeleton. 0765 * DateFormat provides the timezone, calendar, 0766 * full pattern, and date format symbols information. 0767 * It should be a SimpleDateFormat object which 0768 * has a pattern in it. 0769 * the DateIntervalInfo provides the interval patterns. 0770 * 0771 * Note: the DateIntervalFormat takes ownership of both 0772 * DateFormat and DateIntervalInfo objects. 0773 * Caller should not delete them. 0774 * 0775 * @param locale the locale of this date interval formatter. 0776 * @param dtItvInfo the DateIntervalInfo object to be adopted. 0777 * @param skeleton the skeleton of the date formatter 0778 * @param status output param set to success/failure code on exit 0779 */ 0780 DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo, 0781 const UnicodeString* skeleton, UErrorCode& status); 0782 0783 0784 /** 0785 * Construct a DateIntervalFormat from DateFormat 0786 * and a DateIntervalInfo. 0787 * 0788 * It is a wrapper of the constructor. 0789 * 0790 * @param locale the locale of this date interval formatter. 0791 * @param dtitvinf the DateIntervalInfo object to be adopted. 0792 * @param skeleton the skeleton of this formatter. 0793 * @param status Output param set to success/failure code. 0794 * @return a date time interval formatter which the caller owns. 0795 */ 0796 static DateIntervalFormat* U_EXPORT2 create(const Locale& locale, 0797 DateIntervalInfo* dtitvinf, 0798 const UnicodeString* skeleton, 0799 UErrorCode& status); 0800 0801 /** 0802 * Below are for generating interval patterns local to the formatter 0803 */ 0804 0805 /** Like fallbackFormat, but only formats the range part of the fallback. */ 0806 void fallbackFormatRange( 0807 Calendar& fromCalendar, 0808 Calendar& toCalendar, 0809 UnicodeString& appendTo, 0810 int8_t& firstIndex, 0811 FieldPositionHandler& fphandler, 0812 UErrorCode& status) const; 0813 0814 /** 0815 * Format 2 Calendars using fall-back interval pattern 0816 * 0817 * The full pattern used in this fall-back format is the 0818 * full pattern of the date formatter. 0819 * 0820 * gFormatterMutex must already be locked when calling this function. 0821 * 0822 * @param fromCalendar calendar set to the from date in date interval 0823 * to be formatted into date interval string 0824 * @param toCalendar calendar set to the to date in date interval 0825 * to be formatted into date interval string 0826 * @param fromToOnSameDay true iff from and to dates are on the same day 0827 * (any difference is in ampm/hours or below) 0828 * @param appendTo Output parameter to receive result. 0829 * Result is appended to existing contents. 0830 * @param firstIndex See formatImpl for more information. 0831 * @param fphandler See formatImpl for more information. 0832 * @param status output param set to success/failure code on exit 0833 * @return Reference to 'appendTo' parameter. 0834 * @internal (private) 0835 */ 0836 UnicodeString& fallbackFormat(Calendar& fromCalendar, 0837 Calendar& toCalendar, 0838 UBool fromToOnSameDay, 0839 UnicodeString& appendTo, 0840 int8_t& firstIndex, 0841 FieldPositionHandler& fphandler, 0842 UErrorCode& status) const; 0843 0844 0845 0846 /** 0847 * Initialize interval patterns locale to this formatter 0848 * 0849 * This code is a bit complicated since 0850 * 1. the interval patterns saved in resource bundle files are interval 0851 * patterns based on date or time only. 0852 * It does not have interval patterns based on both date and time. 0853 * Interval patterns on both date and time are algorithm generated. 0854 * 0855 * For example, it has interval patterns on skeleton "dMy" and "hm", 0856 * but it does not have interval patterns on skeleton "dMyhm". 0857 * 0858 * The rule to generate interval patterns for both date and time skeleton are 0859 * 1) when the year, month, or day differs, concatenate the two original 0860 * expressions with a separator between, 0861 * For example, interval pattern from "Jan 10, 2007 10:10 am" 0862 * to "Jan 11, 2007 10:10am" is 0863 * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am" 0864 * 0865 * 2) otherwise, present the date followed by the range expression 0866 * for the time. 0867 * For example, interval pattern from "Jan 10, 2007 10:10 am" 0868 * to "Jan 10, 2007 11:10am" is 0869 * "Jan 10, 2007 10:10 am - 11:10am" 0870 * 0871 * 2. even a pattern does not request a certain calendar field, 0872 * the interval pattern needs to include such field if such fields are 0873 * different between 2 dates. 0874 * For example, a pattern/skeleton is "hm", but the interval pattern 0875 * includes year, month, and date when year, month, and date differs. 0876 * 0877 * 0878 * @param status output param set to success/failure code on exit 0879 */ 0880 void initializePattern(UErrorCode& status); 0881 0882 0883 0884 /** 0885 * Set fall back interval pattern given a calendar field, 0886 * a skeleton, and a date time pattern generator. 0887 * @param field the largest different calendar field 0888 * @param skeleton a skeleton 0889 * @param status output param set to success/failure code on exit 0890 */ 0891 void setFallbackPattern(UCalendarDateFields field, 0892 const UnicodeString& skeleton, 0893 UErrorCode& status); 0894 0895 0896 0897 /** 0898 * Converts special hour metacharacters (such as 'j') in the skeleton into locale-appropriate 0899 * pattern characters. 0900 * 0901 * 0902 * @param skeleton The skeleton to convert 0903 * @return A copy of the skeleton, which "j" and any other special hour metacharacters converted to the regular ones. 0904 * 0905 */ 0906 UnicodeString normalizeHourMetacharacters(const UnicodeString& skeleton) const; 0907 0908 0909 0910 /** 0911 * get separated date and time skeleton from a combined skeleton. 0912 * 0913 * The difference between date skeleton and normalizedDateSkeleton are: 0914 * 1. both 'y' and 'd' are appeared only once in normalizeDateSkeleton 0915 * 2. 'E' and 'EE' are normalized into 'EEE' 0916 * 3. 'MM' is normalized into 'M' 0917 * 0918 ** the difference between time skeleton and normalizedTimeSkeleton are: 0919 * 1. both 'H' and 'h' are normalized as 'h' in normalized time skeleton, 0920 * 2. 'a' is omitted in normalized time skeleton. 0921 * 3. there is only one appearance for 'h', 'm','v', 'z' in normalized time 0922 * skeleton 0923 * 0924 * 0925 * @param skeleton given combined skeleton. 0926 * @param date Output parameter for date only skeleton. 0927 * @param normalizedDate Output parameter for normalized date only 0928 * 0929 * @param time Output parameter for time only skeleton. 0930 * @param normalizedTime Output parameter for normalized time only 0931 * skeleton. 0932 * 0933 */ 0934 static void U_EXPORT2 getDateTimeSkeleton(const UnicodeString& skeleton, 0935 UnicodeString& date, 0936 UnicodeString& normalizedDate, 0937 UnicodeString& time, 0938 UnicodeString& normalizedTime); 0939 0940 0941 0942 /** 0943 * Generate date or time interval pattern from resource, 0944 * and set them into the interval pattern locale to this formatter. 0945 * 0946 * It needs to handle the following: 0947 * 1. need to adjust field width. 0948 * For example, the interval patterns saved in DateIntervalInfo 0949 * includes "dMMMy", but not "dMMMMy". 0950 * Need to get interval patterns for dMMMMy from dMMMy. 0951 * Another example, the interval patterns saved in DateIntervalInfo 0952 * includes "hmv", but not "hmz". 0953 * Need to get interval patterns for "hmz' from 'hmv' 0954 * 0955 * 2. there might be no pattern for 'y' differ for skeleton "Md", 0956 * in order to get interval patterns for 'y' differ, 0957 * need to look for it from skeleton 'yMd' 0958 * 0959 * @param dateSkeleton normalized date skeleton 0960 * @param timeSkeleton normalized time skeleton 0961 * @return whether the resource is found for the skeleton. 0962 * true if interval pattern found for the skeleton, 0963 * false otherwise. 0964 */ 0965 UBool setSeparateDateTimePtn(const UnicodeString& dateSkeleton, 0966 const UnicodeString& timeSkeleton); 0967 0968 0969 0970 0971 /** 0972 * Generate interval pattern from existing resource 0973 * 0974 * It not only save the interval patterns, 0975 * but also return the extended skeleton and its best match skeleton. 0976 * 0977 * @param field largest different calendar field 0978 * @param skeleton skeleton 0979 * @param bestSkeleton the best match skeleton which has interval pattern 0980 * defined in resource 0981 * @param differenceInfo the difference between skeleton and best skeleton 0982 * 0 means the best matched skeleton is the same as input skeleton 0983 * 1 means the fields are the same, but field width are different 0984 * 2 means the only difference between fields are v/z, 0985 * -1 means there are other fields difference 0986 * 0987 * @param extendedSkeleton extended skeleton 0988 * @param extendedBestSkeleton extended best match skeleton 0989 * @return whether the interval pattern is found 0990 * through extending skeleton or not. 0991 * true if interval pattern is found by 0992 * extending skeleton, false otherwise. 0993 */ 0994 UBool setIntervalPattern(UCalendarDateFields field, 0995 const UnicodeString* skeleton, 0996 const UnicodeString* bestSkeleton, 0997 int8_t differenceInfo, 0998 UnicodeString* extendedSkeleton = nullptr, 0999 UnicodeString* extendedBestSkeleton = nullptr); 1000 1001 /** 1002 * Adjust field width in best match interval pattern to match 1003 * the field width in input skeleton. 1004 * 1005 * TODO (xji) make a general solution 1006 * The adjusting rule can be: 1007 * 1. always adjust 1008 * 2. never adjust 1009 * 3. default adjust, which means adjust according to the following rules 1010 * 3.1 always adjust string, such as MMM and MMMM 1011 * 3.2 never adjust between string and numeric, such as MM and MMM 1012 * 3.3 always adjust year 1013 * 3.4 do not adjust 'd', 'h', or 'm' if h presents 1014 * 3.5 do not adjust 'M' if it is numeric(?) 1015 * 1016 * Since date interval format is well-formed format, 1017 * date and time skeletons are normalized previously, 1018 * till this stage, the adjust here is only "adjust strings, such as MMM 1019 * and MMMM, EEE and EEEE. 1020 * 1021 * @param inputSkeleton the input skeleton 1022 * @param bestMatchSkeleton the best match skeleton 1023 * @param bestMatchIntervalPattern the best match interval pattern 1024 * @param differenceInfo the difference between 2 skeletons 1025 * 1 means only field width differs 1026 * 2 means v/z exchange 1027 * @param suppressDayPeriodField if true, remove the day period field from the pattern, if there is one 1028 * @param adjustedIntervalPattern adjusted interval pattern 1029 */ 1030 static void U_EXPORT2 adjustFieldWidth( 1031 const UnicodeString& inputSkeleton, 1032 const UnicodeString& bestMatchSkeleton, 1033 const UnicodeString& bestMatchIntervalPattern, 1034 int8_t differenceInfo, 1035 UBool suppressDayPeriodField, 1036 UnicodeString& adjustedIntervalPattern); 1037 1038 /** 1039 * Does the same thing as UnicodeString::findAndReplace(), except that it won't perform 1040 * the substitution inside quoted literal text. 1041 * @param targetString The string to perform the find-replace operation on. 1042 * @param strToReplace The string to search for and replace in the target string. 1043 * @param strToReplaceWith The string to substitute in wherever `stringToReplace` was found. 1044 */ 1045 static void U_EXPORT2 findReplaceInPattern(UnicodeString& targetString, 1046 const UnicodeString& strToReplace, 1047 const UnicodeString& strToReplaceWith); 1048 1049 /** 1050 * Concat a single date pattern with a time interval pattern, 1051 * set it into the intervalPatterns, while field is time field. 1052 * This is used to handle time interval patterns on skeleton with 1053 * both time and date. Present the date followed by 1054 * the range expression for the time. 1055 * @param format date and time format 1056 * @param datePattern date pattern 1057 * @param field time calendar field: AM_PM, HOUR, MINUTE 1058 * @param status output param set to success/failure code on exit 1059 */ 1060 void concatSingleDate2TimeInterval(UnicodeString& format, 1061 const UnicodeString& datePattern, 1062 UCalendarDateFields field, 1063 UErrorCode& status); 1064 1065 /** 1066 * check whether a calendar field present in a skeleton. 1067 * @param field calendar field need to check 1068 * @param skeleton given skeleton on which to check the calendar field 1069 * @return true if field present in a skeleton. 1070 */ 1071 static UBool U_EXPORT2 fieldExistsInSkeleton(UCalendarDateFields field, 1072 const UnicodeString& skeleton); 1073 1074 1075 /** 1076 * Split interval patterns into 2 part. 1077 * @param intervalPattern interval pattern 1078 * @return the index in interval pattern which split the pattern into 2 part 1079 */ 1080 static int32_t U_EXPORT2 splitPatternInto2Part(const UnicodeString& intervalPattern); 1081 1082 1083 /** 1084 * Break interval patterns as 2 part and save them into pattern info. 1085 * @param field calendar field 1086 * @param intervalPattern interval pattern 1087 */ 1088 void setIntervalPattern(UCalendarDateFields field, 1089 const UnicodeString& intervalPattern); 1090 1091 1092 /** 1093 * Break interval patterns as 2 part and save them into pattern info. 1094 * @param field calendar field 1095 * @param intervalPattern interval pattern 1096 * @param laterDateFirst whether later date appear first in interval pattern 1097 */ 1098 void setIntervalPattern(UCalendarDateFields field, 1099 const UnicodeString& intervalPattern, 1100 UBool laterDateFirst); 1101 1102 1103 /** 1104 * Set pattern information. 1105 * 1106 * @param field calendar field 1107 * @param firstPart the first part in interval pattern 1108 * @param secondPart the second part in interval pattern 1109 * @param laterDateFirst whether the first date in intervalPattern 1110 * is earlier date or later date 1111 */ 1112 void setPatternInfo(UCalendarDateFields field, 1113 const UnicodeString* firstPart, 1114 const UnicodeString* secondPart, 1115 UBool laterDateFirst); 1116 1117 /** 1118 * Format 2 Calendars to produce a string. 1119 * Implementation of the similar public format function. 1120 * Must be called with gFormatterMutex already locked. 1121 * 1122 * Note: "fromCalendar" and "toCalendar" are not const, 1123 * since calendar is not const in SimpleDateFormat::format(Calendar&), 1124 * 1125 * @param fromCalendar calendar set to the from date in date interval 1126 * to be formatted into date interval string 1127 * @param toCalendar calendar set to the to date in date interval 1128 * to be formatted into date interval string 1129 * @param appendTo Output parameter to receive result. 1130 * Result is appended to existing contents. 1131 * @param firstIndex 0 if the first output date is fromCalendar; 1132 * 1 if it corresponds to toCalendar; 1133 * -1 if there is only one date printed. 1134 * @param fphandler Handler for field position information. 1135 * The fields will be from the UDateFormatField enum. 1136 * @param status Output param filled with success/failure status. 1137 * Caller needs to make sure it is SUCCESS 1138 * at the function entrance 1139 * @return Reference to 'appendTo' parameter. 1140 * @internal (private) 1141 */ 1142 UnicodeString& formatImpl(Calendar& fromCalendar, 1143 Calendar& toCalendar, 1144 UnicodeString& appendTo, 1145 int8_t& firstIndex, 1146 FieldPositionHandler& fphandler, 1147 UErrorCode& status) const ; 1148 1149 /** Version of formatImpl for DateInterval. */ 1150 UnicodeString& formatIntervalImpl(const DateInterval& dtInterval, 1151 UnicodeString& appendTo, 1152 int8_t& firstIndex, 1153 FieldPositionHandler& fphandler, 1154 UErrorCode& status) const; 1155 1156 1157 // from calendar field to pattern letter 1158 static const char16_t fgCalendarFieldToPatternLetter[]; 1159 1160 1161 /** 1162 * The interval patterns for this locale. 1163 */ 1164 DateIntervalInfo* fInfo; 1165 1166 /** 1167 * The DateFormat object used to format single pattern 1168 */ 1169 SimpleDateFormat* fDateFormat; 1170 1171 /** 1172 * The 2 calendars with the from and to date. 1173 * could re-use the calendar in fDateFormat, 1174 * but keeping 2 calendars make it clear and clean. 1175 */ 1176 Calendar* fFromCalendar; 1177 Calendar* fToCalendar; 1178 1179 Locale fLocale; 1180 1181 /** 1182 * Following are interval information relevant (locale) to this formatter. 1183 */ 1184 UnicodeString fSkeleton; 1185 PatternInfo fIntervalPatterns[DateIntervalInfo::kIPI_MAX_INDEX]; 1186 1187 /** 1188 * Patterns for fallback formatting. 1189 */ 1190 UnicodeString* fDatePattern; 1191 UnicodeString* fTimePattern; 1192 UnicodeString* fDateTimeFormat; 1193 1194 /** 1195 * Other formatting information 1196 */ 1197 UDisplayContext fCapitalizationContext; 1198 }; 1199 1200 inline bool 1201 DateIntervalFormat::operator!=(const Format& other) const { 1202 return !operator==(other); 1203 } 1204 1205 U_NAMESPACE_END 1206 1207 #endif /* #if !UCONFIG_NO_FORMATTING */ 1208 1209 #endif /* U_SHOW_CPLUSPLUS_API */ 1210 1211 #endif // _DTITVFMT_H__ 1212 //eof
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |