michael@0: /******************************************************************************** michael@0: * Copyright (C) 2008-2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * michael@0: * File DTITVFMT.H michael@0: * michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #ifndef __DTITVFMT_H__ michael@0: #define __DTITVFMT_H__ michael@0: michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Format and parse date interval in a language-independent manner. michael@0: */ michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/ucal.h" michael@0: #include "unicode/smpdtfmt.h" michael@0: #include "unicode/dtintrv.h" michael@0: #include "unicode/dtitvinf.h" michael@0: #include "unicode/dtptngen.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: michael@0: michael@0: /** michael@0: * DateIntervalFormat is a class for formatting and parsing date michael@0: * intervals in a language-independent manner. michael@0: * Only formatting is supported, parsing is not supported. michael@0: * michael@0: *

michael@0: * Date interval means from one date to another date, michael@0: * for example, from "Jan 11, 2008" to "Jan 18, 2008". michael@0: * We introduced class DateInterval to represent it. michael@0: * DateInterval is a pair of UDate, which is michael@0: * the standard milliseconds since 24:00 GMT, Jan 1, 1970. michael@0: * michael@0: *

michael@0: * DateIntervalFormat formats a DateInterval into michael@0: * text as compactly as possible. michael@0: * For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008" michael@0: * is "Jan 11-18, 2008" for English. michael@0: * And it parses text into DateInterval, michael@0: * although initially, parsing is not supported. michael@0: * michael@0: *

michael@0: * There is no structural information in date time patterns. michael@0: * For any punctuations and string literals inside a date time pattern, michael@0: * we do not know whether it is just a separator, or a prefix, or a suffix. michael@0: * Without such information, so, it is difficult to generate a sub-pattern michael@0: * (or super-pattern) by algorithm. michael@0: * So, formatting a DateInterval is pattern-driven. It is very michael@0: * similar to formatting in SimpleDateFormat. michael@0: * We introduce class DateIntervalInfo to save date interval michael@0: * patterns, similar to date time pattern in SimpleDateFormat. michael@0: * michael@0: *

michael@0: * Logically, the interval patterns are mappings michael@0: * from (skeleton, the_largest_different_calendar_field) michael@0: * to (date_interval_pattern). michael@0: * michael@0: *

michael@0: * A skeleton michael@0: *

    michael@0: *
  1. michael@0: * only keeps the field pattern letter and ignores all other parts michael@0: * in a pattern, such as space, punctuations, and string literals. michael@0: *
  2. michael@0: *
  3. michael@0: * hides the order of fields. michael@0: *
  4. michael@0: *
  5. michael@0: * might hide a field's pattern letter length. michael@0: *
  6. michael@0: *
michael@0: * michael@0: * For those non-digit calendar fields, the pattern letter length is michael@0: * important, such as MMM, MMMM, and MMMMM; EEE and EEEE, michael@0: * and the field's pattern letter length is honored. michael@0: * michael@0: * For the digit calendar fields, such as M or MM, d or dd, yy or yyyy, michael@0: * the field pattern length is ignored and the best match, which is defined michael@0: * in date time patterns, will be returned without honor the field pattern michael@0: * letter length in skeleton. michael@0: * michael@0: *

michael@0: * The calendar fields we support for interval formatting are: michael@0: * year, month, date, day-of-week, am-pm, hour, hour-of-day, and minute. michael@0: * Those calendar fields can be defined in the following order: michael@0: * year > month > date > hour (in day) > minute michael@0: * michael@0: * The largest different calendar fields between 2 calendars is the michael@0: * first different calendar field in above order. michael@0: * michael@0: * For example: the largest different calendar fields between "Jan 10, 2007" michael@0: * and "Feb 20, 2008" is year. michael@0: * michael@0: *

michael@0: * For other calendar fields, the compact interval formatting is not michael@0: * supported. And the interval format will be fall back to fall-back michael@0: * patterns, which is mostly "{date0} - {date1}". michael@0: * michael@0: *

michael@0: * There is a set of pre-defined static skeleton strings. michael@0: * There are pre-defined interval patterns for those pre-defined skeletons michael@0: * in locales' resource files. michael@0: * For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is "yMMMd", michael@0: * in en_US, if the largest different calendar field between date1 and date2 michael@0: * is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy", michael@0: * such as "Jan 10, 2007 - Jan 10, 2008". michael@0: * If the largest different calendar field between date1 and date2 is "month", michael@0: * the date interval pattern is "MMM d - MMM d, yyyy", michael@0: * such as "Jan 10 - Feb 10, 2007". michael@0: * If the largest different calendar field between date1 and date2 is "day", michael@0: * the date interval pattern is "MMM d-d, yyyy", such as "Jan 10-20, 2007". michael@0: * michael@0: * For date skeleton, the interval patterns when year, or month, or date is michael@0: * different are defined in resource files. michael@0: * For time skeleton, the interval patterns when am/pm, or hour, or minute is michael@0: * different are defined in resource files. michael@0: * michael@0: *

michael@0: * If a skeleton is not found in a locale's DateIntervalInfo, which means michael@0: * the interval patterns for the skeleton is not defined in resource file, michael@0: * the interval pattern will falls back to the interval "fallback" pattern michael@0: * defined in resource file. michael@0: * If the interval "fallback" pattern is not defined, the default fall-back michael@0: * is "{date0} - {data1}". michael@0: * michael@0: *

michael@0: * For the combination of date and time, michael@0: * The rule to generate interval patterns are: michael@0: *

    michael@0: *
  1. michael@0: * when the year, month, or day differs, falls back to fall-back michael@0: * interval pattern, which mostly is the concatenate the two original michael@0: * expressions with a separator between, michael@0: * For example, interval pattern from "Jan 10, 2007 10:10 am" michael@0: * to "Jan 11, 2007 10:10am" is michael@0: * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am" michael@0: *
  2. michael@0: *
  3. michael@0: * otherwise, present the date followed by the range expression michael@0: * for the time. michael@0: * For example, interval pattern from "Jan 10, 2007 10:10 am" michael@0: * to "Jan 10, 2007 11:10am" is "Jan 10, 2007 10:10 am - 11:10am" michael@0: *
  4. michael@0: *
michael@0: * michael@0: * michael@0: *

michael@0: * If two dates are the same, the interval pattern is the single date pattern. michael@0: * For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is michael@0: * "Jan 10, 2007". michael@0: * michael@0: * Or if the presenting fields between 2 dates have the exact same values, michael@0: * the interval pattern is the single date pattern. michael@0: * For example, if user only requests year and month, michael@0: * the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007". michael@0: * michael@0: *

michael@0: * DateIntervalFormat needs the following information for correct michael@0: * formatting: time zone, calendar type, pattern, date format symbols, michael@0: * and date interval patterns. michael@0: * It can be instantiated in 2 ways: michael@0: *

    michael@0: *
  1. michael@0: * create an instance using default or given locale plus given skeleton. michael@0: * Users are encouraged to created date interval formatter this way and michael@0: * to use the pre-defined skeleton macros, such as michael@0: * UDAT_YEAR_NUM_MONTH, which consists the calendar fields and michael@0: * the format style. michael@0: *
  2. michael@0: *
  3. michael@0: * create an instance using default or given locale plus given skeleton michael@0: * plus a given DateIntervalInfo. michael@0: * This factory method is for powerful users who want to provide their own michael@0: * interval patterns. michael@0: * Locale provides the timezone, calendar, and format symbols information. michael@0: * Local plus skeleton provides full pattern information. michael@0: * DateIntervalInfo provides the date interval patterns. michael@0: *
  4. michael@0: *
michael@0: * michael@0: *

michael@0: * For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc. michael@0: * DateIntervalFormat uses the same syntax as that of michael@0: * DateTime format. michael@0: * michael@0: *

michael@0: * Code Sample: general usage michael@0: *

michael@0:  * \code
michael@0:  *   // the date interval object which the DateIntervalFormat formats on
michael@0:  *   // and parses into
michael@0:  *   DateInterval*  dtInterval = new DateInterval(1000*3600*24, 1000*3600*24*2);
michael@0:  *   UErrorCode status = U_ZERO_ERROR;
michael@0:  *   DateIntervalFormat* dtIntervalFmt = DateIntervalFormat::createInstance(
michael@0:  *                           UDAT_YEAR_MONTH_DAY,
michael@0:  *                           Locale("en", "GB", ""), status);
michael@0:  *   UnicodeUnicodeString dateIntervalString;
michael@0:  *   FieldPosition pos = 0;
michael@0:  *   // formatting
michael@0:  *   dtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status);
michael@0:  *   delete dtIntervalFmt;
michael@0:  * \endcode
michael@0:  * 
michael@0: */ michael@0: michael@0: class U_I18N_API DateIntervalFormat : public Format { michael@0: public: michael@0: michael@0: /** michael@0: * Construct a DateIntervalFormat from skeleton and the default locale. michael@0: * michael@0: * This is a convenient override of michael@0: * createInstance(const UnicodeString& skeleton, const Locale& locale, michael@0: * UErrorCode&) michael@0: * with the value of locale as default locale. michael@0: * michael@0: * @param skeleton the skeleton on which interval format based. michael@0: * @param status output param set to success/failure code on exit michael@0: * @return a date time interval formatter which the caller owns. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: static DateIntervalFormat* U_EXPORT2 createInstance( michael@0: const UnicodeString& skeleton, michael@0: UErrorCode& status); michael@0: michael@0: /** michael@0: * Construct a DateIntervalFormat from skeleton and a given locale. michael@0: *

michael@0: * In this factory method, michael@0: * the date interval pattern information is load from resource files. michael@0: * Users are encouraged to created date interval formatter this way and michael@0: * to use the pre-defined skeleton macros. michael@0: * michael@0: *

michael@0: * There are pre-defined skeletons (defined in udate.h) having predefined michael@0: * interval patterns in resource files. michael@0: * Users are encouraged to use those macros. michael@0: * For example: michael@0: * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status) michael@0: * michael@0: * The given Locale provides the interval patterns. michael@0: * For example, for en_GB, if skeleton is UDAT_YEAR_ABBR_MONTH_WEEKDAY_DAY, michael@0: * which is "yMMMEEEd", michael@0: * the interval patterns defined in resource file to above skeleton are: michael@0: * "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs, michael@0: * "EEE, d MMM - EEE, d MMM, yyyy" for month differs, michael@0: * "EEE, d - EEE, d MMM, yyyy" for day differs, michael@0: * @param skeleton the skeleton on which the interval format is based. michael@0: * @param locale the given locale michael@0: * @param status output param set to success/failure code on exit michael@0: * @return a date time interval formatter which the caller owns. michael@0: * @stable ICU 4.0 michael@0: *

michael@0: *

Sample code

michael@0: * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1 michael@0: * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined michael@0: *

michael@0: */ michael@0: michael@0: static DateIntervalFormat* U_EXPORT2 createInstance( michael@0: const UnicodeString& skeleton, michael@0: const Locale& locale, michael@0: UErrorCode& status); michael@0: michael@0: /** michael@0: * Construct a DateIntervalFormat from skeleton michael@0: * DateIntervalInfo, and default locale. michael@0: * michael@0: * This is a convenient override of michael@0: * createInstance(const UnicodeString& skeleton, const Locale& locale, michael@0: * const DateIntervalInfo& dtitvinf, UErrorCode&) michael@0: * with the locale value as default locale. michael@0: * michael@0: * @param skeleton the skeleton on which interval format based. michael@0: * @param dtitvinf the DateIntervalInfo object. michael@0: * @param status output param set to success/failure code on exit michael@0: * @return a date time interval formatter which the caller owns. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: static DateIntervalFormat* U_EXPORT2 createInstance( michael@0: const UnicodeString& skeleton, michael@0: const DateIntervalInfo& dtitvinf, michael@0: UErrorCode& status); michael@0: michael@0: /** michael@0: * Construct a DateIntervalFormat from skeleton michael@0: * a DateIntervalInfo, and the given locale. michael@0: * michael@0: *

michael@0: * In this factory method, user provides its own date interval pattern michael@0: * information, instead of using those pre-defined data in resource file. michael@0: * This factory method is for powerful users who want to provide their own michael@0: * interval patterns. michael@0: *

michael@0: * There are pre-defined skeletons (defined in udate.h) having predefined michael@0: * interval patterns in resource files. michael@0: * Users are encouraged to use those macros. michael@0: * For example: michael@0: * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status) michael@0: * michael@0: * The DateIntervalInfo provides the interval patterns. michael@0: * and the DateIntervalInfo ownership remains to the caller. michael@0: * michael@0: * User are encouraged to set default interval pattern in DateIntervalInfo michael@0: * as well, if they want to set other interval patterns ( instead of michael@0: * reading the interval patterns from resource files). michael@0: * When the corresponding interval pattern for a largest calendar different michael@0: * field is not found ( if user not set it ), interval format fallback to michael@0: * the default interval pattern. michael@0: * If user does not provide default interval pattern, it fallback to michael@0: * "{date0} - {date1}" michael@0: * michael@0: * @param skeleton the skeleton on which interval format based. michael@0: * @param locale the given locale michael@0: * @param dtitvinf the DateIntervalInfo object. michael@0: * @param status output param set to success/failure code on exit michael@0: * @return a date time interval formatter which the caller owns. michael@0: * @stable ICU 4.0 michael@0: *

michael@0: *

Sample code

michael@0: * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1 michael@0: * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtCustomized michael@0: *

michael@0: */ michael@0: static DateIntervalFormat* U_EXPORT2 createInstance( michael@0: const UnicodeString& skeleton, michael@0: const Locale& locale, michael@0: const DateIntervalInfo& dtitvinf, michael@0: UErrorCode& status); michael@0: michael@0: /** michael@0: * Destructor. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: virtual ~DateIntervalFormat(); michael@0: michael@0: /** michael@0: * Clone this Format object polymorphically. The caller owns the result and michael@0: * should delete it when done. michael@0: * @return A copy of the object. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: virtual Format* clone(void) const; michael@0: michael@0: /** michael@0: * Return true if the given Format objects are semantically equal. Objects michael@0: * of different subclasses are considered unequal. michael@0: * @param other the object to be compared with. michael@0: * @return true if the given Format objects are semantically equal. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: virtual UBool operator==(const Format& other) const; michael@0: michael@0: /** michael@0: * Return true if the given Format objects are not semantically equal. michael@0: * Objects of different subclasses are considered unequal. michael@0: * @param other the object to be compared with. michael@0: * @return true if the given Format objects are not semantically equal. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: UBool operator!=(const Format& other) const; michael@0: michael@0: michael@0: using Format::format; michael@0: michael@0: /** michael@0: * Format an object to produce a string. This method handles Formattable michael@0: * objects with a DateInterval type. michael@0: * If a the Formattable object type is not a DateInterval, michael@0: * then it returns a failing UErrorCode. michael@0: * michael@0: * @param obj The object to format. michael@0: * Must be a DateInterval. michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param fieldPosition On input: an alignment field, if desired. michael@0: * On output: the offsets of the alignment field. michael@0: * @param status Output param filled with success/failure status. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: virtual UnicodeString& format(const Formattable& obj, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& fieldPosition, michael@0: UErrorCode& status) const ; michael@0: michael@0: michael@0: michael@0: /** michael@0: * Format a DateInterval to produce a string. michael@0: * michael@0: * @param dtInterval DateInterval to be formatted. michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param fieldPosition On input: an alignment field, if desired. michael@0: * On output: the offsets of the alignment field. michael@0: * @param status Output param filled with success/failure status. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: UnicodeString& format(const DateInterval* dtInterval, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& fieldPosition, michael@0: UErrorCode& status) const ; michael@0: michael@0: michael@0: /** michael@0: * Format 2 Calendars to produce a string. michael@0: * michael@0: * Note: "fromCalendar" and "toCalendar" are not const, michael@0: * since calendar is not const in SimpleDateFormat::format(Calendar&), michael@0: * michael@0: * @param fromCalendar calendar set to the from date in date interval michael@0: * to be formatted into date interval string michael@0: * @param toCalendar calendar set to the to date in date interval michael@0: * to be formatted into date interval string michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param fieldPosition On input: an alignment field, if desired. michael@0: * On output: the offsets of the alignment field. michael@0: * @param status Output param filled with success/failure status. michael@0: * Caller needs to make sure it is SUCCESS michael@0: * at the function entrance michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: UnicodeString& format(Calendar& fromCalendar, michael@0: Calendar& toCalendar, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& fieldPosition, michael@0: UErrorCode& status) const ; michael@0: michael@0: /** michael@0: * Date interval parsing is not supported. Please do not use. michael@0: *

michael@0: * This method should handle parsing of michael@0: * date time interval strings into Formattable objects with michael@0: * DateInterval type, which is a pair of UDate. michael@0: *

michael@0: * Before calling, set parse_pos.index to the offset you want to start michael@0: * parsing at in the source. After calling, parse_pos.index is the end of michael@0: * the text you parsed. If error occurs, index is unchanged. michael@0: *

michael@0: * When parsing, leading whitespace is discarded (with a successful parse), michael@0: * while trailing whitespace is left as is. michael@0: *

michael@0: * See Format::parseObject() for more. michael@0: * michael@0: * @param source The string to be parsed into an object. michael@0: * @param result Formattable to be set to the parse result. michael@0: * If parse fails, return contents are undefined. michael@0: * @param parse_pos The position to start parsing at. Since no parsing michael@0: * is supported, upon return this param is unchanged. michael@0: * @return A newly created Formattable* object, or NULL michael@0: * on failure. The caller owns this and should michael@0: * delete it when done. michael@0: * @internal ICU 4.0 michael@0: */ michael@0: virtual void parseObject(const UnicodeString& source, michael@0: Formattable& result, michael@0: ParsePosition& parse_pos) const; michael@0: michael@0: michael@0: /** michael@0: * Gets the date time interval patterns. michael@0: * @return the date time interval patterns associated with michael@0: * this date interval formatter. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: const DateIntervalInfo* getDateIntervalInfo(void) const; michael@0: michael@0: michael@0: /** michael@0: * Set the date time interval patterns. michael@0: * @param newIntervalPatterns the given interval patterns to copy. michael@0: * @param status output param set to success/failure code on exit michael@0: * @stable ICU 4.0 michael@0: */ michael@0: void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns, michael@0: UErrorCode& status); michael@0: michael@0: michael@0: /** michael@0: * Gets the date formatter michael@0: * @return the date formatter associated with this date interval formatter. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: const DateFormat* getDateFormat(void) const; michael@0: michael@0: /** michael@0: * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar. michael@0: * @return the time zone associated with the calendar of DateIntervalFormat. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual const TimeZone& getTimeZone(void) const; michael@0: michael@0: /** michael@0: * Sets the time zone for the calendar used by this DateIntervalFormat object. The michael@0: * caller no longer owns the TimeZone object and should not delete it after this call. michael@0: * @param zoneToAdopt the TimeZone to be adopted. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual void adoptTimeZone(TimeZone* zoneToAdopt); michael@0: michael@0: /** michael@0: * Sets the time zone for the calendar used by this DateIntervalFormat object. michael@0: * @param zone the new time zone. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual void setTimeZone(const TimeZone& zone); michael@0: michael@0: /** michael@0: * Return the class ID for this class. This is useful only for comparing to michael@0: * a return value from getDynamicClassID(). For example: michael@0: *

michael@0:      * .   Base* polymorphic_pointer = createPolymorphicObject();
michael@0:      * .   if (polymorphic_pointer->getDynamicClassID() ==
michael@0:      * .       erived::getStaticClassID()) ...
michael@0:      * 
michael@0: * @return The class ID for all objects of this class. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(void); michael@0: michael@0: /** michael@0: * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This michael@0: * method is to implement a simple version of RTTI, since not all C++ michael@0: * compilers support genuine RTTI. Polymorphic operator==() and clone() michael@0: * methods call this method. michael@0: * michael@0: * @return The class ID for this object. All objects of a michael@0: * given class have the same class ID. Objects of michael@0: * other classes have different class IDs. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: virtual UClassID getDynamicClassID(void) const; michael@0: michael@0: protected: michael@0: michael@0: /** michael@0: * Copy constructor. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: DateIntervalFormat(const DateIntervalFormat&); michael@0: michael@0: /** michael@0: * Assignment operator. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: DateIntervalFormat& operator=(const DateIntervalFormat&); michael@0: michael@0: private: michael@0: michael@0: /* michael@0: * This is for ICU internal use only. Please do not use. michael@0: * Save the interval pattern information. michael@0: * Interval pattern consists of 2 single date patterns and the separator. michael@0: * For example, interval pattern "MMM d - MMM d, yyyy" consists michael@0: * a single date pattern "MMM d", another single date pattern "MMM d, yyyy", michael@0: * and a separator "-". michael@0: * The pattern is divided into 2 parts. For above example, michael@0: * the first part is "MMM d - ", and the second part is "MMM d, yyyy". michael@0: * Also, the first date appears in an interval pattern could be michael@0: * the earlier date or the later date. michael@0: * And such information is saved in the interval pattern as well. michael@0: */ michael@0: struct PatternInfo { michael@0: UnicodeString firstPart; michael@0: UnicodeString secondPart; michael@0: /** michael@0: * Whether the first date in interval pattern is later date or not. michael@0: * Fallback format set the default ordering. michael@0: * And for a particular interval pattern, the order can be michael@0: * overriden by prefixing the interval pattern with "latestFirst:" or michael@0: * "earliestFirst:" michael@0: * For example, given 2 date, Jan 10, 2007 to Feb 10, 2007. michael@0: * if the fallback format is "{0} - {1}", michael@0: * and the pattern is "d MMM - d MMM yyyy", the interval format is michael@0: * "10 Jan - 10 Feb, 2007". michael@0: * If the pattern is "latestFirst:d MMM - d MMM yyyy", michael@0: * the interval format is "10 Feb - 10 Jan, 2007" michael@0: */ michael@0: UBool laterDateFirst; michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * default constructor michael@0: * @internal ICU 4.0 michael@0: */ michael@0: DateIntervalFormat(); michael@0: michael@0: /** michael@0: * Construct a DateIntervalFormat from DateFormat, michael@0: * a DateIntervalInfo, and skeleton. michael@0: * DateFormat provides the timezone, calendar, michael@0: * full pattern, and date format symbols information. michael@0: * It should be a SimpleDateFormat object which michael@0: * has a pattern in it. michael@0: * the DateIntervalInfo provides the interval patterns. michael@0: * michael@0: * Note: the DateIntervalFormat takes ownership of both michael@0: * DateFormat and DateIntervalInfo objects. michael@0: * Caller should not delete them. michael@0: * michael@0: * @param locale the locale of this date interval formatter. michael@0: * @param dtItvInfo the DateIntervalInfo object to be adopted. michael@0: * @param skeleton the skeleton of the date formatter michael@0: * @param status output param set to success/failure code on exit michael@0: */ michael@0: DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo, michael@0: const UnicodeString* skeleton, UErrorCode& status); michael@0: michael@0: michael@0: /** michael@0: * Construct a DateIntervalFormat from DateFormat michael@0: * and a DateIntervalInfo. michael@0: * michael@0: * It is a wrapper of the constructor. michael@0: * michael@0: * @param locale the locale of this date interval formatter. michael@0: * @param dtitvinf the DateIntervalInfo object to be adopted. michael@0: * @param skeleton the skeleton of this formatter. michael@0: * @param status Output param set to success/failure code. michael@0: * @return a date time interval formatter which the caller owns. michael@0: */ michael@0: static DateIntervalFormat* U_EXPORT2 create(const Locale& locale, michael@0: DateIntervalInfo* dtitvinf, michael@0: const UnicodeString* skeleton, michael@0: UErrorCode& status); michael@0: michael@0: /** michael@0: * Create a simple date/time formatter from skeleton, given locale, michael@0: * and date time pattern generator. michael@0: * michael@0: * @param skeleton the skeleton on which date format based. michael@0: * @param locale the given locale. michael@0: * @param dtpng the date time pattern generator. michael@0: * @param status Output param to be set to success/failure code. michael@0: * If it is failure, the returned date formatter will michael@0: * be NULL. michael@0: * @return a simple date formatter which the caller owns. michael@0: */ michael@0: static SimpleDateFormat* U_EXPORT2 createSDFPatternInstance( michael@0: const UnicodeString& skeleton, michael@0: const Locale& locale, michael@0: DateTimePatternGenerator* dtpng, michael@0: UErrorCode& status); michael@0: michael@0: michael@0: /** michael@0: * Below are for generating interval patterns local to the formatter michael@0: */ michael@0: michael@0: michael@0: /** michael@0: * Format 2 Calendars using fall-back interval pattern michael@0: * michael@0: * The full pattern used in this fall-back format is the michael@0: * full pattern of the date formatter. michael@0: * michael@0: * @param fromCalendar calendar set to the from date in date interval michael@0: * to be formatted into date interval string michael@0: * @param toCalendar calendar set to the to date in date interval michael@0: * to be formatted into date interval string michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param pos On input: an alignment field, if desired. michael@0: * On output: the offsets of the alignment field. michael@0: * @param status output param set to success/failure code on exit michael@0: * @return Reference to 'appendTo' parameter. michael@0: */ michael@0: UnicodeString& fallbackFormat(Calendar& fromCalendar, michael@0: Calendar& toCalendar, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& pos, michael@0: UErrorCode& status) const; michael@0: michael@0: michael@0: michael@0: /** michael@0: * Initialize interval patterns locale to this formatter michael@0: * michael@0: * This code is a bit complicated since michael@0: * 1. the interval patterns saved in resource bundle files are interval michael@0: * patterns based on date or time only. michael@0: * It does not have interval patterns based on both date and time. michael@0: * Interval patterns on both date and time are algorithm generated. michael@0: * michael@0: * For example, it has interval patterns on skeleton "dMy" and "hm", michael@0: * but it does not have interval patterns on skeleton "dMyhm". michael@0: * michael@0: * The rule to generate interval patterns for both date and time skeleton are michael@0: * 1) when the year, month, or day differs, concatenate the two original michael@0: * expressions with a separator between, michael@0: * For example, interval pattern from "Jan 10, 2007 10:10 am" michael@0: * to "Jan 11, 2007 10:10am" is michael@0: * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am" michael@0: * michael@0: * 2) otherwise, present the date followed by the range expression michael@0: * for the time. michael@0: * For example, interval pattern from "Jan 10, 2007 10:10 am" michael@0: * to "Jan 10, 2007 11:10am" is michael@0: * "Jan 10, 2007 10:10 am - 11:10am" michael@0: * michael@0: * 2. even a pattern does not request a certain calendar field, michael@0: * the interval pattern needs to include such field if such fields are michael@0: * different between 2 dates. michael@0: * For example, a pattern/skeleton is "hm", but the interval pattern michael@0: * includes year, month, and date when year, month, and date differs. michael@0: * michael@0: * michael@0: * @param status output param set to success/failure code on exit michael@0: */ michael@0: void initializePattern(UErrorCode& status); michael@0: michael@0: michael@0: michael@0: /** michael@0: * Set fall back interval pattern given a calendar field, michael@0: * a skeleton, and a date time pattern generator. michael@0: * @param field the largest different calendar field michael@0: * @param skeleton a skeleton michael@0: * @param status output param set to success/failure code on exit michael@0: */ michael@0: void setFallbackPattern(UCalendarDateFields field, michael@0: const UnicodeString& skeleton, michael@0: UErrorCode& status); michael@0: michael@0: michael@0: michael@0: /** michael@0: * get separated date and time skeleton from a combined skeleton. michael@0: * michael@0: * The difference between date skeleton and normalizedDateSkeleton are: michael@0: * 1. both 'y' and 'd' are appeared only once in normalizeDateSkeleton michael@0: * 2. 'E' and 'EE' are normalized into 'EEE' michael@0: * 3. 'MM' is normalized into 'M' michael@0: * michael@0: ** the difference between time skeleton and normalizedTimeSkeleton are: michael@0: * 1. both 'H' and 'h' are normalized as 'h' in normalized time skeleton, michael@0: * 2. 'a' is omitted in normalized time skeleton. michael@0: * 3. there is only one appearance for 'h', 'm','v', 'z' in normalized time michael@0: * skeleton michael@0: * michael@0: * michael@0: * @param skeleton given combined skeleton. michael@0: * @param date Output parameter for date only skeleton. michael@0: * @param normalizedDate Output parameter for normalized date only michael@0: * michael@0: * @param time Output parameter for time only skeleton. michael@0: * @param normalizedTime Output parameter for normalized time only michael@0: * skeleton. michael@0: * michael@0: */ michael@0: static void U_EXPORT2 getDateTimeSkeleton(const UnicodeString& skeleton, michael@0: UnicodeString& date, michael@0: UnicodeString& normalizedDate, michael@0: UnicodeString& time, michael@0: UnicodeString& normalizedTime); michael@0: michael@0: michael@0: michael@0: /** michael@0: * Generate date or time interval pattern from resource, michael@0: * and set them into the interval pattern locale to this formatter. michael@0: * michael@0: * It needs to handle the following: michael@0: * 1. need to adjust field width. michael@0: * For example, the interval patterns saved in DateIntervalInfo michael@0: * includes "dMMMy", but not "dMMMMy". michael@0: * Need to get interval patterns for dMMMMy from dMMMy. michael@0: * Another example, the interval patterns saved in DateIntervalInfo michael@0: * includes "hmv", but not "hmz". michael@0: * Need to get interval patterns for "hmz' from 'hmv' michael@0: * michael@0: * 2. there might be no pattern for 'y' differ for skeleton "Md", michael@0: * in order to get interval patterns for 'y' differ, michael@0: * need to look for it from skeleton 'yMd' michael@0: * michael@0: * @param dateSkeleton normalized date skeleton michael@0: * @param timeSkeleton normalized time skeleton michael@0: * @return whether the resource is found for the skeleton. michael@0: * TRUE if interval pattern found for the skeleton, michael@0: * FALSE otherwise. michael@0: */ michael@0: UBool setSeparateDateTimePtn(const UnicodeString& dateSkeleton, michael@0: const UnicodeString& timeSkeleton); michael@0: michael@0: michael@0: michael@0: michael@0: /** michael@0: * Generate interval pattern from existing resource michael@0: * michael@0: * It not only save the interval patterns, michael@0: * but also return the extended skeleton and its best match skeleton. michael@0: * michael@0: * @param field largest different calendar field michael@0: * @param skeleton skeleton michael@0: * @param bestSkeleton the best match skeleton which has interval pattern michael@0: * defined in resource michael@0: * @param differenceInfo the difference between skeleton and best skeleton michael@0: * 0 means the best matched skeleton is the same as input skeleton michael@0: * 1 means the fields are the same, but field width are different michael@0: * 2 means the only difference between fields are v/z, michael@0: * -1 means there are other fields difference michael@0: * michael@0: * @param extendedSkeleton extended skeleton michael@0: * @param extendedBestSkeleton extended best match skeleton michael@0: * @return whether the interval pattern is found michael@0: * through extending skeleton or not. michael@0: * TRUE if interval pattern is found by michael@0: * extending skeleton, FALSE otherwise. michael@0: */ michael@0: UBool setIntervalPattern(UCalendarDateFields field, michael@0: const UnicodeString* skeleton, michael@0: const UnicodeString* bestSkeleton, michael@0: int8_t differenceInfo, michael@0: UnicodeString* extendedSkeleton = NULL, michael@0: UnicodeString* extendedBestSkeleton = NULL); michael@0: michael@0: /** michael@0: * Adjust field width in best match interval pattern to match michael@0: * the field width in input skeleton. michael@0: * michael@0: * TODO (xji) make a general solution michael@0: * The adjusting rule can be: michael@0: * 1. always adjust michael@0: * 2. never adjust michael@0: * 3. default adjust, which means adjust according to the following rules michael@0: * 3.1 always adjust string, such as MMM and MMMM michael@0: * 3.2 never adjust between string and numeric, such as MM and MMM michael@0: * 3.3 always adjust year michael@0: * 3.4 do not adjust 'd', 'h', or 'm' if h presents michael@0: * 3.5 do not adjust 'M' if it is numeric(?) michael@0: * michael@0: * Since date interval format is well-formed format, michael@0: * date and time skeletons are normalized previously, michael@0: * till this stage, the adjust here is only "adjust strings, such as MMM michael@0: * and MMMM, EEE and EEEE. michael@0: * michael@0: * @param inputSkeleton the input skeleton michael@0: * @param bestMatchSkeleton the best match skeleton michael@0: * @param bestMatchIntervalPattern the best match interval pattern michael@0: * @param differenceInfo the difference between 2 skeletons michael@0: * 1 means only field width differs michael@0: * 2 means v/z exchange michael@0: * @param adjustedIntervalPattern adjusted interval pattern michael@0: */ michael@0: static void U_EXPORT2 adjustFieldWidth( michael@0: const UnicodeString& inputSkeleton, michael@0: const UnicodeString& bestMatchSkeleton, michael@0: const UnicodeString& bestMatchIntervalPattern, michael@0: int8_t differenceInfo, michael@0: UnicodeString& adjustedIntervalPattern); michael@0: michael@0: /** michael@0: * Concat a single date pattern with a time interval pattern, michael@0: * set it into the intervalPatterns, while field is time field. michael@0: * This is used to handle time interval patterns on skeleton with michael@0: * both time and date. Present the date followed by michael@0: * the range expression for the time. michael@0: * @param format date and time format michael@0: * @param formatLen format string length michael@0: * @param datePattern date pattern michael@0: * @param field time calendar field: AM_PM, HOUR, MINUTE michael@0: * @param status output param set to success/failure code on exit michael@0: */ michael@0: void concatSingleDate2TimeInterval(const UChar* format, michael@0: int32_t formatLen, michael@0: const UnicodeString& datePattern, michael@0: UCalendarDateFields field, michael@0: UErrorCode& status); michael@0: michael@0: /** michael@0: * check whether a calendar field present in a skeleton. michael@0: * @param field calendar field need to check michael@0: * @param skeleton given skeleton on which to check the calendar field michael@0: * @return true if field present in a skeleton. michael@0: */ michael@0: static UBool U_EXPORT2 fieldExistsInSkeleton(UCalendarDateFields field, michael@0: const UnicodeString& skeleton); michael@0: michael@0: michael@0: /** michael@0: * Split interval patterns into 2 part. michael@0: * @param intervalPattern interval pattern michael@0: * @return the index in interval pattern which split the pattern into 2 part michael@0: */ michael@0: static int32_t U_EXPORT2 splitPatternInto2Part(const UnicodeString& intervalPattern); michael@0: michael@0: michael@0: /** michael@0: * Break interval patterns as 2 part and save them into pattern info. michael@0: * @param field calendar field michael@0: * @param intervalPattern interval pattern michael@0: */ michael@0: void setIntervalPattern(UCalendarDateFields field, michael@0: const UnicodeString& intervalPattern); michael@0: michael@0: michael@0: /** michael@0: * Break interval patterns as 2 part and save them into pattern info. michael@0: * @param field calendar field michael@0: * @param intervalPattern interval pattern michael@0: * @param laterDateFirst whether later date appear first in interval pattern michael@0: */ michael@0: void setIntervalPattern(UCalendarDateFields field, michael@0: const UnicodeString& intervalPattern, michael@0: UBool laterDateFirst); michael@0: michael@0: michael@0: /** michael@0: * Set pattern information. michael@0: * michael@0: * @param field calendar field michael@0: * @param firstPart the first part in interval pattern michael@0: * @param secondPart the second part in interval pattern michael@0: * @param laterDateFirst whether the first date in intervalPattern michael@0: * is earlier date or later date michael@0: */ michael@0: void setPatternInfo(UCalendarDateFields field, michael@0: const UnicodeString* firstPart, michael@0: const UnicodeString* secondPart, michael@0: UBool laterDateFirst); michael@0: michael@0: michael@0: // from calendar field to pattern letter michael@0: static const UChar fgCalendarFieldToPatternLetter[]; michael@0: michael@0: michael@0: /** michael@0: * The interval patterns for this locale. michael@0: */ michael@0: DateIntervalInfo* fInfo; michael@0: michael@0: /** michael@0: * The DateFormat object used to format single pattern michael@0: */ michael@0: SimpleDateFormat* fDateFormat; michael@0: michael@0: /** michael@0: * The 2 calendars with the from and to date. michael@0: * could re-use the calendar in fDateFormat, michael@0: * but keeping 2 calendars make it clear and clean. michael@0: */ michael@0: Calendar* fFromCalendar; michael@0: Calendar* fToCalendar; michael@0: michael@0: /** michael@0: * Date time pattern generator michael@0: */ michael@0: DateTimePatternGenerator* fDtpng; michael@0: michael@0: /** michael@0: * Following are interval information relavent (locale) to this formatter. michael@0: */ michael@0: UnicodeString fSkeleton; michael@0: PatternInfo fIntervalPatterns[DateIntervalInfo::kIPI_MAX_INDEX]; michael@0: }; michael@0: michael@0: inline UBool michael@0: DateIntervalFormat::operator!=(const Format& other) const { michael@0: return !operator==(other); michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif // _DTITVFMT_H__ michael@0: //eof