2 ********************************************************************************
3 * Copyright (C) 1997-2008, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 02/19/97 aliu Converted from java.
13 * 07/21/98 stephen Added getZoneIndex()
14 * Changed to match C++ conventions
15 ********************************************************************************
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_FORMATTING
25 #include "unicode/calendar.h"
26 #include "unicode/uobject.h"
27 #include "unicode/locid.h"
28 #include "unicode/ures.h"
32 * \brief C++ API: Symbols for formatting dates.
37 /* forward declaration */
38 class SimpleDateFormat
;
40 class ZoneStringFormat
;
41 class SafeZoneStringFormatPtr
;
44 * DateFormatSymbols is a public class for encapsulating localizable date-time
45 * formatting data -- including timezone data. DateFormatSymbols is used by
46 * DateFormat and SimpleDateFormat.
48 * Rather than first creating a DateFormatSymbols to get a date-time formatter
49 * by using a SimpleDateFormat constructor, clients are encouraged to create a
50 * date-time formatter using the getTimeInstance(), getDateInstance(), or
51 * getDateTimeInstance() method in DateFormat. Each of these methods can return a
52 * date/time formatter initialized with a default format pattern along with the
53 * date-time formatting data for a given or default locale. After a formatter is
54 * created, clients may modify the format pattern using the setPattern function
55 * as so desired. For more information on using these formatter factory
56 * functions, see DateFormat.
58 * If clients decide to create a date-time formatter with a particular format
59 * pattern and locale, they can do so with new SimpleDateFormat(aPattern,
60 * new DateFormatSymbols(aLocale)). This will load the appropriate date-time
61 * formatting data from the locale.
63 * DateFormatSymbols objects are clonable. When clients obtain a
64 * DateFormatSymbols object, they can feel free to modify the date-time
65 * formatting data as necessary. For instance, clients can
66 * replace the localized date-time format pattern characters with the ones that
67 * they feel easy to remember. Or they can change the representative cities
68 * originally picked by default to using their favorite ones.
70 * DateFormatSymbols are not expected to be subclassed. Data for a calendar is
71 * loaded out of resource bundles. The 'type' parameter indicates the type of
72 * calendar, for example, "gregorian" or "japanese". If the type is not gregorian
73 * (or NULL, or an empty string) then the type is appended to the resource name,
74 * for example, 'Eras_japanese' instead of 'Eras'. If the resource 'Eras_japanese' did
75 * not exist (even in root), then this class will fall back to just 'Eras', that is,
76 * Gregorian data. Therefore, the calendar implementor MUST ensure that the root
77 * locale at least contains any resources that are to be particularized for the
80 class U_I18N_API DateFormatSymbols
: public UObject
{
83 * Construct a DateFormatSymbols object by loading format data from
84 * resources for the default locale, in the default calendar (Gregorian).
86 * NOTE: This constructor will never fail; if it cannot get resource
87 * data for the default locale, it will return a last-resort object
88 * based on hard-coded strings.
90 * @param status Status code. Failure
91 * results if the resources for the default cannot be
92 * found or cannot be loaded
95 DateFormatSymbols(UErrorCode
& status
);
98 * Construct a DateFormatSymbols object by loading format data from
99 * resources for the given locale, in the default calendar (Gregorian).
101 * @param locale Locale to load format data from.
102 * @param status Status code. Failure
103 * results if the resources for the locale cannot be
104 * found or cannot be loaded
107 DateFormatSymbols(const Locale
& locale
,
111 * Construct a DateFormatSymbols object by loading format data from
112 * resources for the default locale, in the default calendar (Gregorian).
114 * NOTE: This constructor will never fail; if it cannot get resource
115 * data for the default locale, it will return a last-resort object
116 * based on hard-coded strings.
118 * @param type Type of calendar (as returned by Calendar::getType).
119 * Will be used to access the correct set of strings.
120 * (NULL or empty string defaults to "gregorian".)
121 * @param status Status code. Failure
122 * results if the resources for the default cannot be
123 * found or cannot be loaded
126 DateFormatSymbols(const char *type
, UErrorCode
& status
);
129 * Construct a DateFormatSymbols object by loading format data from
130 * resources for the given locale, in the default calendar (Gregorian).
132 * @param locale Locale to load format data from.
133 * @param type Type of calendar (as returned by Calendar::getType).
134 * Will be used to access the correct set of strings.
135 * (NULL or empty string defaults to "gregorian".)
136 * @param status Status code. Failure
137 * results if the resources for the locale cannot be
138 * found or cannot be loaded
141 DateFormatSymbols(const Locale
& locale
,
149 DateFormatSymbols(const DateFormatSymbols
&);
152 * Assignment operator.
155 DateFormatSymbols
& operator=(const DateFormatSymbols
&);
158 * Destructor. This is nonvirtual because this class is not designed to be
162 virtual ~DateFormatSymbols();
165 * Return true if another object is semantically equal to this one.
167 * @param other the DateFormatSymbols object to be compared with.
168 * @return true if other is semantically equal to this.
171 UBool
operator==(const DateFormatSymbols
& other
) const;
174 * Return true if another object is semantically unequal to this one.
176 * @param other the DateFormatSymbols object to be compared with.
177 * @return true if other is semantically unequal to this.
180 UBool
operator!=(const DateFormatSymbols
& other
) const { return !operator==(other
); }
183 * Gets abbreviated era strings. For example: "AD" and "BC".
185 * @param count Filled in with length of the array.
186 * @return the era strings.
189 const UnicodeString
* getEras(int32_t& count
) const;
192 * Sets abbreviated era strings. For example: "AD" and "BC".
193 * @param eras Array of era strings (DateFormatSymbols retains ownership.)
194 * @param count Filled in with length of the array.
197 void setEras(const UnicodeString
* eras
, int32_t count
);
200 * Gets era name strings. For example: "Anno Domini" and "Before Christ".
202 * @param count Filled in with length of the array.
203 * @return the era name strings.
206 const UnicodeString
* getEraNames(int32_t& count
) const;
209 * Sets era name strings. For example: "Anno Domini" and "Before Christ".
210 * @param eraNames Array of era name strings (DateFormatSymbols retains ownership.)
211 * @param count Filled in with length of the array.
214 void setEraNames(const UnicodeString
* eraNames
, int32_t count
);
217 * Gets narrow era strings. For example: A" and "D".
219 * @param count Filled in with length of the array.
220 * @return the narrow era strings.
223 const UnicodeString
* getNarrowEras(int32_t& count
) const;
226 * Sets narrow era strings. For example: "A" and "B".
227 * @param narrowEras Array of narrow era strings (DateFormatSymbols retains ownership.)
228 * @param count Filled in with length of the array.
231 void setNarrowEras(const UnicodeString
* narrowEras
, int32_t count
);
234 * Gets month strings. For example: "January", "February", etc.
235 * @param count Filled in with length of the array.
236 * @return the month strings. (DateFormatSymbols retains ownership.)
239 const UnicodeString
* getMonths(int32_t& count
) const;
242 * Sets month strings. For example: "January", "February", etc.
244 * @param months the new month strings. (not adopted; caller retains ownership)
245 * @param count Filled in with length of the array.
248 void setMonths(const UnicodeString
* months
, int32_t count
);
251 * Gets short month strings. For example: "Jan", "Feb", etc.
253 * @param count Filled in with length of the array.
254 * @return the short month strings. (DateFormatSymbols retains ownership.)
257 const UnicodeString
* getShortMonths(int32_t& count
) const;
260 * Sets short month strings. For example: "Jan", "Feb", etc.
261 * @param count Filled in with length of the array.
262 * @param shortMonths the new short month strings. (not adopted; caller retains ownership)
265 void setShortMonths(const UnicodeString
* shortMonths
, int32_t count
);
268 * Selector for date formatting context
278 * Selector for date formatting width
289 * Gets month strings by width and context. For example: "January", "February", etc.
290 * @param count Filled in with length of the array.
291 * @param context The formatting context, either FORMAT or STANDALONE
292 * @param width The width of returned strings, either WIDE, ABBREVIATED, or NARROW.
293 * @return the month strings. (DateFormatSymbols retains ownership.)
296 const UnicodeString
* getMonths(int32_t& count
, DtContextType context
, DtWidthType width
) const;
299 * Sets month strings by width and context. For example: "January", "February", etc.
301 * @param months The new month strings. (not adopted; caller retains ownership)
302 * @param count Filled in with length of the array.
303 * @param context The formatting context, either FORMAT or STANDALONE
304 * @param width The width of returned strings, either WIDE, ABBREVIATED, or NARROW.
307 void setMonths(const UnicodeString
* months
, int32_t count
, DtContextType context
, DtWidthType width
);
310 * Gets weekday strings. For example: "Sunday", "Monday", etc.
311 * @param count Filled in with length of the array.
312 * @return the weekday strings. (DateFormatSymbols retains ownership.)
315 const UnicodeString
* getWeekdays(int32_t& count
) const;
319 * Sets weekday strings. For example: "Sunday", "Monday", etc.
320 * @param weekdays the new weekday strings. (not adopted; caller retains ownership)
321 * @param count Filled in with length of the array.
324 void setWeekdays(const UnicodeString
* weekdays
, int32_t count
);
327 * Gets short weekday strings. For example: "Sun", "Mon", etc.
328 * @param count Filled in with length of the array.
329 * @return the short weekday strings. (DateFormatSymbols retains ownership.)
332 const UnicodeString
* getShortWeekdays(int32_t& count
) const;
335 * Sets short weekday strings. For example: "Sun", "Mon", etc.
336 * @param shortWeekdays the new short weekday strings. (not adopted; caller retains ownership)
337 * @param count Filled in with length of the array.
340 void setShortWeekdays(const UnicodeString
* shortWeekdays
, int32_t count
);
343 * Gets weekday strings by width and context. For example: "Sunday", "Monday", etc.
344 * @param count Filled in with length of the array.
345 * @param context The formatting context, either FORMAT or STANDALONE
346 * @param width The width of returned strings, either WIDE, ABBREVIATED, or NARROW
347 * @return the month strings. (DateFormatSymbols retains ownership.)
350 const UnicodeString
* getWeekdays(int32_t& count
, DtContextType context
, DtWidthType width
) const;
353 * Sets weekday strings by width and context. For example: "Sunday", "Monday", etc.
354 * @param weekdays The new weekday strings. (not adopted; caller retains ownership)
355 * @param count Filled in with length of the array.
356 * @param context The formatting context, either FORMAT or STANDALONE
357 * @param width The width of returned strings, either WIDE, ABBREVIATED, or NARROW
360 void setWeekdays(const UnicodeString
* weekdays
, int32_t count
, DtContextType context
, DtWidthType width
);
363 * Gets quarter strings by width and context. For example: "1st Quarter", "2nd Quarter", etc.
364 * @param count Filled in with length of the array.
365 * @param context The formatting context, either FORMAT or STANDALONE
366 * @param width The width of returned strings, either WIDE or ABBREVIATED. There
367 * are no NARROW quarters.
368 * @return the quarter strings. (DateFormatSymbols retains ownership.)
371 const UnicodeString
* getQuarters(int32_t& count
, DtContextType context
, DtWidthType width
) const;
374 * Sets quarter strings by width and context. For example: "1st Quarter", "2nd Quarter", etc.
376 * @param quarters The new quarter strings. (not adopted; caller retains ownership)
377 * @param count Filled in with length of the array.
378 * @param context The formatting context, either FORMAT or STANDALONE
379 * @param width The width of returned strings, either WIDE or ABBREVIATED. There
380 * are no NARROW quarters.
383 void setQuarters(const UnicodeString
* quarters
, int32_t count
, DtContextType context
, DtWidthType width
);
386 * Gets AM/PM strings. For example: "AM" and "PM".
387 * @param count Filled in with length of the array.
388 * @return the weekday strings. (DateFormatSymbols retains ownership.)
391 const UnicodeString
* getAmPmStrings(int32_t& count
) const;
394 * Sets ampm strings. For example: "AM" and "PM".
395 * @param ampms the new ampm strings. (not adopted; caller retains ownership)
396 * @param count Filled in with length of the array.
399 void setAmPmStrings(const UnicodeString
* ampms
, int32_t count
);
402 * Gets timezone strings. These strings are stored in a 2-dimensional array.
403 * @param rowCount Output param to receive number of rows.
404 * @param columnCount Output param to receive number of columns.
405 * @return The timezone strings as a 2-d array. (DateFormatSymbols retains ownership.)
406 * @deprecated ICU 3.6
408 const UnicodeString
** getZoneStrings(int32_t& rowCount
, int32_t& columnCount
) const;
411 * Sets timezone strings. These strings are stored in a 2-dimensional array.
412 * @param strings The timezone strings as a 2-d array to be copied. (not adopted; caller retains ownership)
413 * @param rowCount The number of rows (count of first index).
414 * @param columnCount The number of columns (count of second index).
417 void setZoneStrings(const UnicodeString
* const* strings
, int32_t rowCount
, int32_t columnCount
);
420 * Get the non-localized date-time pattern characters.
421 * @return the non-localized date-time pattern characters
424 static const UChar
* U_EXPORT2
getPatternUChars(void);
427 * Gets localized date-time pattern characters. For example: 'u', 't', etc.
429 * Note: ICU no longer provides localized date-time pattern characters for a locale
430 * starting ICU 3.8. This method returns the non-localized date-time pattern
431 * characters unless user defined localized data is set by setLocalPatternChars.
432 * @param result Output param which will receive the localized date-time pattern characters.
433 * @return A reference to 'result'.
436 UnicodeString
& getLocalPatternChars(UnicodeString
& result
) const;
439 * Sets localized date-time pattern characters. For example: 'u', 't', etc.
440 * @param newLocalPatternChars the new localized date-time
441 * pattern characters.
444 void setLocalPatternChars(const UnicodeString
& newLocalPatternChars
);
447 * Returns the locale for this object. Two flavors are available:
448 * valid and actual locale.
451 Locale
getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const;
454 * ICU "poor man's RTTI", returns a UClassID for the actual class.
458 virtual UClassID
getDynamicClassID() const;
461 * ICU "poor man's RTTI", returns a UClassID for this class.
465 static UClassID U_EXPORT2
getStaticClassID();
469 friend class SimpleDateFormat
;
470 friend class DateFormatSymbolsSingleSetter
; // see udat.cpp
473 * Abbreviated era strings. For example: "AD" and "BC".
475 UnicodeString
* fEras
;
479 * Era name strings. For example: "Anno Domini" and "Before Christ".
481 UnicodeString
* fEraNames
;
482 int32_t fEraNamesCount
;
485 * Narrow era strings. For example: "A" and "B".
487 UnicodeString
* fNarrowEras
;
488 int32_t fNarrowErasCount
;
491 * Month strings. For example: "January", "February", etc.
493 UnicodeString
* fMonths
;
494 int32_t fMonthsCount
;
497 * Short month strings. For example: "Jan", "Feb", etc.
499 UnicodeString
* fShortMonths
;
500 int32_t fShortMonthsCount
;
503 * Narrow month strings. For example: "J", "F", etc.
505 UnicodeString
* fNarrowMonths
;
506 int32_t fNarrowMonthsCount
;
509 * Standalone Month strings. For example: "January", "February", etc.
511 UnicodeString
* fStandaloneMonths
;
512 int32_t fStandaloneMonthsCount
;
515 * Standalone Short month strings. For example: "Jan", "Feb", etc.
517 UnicodeString
* fStandaloneShortMonths
;
518 int32_t fStandaloneShortMonthsCount
;
521 * Standalone Narrow month strings. For example: "J", "F", etc.
523 UnicodeString
* fStandaloneNarrowMonths
;
524 int32_t fStandaloneNarrowMonthsCount
;
527 * Weekday strings. For example: "Sunday", "Monday", etc.
529 UnicodeString
* fWeekdays
;
530 int32_t fWeekdaysCount
;
533 * Short weekday strings. For example: "Sun", "Mon", etc.
535 UnicodeString
* fShortWeekdays
;
536 int32_t fShortWeekdaysCount
;
539 * Narrow weekday strings. For example: "Sun", "Mon", etc.
541 UnicodeString
* fNarrowWeekdays
;
542 int32_t fNarrowWeekdaysCount
;
545 * Standalone Weekday strings. For example: "Sunday", "Monday", etc.
547 UnicodeString
* fStandaloneWeekdays
;
548 int32_t fStandaloneWeekdaysCount
;
551 * Standalone Short weekday strings. For example: "Sun", "Mon", etc.
553 UnicodeString
* fStandaloneShortWeekdays
;
554 int32_t fStandaloneShortWeekdaysCount
;
557 * Standalone Narrow weekday strings. For example: "Sun", "Mon", etc.
559 UnicodeString
* fStandaloneNarrowWeekdays
;
560 int32_t fStandaloneNarrowWeekdaysCount
;
563 * Ampm strings. For example: "AM" and "PM".
565 UnicodeString
* fAmPms
;
569 * Quarter strings. For example: "1st quarter", "2nd quarter", etc.
571 UnicodeString
*fQuarters
;
572 int32_t fQuartersCount
;
575 * Short quarters. For example: "Q1", "Q2", etc.
577 UnicodeString
*fShortQuarters
;
578 int32_t fShortQuartersCount
;
581 * Standalone quarter strings. For example: "1st quarter", "2nd quarter", etc.
583 UnicodeString
*fStandaloneQuarters
;
584 int32_t fStandaloneQuartersCount
;
587 * Standalone short quarter strings. For example: "Q1", "Q2", etc.
589 UnicodeString
*fStandaloneShortQuarters
;
590 int32_t fStandaloneShortQuartersCount
;
593 * The format data of all the timezones in this locale.
595 UnicodeString
**fZoneStrings
; // Zone string array set by setZoneStrings
596 UnicodeString
**fLocaleZoneStrings
; // Zone string array created by the locale
597 int32_t fZoneStringsRowCount
;
598 int32_t fZoneStringsColCount
;
600 const ZoneStringFormat
*fZoneStringFormat
;
601 ZoneStringFormat
*fZSFLocal
; // Local ZoneStringFormat instance
602 SafeZoneStringFormatPtr
*fZSFCachePtr
; // Cached ZoneStringFormat
603 Locale fZSFLocale
; // Locale used for getting ZoneStringFormat
606 * Pattern string used for localized time zone GMT format. For example, "GMT{0}"
608 UnicodeString fGmtFormat
;
611 * Pattern strings used for formatting zone offset in a localized time zone GMT string.
613 UnicodeString
*fGmtHourFormats
;
614 int32_t fGmtHourFormatsCount
;
617 GMT_NEGATIVE_HMS
= 0,
625 * Localized date-time pattern characters. For example: use 'u' as 'y'.
627 UnicodeString fLocalPatternChars
;
630 /** valid/actual locale information
631 * these are always ICU locales, so the length should not be a problem
633 char validLocale
[ULOC_FULLNAME_CAPACITY
];
634 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
636 DateFormatSymbols(); // default constructor not implemented
639 * Called by the constructors to actually load data from the resources
641 * @param locale The locale to get symbols for.
642 * @param type Calendar Type (as from Calendar::getType())
643 * @param status Input/output parameter, set to success or
644 * failure code upon return.
645 * @param useLastResortData determine if use last resort data
647 void initializeData(const Locale
&, const char *type
, UErrorCode
& status
, UBool useLastResortData
= FALSE
);
650 * Copy or alias an array in another object, as appropriate.
652 * @param dstArray the copy destination array.
653 * @param dstCount fill in with the lenth of 'dstArray'.
654 * @param srcArray the source array to be copied.
655 * @param srcCount the length of items to be copied from the 'srcArray'.
657 static void assignArray(UnicodeString
*& dstArray
,
659 const UnicodeString
* srcArray
,
663 * Return true if the given arrays' contents are equal, or if the arrays are
664 * identical (pointers are equal).
666 * @param array1 one array to be compared with.
667 * @param array2 another array to be compared with.
668 * @param count the length of items to be copied.
669 * @return true if the given arrays' contents are equal, or if the arrays are
670 * identical (pointers are equal).
672 static UBool
arrayCompare(const UnicodeString
* array1
,
673 const UnicodeString
* array2
,
677 * Create a copy, in fZoneStrings, of the given zone strings array. The
678 * member variables fZoneStringsRowCount and fZoneStringsColCount should be
679 * set already by the caller.
681 void createZoneStrings(const UnicodeString
*const * otherStrings
);
684 * Delete all the storage owned by this object.
689 * Copy all of the other's data to this.
690 * @param other the object to be copied.
692 void copyData(const DateFormatSymbols
& other
);
696 * Returns a ZoneStringFormat, used only by SimpleDateFormat for now.
698 const ZoneStringFormat
* getZoneStringFormat(void) const;
701 * Create a ZoneStringFormat by locale if not yet availble
703 void initZoneStringFormat(void);
706 * Create zone strings array by locale if not yet available
708 void initZoneStringsArray(void);
711 * Delete just the zone strings.
713 void disposeZoneStrings(void);
718 #endif /* #if !UCONFIG_NO_FORMATTING */