intl/icu/source/i18n/unicode/datefmt.h

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial