120 * Date interval means from one date to another date, 121 * for example, from "Jan 11, 2008" to "Jan 18, 2008". 122 * We introduced class DateInterval to represent it. 123 * DateInterval is a pair of UDate, which is 124 * the standard milliseconds since 24:00 GMT, Jan 1, 1970. 125 * 126 *
127 * DateIntervalFormat formats a DateInterval into 128 * text as compactly as possible. 129 * For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008" 130 * is "Jan 11-18, 2008" for English. 131 * And it parses text into DateInterval, 132 * although initially, parsing is not supported. 133 * 134 *
135 * There is no structural information in date time patterns. 136 * For any punctuations and string literals inside a date time pattern, 137 * we do not know whether it is just a separator, or a prefix, or a suffix. 138 * Without such information, so, it is difficult to generate a sub-pattern 139 * (or super-pattern) by algorithm. 140 * So, formatting a DateInterval is pattern-driven. It is very 141 * similar to formatting in SimpleDateFormat. 142 * We introduce class DateIntervalInfo to save date interval 143 * patterns, similar to date time pattern in SimpleDateFormat. 144 * 145 *
146 * Logically, the interval patterns are mappings 147 * from (skeleton, the_largest_different_calendar_field) 148 * to (date_interval_pattern). 149 * 150 *
151 * A skeleton 152 *
175 * The calendar fields we support for interval formatting are: 176 * year, month, date, day-of-week, am-pm, hour, hour-of-day, minute, second, 177 * and millisecond. 178 * (though we do not currently have specific intervalFormat date for skeletons 179 * with seconds and millisecond). 180 * Those calendar fields can be defined in the following order: 181 * year > month > date > hour (in day) > minute > second > millisecond 182 * 183 * The largest different calendar fields between 2 calendars is the 184 * first different calendar field in above order. 185 * 186 * For example: the largest different calendar fields between "Jan 10, 2007" 187 * and "Feb 20, 2008" is year. 188 * 189 *
190 * For other calendar fields, the compact interval formatting is not 191 * supported. And the interval format will be fall back to fall-back 192 * patterns, which is mostly "{date0} - {date1}". 193 * 194 *
195 * There is a set of pre-defined static skeleton strings. 196 * There are pre-defined interval patterns for those pre-defined skeletons 197 * in locales' resource files. 198 * For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is "yMMMd", 199 * in en_US, if the largest different calendar field between date1 and date2 200 * is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy", 201 * such as "Jan 10, 2007 - Jan 10, 2008". 202 * If the largest different calendar field between date1 and date2 is "month", 203 * the date interval pattern is "MMM d - MMM d, yyyy", 204 * such as "Jan 10 - Feb 10, 2007". 205 * If the largest different calendar field between date1 and date2 is "day", 206 * the date interval pattern is "MMM d-d, yyyy", such as "Jan 10-20, 2007". 207 * 208 * For date skeleton, the interval patterns when year, or month, or date is 209 * different are defined in resource files. 210 * For time skeleton, the interval patterns when am/pm, or hour, or minute is 211 * different are defined in resource files. 212 * 213 *
214 * If a skeleton is not found in a locale's DateIntervalInfo, which means 215 * the interval patterns for the skeleton is not defined in resource file, 216 * the interval pattern will falls back to the interval "fallback" pattern 217 * defined in resource file. 218 * If the interval "fallback" pattern is not defined, the default fall-back 219 * is "{date0} - {data1}". 220 * 221 *
222 * For the combination of date and time, 223 * The rule to generate interval patterns are: 224 *
243 * If two dates are the same, the interval pattern is the single date pattern. 244 * For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is 245 * "Jan 10, 2007". 246 * 247 * Or if the presenting fields between 2 dates have the exact same values, 248 * the interval pattern is the single date pattern. 249 * For example, if user only requests year and month, 250 * the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007". 251 * 252 *
253 * DateIntervalFormat needs the following information for correct 254 * formatting: time zone, calendar type, pattern, date format symbols, 255 * and date interval patterns. 256 * It can be instantiated in 2 ways: 257 *
277 * For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc. 278 * DateIntervalFormat uses the same syntax as that of 279 * DateTime format. 280 * 281 *
282 * Code Sample: general usage 283 *
284 * \code 285 * // the date interval object which the DateIntervalFormat formats on 286 * // and parses into 287 * DateInterval* dtInterval = new DateInterval(1000*3600*24, 1000*3600*24*2); 288 * UErrorCode status = U_ZERO_ERROR; 289 * DateIntervalFormat* dtIntervalFmt = DateIntervalFormat::createInstance( 290 * UDAT_YEAR_MONTH_DAY, 291 * Locale("en", "GB", ""), status); 292 * UnicodeUnicodeString dateIntervalString; 293 * FieldPosition pos = 0; 294 * // formatting 295 * dtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status); 296 * delete dtIntervalFmt; 297 * \endcode 298 *
323 * In this factory method, 324 * the date interval pattern information is load from resource files. 325 * Users are encouraged to created date interval formatter this way and 326 * to use the pre-defined skeleton macros. 327 * 328 *
329 * There are pre-defined skeletons (defined in udate.h) having predefined 330 * interval patterns in resource files. 331 * Users are encouraged to use those macros. 332 * For example: 333 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status) 334 * 335 * The given Locale provides the interval patterns. 336 * For example, for en_GB, if skeleton is UDAT_YEAR_ABBR_MONTH_WEEKDAY_DAY, 337 * which is "yMMMEEEd", 338 * the interval patterns defined in resource file to above skeleton are: 339 * "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs, 340 * "EEE, d MMM - EEE, d MMM, yyyy" for month differs, 341 * "EEE, d - EEE, d MMM, yyyy" for day differs, 342 * @param skeleton the skeleton on which the interval format is based. 343 * @param locale the given locale 344 * @param status output param set to success/failure code on exit 345 * @return a date time interval formatter which the caller owns. 346 * @stable ICU 4.0 347 *
348 *
352 */ 353 354 static DateIntervalFormat* U_EXPORT2 createInstance( 355 const UnicodeString& skeleton, 356 const Locale& locale, 357 UErrorCode& status); 358 359 /** 360 * Construct a DateIntervalFormat from skeleton 361 * DateIntervalInfo, and default locale. 362 * 363 * This is a convenient override of 364 * createInstance(const UnicodeString& skeleton, const Locale& locale, 365 * const DateIntervalInfo& dtitvinf, UErrorCode&) 366 * with the locale value as default locale. 367 * 368 * @param skeleton the skeleton on which interval format based. 369 * @param dtitvinf the DateIntervalInfo object. 370 * @param status output param set to success/failure code on exit 371 * @return a date time interval formatter which the caller owns. 372 * @stable ICU 4.0 373 */ 374 static DateIntervalFormat* U_EXPORT2 createInstance( 375 const UnicodeString& skeleton, 376 const DateIntervalInfo& dtitvinf, 377 UErrorCode& status); 378 379 /** 380 * Construct a DateIntervalFormat from skeleton 381 * a DateIntervalInfo, and the given locale. 382 * 383 *
384 * In this factory method, user provides its own date interval pattern 385 * information, instead of using those pre-defined data in resource file. 386 * This factory method is for powerful users who want to provide their own 387 * interval patterns. 388 *
389 * There are pre-defined skeletons (defined in udate.h) having predefined 390 * interval patterns in resource files. 391 * Users are encouraged to use those macros. 392 * For example: 393 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status) 394 * 395 * The DateIntervalInfo provides the interval patterns. 396 * and the DateIntervalInfo ownership remains to the caller. 397 * 398 * User are encouraged to set default interval pattern in DateIntervalInfo 399 * as well, if they want to set other interval patterns ( instead of 400 * reading the interval patterns from resource files). 401 * When the corresponding interval pattern for a largest calendar different 402 * field is not found ( if user not set it ), interval format fallback to 403 * the default interval pattern. 404 * If user does not provide default interval pattern, it fallback to 405 * "{date0} - {date1}" 406 * 407 * @param skeleton the skeleton on which interval format based. 408 * @param locale the given locale 409 * @param dtitvinf the DateIntervalInfo object. 410 * @param status output param set to success/failure code on exit 411 * @return a date time interval formatter which the caller owns. 412 * @stable ICU 4.0 413 *
414 *
418 */ 419 static DateIntervalFormat* U_EXPORT2 createInstance( 420 const UnicodeString& skeleton, 421 const Locale& locale, 422 const DateIntervalInfo& dtitvinf, 423 UErrorCode& status); 424 425 /** 426 * Destructor. 427 * @stable ICU 4.0 428 */ 429 virtual ~DateIntervalFormat(); 430 431 /** 432 * Clone this Format object polymorphically. The caller owns the result and 433 * should delete it when done. 434 * @return A copy of the object. 435 * @stable ICU 4.0 436 */ 437 virtual DateIntervalFormat* clone() const override; 438 439 /** 440 * Return true if the given Format objects are semantically equal. Objects 441 * of different subclasses are considered unequal. 442 * @param other the object to be compared with. 443 * @return true if the given Format objects are semantically equal. 444 * @stable ICU 4.0 445 */ 446 virtual bool operator==(const Format& other) const override; 447 448 /** 449 * Return true if the given Format objects are not semantically equal. 450 * Objects of different subclasses are considered unequal. 451 * @param other the object to be compared with. 452 * @return true if the given Format objects are not semantically equal. 453 * @stable ICU 4.0 454 */ 455 bool operator!=(const Format& other) const; 456 457 458 using Format::format; 459 460 /** 461 * Format an object to produce a string. This method handles Formattable 462 * objects with a DateInterval type. 463 * If a the Formattable object type is not a DateInterval, 464 * then it returns a failing UErrorCode. 465 * 466 * @param obj The object to format. 467 * Must be a DateInterval. 468 * @param appendTo Output parameter to receive result. 469 * Result is appended to existing contents. 470 * @param fieldPosition On input: an alignment field, if desired. 471 * On output: the offsets of the alignment field. 472 * There may be multiple instances of a given field type 473 * in an interval format; in this case the fieldPosition 474 * offsets refer to the first instance. 475 * @param status Output param filled with success/failure status. 476 * @return Reference to 'appendTo' parameter. 477 * @stable ICU 4.0 478 */ 479 virtual UnicodeString& format(const Formattable& obj, 480 UnicodeString& appendTo, 481 FieldPosition& fieldPosition, 482 UErrorCode& status) const override; 483 484 485 486 /** 487 * Format a DateInterval to produce a string. 488 * 489 * @param dtInterval DateInterval to be formatted. 490 * @param appendTo Output parameter to receive result. 491 * Result is appended to existing contents. 492 * @param fieldPosition On input: an alignment field, if desired. 493 * On output: the offsets of the alignment field. 494 * There may be multiple instances of a given field type 495 * in an interval format; in this case the fieldPosition 496 * offsets refer to the first instance. 497 * @param status Output param filled with success/failure status. 498 * @return Reference to 'appendTo' parameter. 499 * @stable ICU 4.0 500 */ 501 UnicodeString& format(const DateInterval* dtInterval, 502 UnicodeString& appendTo, 503 FieldPosition& fieldPosition, 504 UErrorCode& status) const ; 505 506 /** 507 * Format a DateInterval to produce a FormattedDateInterval. 508 * 509 * The FormattedDateInterval exposes field information about the formatted string. 510 * 511 * @param dtInterval DateInterval to be formatted. 512 * @param status Set if an error occurs. 513 * @return A FormattedDateInterval containing the format result. 514 * @stable ICU 64 515 */ 516 FormattedDateInterval formatToValue( 517 const DateInterval& dtInterval, 518 UErrorCode& status) const; 519 520 /** 521 * Format 2 Calendars to produce a string. 522 * 523 * Note: "fromCalendar" and "toCalendar" are not const, 524 * since calendar is not const in SimpleDateFormat::format(Calendar&), 525 * 526 * @param fromCalendar calendar set to the from date in date interval 527 * to be formatted into date interval string 528 * @param toCalendar calendar set to the to date in date interval 529 * to be formatted into date interval string 530 * @param appendTo Output parameter to receive result. 531 * Result is appended to existing contents. 532 * @param fieldPosition On input: an alignment field, if desired. 533 * On output: the offsets of the alignment field. 534 * There may be multiple instances of a given field type 535 * in an interval format; in this case the fieldPosition 536 * offsets refer to the first instance. 537 * @param status Output param filled with success/failure status. 538 * Caller needs to make sure it is SUCCESS 539 * at the function entrance 540 * @return Reference to 'appendTo' parameter. 541 * @stable ICU 4.0 542 */ 543 UnicodeString& format(Calendar& fromCalendar, 544 Calendar& toCalendar, 545 UnicodeString& appendTo, 546 FieldPosition& fieldPosition, 547 UErrorCode& status) const ; 548 549 /** 550 * Format 2 Calendars to produce a FormattedDateInterval. 551 * 552 * The FormattedDateInterval exposes field information about the formatted string. 553 * 554 * Note: "fromCalendar" and "toCalendar" are not const, 555 * since calendar is not const in SimpleDateFormat::format(Calendar&), 556 * 557 * @param fromCalendar calendar set to the from date in date interval 558 * to be formatted into date interval string 559 * @param toCalendar calendar set to the to date in date interval 560 * to be formatted into date interval string 561 * @param status Set if an error occurs. 562 * @return A FormattedDateInterval containing the format result. 563 * @stable ICU 64 564 */ 565 FormattedDateInterval formatToValue( 566 Calendar& fromCalendar, 567 Calendar& toCalendar, 568 UErrorCode& status) const; 569 570 /** 571 * Date interval parsing is not supported. Please do not use. 572 *
573 * This method should handle parsing of 574 * date time interval strings into Formattable objects with 575 * DateInterval type, which is a pair of UDate. 576 *
577 * Before calling, set parse_pos.index to the offset you want to start 578 * parsing at in the source. After calling, parse_pos.index is the end of 579 * the text you parsed. If error occurs, index is unchanged. 580 *
581 * When parsing, leading whitespace is discarded (with a successful parse), 582 * while trailing whitespace is left as is. 583 *
584 * See Format::parseObject() for more. 585 * 586 * @param source The string to be parsed into an object. 587 * @param result Formattable to be set to the parse result. 588 * If parse fails, return contents are undefined. 589 * @param parse_pos The position to start parsing at. Since no parsing 590 * is supported, upon return this param is unchanged. 591 * @return A newly created Formattable* object, or nullptr 592 * on failure. The caller owns this and should 593 * delete it when done. 594 * @internal ICU 4.0 595 */ 596 virtual void parseObject(const UnicodeString& source, 597 Formattable& result, 598 ParsePosition& parse_pos) const override; 599 600 601 /** 602 * Gets the date time interval patterns. 603 * @return the date time interval patterns associated with 604 * this date interval formatter. 605 * @stable ICU 4.0 606 */ 607 const DateIntervalInfo* getDateIntervalInfo(void) const; 608 609 610 /** 611 * Set the date time interval patterns. 612 * @param newIntervalPatterns the given interval patterns to copy. 613 * @param status output param set to success/failure code on exit 614 * @stable ICU 4.0 615 */ 616 void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns, 617 UErrorCode& status); 618 619 620 /** 621 * Gets the date formatter. The DateIntervalFormat instance continues to own 622 * the returned DateFormatter object, and will use and possibly modify it 623 * during format operations. In a multi-threaded environment, the returned 624 * DateFormat can only be used if it is certain that no other threads are 625 * concurrently using this DateIntervalFormatter, even for nominally const 626 * functions. 627 * 628 * @return the date formatter associated with this date interval formatter. 629 * @stable ICU 4.0 630 */ 631 const DateFormat* getDateFormat(void) const; 632 633 /** 634 * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar. 635 * @return the time zone associated with the calendar of DateIntervalFormat. 636 * @stable ICU 4.8 637 */ 638 virtual const TimeZone& getTimeZone(void) const; 639 640 /** 641 * Sets the time zone for the calendar used by this DateIntervalFormat object. The 642 * caller no longer owns the TimeZone object and should not delete it after this call. 643 * @param zoneToAdopt the TimeZone to be adopted. 644 * @stable ICU 4.8 645 */ 646 virtual void adoptTimeZone(TimeZone* zoneToAdopt); 647 648 /** 649 * Sets the time zone for the calendar used by this DateIntervalFormat object. 650 * @param zone the new time zone. 651 * @stable ICU 4.8 652 */ 653 virtual void setTimeZone(const TimeZone& zone); 654 655 /** 656 * Set a particular UDisplayContext value in the formatter, such as 657 * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. This causes the formatted 658 * result to be capitalized appropriately for the context in which 659 * it is intended to be used, considering both the locale and the 660 * type of field at the beginning of the formatted result. 661 * @param value The UDisplayContext value to set. 662 * @param status Input/output status. If at entry this indicates a failure 663 * status, the function will do nothing; otherwise this will be 664 * updated with any new status from the function. 665 * @stable ICU 68 666 */ 667 virtual void setContext(UDisplayContext value, UErrorCode& status); 668 669 /** 670 * Get the formatter's UDisplayContext value for the specified UDisplayContextType, 671 * such as UDISPCTX_TYPE_CAPITALIZATION. 672 * @param type The UDisplayContextType whose value to return 673 * @param status Input/output status. If at entry this indicates a failure 674 * status, the function will do nothing; otherwise this will be 675 * updated with any new status from the function. 676 * @return The UDisplayContextValue for the specified type. 677 * @stable ICU 68 678 */ 679 virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const; 680 681 /** 682 * Return the class ID for this class. This is useful only for comparing to 683 * a return value from getDynamicClassID(). For example: 684 *
685 * . Base* polymorphic_pointer = createPolymorphicObject(); 686 * . if (polymorphic_pointer->getDynamicClassID() == 687 * . erived::getStaticClassID()) ... 688 *