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

source code of include/unicode/datefmt.h