michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2008-2011,2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * michael@0: * File DTITVINF.H michael@0: * michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #ifndef __DTITVINF_H__ michael@0: #define __DTITVINF_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Date/Time interval patterns for formatting date/time interval michael@0: */ michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/udat.h" michael@0: #include "unicode/locid.h" michael@0: #include "unicode/ucal.h" michael@0: #include "unicode/dtptngen.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /** michael@0: * DateIntervalInfo is a public class for encapsulating localizable michael@0: * date time interval patterns. It is used by DateIntervalFormat. michael@0: * michael@0: *

michael@0: * For most users, ordinary use of DateIntervalFormat does not need to create michael@0: * DateIntervalInfo object directly. michael@0: * DateIntervalFormat will take care of it when creating a date interval michael@0: * formatter when user pass in skeleton and locale. michael@0: * michael@0: *

michael@0: * For power users, who want to create their own date interval patterns, michael@0: * or want to re-set date interval patterns, they could do so by michael@0: * directly creating DateIntervalInfo and manupulating it. 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: * hides the order of fields. michael@0: *
  3. michael@0: * might hide a field's pattern letter length. 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: *

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 > am-pm > hour > 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: * 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: *

michael@0: * There are 2 dates in interval pattern. For most locales, the first date michael@0: * in an interval pattern is the earlier date. There might be a locale in which michael@0: * the first date in an interval pattern is the later date. michael@0: * We use fallback format for the default order for the locale. michael@0: * For example, if the fallback format is "{0} - {1}", it means michael@0: * the first date in the interval pattern for this locale is earlier date. michael@0: * If the fallback format is "{1} - {0}", it means the first date is the michael@0: * later date. michael@0: * For a particular interval pattern, the default order can be overriden michael@0: * by prefixing "latestFirst:" or "earliestFirst:" to the interval pattern. michael@0: * For example, if the fallback format is "{0}-{1}", michael@0: * but for skeleton "yMMMd", the interval pattern when day is different is michael@0: * "latestFirst:d-d MMM yy", it means by default, the first date in interval michael@0: * pattern is the earlier date. But for skeleton "yMMMd", when day is different, michael@0: * the first date in "d-d MMM yy" is the later date. michael@0: * michael@0: *

michael@0: * The recommended way to create a DateIntervalFormat object is to pass in michael@0: * the locale. michael@0: * By using a Locale parameter, the DateIntervalFormat object is michael@0: * initialized with the pre-defined interval patterns for a given or michael@0: * default locale. michael@0: *

michael@0: * Users can also create DateIntervalFormat object michael@0: * by supplying their own interval patterns. michael@0: * It provides flexibility for power users. michael@0: * michael@0: *

michael@0: * After a DateIntervalInfo object is created, clients may modify michael@0: * the interval patterns using setIntervalPattern function as so desired. michael@0: * Currently, users can only set interval patterns when the following michael@0: * calendar fields are different: ERA, YEAR, MONTH, DATE, DAY_OF_MONTH, michael@0: * DAY_OF_WEEK, AM_PM, HOUR, HOUR_OF_DAY, and MINUTE. michael@0: * Interval patterns when other calendar fields are different is not supported. michael@0: *

michael@0: * DateIntervalInfo objects are cloneable. michael@0: * When clients obtain a DateIntervalInfo object, michael@0: * they can feel free to modify it as necessary. michael@0: *

