40 * For most users, ordinary use of DateIntervalFormat does not need to create 41 * DateIntervalInfo object directly. 42 * DateIntervalFormat will take care of it when creating a date interval 43 * formatter when user pass in skeleton and locale. 44 * 45 *
46 * For power users, who want to create their own date interval patterns, 47 * or want to re-set date interval patterns, they could do so by 48 * directly creating DateIntervalInfo and manipulating it. 49 * 50 *
51 * Logically, the interval patterns are mappings 52 * from (skeleton, the_largest_different_calendar_field) 53 * to (date_interval_pattern). 54 * 55 *
56 * A skeleton 57 *
77 * The calendar fields we support for interval formatting are: 78 * year, month, date, day-of-week, am-pm, hour, hour-of-day, and minute. 79 * Those calendar fields can be defined in the following order: 80 * year > month > date > am-pm > hour > minute 81 * 82 * The largest different calendar fields between 2 calendars is the 83 * first different calendar field in above order. 84 * 85 * For example: the largest different calendar fields between "Jan 10, 2007" 86 * and "Feb 20, 2008" is year. 87 * 88 *
89 * There is a set of pre-defined static skeleton strings. 90 * There are pre-defined interval patterns for those pre-defined skeletons 91 * in locales' resource files. 92 * For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is "yMMMd", 93 * in en_US, if the largest different calendar field between date1 and date2 94 * is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy", 95 * such as "Jan 10, 2007 - Jan 10, 2008". 96 * If the largest different calendar field between date1 and date2 is "month", 97 * the date interval pattern is "MMM d - MMM d, yyyy", 98 * such as "Jan 10 - Feb 10, 2007". 99 * If the largest different calendar field between date1 and date2 is "day", 100 * the date interval pattern is "MMM d-d, yyyy", such as "Jan 10-20, 2007". 101 * 102 * For date skeleton, the interval patterns when year, or month, or date is 103 * different are defined in resource files. 104 * For time skeleton, the interval patterns when am/pm, or hour, or minute is 105 * different are defined in resource files. 106 * 107 * 108 *
109 * There are 2 dates in interval pattern. For most locales, the first date 110 * in an interval pattern is the earlier date. There might be a locale in which 111 * the first date in an interval pattern is the later date. 112 * We use fallback format for the default order for the locale. 113 * For example, if the fallback format is "{0} - {1}", it means 114 * the first date in the interval pattern for this locale is earlier date. 115 * If the fallback format is "{1} - {0}", it means the first date is the 116 * later date. 117 * For a particular interval pattern, the default order can be overridden 118 * by prefixing "latestFirst:" or "earliestFirst:" to the interval pattern. 119 * For example, if the fallback format is "{0}-{1}", 120 * but for skeleton "yMMMd", the interval pattern when day is different is 121 * "latestFirst:d-d MMM yy", it means by default, the first date in interval 122 * pattern is the earlier date. But for skeleton "yMMMd", when day is different, 123 * the first date in "d-d MMM yy" is the later date. 124 * 125 *
126 * The recommended way to create a DateIntervalFormat object is to pass in 127 * the locale. 128 * By using a Locale parameter, the DateIntervalFormat object is 129 * initialized with the pre-defined interval patterns for a given or 130 * default locale. 131 *
132 * Users can also create DateIntervalFormat object 133 * by supplying their own interval patterns. 134 * It provides flexibility for power users. 135 * 136 *
137 * After a DateIntervalInfo object is created, clients may modify 138 * the interval patterns using setIntervalPattern function as so desired. 139 * Currently, users can only set interval patterns when the following 140 * calendar fields are different: ERA, YEAR, MONTH, DATE, DAY_OF_MONTH, 141 * DAY_OF_WEEK, AM_PM, HOUR, HOUR_OF_DAY, MINUTE, SECOND, and MILLISECOND. 142 * Interval patterns when other calendar fields are different is not supported. 143 *
144 * DateIntervalInfo objects are cloneable. 145 * When clients obtain a DateIntervalInfo object, 146 * they can feel free to modify it as necessary. 147 *
148 * DateIntervalInfo are not expected to be subclassed. 149 * Data for a calendar is loaded out of resource bundles. 150 * Through ICU 4.4, date interval patterns are only supported in the Gregorian 151 * calendar; non-Gregorian calendars are supported from ICU 4.4.1. 152 * @stable ICU 4.0 153 **/ 154 class U_I18N_API DateIntervalInfo final : public UObject { 155 public: 156 /** 157 * Default constructor. 158 * It does not initialize any interval patterns except 159 * that it initialize default fall-back pattern as "{0} - {1}", 160 * which can be reset by setFallbackIntervalPattern(). 161 * It should be followed by setFallbackIntervalPattern() and 162 * setIntervalPattern(), 163 * and is recommended to be used only for power users who 164 * wants to create their own interval patterns and use them to create 165 * date interval formatter. 166 * @param status output param set to success/failure code on exit 167 * @internal ICU 4.0 168 */ 169 DateIntervalInfo(UErrorCode& status); 170 171 172 /** 173 * Construct DateIntervalInfo for the given locale, 174 * @param locale the interval patterns are loaded from the appropriate calendar 175 * data (specified calendar or default calendar) in this locale. 176 * @param status output param set to success/failure code on exit 177 * @stable ICU 4.0 178 */ 179 DateIntervalInfo(const Locale& locale, UErrorCode& status); 180 181 182 /** 183 * Copy constructor. 184 * @stable ICU 4.0 185 */ 186 DateIntervalInfo(const DateIntervalInfo&); 187 188 /** 189 * Assignment operator 190 * @stable ICU 4.0 191 */ 192 DateIntervalInfo& operator=(const DateIntervalInfo&); 193 194 /** 195 * Clone this object polymorphically. 196 * The caller owns the result and should delete it when done. 197 * @return a copy of the object 198 * @stable ICU 4.0 199 */ 200 virtual DateIntervalInfo* clone() const; 201 202 /** 203 * Destructor. 204 * It is virtual to be safe, but it is not designed to be subclassed. 205 * @stable ICU 4.0 206 */ 207 virtual ~DateIntervalInfo(); 208 209 210 /** 211 * Return true if another object is semantically equal to this one. 212 * 213 * @param other the DateIntervalInfo object to be compared with. 214 * @return true if other is semantically equal to this. 215 * @stable ICU 4.0 216 */ 217 virtual bool operator==(const DateIntervalInfo& other) const; 218 219 /** 220 * Return true if another object is semantically unequal to this one. 221 * 222 * @param other the DateIntervalInfo object to be compared with. 223 * @return true if other is semantically unequal to this. 224 * @stable ICU 4.0 225 */ 226 bool operator!=(const DateIntervalInfo& other) const; 227 228 229 230 /** 231 * Provides a way for client to build interval patterns. 232 * User could construct DateIntervalInfo by providing a list of skeletons 233 * and their patterns. 234 *
235 * For example: 236 *
237 * UErrorCode status = U_ZERO_ERROR; 238 * DateIntervalInfo dIntervalInfo = new DateIntervalInfo(); 239 * dIntervalInfo->setFallbackIntervalPattern("{0} ~ {1}"); 240 * dIntervalInfo->setIntervalPattern("yMd", UCAL_YEAR, "'from' yyyy-M-d 'to' yyyy-M-d", status); 241 * dIntervalInfo->setIntervalPattern("yMMMd", UCAL_MONTH, "'from' yyyy MMM d 'to' MMM d", status); 242 * dIntervalInfo->setIntervalPattern("yMMMd", UCAL_DAY, "yyyy MMM d-d", status, status); 243 *