1 /* 2 ******************************************************************************** 3 * Copyright (C) 1997-2013, International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ******************************************************************************** 6 * 7 * File DATEFMT.H 8 * 9 * Modification History: 10 * 11 * Date Name Description 12 * 02/19/97 aliu Converted from java. 13 * 04/01/97 aliu Added support for centuries. 14 * 07/23/98 stephen JDK 1.2 sync 15 * 11/15/99 weiv Added support for week of year/day of week formatting 16 ******************************************************************************** 17 */ 18 19 #ifndef DATEFMT_H 20 #define DATEFMT_H 21 22 #include "unicode/utypes.h" 23 24 #if !UCONFIG_NO_FORMATTING 25 26 #include "unicode/udat.h" 27 #include "unicode/calendar.h" 28 #include "unicode/numfmt.h" 29 #include "unicode/format.h" 30 #include "unicode/locid.h" 31 #include "unicode/enumset.h" 32 33 /** 34 * \file 35 * \brief C++ API: Abstract class for converting dates. 36 */ 37 38 U_NAMESPACE_BEGIN 39 40 class TimeZone; 41 class DateTimePatternGenerator; 42 43 // explicit template instantiation. see digitlst.h 44 #if defined (_MSC_VER) 45 template class U_I18N_API EnumSet<UDateFormatBooleanAttribute, 46 0, 47 UDAT_BOOLEAN_ATTRIBUTE_COUNT>; 48 #endif 49 50 /** 51 * DateFormat is an abstract class for a family of classes that convert dates and 52 * times from their internal representations to textual form and back again in a 53 * language-independent manner. Converting from the internal representation (milliseconds 54 * since midnight, January 1, 1970) to text is known as "formatting," and converting 55 * from text to millis is known as "parsing." We currently define only one concrete 56 * subclass of DateFormat: SimpleDateFormat, which can handle pretty much all normal 57 * date formatting and parsing actions. 58 * <P> 59 * DateFormat helps you to format and parse dates for any locale. Your code can 60 * be completely independent of the locale conventions for months, days of the 61 * week, or even the calendar format: lunar vs. solar. 62 * <P> 63 * To format a date for the current Locale, use one of the static factory 64 * methods: 65 * <pre> 66 * \code 67 * DateFormat* dfmt = DateFormat::createDateInstance(); 68 * UDate myDate = Calendar::getNow(); 69 * UnicodeString myString; 70 * myString = dfmt->format( myDate, myString ); 71 * \endcode 72 * </pre> 73 * If you are formatting multiple numbers, it is more efficient to get the 74 * format and use it multiple times so that the system doesn't have to fetch the 75 * information about the local language and country conventions multiple times. 76 * <pre> 77 * \code 78 * DateFormat* df = DateFormat::createDateInstance(); 79 * UnicodeString myString; 80 * UDate myDateArr[] = { 0.0, 100000000.0, 2000000000.0 }; // test values 81 * for (int32_t i = 0; i < 3; ++i) { 82 * myString.remove(); 83 * cout << df->format( myDateArr[i], myString ) << endl; 84 * } 85 * \endcode 86 * </pre> 87 * To get specific fields of a date, you can use UFieldPosition to 88 * get specific fields. 89 * <pre> 90 * \code 91 * DateFormat* dfmt = DateFormat::createDateInstance(); 92 * FieldPosition pos(DateFormat::YEAR_FIELD); 93 * UnicodeString myString; 94 * myString = dfmt->format( myDate, myString ); 95 * cout << myString << endl; 96 * cout << pos.getBeginIndex() << "," << pos. getEndIndex() << endl; 97 * \endcode 98 * </pre> 99 * To format a date for a different Locale, specify it in the call to 100 * createDateInstance(). 101 * <pre> 102 * \code 103 * DateFormat* df = 104 * DateFormat::createDateInstance( DateFormat::SHORT, Locale::getFrance()); 105 * \endcode 106 * </pre> 107 * You can use a DateFormat to parse also. 108 * <pre> 109 * \code 110 * UErrorCode status = U_ZERO_ERROR; 111 * UDate myDate = df->parse(myString, status); 112 * \endcode 113 * </pre> 114 * Use createDateInstance() to produce the normal date format for that country. 115 * There are other static factory methods available. Use createTimeInstance() 116 * to produce the normal time format for that country. Use createDateTimeInstance() 117 * to produce a DateFormat that formats both date and time. You can pass in 118 * different options to these factory methods to control the length of the 119 * result; from SHORT to MEDIUM to LONG to FULL. The exact result depends on the 120 * locale, but generally: 121 * <ul type=round> 122 * <li> SHORT is completely numeric, such as 12/13/52 or 3:30pm 123 * <li> MEDIUM is longer, such as Jan 12, 1952 124 * <li> LONG is longer, such as January 12, 1952 or 3:30:32pm 125 * <li> FULL is pretty completely specified, such as 126 * Tuesday, April 12, 1952 AD or 3:30:42pm PST. 127 * </ul> 128 * You can also set the time zone on the format if you wish. If you want even 129 * more control over the format or parsing, (or want to give your users more 130 * control), you can try casting the DateFormat you get from the factory methods 131 * to a SimpleDateFormat. This will work for the majority of countries; just 132 * remember to chck getDynamicClassID() before carrying out the cast. 133 * <P> 134 * You can also use forms of the parse and format methods with ParsePosition and 135 * FieldPosition to allow you to 136 * <ul type=round> 137 * <li> Progressively parse through pieces of a string. 138 * <li> Align any particular field, or find out where it is for selection 139 * on the screen. 140 * </ul> 141 * 142 * <p><em>User subclasses are not supported.</em> While clients may write 143 * subclasses, such code will not necessarily work and will not be 144 * guaranteed to work stably from release to release. 145 */ 146 class U_I18N_API DateFormat : public Format { 147 public: 148 149 /** 150 * Constants for various style patterns. These reflect the order of items in 151 * the DateTimePatterns resource. There are 4 time patterns, 4 date patterns, 152 * the default date-time pattern, and 4 date-time patterns. Each block of 4 values 153 * in the resource occurs in the order full, long, medium, short. 154 * @stable ICU 2.4 155 */ 156 enum EStyle 157 { 158 kNone = -1, 159 160 kFull = 0, 161 kLong = 1, 162 kMedium = 2, 163 kShort = 3, 164 165 kDateOffset = kShort + 1, 166 // kFull + kDateOffset = 4 167 // kLong + kDateOffset = 5 168 // kMedium + kDateOffset = 6 169 // kShort + kDateOffset = 7 170 171 kDateTime = 8, 172 // Default DateTime 173 174 kDateTimeOffset = kDateTime + 1, 175 // kFull + kDateTimeOffset = 9 176 // kLong + kDateTimeOffset = 10 177 // kMedium + kDateTimeOffset = 11 178 // kShort + kDateTimeOffset = 12 179 180 // relative dates 181 kRelative = (1 << 7), 182 183 kFullRelative = (kFull | kRelative), 184 185 kLongRelative = kLong | kRelative, 186 187 kMediumRelative = kMedium | kRelative, 188 189 kShortRelative = kShort | kRelative, 190 191 192 kDefault = kMedium, 193 194 195 196 /** 197 * These constants are provided for backwards compatibility only. 198 * Please use the C++ style constants defined above. 199 */ 200 FULL = kFull, 201 LONG = kLong, 202 MEDIUM = kMedium, 203 SHORT = kShort, 204 DEFAULT = kDefault, 205 DATE_OFFSET = kDateOffset, 206 NONE = kNone, 207 DATE_TIME = kDateTime 208 }; 209 210 /** 211 * Destructor. 212 * @stable ICU 2.0 213 */ 214 virtual ~DateFormat(); 215 216 /** 217 * Equality operator. Returns true if the two formats have the same behavior. 218 * @stable ICU 2.0 219 */ 220 virtual UBool operator==(const Format&) const; 221 222 223 using Format::format; 224 225 /** 226 * Format an object to produce a string. This method handles Formattable 227 * objects with a UDate type. If a the Formattable object type is not a Date, 228 * then it returns a failing UErrorCode. 229 * 230 * @param obj The object to format. Must be a Date. 231 * @param appendTo Output parameter to receive result. 232 * Result is appended to existing contents. 233 * @param pos On input: an alignment field, if desired. 234 * On output: the offsets of the alignment field. 235 * @param status Output param filled with success/failure status. 236 * @return Reference to 'appendTo' parameter. 237 * @stable ICU 2.0 238 */ 239 virtual UnicodeString& format(const Formattable& obj, 240 UnicodeString& appendTo, 241 FieldPosition& pos, 242 UErrorCode& status) const; 243 244 /** 245 * Format an object to produce a string. This method handles Formattable 246 * objects with a UDate type. If a the Formattable object type is not a Date, 247 * then it returns a failing UErrorCode. 248 * 249 * @param obj The object to format. Must be a Date. 250 * @param appendTo Output parameter to receive result. 251 * Result is appended to existing contents. 252 * @param posIter On return, can be used to iterate over positions 253 * of fields generated by this format call. Field values 254 * are defined in UDateFormatField. Can be NULL. 255 * @param status Output param filled with success/failure status. 256 * @return Reference to 'appendTo' parameter. 257 * @stable ICU 4.4 258 */ 259 virtual UnicodeString& format(const Formattable& obj, 260 UnicodeString& appendTo, 261 FieldPositionIterator* posIter, 262 UErrorCode& status) const; 263 /** 264 * Formats a date into a date/time string. This is an abstract method which 265 * concrete subclasses must implement. 266 * <P> 267 * On input, the FieldPosition parameter may have its "field" member filled with 268 * an enum value specifying a field. On output, the FieldPosition will be filled 269 * in with the text offsets for that field. 270 * <P> For example, given a time text 271 * "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition.field is 272 * UDAT_YEAR_FIELD, the offsets fieldPosition.beginIndex and 273 * statfieldPositionus.getEndIndex will be set to 0 and 4, respectively. 274 * <P> Notice 275 * that if the same time field appears more than once in a pattern, the status will 276 * be set for the first occurence of that time field. For instance, 277 * formatting a UDate to the time string "1 PM PDT (Pacific Daylight Time)" 278 * using the pattern "h a z (zzzz)" and the alignment field 279 * DateFormat::TIMEZONE_FIELD, the offsets fieldPosition.beginIndex and 280 * fieldPosition.getEndIndex will be set to 5 and 8, respectively, for the first 281 * occurence of the timezone pattern character 'z'. 282 * 283 * @param cal Calendar set to the date and time to be formatted 284 * into a date/time string. When the calendar type is 285 * different from the internal calendar held by this 286 * DateFormat instance, the date and the time zone will 287 * be inherited from the input calendar, but other calendar 288 * field values will be calculated by the internal calendar. 289 * @param appendTo Output parameter to receive result. 290 * Result is appended to existing contents. 291 * @param fieldPosition On input: an alignment field, if desired (see examples above) 292 * On output: the offsets of the alignment field (see examples above) 293 * @return Reference to 'appendTo' parameter. 294 * @stable ICU 2.1 295 */ 296 virtual UnicodeString& format( Calendar& cal, 297 UnicodeString& appendTo, 298 FieldPosition& fieldPosition) const = 0; 299 300 /** 301 * Formats a date into a date/time string. Subclasses should implement this method. 302 * 303 * @param cal Calendar set to the date and time to be formatted 304 * into a date/time string. When the calendar type is 305 * different from the internal calendar held by this 306 * DateFormat instance, the date and the time zone will 307 * be inherited from the input calendar, but other calendar 308 * field values will be calculated by the internal calendar. 309 * @param appendTo Output parameter to receive result. 310 * Result is appended to existing contents. 311 * @param posIter On return, can be used to iterate over positions 312 * of fields generated by this format call. Field values 313 * are defined in UDateFormatField. Can be NULL. 314 * @param status error status. 315 * @return Reference to 'appendTo' parameter. 316 * @stable ICU 4.4 317 */ 318 virtual UnicodeString& format(Calendar& cal, 319 UnicodeString& appendTo, 320 FieldPositionIterator* posIter, 321 UErrorCode& status) const; 322 /** 323 * Formats a UDate into a date/time string. 324 * <P> 325 * On input, the FieldPosition parameter may have its "field" member filled with 326 * an enum value specifying a field. On output, the FieldPosition will be filled 327 * in with the text offsets for that field. 328 * <P> For example, given a time text 329 * "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition.field is 330 * UDAT_YEAR_FIELD, the offsets fieldPosition.beginIndex and 331 * statfieldPositionus.getEndIndex will be set to 0 and 4, respectively. 332 * <P> Notice 333 * that if the same time field appears more than once in a pattern, the status will 334 * be set for the first occurence of that time field. For instance, 335 * formatting a UDate to the time string "1 PM PDT (Pacific Daylight Time)" 336 * using the pattern "h a z (zzzz)" and the alignment field 337 * DateFormat::TIMEZONE_FIELD, the offsets fieldPosition.beginIndex and 338 * fieldPosition.getEndIndex will be set to 5 and 8, respectively, for the first 339 * occurence of the timezone pattern character 'z'. 340 * 341 * @param date UDate to be formatted into a date/time string. 342 * @param appendTo Output parameter to receive result. 343 * Result is appended to existing contents. 344 * @param fieldPosition On input: an alignment field, if desired (see examples above) 345 * On output: the offsets of the alignment field (see examples above) 346 * @return Reference to 'appendTo' parameter. 347 * @stable ICU 2.0 348 */ 349 UnicodeString& format( UDate date, 350 UnicodeString& appendTo, 351 FieldPosition& fieldPosition) const; 352 353 /** 354 * Formats a UDate into a date/time string. 355 * 356 * @param date UDate to be formatted into a date/time string. 357 * @param appendTo Output parameter to receive result. 358 * Result is appended to existing contents. 359 * @param posIter On return, can be used to iterate over positions 360 * of fields generated by this format call. Field values 361 * are defined in UDateFormatField. Can be NULL. 362 * @param status error status. 363 * @return Reference to 'appendTo' parameter. 364 * @stable ICU 4.4 365 */ 366 UnicodeString& format(UDate date, 367 UnicodeString& appendTo, 368 FieldPositionIterator* posIter, 369 UErrorCode& status) const; 370 /** 371 * Formats a UDate into a date/time string. If there is a problem, you won't 372 * know, using this method. Use the overloaded format() method which takes a 373 * FieldPosition& to detect formatting problems. 374 * 375 * @param date The UDate value to be formatted into a string. 376 * @param appendTo Output parameter to receive result. 377 * Result is appended to existing contents. 378 * @return Reference to 'appendTo' parameter. 379 * @stable ICU 2.0 380 */ 381 UnicodeString& format(UDate date, UnicodeString& appendTo) const; 382 383 /** 384 * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT" 385 * will be parsed into a UDate that is equivalent to Date(837039928046). 386 * Parsing begins at the beginning of the string and proceeds as far as 387 * possible. Assuming no parse errors were encountered, this function 388 * doesn't return any information about how much of the string was consumed 389 * by the parsing. If you need that information, use the version of 390 * parse() that takes a ParsePosition. 391 * <P> 392 * By default, parsing is lenient: If the input is not in the form used by 393 * this object's format method but can still be parsed as a date, then the 394 * parse succeeds. Clients may insist on strict adherence to the format by 395 * calling setLenient(false). 396 * @see DateFormat::setLenient(boolean) 397 * <P> 398 * Note that the normal date formats associated with some calendars - such 399 * as the Chinese lunar calendar - do not specify enough fields to enable 400 * dates to be parsed unambiguously. In the case of the Chinese lunar 401 * calendar, while the year within the current 60-year cycle is specified, 402 * the number of such cycles since the start date of the calendar (in the 403 * ERA field of the Calendar object) is not normally part of the format, 404 * and parsing may assume the wrong era. For cases such as this it is 405 * recommended that clients parse using the method 406 * parse(const UnicodeString&, Calendar& cal, ParsePosition&) 407 * with the Calendar passed in set to the current date, or to a date 408 * within the era/cycle that should be assumed if absent in the format. 409 * 410 * @param text The date/time string to be parsed into a UDate value. 411 * @param status Output param to be set to success/failure code. If 412 * 'text' cannot be parsed, it will be set to a failure 413 * code. 414 * @return The parsed UDate value, if successful. 415 * @stable ICU 2.0 416 */ 417 virtual UDate parse( const UnicodeString& text, 418 UErrorCode& status) const; 419 420 /** 421 * Parse a date/time string beginning at the given parse position. For 422 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date 423 * that is equivalent to Date(837039928046). 424 * <P> 425 * By default, parsing is lenient: If the input is not in the form used by 426 * this object's format method but can still be parsed as a date, then the 427 * parse succeeds. Clients may insist on strict adherence to the format by 428 * calling setLenient(false). 429 * @see DateFormat::setLenient(boolean) 430 * 431 * @param text The date/time string to be parsed. 432 * @param cal A Calendar set on input to the date and time to be used for 433 * missing values in the date/time string being parsed, and set 434 * on output to the parsed date/time. When the calendar type is 435 * different from the internal calendar held by this DateFormat 436 * instance, the internal calendar will be cloned to a work 437 * calendar set to the same milliseconds and time zone as the 438 * cal parameter, field values will be parsed based on the work 439 * calendar, then the result (milliseconds and time zone) will 440 * be set in this calendar. 441 * @param pos On input, the position at which to start parsing; on 442 * output, the position at which parsing terminated, or the 443 * start position if the parse failed. 444 * @stable ICU 2.1 445 */ 446 virtual void parse( const UnicodeString& text, 447 Calendar& cal, 448 ParsePosition& pos) const = 0; 449 450 /** 451 * Parse a date/time string beginning at the given parse position. For 452 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date 453 * that is equivalent to Date(837039928046). 454 * <P> 455 * By default, parsing is lenient: If the input is not in the form used by 456 * this object's format method but can still be parsed as a date, then the 457 * parse succeeds. Clients may insist on strict adherence to the format by 458 * calling setLenient(false). 459 * @see DateFormat::setLenient(boolean) 460 * <P> 461 * Note that the normal date formats associated with some calendars - such 462 * as the Chinese lunar calendar - do not specify enough fields to enable 463 * dates to be parsed unambiguously. In the case of the Chinese lunar 464 * calendar, while the year within the current 60-year cycle is specified, 465 * the number of such cycles since the start date of the calendar (in the 466 * ERA field of the Calendar object) is not normally part of the format, 467 * and parsing may assume the wrong era. For cases such as this it is 468 * recommended that clients parse using the method 469 * parse(const UnicodeString&, Calendar& cal, ParsePosition&) 470 * with the Calendar passed in set to the current date, or to a date 471 * within the era/cycle that should be assumed if absent in the format. 472 * 473 * @param text The date/time string to be parsed into a UDate value. 474 * @param pos On input, the position at which to start parsing; on 475 * output, the position at which parsing terminated, or the 476 * start position if the parse failed. 477 * @return A valid UDate if the input could be parsed. 478 * @stable ICU 2.0 479 */ 480 UDate parse( const UnicodeString& text, 481 ParsePosition& pos) const; 482 483 /** 484 * Parse a string to produce an object. This methods handles parsing of 485 * date/time strings into Formattable objects with UDate types. 486 * <P> 487 * Before calling, set parse_pos.index to the offset you want to start 488 * parsing at in the source. After calling, parse_pos.index is the end of 489 * the text you parsed. If error occurs, index is unchanged. 490 * <P> 491 * When parsing, leading whitespace is discarded (with a successful parse), 492 * while trailing whitespace is left as is. 493 * <P> 494 * See Format::parseObject() for more. 495 * 496 * @param source The string to be parsed into an object. 497 * @param result Formattable to be set to the parse result. 498 * If parse fails, return contents are undefined. 499 * @param parse_pos The position to start parsing at. Upon return 500 * this param is set to the position after the 501 * last character successfully parsed. If the 502 * source is not parsed successfully, this param 503 * will remain unchanged. 504 * @stable ICU 2.0 505 */ 506 virtual void parseObject(const UnicodeString& source, 507 Formattable& result, 508 ParsePosition& parse_pos) const; 509 510 /** 511 * Create a default date/time formatter that uses the SHORT style for both 512 * the date and the time. 513 * 514 * @return A date/time formatter which the caller owns. 515 * @stable ICU 2.0 516 */ 517 static DateFormat* U_EXPORT2 createInstance(void); 518 519 /** 520 * Creates a time formatter with the given formatting style for the given 521 * locale. 522 * 523 * @param style The given formatting style. For example, 524 * SHORT for "h:mm a" in the US locale. Relative 525 * time styles are not currently supported. 526 * @param aLocale The given locale. 527 * @return A time formatter which the caller owns. 528 * @stable ICU 2.0 529 */ 530 static DateFormat* U_EXPORT2 createTimeInstance(EStyle style = kDefault, 531 const Locale& aLocale = Locale::getDefault()); 532 533 /** 534 * Creates a date formatter with the given formatting style for the given 535 * const locale. 536 * 537 * @param style The given formatting style. For example, SHORT for "M/d/yy" in the 538 * US locale. As currently implemented, relative date formatting only 539 * affects a limited range of calendar days before or after the 540 * current date, based on the CLDR <field type="day">/<relative> data: 541 * For example, in English, "Yesterday", "Today", and "Tomorrow". 542 * Outside of this range, dates are formatted using the corresponding 543 * non-relative style. 544 * @param aLocale The given locale. 545 * @return A date formatter which the caller owns. 546 * @stable ICU 2.0 547 */ 548 static DateFormat* U_EXPORT2 createDateInstance(EStyle style = kDefault, 549 const Locale& aLocale = Locale::getDefault()); 550 551 /** 552 * Creates a date/time formatter with the given formatting styles for the 553 * given locale. 554 * 555 * @param dateStyle The given formatting style for the date portion of the result. 556 * For example, SHORT for "M/d/yy" in the US locale. As currently 557 * implemented, relative date formatting only affects a limited range 558 * of calendar days before or after the current date, based on the 559 * CLDR <field type="day">/<relative> data: For example, in English, 560 * "Yesterday", "Today", and "Tomorrow". Outside of this range, dates 561 * are formatted using the corresponding non-relative style. 562 * @param timeStyle The given formatting style for the time portion of the result. 563 * For example, SHORT for "h:mm a" in the US locale. Relative 564 * time styles are not currently supported. 565 * @param aLocale The given locale. 566 * @return A date/time formatter which the caller owns. 567 * @stable ICU 2.0 568 */ 569 static DateFormat* U_EXPORT2 createDateTimeInstance(EStyle dateStyle = kDefault, 570 EStyle timeStyle = kDefault, 571 const Locale& aLocale = Locale::getDefault()); 572 573 /** 574 * Gets the set of locales for which DateFormats are installed. 575 * @param count Filled in with the number of locales in the list that is returned. 576 * @return the set of locales for which DateFormats are installed. The caller 577 * does NOT own this list and must not delete it. 578 * @stable ICU 2.0 579 */ 580 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count); 581 582 /** 583 * Returns true if the encapsulated Calendar object is set for lenient parsing. 584 * @stable ICU 2.0 585 */ 586 virtual UBool isLenient(void) const; 587 588 /** 589 * Specify whether or not date/time parsing is to be lenient. With lenient 590 * parsing, the parser may use heuristics to interpret inputs that do not 591 * precisely match this object's format. With strict parsing, inputs must 592 * match this object's format. 593 * 594 * Note: This method is specific to the encapsulated Calendar object. DateFormat 595 * leniency aspects are controlled by setBooleanAttribute. 596 * 597 * @param lenient True specifies date/time interpretation to be lenient. 598 * @see Calendar::setLenient 599 * @stable ICU 2.0 600 */ 601 virtual void setLenient(UBool lenient); 602 603 /** 604 * Gets the calendar associated with this date/time formatter. 605 * @return the calendar associated with this date/time formatter. 606 * @stable ICU 2.0 607 */ 608 virtual const Calendar* getCalendar(void) const; 609 610 /** 611 * Set the calendar to be used by this date format. Initially, the default 612 * calendar for the specified or default locale is used. The caller should 613 * not delete the Calendar object after it is adopted by this call. 614 * Adopting a new calendar will change to the default symbols. 615 * 616 * @param calendarToAdopt Calendar object to be adopted. 617 * @stable ICU 2.0 618 */ 619 virtual void adoptCalendar(Calendar* calendarToAdopt); 620 621 /** 622 * Set the calendar to be used by this date format. Initially, the default 623 * calendar for the specified or default locale is used. 624 * 625 * @param newCalendar Calendar object to be set. 626 * @stable ICU 2.0 627 */ 628 virtual void setCalendar(const Calendar& newCalendar); 629 630 631 /** 632 * Gets the number formatter which this date/time formatter uses to format 633 * and parse the numeric portions of the pattern. 634 * @return the number formatter which this date/time formatter uses. 635 * @stable ICU 2.0 636 */ 637 virtual const NumberFormat* getNumberFormat(void) const; 638 639 /** 640 * Allows you to set the number formatter. The caller should 641 * not delete the NumberFormat object after it is adopted by this call. 642 * @param formatToAdopt NumberFormat object to be adopted. 643 * @stable ICU 2.0 644 */ 645 virtual void adoptNumberFormat(NumberFormat* formatToAdopt); 646 647 /** 648 * Allows you to set the number formatter. 649 * @param newNumberFormat NumberFormat object to be set. 650 * @stable ICU 2.0 651 */ 652 virtual void setNumberFormat(const NumberFormat& newNumberFormat); 653 654 /** 655 * Returns a reference to the TimeZone used by this DateFormat's calendar. 656 * @return the time zone associated with the calendar of DateFormat. 657 * @stable ICU 2.0 658 */ 659 virtual const TimeZone& getTimeZone(void) const; 660 661 /** 662 * Sets the time zone for the calendar of this DateFormat object. The caller 663 * no longer owns the TimeZone object and should not delete it after this call. 664 * @param zoneToAdopt the TimeZone to be adopted. 665 * @stable ICU 2.0 666 */ 667 virtual void adoptTimeZone(TimeZone* zoneToAdopt); 668 669 /** 670 * Sets the time zone for the calendar of this DateFormat object. 671 * @param zone the new time zone. 672 * @stable ICU 2.0 673 */ 674 virtual void setTimeZone(const TimeZone& zone); 675 676 /** 677 * Set an boolean attribute on this DateFormat. 678 * May return U_UNSUPPORTED_ERROR if this instance does not support 679 * the specified attribute. 680 * @param attr the attribute to set 681 * @param newvalue new value 682 * @param status the error type 683 * @return *this - for chaining (example: format.setAttribute(...).setAttribute(...) ) 684 * @internal ICU technology preview 685 */ 686 687 virtual DateFormat& U_EXPORT2 setBooleanAttribute(UDateFormatBooleanAttribute attr, 688 UBool newvalue, 689 UErrorCode &status); 690 691 /** 692 * Get an boolean from this DateFormat 693 * May return U_UNSUPPORTED_ERROR if this instance does not support 694 * the specified attribute. 695 * @param attr the attribute to set 696 * @param status the error type 697 * @return the attribute value. Undefined if there is an error. 698 * @internal ICU technology preview 699 */ 700 virtual UBool U_EXPORT2 getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &status) const; 701 702 protected: 703 /** 704 * Default constructor. Creates a DateFormat with no Calendar or NumberFormat 705 * associated with it. This constructor depends on the subclasses to fill in 706 * the calendar and numberFormat fields. 707 * @stable ICU 2.0 708 */ 709 DateFormat(); 710 711 /** 712 * Copy constructor. 713 * @stable ICU 2.0 714 */ 715 DateFormat(const DateFormat&); 716 717 /** 718 * Default assignment operator. 719 * @stable ICU 2.0 720 */ 721 DateFormat& operator=(const DateFormat&); 722 723 /** 724 * The calendar that DateFormat uses to produce the time field values needed 725 * to implement date/time formatting. Subclasses should generally initialize 726 * this to the default calendar for the locale associated with this DateFormat. 727 * @stable ICU 2.4 728 */ 729 Calendar* fCalendar; 730 731 /** 732 * The number formatter that DateFormat uses to format numbers in dates and 733 * times. Subclasses should generally initialize this to the default number 734 * format for the locale associated with this DateFormat. 735 * @stable ICU 2.4 736 */ 737 NumberFormat* fNumberFormat; 738 739 740 private: 741 /** 742 * Gets the date/time formatter with the given formatting styles for the 743 * given locale. 744 * @param dateStyle the given date formatting style. 745 * @param timeStyle the given time formatting style. 746 * @param inLocale the given locale. 747 * @return a date/time formatter, or 0 on failure. 748 */ 749 static DateFormat* U_EXPORT2 create(EStyle timeStyle, EStyle dateStyle, const Locale& inLocale); 750 751 752 /** 753 * enum set of active boolean attributes for this instance 754 */ 755 EnumSet<UDateFormatBooleanAttribute, 0, UDAT_BOOLEAN_ATTRIBUTE_COUNT> fBoolFlags; 756 757 758 public: 759 #ifndef U_HIDE_OBSOLETE_API 760 /** 761 * Field selector for FieldPosition for DateFormat fields. 762 * @obsolete ICU 3.4 use UDateFormatField instead, since this API will be 763 * removed in that release 764 */ 765 enum EField 766 { 767 // Obsolete; use UDateFormatField instead 768 kEraField = UDAT_ERA_FIELD, 769 kYearField = UDAT_YEAR_FIELD, 770 kMonthField = UDAT_MONTH_FIELD, 771 kDateField = UDAT_DATE_FIELD, 772 kHourOfDay1Field = UDAT_HOUR_OF_DAY1_FIELD, 773 kHourOfDay0Field = UDAT_HOUR_OF_DAY0_FIELD, 774 kMinuteField = UDAT_MINUTE_FIELD, 775 kSecondField = UDAT_SECOND_FIELD, 776 kMillisecondField = UDAT_FRACTIONAL_SECOND_FIELD, 777 kDayOfWeekField = UDAT_DAY_OF_WEEK_FIELD, 778 kDayOfYearField = UDAT_DAY_OF_YEAR_FIELD, 779 kDayOfWeekInMonthField = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD, 780 kWeekOfYearField = UDAT_WEEK_OF_YEAR_FIELD, 781 kWeekOfMonthField = UDAT_WEEK_OF_MONTH_FIELD, 782 kAmPmField = UDAT_AM_PM_FIELD, 783 kHour1Field = UDAT_HOUR1_FIELD, 784 kHour0Field = UDAT_HOUR0_FIELD, 785 kTimezoneField = UDAT_TIMEZONE_FIELD, 786 kYearWOYField = UDAT_YEAR_WOY_FIELD, 787 kDOWLocalField = UDAT_DOW_LOCAL_FIELD, 788 kExtendedYearField = UDAT_EXTENDED_YEAR_FIELD, 789 kJulianDayField = UDAT_JULIAN_DAY_FIELD, 790 kMillisecondsInDayField = UDAT_MILLISECONDS_IN_DAY_FIELD, 791 792 // Obsolete; use UDateFormatField instead 793 ERA_FIELD = UDAT_ERA_FIELD, 794 YEAR_FIELD = UDAT_YEAR_FIELD, 795 MONTH_FIELD = UDAT_MONTH_FIELD, 796 DATE_FIELD = UDAT_DATE_FIELD, 797 HOUR_OF_DAY1_FIELD = UDAT_HOUR_OF_DAY1_FIELD, 798 HOUR_OF_DAY0_FIELD = UDAT_HOUR_OF_DAY0_FIELD, 799 MINUTE_FIELD = UDAT_MINUTE_FIELD, 800 SECOND_FIELD = UDAT_SECOND_FIELD, 801 MILLISECOND_FIELD = UDAT_FRACTIONAL_SECOND_FIELD, 802 DAY_OF_WEEK_FIELD = UDAT_DAY_OF_WEEK_FIELD, 803 DAY_OF_YEAR_FIELD = UDAT_DAY_OF_YEAR_FIELD, 804 DAY_OF_WEEK_IN_MONTH_FIELD = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD, 805 WEEK_OF_YEAR_FIELD = UDAT_WEEK_OF_YEAR_FIELD, 806 WEEK_OF_MONTH_FIELD = UDAT_WEEK_OF_MONTH_FIELD, 807 AM_PM_FIELD = UDAT_AM_PM_FIELD, 808 HOUR1_FIELD = UDAT_HOUR1_FIELD, 809 HOUR0_FIELD = UDAT_HOUR0_FIELD, 810 TIMEZONE_FIELD = UDAT_TIMEZONE_FIELD 811 }; 812 #endif /* U_HIDE_OBSOLETE_API */ 813 }; 814 815 U_NAMESPACE_END 816 817 #endif /* #if !UCONFIG_NO_FORMATTING */ 818 819 #endif // _DATEFMT 820 //eof 821