* Date interval means from one date to another date, * for example, from "Jan 11, 2008" to "Jan 18, 2008". * We introduced class DateInterval to represent it. * DateInterval is a pair of UDate, which is * the standard milliseconds since 24:00 GMT, Jan 1, 1970. * *
* DateIntervalFormat formats a DateInterval into * text as compactly as possible. * For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008" * is "Jan 11-18, 2008" for English. * And it parses text into DateInterval, * although initially, parsing is not supported. * *
* There is no structural information in date time patterns. * For any punctuations and string literals inside a date time pattern, * we do not know whether it is just a separator, or a prefix, or a suffix. * Without such information, so, it is difficult to generate a sub-pattern * (or super-pattern) by algorithm. * So, formatting a DateInterval is pattern-driven. It is very * similar to formatting in SimpleDateFormat. * We introduce class DateIntervalInfo to save date interval * patterns, similar to date time pattern in SimpleDateFormat. * *
* Logically, the interval patterns are mappings * from (skeleton, the_largest_different_calendar_field) * to (date_interval_pattern). * *
* A skeleton *
* The calendar fields we support for interval formatting are: * year, month, date, day-of-week, am-pm, hour, hour-of-day, minute, second, * and millisecond. * (though we do not currently have specific intervalFormat date for skeletons * with seconds and millisecond). * Those calendar fields can be defined in the following order: * year > month > date > hour (in day) > minute > second > millisecond * * The largest different calendar fields between 2 calendars is the * first different calendar field in above order. * * For example: the largest different calendar fields between "Jan 10, 2007" * and "Feb 20, 2008" is year. * *
* For other calendar fields, the compact interval formatting is not * supported. And the interval format will be fall back to fall-back * patterns, which is mostly "{date0} - {date1}". * *
* There is a set of pre-defined static skeleton strings. * There are pre-defined interval patterns for those pre-defined skeletons * in locales' resource files. * For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is "yMMMd", * in en_US, if the largest different calendar field between date1 and date2 * is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy", * such as "Jan 10, 2007 - Jan 10, 2008". * If the largest different calendar field between date1 and date2 is "month", * the date interval pattern is "MMM d - MMM d, yyyy", * such as "Jan 10 - Feb 10, 2007". * If the largest different calendar field between date1 and date2 is "day", * the date interval pattern is "MMM d-d, yyyy", such as "Jan 10-20, 2007". * * For date skeleton, the interval patterns when year, or month, or date is * different are defined in resource files. * For time skeleton, the interval patterns when am/pm, or hour, or minute is * different are defined in resource files. * *
* If a skeleton is not found in a locale's DateIntervalInfo, which means * the interval patterns for the skeleton is not defined in resource file, * the interval pattern will falls back to the interval "fallback" pattern * defined in resource file. * If the interval "fallback" pattern is not defined, the default fall-back * is "{date0} - {data1}". * *
* For the combination of date and time, * The rule to generate interval patterns are: *
* If two dates are the same, the interval pattern is the single date pattern. * For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is * "Jan 10, 2007". * * Or if the presenting fields between 2 dates have the exact same values, * the interval pattern is the single date pattern. * For example, if user only requests year and month, * the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007". * *
* DateIntervalFormat needs the following information for correct * formatting: time zone, calendar type, pattern, date format symbols, * and date interval patterns. * It can be instantiated in 2 ways: *
* For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc. * DateIntervalFormat uses the same syntax as that of * DateTime format. * *
* Code Sample: general usage *
* \code * // the date interval object which the DateIntervalFormat formats on * // and parses into * DateInterval* dtInterval = new DateInterval(1000*3600*24, 1000*3600*24*2); * UErrorCode status = U_ZERO_ERROR; * DateIntervalFormat* dtIntervalFmt = DateIntervalFormat::createInstance( * UDAT_YEAR_MONTH_DAY, * Locale("en", "GB", ""), status); * UnicodeUnicodeString dateIntervalString; * FieldPosition pos = 0; * // formatting * dtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status); * delete dtIntervalFmt; * \endcode *
* In this factory method, * the date interval pattern information is load from resource files. * Users are encouraged to created date interval formatter this way and * to use the pre-defined skeleton macros. * *
* There are pre-defined skeletons (defined in udate.h) having predefined * interval patterns in resource files. * Users are encouraged to use those macros. * For example: * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status) * * The given Locale provides the interval patterns. * For example, for en_GB, if skeleton is UDAT_YEAR_ABBR_MONTH_WEEKDAY_DAY, * which is "yMMMEEEd", * the interval patterns defined in resource file to above skeleton are: * "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs, * "EEE, d MMM - EEE, d MMM, yyyy" for month differs, * "EEE, d - EEE, d MMM, yyyy" for day differs, * @param skeleton the skeleton on which the interval format is based. * @param locale the given locale * @param status output param set to success/failure code on exit * @return a date time interval formatter which the caller owns. * @stable ICU 4.0 *
*
*/ static DateIntervalFormat* U_EXPORT2 createInstance( const UnicodeString& skeleton, const Locale& locale, UErrorCode& status); /** * Construct a DateIntervalFormat from skeleton * DateIntervalInfo, and default locale. * * This is a convenient override of * createInstance(const UnicodeString& skeleton, const Locale& locale, * const DateIntervalInfo& dtitvinf, UErrorCode&) * with the locale value as default locale. * * @param skeleton the skeleton on which interval format based. * @param dtitvinf the DateIntervalInfo object. * @param status output param set to success/failure code on exit * @return a date time interval formatter which the caller owns. * @stable ICU 4.0 */ static DateIntervalFormat* U_EXPORT2 createInstance( const UnicodeString& skeleton, const DateIntervalInfo& dtitvinf, UErrorCode& status); /** * Construct a DateIntervalFormat from skeleton * a DateIntervalInfo, and the given locale. * *
* In this factory method, user provides its own date interval pattern * information, instead of using those pre-defined data in resource file. * This factory method is for powerful users who want to provide their own * interval patterns. *
* There are pre-defined skeletons (defined in udate.h) having predefined * interval patterns in resource files. * Users are encouraged to use those macros. * For example: * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status) * * The DateIntervalInfo provides the interval patterns. * and the DateIntervalInfo ownership remains to the caller. * * User are encouraged to set default interval pattern in DateIntervalInfo * as well, if they want to set other interval patterns ( instead of * reading the interval patterns from resource files). * When the corresponding interval pattern for a largest calendar different * field is not found ( if user not set it ), interval format fallback to * the default interval pattern. * If user does not provide default interval pattern, it fallback to * "{date0} - {date1}" * * @param skeleton the skeleton on which interval format based. * @param locale the given locale * @param dtitvinf the DateIntervalInfo object. * @param status output param set to success/failure code on exit * @return a date time interval formatter which the caller owns. * @stable ICU 4.0 *
*/ static DateIntervalFormat* U_EXPORT2 createInstance( const UnicodeString& skeleton, const Locale& locale, const DateIntervalInfo& dtitvinf, UErrorCode& status); /** * Destructor. * @stable ICU 4.0 */ virtual ~DateIntervalFormat(); /** * Clone this Format object polymorphically. The caller owns the result and * should delete it when done. * @return A copy of the object. * @stable ICU 4.0 */ virtual DateIntervalFormat* clone() const; /** * Return true if the given Format objects are semantically equal. Objects * of different subclasses are considered unequal. * @param other the object to be compared with. * @return true if the given Format objects are semantically equal. * @stable ICU 4.0 */ virtual UBool operator==(const Format& other) const; /** * Return true if the given Format objects are not semantically equal. * Objects of different subclasses are considered unequal. * @param other the object to be compared with. * @return true if the given Format objects are not semantically equal. * @stable ICU 4.0 */ UBool operator!=(const Format& other) const; using Format::format; /** * Format an object to produce a string. This method handles Formattable * objects with a DateInterval type. * If a the Formattable object type is not a DateInterval, * then it returns a failing UErrorCode. * * @param obj The object to format. * Must be a DateInterval. * @param appendTo Output parameter to receive result. * Result is appended to existing contents. * @param fieldPosition On input: an alignment field, if desired. * On output: the offsets of the alignment field. * There may be multiple instances of a given field type * in an interval format; in this case the fieldPosition * offsets refer to the first instance. * @param status Output param filled with success/failure status. * @return Reference to 'appendTo' parameter. * @stable ICU 4.0 */ virtual UnicodeString& format(const Formattable& obj, UnicodeString& appendTo, FieldPosition& fieldPosition, UErrorCode& status) const ; /** * Format a DateInterval to produce a string. * * @param dtInterval DateInterval to be formatted. * @param appendTo Output parameter to receive result. * Result is appended to existing contents. * @param fieldPosition On input: an alignment field, if desired. * On output: the offsets of the alignment field. * There may be multiple instances of a given field type * in an interval format; in this case the fieldPosition * offsets refer to the first instance. * @param status Output param filled with success/failure status. * @return Reference to 'appendTo' parameter. * @stable ICU 4.0 */ UnicodeString& format(const DateInterval* dtInterval, UnicodeString& appendTo, FieldPosition& fieldPosition, UErrorCode& status) const ; /** * Format a DateInterval to produce a FormattedDateInterval. * * The FormattedDateInterval exposes field information about the formatted string. * * @param dtInterval DateInterval to be formatted. * @param status Set if an error occurs. * @return A FormattedDateInterval containing the format result. * @stable ICU 64 */ FormattedDateInterval formatToValue( const DateInterval& dtInterval, UErrorCode& status) const; /** * Format 2 Calendars to produce a string. * * Note: "fromCalendar" and "toCalendar" are not const, * since calendar is not const in SimpleDateFormat::format(Calendar&), * * @param fromCalendar calendar set to the from date in date interval * to be formatted into date interval string * @param toCalendar calendar set to the to date in date interval * to be formatted into date interval string * @param appendTo Output parameter to receive result. * Result is appended to existing contents. * @param fieldPosition On input: an alignment field, if desired. * On output: the offsets of the alignment field. * There may be multiple instances of a given field type * in an interval format; in this case the fieldPosition * offsets refer to the first instance. * @param status Output param filled with success/failure status. * Caller needs to make sure it is SUCCESS * at the function entrance * @return Reference to 'appendTo' parameter. * @stable ICU 4.0 */ UnicodeString& format(Calendar& fromCalendar, Calendar& toCalendar, UnicodeString& appendTo, FieldPosition& fieldPosition, UErrorCode& status) const ; /** * Format 2 Calendars to produce a FormattedDateInterval. * * The FormattedDateInterval exposes field information about the formatted string. * * Note: "fromCalendar" and "toCalendar" are not const, * since calendar is not const in SimpleDateFormat::format(Calendar&), * * @param fromCalendar calendar set to the from date in date interval * to be formatted into date interval string * @param toCalendar calendar set to the to date in date interval * to be formatted into date interval string * @param status Set if an error occurs. * @return A FormattedDateInterval containing the format result. * @stable ICU 64 */ FormattedDateInterval formatToValue( Calendar& fromCalendar, Calendar& toCalendar, UErrorCode& status) const; /** * Date interval parsing is not supported. Please do not use. *
* This method should handle parsing of * date time interval strings into Formattable objects with * DateInterval type, which is a pair of UDate. *
* Before calling, set parse_pos.index to the offset you want to start * parsing at in the source. After calling, parse_pos.index is the end of * the text you parsed. If error occurs, index is unchanged. *
* When parsing, leading whitespace is discarded (with a successful parse), * while trailing whitespace is left as is. *
* See Format::parseObject() for more. * * @param source The string to be parsed into an object. * @param result Formattable to be set to the parse result. * If parse fails, return contents are undefined. * @param parse_pos The position to start parsing at. Since no parsing * is supported, upon return this param is unchanged. * @return A newly created Formattable* object, or NULL * on failure. The caller owns this and should * delete it when done. * @internal ICU 4.0 */ virtual void parseObject(const UnicodeString& source, Formattable& result, ParsePosition& parse_pos) const; /** * Gets the date time interval patterns. * @return the date time interval patterns associated with * this date interval formatter. * @stable ICU 4.0 */ const DateIntervalInfo* getDateIntervalInfo(void) const; /** * Set the date time interval patterns. * @param newIntervalPatterns the given interval patterns to copy. * @param status output param set to success/failure code on exit * @stable ICU 4.0 */ void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns, UErrorCode& status); /** * Gets the date formatter. The DateIntervalFormat instance continues to own * the returned DateFormatter object, and will use and possibly modify it * during format operations. In a multi-threaded environment, the returned * DateFormat can only be used if it is certain that no other threads are * concurrently using this DateIntervalFormatter, even for nominally const * functions. * * @return the date formatter associated with this date interval formatter. * @stable ICU 4.0 */ const DateFormat* getDateFormat(void) const; /** * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar. * @return the time zone associated with the calendar of DateIntervalFormat. * @stable ICU 4.8 */ virtual const TimeZone& getTimeZone(void) const; /** * Sets the time zone for the calendar used by this DateIntervalFormat object. The * caller no longer owns the TimeZone object and should not delete it after this call. * @param zoneToAdopt the TimeZone to be adopted. * @stable ICU 4.8 */ virtual void adoptTimeZone(TimeZone* zoneToAdopt); /** * Sets the time zone for the calendar used by this DateIntervalFormat object. * @param zone the new time zone. * @stable ICU 4.8 */ virtual void setTimeZone(const TimeZone& zone); /** * Return the class ID for this class. This is useful only for comparing to * a return value from getDynamicClassID(). For example: *
* . Base* polymorphic_pointer = createPolymorphicObject(); * . if (polymorphic_pointer->getDynamicClassID() == * . erived::getStaticClassID()) ... *