michael@0: * DateIntervalInfo are not expected to be subclassed. michael@0: * Data for a calendar is loaded out of resource bundles. michael@0: * Through ICU 4.4, date interval patterns are only supported in the Gregorian michael@0: * calendar; non-Gregorian calendars are supported from ICU 4.4.1. michael@0: * @stable ICU 4.0 michael@0: **/ michael@0: michael@0: class U_I18N_API DateIntervalInfo : public UObject { michael@0: public: michael@0: #ifndef U_HIDE_INTERNAL_API michael@0: /** michael@0: * Default constructor. michael@0: * It does not initialize any interval patterns except michael@0: * that it initialize default fall-back pattern as "{0} - {1}", michael@0: * which can be reset by setFallbackIntervalPattern(). michael@0: * It should be followed by setFallbackIntervalPattern() and michael@0: * setIntervalPattern(), michael@0: * and is recommended to be used only for power users who michael@0: * wants to create their own interval patterns and use them to create michael@0: * date interval formatter. michael@0: * @param status output param set to success/failure code on exit michael@0: * @internal ICU 4.0 michael@0: */ michael@0: DateIntervalInfo(UErrorCode& status); michael@0: #endif /* U_HIDE_INTERNAL_API */ michael@0: michael@0: michael@0: /** michael@0: * Construct DateIntervalInfo for the given locale, michael@0: * @param locale the interval patterns are loaded from the appropriate calendar michael@0: * data (specified calendar or default calendar) in this locale. michael@0: * @param status output param set to success/failure code on exit michael@0: * @stable ICU 4.0 michael@0: */ michael@0: DateIntervalInfo(const Locale& locale, UErrorCode& status); michael@0: michael@0: michael@0: /** michael@0: * Copy constructor. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: DateIntervalInfo(const DateIntervalInfo&); michael@0: michael@0: /** michael@0: * Assignment operator michael@0: * @stable ICU 4.0 michael@0: */ michael@0: DateIntervalInfo& operator=(const DateIntervalInfo&); michael@0: michael@0: /** michael@0: * Clone this object polymorphically. michael@0: * The caller owns the result and should delete it when done. michael@0: * @return a copy of the object michael@0: * @stable ICU 4.0 michael@0: */ michael@0: virtual DateIntervalInfo* clone(void) const; michael@0: michael@0: /** michael@0: * Destructor. michael@0: * It is virtual to be safe, but it is not designed to be subclassed. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: virtual ~DateIntervalInfo(); michael@0: michael@0: michael@0: /** michael@0: * Return true if another object is semantically equal to this one. michael@0: * michael@0: * @param other the DateIntervalInfo object to be compared with. michael@0: * @return true if other is semantically equal to this. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: virtual UBool operator==(const DateIntervalInfo& other) const; michael@0: michael@0: /** michael@0: * Return true if another object is semantically unequal to this one. michael@0: * michael@0: * @param other the DateIntervalInfo object to be compared with. michael@0: * @return true if other is semantically unequal to this. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: UBool operator!=(const DateIntervalInfo& other) const; michael@0: michael@0: michael@0: michael@0: /** michael@0: * Provides a way for client to build interval patterns. michael@0: * User could construct DateIntervalInfo by providing a list of skeletons michael@0: * and their patterns. michael@0: *

michael@0: * For example: michael@0: *

michael@0:      * UErrorCode status = U_ZERO_ERROR;
michael@0:      * DateIntervalInfo dIntervalInfo = new DateIntervalInfo();
michael@0:      * dIntervalInfo->setFallbackIntervalPattern("{0} ~ {1}");
michael@0:      * dIntervalInfo->setIntervalPattern("yMd", UCAL_YEAR, "'from' yyyy-M-d 'to' yyyy-M-d", status); 
michael@0:      * dIntervalInfo->setIntervalPattern("yMMMd", UCAL_MONTH, "'from' yyyy MMM d 'to' MMM d", status);
michael@0:      * dIntervalInfo->setIntervalPattern("yMMMd", UCAL_DAY, "yyyy MMM d-d", status, status);
michael@0:      * 
michael@0: * michael@0: * Restriction: michael@0: * Currently, users can only set interval patterns when the following michael@0: * calendar fields are different: ERA, YEAR, MONTH, DATE, DAY_OF_MONTH, michael@0: * DAY_OF_WEEK, AM_PM, HOUR, HOUR_OF_DAY, and MINUTE. michael@0: * Interval patterns when other calendar fields are different are michael@0: * not supported. michael@0: * michael@0: * @param skeleton the skeleton on which interval pattern based michael@0: * @param lrgDiffCalUnit the largest different calendar unit. michael@0: * @param intervalPattern the interval pattern on the largest different michael@0: * calendar unit. michael@0: * For example, if lrgDiffCalUnit is michael@0: * "year", the interval pattern for en_US when year michael@0: * is different could be "'from' yyyy 'to' yyyy". michael@0: * @param status output param set to success/failure code on exit michael@0: * @stable ICU 4.0 michael@0: */ michael@0: void setIntervalPattern(const UnicodeString& skeleton, michael@0: UCalendarDateFields lrgDiffCalUnit, michael@0: const UnicodeString& intervalPattern, michael@0: UErrorCode& status); michael@0: michael@0: /** michael@0: * Get the interval pattern given skeleton and michael@0: * the largest different calendar field. michael@0: * @param skeleton the skeleton michael@0: * @param field the largest different calendar field michael@0: * @param result output param to receive the pattern michael@0: * @param status output param set to success/failure code on exit michael@0: * @return a reference to 'result' michael@0: * @stable ICU 4.0 michael@0: */ michael@0: UnicodeString& getIntervalPattern(const UnicodeString& skeleton, michael@0: UCalendarDateFields field, michael@0: UnicodeString& result, michael@0: UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Get the fallback interval pattern. michael@0: * @param result output param to receive the pattern michael@0: * @return a reference to 'result' michael@0: * @stable ICU 4.0 michael@0: */ michael@0: UnicodeString& getFallbackIntervalPattern(UnicodeString& result) const; michael@0: michael@0: michael@0: /** michael@0: * Re-set the fallback interval pattern. michael@0: * michael@0: * In construction, default fallback pattern is set as "{0} - {1}". michael@0: * And constructor taking locale as parameter will set the michael@0: * fallback pattern as what defined in the locale resource file. michael@0: * michael@0: * This method provides a way for user to replace the fallback pattern. michael@0: * michael@0: * @param fallbackPattern fall-back interval pattern. michael@0: * @param status output param set to success/failure code on exit michael@0: * @stable ICU 4.0 michael@0: */ michael@0: void setFallbackIntervalPattern(const UnicodeString& fallbackPattern, michael@0: UErrorCode& status); michael@0: michael@0: michael@0: /** Get default order -- whether the first date in pattern is later date michael@0: or not. michael@0: * return default date ordering in interval pattern. TRUE if the first date michael@0: * in pattern is later date, FALSE otherwise. michael@0: * @stable ICU 4.0 michael@0: */ michael@0: UBool getDefaultOrder() const; michael@0: michael@0: michael@0: /** michael@0: * ICU "poor man's RTTI", returns a UClassID for the actual class. michael@0: * michael@0: * @stable ICU 4.0 michael@0: */ michael@0: virtual UClassID getDynamicClassID() const; michael@0: michael@0: /** michael@0: * ICU "poor man's RTTI", returns a UClassID for this class. michael@0: * michael@0: * @stable ICU 4.0 michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(); michael@0: michael@0: michael@0: private: michael@0: /** michael@0: * DateIntervalFormat will need access to michael@0: * getBestSkeleton(), parseSkeleton(), enum IntervalPatternIndex, michael@0: * and calendarFieldToPatternIndex(). michael@0: * michael@0: * Instead of making above public, michael@0: * make DateIntervalFormat a friend of DateIntervalInfo. michael@0: */ michael@0: friend class DateIntervalFormat; michael@0: michael@0: /** michael@0: * Following is for saving the interval patterns. michael@0: * We only support interval patterns on michael@0: * ERA, YEAR, MONTH, DAY, AM_PM, HOUR, and MINUTE michael@0: */ michael@0: enum IntervalPatternIndex michael@0: { michael@0: kIPI_ERA, michael@0: kIPI_YEAR, michael@0: kIPI_MONTH, michael@0: kIPI_DATE, michael@0: kIPI_AM_PM, michael@0: kIPI_HOUR, michael@0: kIPI_MINUTE, michael@0: kIPI_MAX_INDEX michael@0: }; michael@0: public: michael@0: #ifndef U_HIDE_INTERNAL_API michael@0: /** michael@0: * Max index for stored interval patterns michael@0: * @internal ICU 4.4 michael@0: */ michael@0: enum { michael@0: kMaxIntervalPatternIndex = kIPI_MAX_INDEX michael@0: }; michael@0: #endif /* U_HIDE_INTERNAL_API */ michael@0: private: michael@0: michael@0: michael@0: /** michael@0: * Initialize the DateIntervalInfo from locale michael@0: * @param locale the given locale. michael@0: * @param status output param set to success/failure code on exit michael@0: */ michael@0: void initializeData(const Locale& locale, UErrorCode& status); michael@0: michael@0: michael@0: /* Set Interval pattern. michael@0: * michael@0: * It sets interval pattern into the hash map. michael@0: * michael@0: * @param skeleton skeleton on which the interval pattern based michael@0: * @param lrgDiffCalUnit the largest different calendar unit. michael@0: * @param intervalPattern the interval pattern on the largest different michael@0: * calendar unit. michael@0: * @param status output param set to success/failure code on exit michael@0: */ michael@0: void setIntervalPatternInternally(const UnicodeString& skeleton, michael@0: UCalendarDateFields lrgDiffCalUnit, michael@0: const UnicodeString& intervalPattern, michael@0: UErrorCode& status); michael@0: michael@0: michael@0: /**given an input skeleton, get the best match skeleton michael@0: * which has pre-defined interval pattern in resource file. michael@0: * Also return the difference between the input skeleton michael@0: * and the best match skeleton. michael@0: * michael@0: * TODO (xji): set field weight or michael@0: * isolate the funtionality in DateTimePatternGenerator michael@0: * @param skeleton input skeleton michael@0: * @param bestMatchDistanceInfo the difference between input skeleton michael@0: * and best match skeleton. michael@0: * 0, if there is exact match for input skeleton michael@0: * 1, if there is only field width difference between michael@0: * the best match and the input skeleton michael@0: * 2, the only field difference is 'v' and 'z' michael@0: * -1, if there is calendar field difference between michael@0: * the best match and the input skeleton michael@0: * @return best match skeleton michael@0: */ michael@0: const UnicodeString* getBestSkeleton(const UnicodeString& skeleton, michael@0: int8_t& bestMatchDistanceInfo) const; michael@0: michael@0: michael@0: /** michael@0: * Parse skeleton, save each field's width. michael@0: * It is used for looking for best match skeleton, michael@0: * and adjust pattern field width. michael@0: * @param skeleton skeleton to be parsed michael@0: * @param skeletonFieldWidth parsed skeleton field width michael@0: */ michael@0: static void U_EXPORT2 parseSkeleton(const UnicodeString& skeleton, michael@0: int32_t* skeletonFieldWidth); michael@0: michael@0: michael@0: /** michael@0: * Check whether one field width is numeric while the other is string. michael@0: * michael@0: * TODO (xji): make it general michael@0: * michael@0: * @param fieldWidth one field width michael@0: * @param anotherFieldWidth another field width michael@0: * @param patternLetter pattern letter char michael@0: * @return true if one field width is numeric and the other is string, michael@0: * false otherwise. michael@0: */ michael@0: static UBool U_EXPORT2 stringNumeric(int32_t fieldWidth, michael@0: int32_t anotherFieldWidth, michael@0: char patternLetter); michael@0: michael@0: michael@0: /** michael@0: * Convert calendar field to the interval pattern index in michael@0: * hash table. michael@0: * michael@0: * Since we only support the following calendar fields: michael@0: * ERA, YEAR, MONTH, DATE, DAY_OF_MONTH, DAY_OF_WEEK, michael@0: * AM_PM, HOUR, HOUR_OF_DAY, and MINUTE, michael@0: * We reserve only 4 interval patterns for a skeleton. michael@0: * michael@0: * @param field calendar field michael@0: * @param status output param set to success/failure code on exit michael@0: * @return interval pattern index in hash table michael@0: */ michael@0: static IntervalPatternIndex U_EXPORT2 calendarFieldToIntervalIndex( michael@0: UCalendarDateFields field, michael@0: UErrorCode& status); michael@0: michael@0: michael@0: /** michael@0: * delete hash table (of type fIntervalPatterns). michael@0: * michael@0: * @param hTable hash table to be deleted michael@0: */ michael@0: void deleteHash(Hashtable* hTable); michael@0: michael@0: michael@0: /** michael@0: * initialize hash table (of type fIntervalPatterns). michael@0: * michael@0: * @param status output param set to success/failure code on exit michael@0: * @return hash table initialized michael@0: */ michael@0: Hashtable* initHash(UErrorCode& status); michael@0: michael@0: michael@0: michael@0: /** michael@0: * copy hash table (of type fIntervalPatterns). michael@0: * michael@0: * @param source the source to copy from michael@0: * @param target the target to copy to michael@0: * @param status output param set to success/failure code on exit michael@0: */ michael@0: void copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status); michael@0: michael@0: michael@0: // data members michael@0: // fallback interval pattern michael@0: UnicodeString fFallbackIntervalPattern; michael@0: // default order michael@0: UBool fFirstDateInPtnIsLaterDate; michael@0: michael@0: // HashMap michael@0: // HashMap( skeleton, pattern[largest_different_field] ) michael@0: Hashtable* fIntervalPatterns; michael@0: michael@0: };// end class DateIntervalInfo michael@0: michael@0: michael@0: inline UBool michael@0: DateIntervalInfo::operator!=(const DateIntervalInfo& other) const { michael@0: return !operator==(other); michael@0: } michael@0: michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif michael@0: michael@0: #endif michael@0: