OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ***************************************************************************** |
| 3 * Copyright (C) 2014, International Business Machines Corporation and |
| 4 * others. |
| 5 * All Rights Reserved. |
| 6 ***************************************************************************** |
| 7 * |
| 8 * File RELDATEFMT.H |
| 9 ***************************************************************************** |
| 10 */ |
| 11 |
| 12 #ifndef __RELDATEFMT_H |
| 13 #define __RELDATEFMT_H |
| 14 |
| 15 #include "unicode/utypes.h" |
| 16 #include "unicode/uobject.h" |
| 17 #include "unicode/udisplaycontext.h" |
| 18 #include "unicode/locid.h" |
| 19 |
| 20 /** |
| 21 * \file |
| 22 * \brief C++ API: Formats relative dates such as "1 day ago" or "tomorrow" |
| 23 */ |
| 24 |
| 25 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION |
| 26 |
| 27 #ifndef U_HIDE_DRAFT_API |
| 28 |
| 29 /** |
| 30 * The formatting style |
| 31 * @draft ICU 54 |
| 32 */ |
| 33 typedef enum UDateRelativeDateTimeFormatterStyle { |
| 34 |
| 35 /** |
| 36 * Everything spelled out. |
| 37 * @draft ICU 54 |
| 38 */ |
| 39 UDAT_STYLE_LONG, |
| 40 |
| 41 /** |
| 42 * Abbreviations used when possible. |
| 43 * @draft ICU 54 |
| 44 */ |
| 45 UDAT_STYLE_SHORT, |
| 46 |
| 47 /** |
| 48 * Use the shortest possible form. |
| 49 * @draft ICU 54 |
| 50 */ |
| 51 UDAT_STYLE_NARROW, |
| 52 |
| 53 /** |
| 54 * The number of styles. |
| 55 * @draft ICU 54 |
| 56 */ |
| 57 UDAT_STYLE_COUNT |
| 58 } UDateRelativeDateTimeFormatterStyle; |
| 59 |
| 60 /** |
| 61 * Represents the unit for formatting a relative date. e.g "in 5 days" |
| 62 * or "in 3 months" |
| 63 * @draft ICU 53 |
| 64 */ |
| 65 typedef enum UDateRelativeUnit { |
| 66 |
| 67 /** |
| 68 * Seconds |
| 69 * @draft ICU 53 |
| 70 */ |
| 71 UDAT_RELATIVE_SECONDS, |
| 72 |
| 73 /** |
| 74 * Minutes |
| 75 * @draft ICU 53 |
| 76 */ |
| 77 UDAT_RELATIVE_MINUTES, |
| 78 |
| 79 /** |
| 80 * Hours |
| 81 * @draft ICU 53 |
| 82 */ |
| 83 UDAT_RELATIVE_HOURS, |
| 84 |
| 85 /** |
| 86 * Days |
| 87 * @draft ICU 53 |
| 88 */ |
| 89 UDAT_RELATIVE_DAYS, |
| 90 |
| 91 /** |
| 92 * Weeks |
| 93 * @draft ICU 53 |
| 94 */ |
| 95 UDAT_RELATIVE_WEEKS, |
| 96 |
| 97 /** |
| 98 * Months |
| 99 * @draft ICU 53 |
| 100 */ |
| 101 UDAT_RELATIVE_MONTHS, |
| 102 |
| 103 /** |
| 104 * Years |
| 105 * @draft ICU 53 |
| 106 */ |
| 107 UDAT_RELATIVE_YEARS, |
| 108 |
| 109 /** |
| 110 * Count of items in this enum. |
| 111 * @draft ICU 53 |
| 112 */ |
| 113 UDAT_RELATIVE_UNIT_COUNT |
| 114 } UDateRelativeUnit; |
| 115 |
| 116 /** |
| 117 * Represents an absolute unit. |
| 118 * @draft ICU 53 |
| 119 */ |
| 120 typedef enum UDateAbsoluteUnit { |
| 121 |
| 122 // Days of week have to remain together and in order from Sunday to |
| 123 // Saturday. |
| 124 /** |
| 125 * Sunday |
| 126 * @draft ICU 53 |
| 127 */ |
| 128 UDAT_ABSOLUTE_SUNDAY, |
| 129 |
| 130 /** |
| 131 * Monday |
| 132 * @draft ICU 53 |
| 133 */ |
| 134 UDAT_ABSOLUTE_MONDAY, |
| 135 |
| 136 /** |
| 137 * Tuesday |
| 138 * @draft ICU 53 |
| 139 */ |
| 140 UDAT_ABSOLUTE_TUESDAY, |
| 141 |
| 142 /** |
| 143 * Wednesday |
| 144 * @draft ICU 53 |
| 145 */ |
| 146 UDAT_ABSOLUTE_WEDNESDAY, |
| 147 |
| 148 /** |
| 149 * Thursday |
| 150 * @draft ICU 53 |
| 151 */ |
| 152 UDAT_ABSOLUTE_THURSDAY, |
| 153 |
| 154 /** |
| 155 * Friday |
| 156 * @draft ICU 53 |
| 157 */ |
| 158 UDAT_ABSOLUTE_FRIDAY, |
| 159 |
| 160 /** |
| 161 * Saturday |
| 162 * @draft ICU 53 |
| 163 */ |
| 164 UDAT_ABSOLUTE_SATURDAY, |
| 165 |
| 166 /** |
| 167 * Day |
| 168 * @draft ICU 53 |
| 169 */ |
| 170 UDAT_ABSOLUTE_DAY, |
| 171 |
| 172 /** |
| 173 * Week |
| 174 * @draft ICU 53 |
| 175 */ |
| 176 UDAT_ABSOLUTE_WEEK, |
| 177 |
| 178 /** |
| 179 * Month |
| 180 * @draft ICU 53 |
| 181 */ |
| 182 UDAT_ABSOLUTE_MONTH, |
| 183 |
| 184 /** |
| 185 * Year |
| 186 * @draft ICU 53 |
| 187 */ |
| 188 UDAT_ABSOLUTE_YEAR, |
| 189 |
| 190 /** |
| 191 * Now |
| 192 * @draft ICU 53 |
| 193 */ |
| 194 UDAT_ABSOLUTE_NOW, |
| 195 |
| 196 /** |
| 197 * Count of items in this enum. |
| 198 * @draft ICU 53 |
| 199 */ |
| 200 UDAT_ABSOLUTE_UNIT_COUNT |
| 201 } UDateAbsoluteUnit; |
| 202 |
| 203 /** |
| 204 * Represents a direction for an absolute unit e.g "Next Tuesday" |
| 205 * or "Last Tuesday" |
| 206 * @draft ICU 53 |
| 207 */ |
| 208 typedef enum UDateDirection { |
| 209 |
| 210 /** |
| 211 * Two before. Not fully supported in every locale. |
| 212 * @draft ICU 53 |
| 213 */ |
| 214 UDAT_DIRECTION_LAST_2, |
| 215 |
| 216 /** |
| 217 * Last |
| 218 * @draft ICU 53 |
| 219 */ |
| 220 UDAT_DIRECTION_LAST, |
| 221 |
| 222 /** |
| 223 * This |
| 224 * @draft ICU 53 |
| 225 */ |
| 226 UDAT_DIRECTION_THIS, |
| 227 |
| 228 /** |
| 229 * Next |
| 230 * @draft ICU 53 |
| 231 */ |
| 232 UDAT_DIRECTION_NEXT, |
| 233 |
| 234 /** |
| 235 * Two after. Not fully supported in every locale. |
| 236 * @draft ICU 53 |
| 237 */ |
| 238 UDAT_DIRECTION_NEXT_2, |
| 239 |
| 240 /** |
| 241 * Plain, which means the absence of a qualifier. |
| 242 * @draft ICU 53 |
| 243 */ |
| 244 UDAT_DIRECTION_PLAIN, |
| 245 |
| 246 /** |
| 247 * Count of items in this enum. |
| 248 * @draft ICU 53 |
| 249 */ |
| 250 UDAT_DIRECTION_COUNT |
| 251 } UDateDirection; |
| 252 |
| 253 |
| 254 U_NAMESPACE_BEGIN |
| 255 |
| 256 class RelativeDateTimeCacheData; |
| 257 class SharedNumberFormat; |
| 258 class SharedPluralRules; |
| 259 class SharedBreakIterator; |
| 260 class NumberFormat; |
| 261 class UnicodeString; |
| 262 |
| 263 /** |
| 264 * Formats simple relative dates. There are two types of relative dates that |
| 265 * it handles: |
| 266 * <ul> |
| 267 * <li>relative dates with a quantity e.g "in 5 days"</li> |
| 268 * <li>relative dates without a quantity e.g "next Tuesday"</li> |
| 269 * </ul> |
| 270 * <p> |
| 271 * This API is very basic and is intended to be a building block for more |
| 272 * fancy APIs. The caller tells it exactly what to display in a locale |
| 273 * independent way. While this class automatically provides the correct plural |
| 274 * forms, the grammatical form is otherwise as neutral as possible. It is the |
| 275 * caller's responsibility to handle cut-off logic such as deciding between |
| 276 * displaying "in 7 days" or "in 1 week." This API supports relative dates |
| 277 * involving one single unit. This API does not support relative dates |
| 278 * involving compound units, |
| 279 * e.g "in 5 days and 4 hours" nor does it support parsing. |
| 280 * <p> |
| 281 * This class is mostly thread safe and immutable with the following caveats: |
| 282 * 1. The assignment operator violates Immutability. It must not be used |
| 283 * concurrently with other operations. |
| 284 * 2. Caller must not hold onto adopted pointers. |
| 285 * <p> |
| 286 * This class is not intended for public subclassing. |
| 287 * <p> |
| 288 * Here are some examples of use: |
| 289 * <blockquote> |
| 290 * <pre> |
| 291 * UErrorCode status = U_ZERO_ERROR; |
| 292 * UnicodeString appendTo; |
| 293 * RelativeDateTimeFormatter fmt(status); |
| 294 * // Appends "in 1 day" |
| 295 * fmt.format( |
| 296 * 1, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status); |
| 297 * // Appends "in 3 days" |
| 298 * fmt.format( |
| 299 * 3, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status); |
| 300 * // Appends "3.2 years ago" |
| 301 * fmt.format( |
| 302 * 3.2, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, appendTo, status); |
| 303 * // Appends "last Sunday" |
| 304 * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SUNDAY, appendTo, status); |
| 305 * // Appends "this Sunday" |
| 306 * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SUNDAY, appendTo, status); |
| 307 * // Appends "next Sunday" |
| 308 * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SUNDAY, appendTo, status); |
| 309 * // Appends "Sunday" |
| 310 * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SUNDAY, appendTo, status); |
| 311 * |
| 312 * // Appends "yesterday" |
| 313 * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_DAY, appendTo, status); |
| 314 * // Appends "today" |
| 315 * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_DAY, appendTo, status); |
| 316 * // Appends "tomorrow" |
| 317 * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_DAY, appendTo, status); |
| 318 * // Appends "now" |
| 319 * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_NOW, appendTo, status); |
| 320 * |
| 321 * </pre> |
| 322 * </blockquote> |
| 323 * <p> |
| 324 * In the future, we may add more forms, such as abbreviated/short forms |
| 325 * (3 secs ago), and relative day periods ("yesterday afternoon"), etc. |
| 326 * |
| 327 * The RelativeDateTimeFormatter class is not intended for public subclassing. |
| 328 * |
| 329 * @draft ICU 53 |
| 330 */ |
| 331 class U_I18N_API RelativeDateTimeFormatter : public UObject { |
| 332 public: |
| 333 |
| 334 /** |
| 335 * Create RelativeDateTimeFormatter with default locale. |
| 336 * @draft ICU 53 |
| 337 */ |
| 338 RelativeDateTimeFormatter(UErrorCode& status); |
| 339 |
| 340 /** |
| 341 * Create RelativeDateTimeFormatter with given locale. |
| 342 * @draft ICU 53 |
| 343 */ |
| 344 RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status); |
| 345 |
| 346 /** |
| 347 * Create RelativeDateTimeFormatter with given locale and NumberFormat. |
| 348 * |
| 349 * @param locale the locale |
| 350 * @param nfToAdopt Constructed object takes ownership of this pointer. |
| 351 * It is an error for caller to delete this pointer or change its |
| 352 * contents after calling this constructor. |
| 353 * @status Any error is returned here. |
| 354 * @draft ICU 53 |
| 355 */ |
| 356 RelativeDateTimeFormatter( |
| 357 const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status); |
| 358 |
| 359 /** |
| 360 * Create RelativeDateTimeFormatter with given locale, NumberFormat, |
| 361 * and capitalization context. |
| 362 * |
| 363 * @param locale the locale |
| 364 * @param nfToAdopt Constructed object takes ownership of this pointer. |
| 365 * It is an error for caller to delete this pointer or change its |
| 366 * contents after calling this constructor. Caller may pass NULL for |
| 367 * this argument if they want default number format behavior. |
| 368 * @param style the format style. The UDAT_RELATIVE bit field has no effect. |
| 369 * @param capitalizationContext A value from UDisplayContext that pertains t
o |
| 370 * capitalization. |
| 371 * @status Any error is returned here. |
| 372 * @draft ICU 54 |
| 373 */ |
| 374 RelativeDateTimeFormatter( |
| 375 const Locale& locale, |
| 376 NumberFormat *nfToAdopt, |
| 377 UDateRelativeDateTimeFormatterStyle style, |
| 378 UDisplayContext capitalizationContext, |
| 379 UErrorCode& status); |
| 380 |
| 381 /** |
| 382 * Copy constructor. |
| 383 * @draft ICU 53 |
| 384 */ |
| 385 RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other); |
| 386 |
| 387 /** |
| 388 * Assignment operator. |
| 389 * @draft ICU 53 |
| 390 */ |
| 391 RelativeDateTimeFormatter& operator=( |
| 392 const RelativeDateTimeFormatter& other); |
| 393 |
| 394 /** |
| 395 * Destructor. |
| 396 * @draft ICU 53 |
| 397 */ |
| 398 virtual ~RelativeDateTimeFormatter(); |
| 399 |
| 400 /** |
| 401 * Formats a relative date with a quantity such as "in 5 days" or |
| 402 * "3 months ago" |
| 403 * @param quantity The numerical amount e.g 5. This value is formatted |
| 404 * according to this object's NumberFormat object. |
| 405 * @param direction NEXT means a future relative date; LAST means a past |
| 406 * relative date. If direction is anything else, this method sets |
| 407 * status to U_ILLEGAL_ARGUMENT_ERROR. |
| 408 * @param unit the unit e.g day? month? year? |
| 409 * @param appendTo The string to which the formatted result will be |
| 410 * appended |
| 411 * @param status ICU error code returned here. |
| 412 * @return appendTo |
| 413 * @draft ICU 53 |
| 414 */ |
| 415 UnicodeString& format( |
| 416 double quantity, |
| 417 UDateDirection direction, |
| 418 UDateRelativeUnit unit, |
| 419 UnicodeString& appendTo, |
| 420 UErrorCode& status) const; |
| 421 |
| 422 /** |
| 423 * Formats a relative date without a quantity. |
| 424 * @param direction NEXT, LAST, THIS, etc. |
| 425 * @param unit e.g SATURDAY, DAY, MONTH |
| 426 * @param appendTo The string to which the formatted result will be |
| 427 * appended. If the value of direction is documented as not being fully |
| 428 * supported in all locales then this method leaves appendTo unchanged if |
| 429 * no format string is available. |
| 430 * @param status ICU error code returned here. |
| 431 * @return appendTo |
| 432 * @draft ICU 53 |
| 433 */ |
| 434 UnicodeString& format( |
| 435 UDateDirection direction, |
| 436 UDateAbsoluteUnit unit, |
| 437 UnicodeString& appendTo, |
| 438 UErrorCode& status) const; |
| 439 |
| 440 /** |
| 441 * Combines a relative date string and a time string in this object's |
| 442 * locale. This is done with the same date-time separator used for the |
| 443 * default calendar in this locale. |
| 444 * |
| 445 * @param relativeDateString the relative date, e.g 'yesterday' |
| 446 * @param timeString the time e.g '3:45' |
| 447 * @param appendTo concatenated date and time appended here |
| 448 * @param status ICU error code returned here. |
| 449 * @return appendTo |
| 450 * @draft ICU 53 |
| 451 */ |
| 452 UnicodeString& combineDateAndTime( |
| 453 const UnicodeString& relativeDateString, |
| 454 const UnicodeString& timeString, |
| 455 UnicodeString& appendTo, |
| 456 UErrorCode& status) const; |
| 457 |
| 458 /** |
| 459 * Returns the NumberFormat this object is using. |
| 460 * |
| 461 * @draft ICU 53 |
| 462 */ |
| 463 const NumberFormat& getNumberFormat() const; |
| 464 |
| 465 /** |
| 466 * Returns the capitalization context. |
| 467 * |
| 468 * @draft ICU 54 |
| 469 */ |
| 470 UDisplayContext getCapitalizationContext() const; |
| 471 |
| 472 /** |
| 473 * Returns the format style. |
| 474 * |
| 475 * @draft ICU 54 |
| 476 */ |
| 477 UDateRelativeDateTimeFormatterStyle getFormatStyle() const; |
| 478 private: |
| 479 const RelativeDateTimeCacheData* fCache; |
| 480 const SharedNumberFormat *fNumberFormat; |
| 481 const SharedPluralRules *fPluralRules; |
| 482 UDateRelativeDateTimeFormatterStyle fStyle; |
| 483 UDisplayContext fContext; |
| 484 const SharedBreakIterator *fOptBreakIterator; |
| 485 Locale fLocale; |
| 486 void init( |
| 487 NumberFormat *nfToAdopt, |
| 488 BreakIterator *brkIter, |
| 489 UErrorCode &status); |
| 490 void adjustForContext(UnicodeString &) const; |
| 491 }; |
| 492 |
| 493 U_NAMESPACE_END |
| 494 |
| 495 #endif /* U_HIDE_DRAFT_API */ |
| 496 |
| 497 #endif /* !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION*/ |
| 498 #endif |
OLD | NEW |