2 ********************************************************************************
3 * Copyright (C) 1997-2006, 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/uobject.h"
26 #include "unicode/locid.h"
27 #include "unicode/ures.h"
31 * \brief C++ API: Symbols for formatting dates.
36 /* forward declaration */
37 class SimpleDateFormat
;
41 * DateFormatSymbols is a public class for encapsulating localizable date-time
42 * formatting data -- including timezone data. DateFormatSymbols is used by
43 * DateFormat and SimpleDateFormat.
45 * Rather than first creating a DateFormatSymbols to get a date-time formatter
46 * by using a SimpleDateFormat constructor, clients are encouraged to create a
47 * date-time formatter using the getTimeInstance(), getDateInstance(), or
48 * getDateTimeInstance() method in DateFormat. Each of these methods can return a
49 * date/time formatter initialized with a default format pattern along with the
50 * date-time formatting data for a given or default locale. After a formatter is
51 * created, clients may modify the format pattern using the setPattern function
52 * as so desired. For more information on using these formatter factory
53 * functions, see DateFormat.
55 * If clients decide to create a date-time formatter with a particular format
56 * pattern and locale, they can do so with new SimpleDateFormat(aPattern,
57 * new DateFormatSymbols(aLocale)). This will load the appropriate date-time
58 * formatting data from the locale.
60 * DateFormatSymbols objects are clonable. When clients obtain a
61 * DateFormatSymbols object, they can feel free to modify the date-time
62 * formatting data as necessary. For instance, clients can
63 * replace the localized date-time format pattern characters with the ones that
64 * they feel easy to remember. Or they can change the representative cities
65 * originally picked by default to using their favorite ones.
67 * DateFormatSymbols are not expected to be subclassed. Data for a calendar is
68 * loaded out of resource bundles. The 'type' parameter indicates the type of
69 * calendar, for example, "gregorian" or "japanese". If the type is not gregorian
70 * (or NULL, or an empty string) then the type is appended to the resource name,
71 * for example, 'Eras_japanese' instead of 'Eras'. If the resource 'Eras_japanese' did
72 * not exist (even in root), then this class will fall back to just 'Eras', that is,
73 * Gregorian data. Therefore, the calendar implementor MUST ensure that the root
74 * locale at least contains any resources that are to be particularized for the
77 class U_I18N_API DateFormatSymbols
: public UObject
{
80 * Construct a DateFormatSymbols object by loading format data from
81 * resources for the default locale, in the default calendar (Gregorian).
83 * NOTE: This constructor will never fail; if it cannot get resource
84 * data for the default locale, it will return a last-resort object
85 * based on hard-coded strings.
87 * @param status Status code. Failure
88 * results if the resources for the default cannot be
89 * found or cannot be loaded
92 DateFormatSymbols(UErrorCode
& status
);
95 * Construct a DateFormatSymbols object by loading format data from
96 * resources for the given locale, in the default calendar (Gregorian).
98 * @param locale Locale to load format data from.
99 * @param status Status code. Failure
100 * results if the resources for the locale cannot be
101 * found or cannot be loaded
104 DateFormatSymbols(const Locale
& locale
,
108 * Construct a DateFormatSymbols object by loading format data from
109 * resources for the default locale, in the default calendar (Gregorian).
111 * NOTE: This constructor will never fail; if it cannot get resource
112 * data for the default locale, it will return a last-resort object
113 * based on hard-coded strings.
115 * @param type Type of calendar (as returned by Calendar::getType).
116 * Will be used to access the correct set of strings.
117 * (NULL or empty string defaults to "gregorian".)
118 * @param status Status code. Failure
119 * results if the resources for the default cannot be
120 * found or cannot be loaded
123 DateFormatSymbols(const char *type
, UErrorCode
& status
);
126 * Construct a DateFormatSymbols object by loading format data from
127 * resources for the given locale, in the default calendar (Gregorian).
129 * @param locale Locale to load format data from.
130 * @param type Type of calendar (as returned by Calendar::getType).
131 * Will be used to access the correct set of strings.
132 * (NULL or empty string defaults to "gregorian".)
133 * @param status Status code. Failure
134 * results if the resources for the locale cannot be
135 * found or cannot be loaded
138 DateFormatSymbols(const Locale
& locale
,
146 DateFormatSymbols(const DateFormatSymbols
&);
149 * Assignment operator.
152 DateFormatSymbols
& operator=(const DateFormatSymbols
&);
155 * Destructor. This is nonvirtual because this class is not designed to be
159 virtual ~DateFormatSymbols();
162 * Return true if another object is semantically equal to this one.
164 * @param other the DateFormatSymbols object to be compared with.
165 * @return true if other is semantically equal to this.
168 UBool
operator==(const DateFormatSymbols
& other
) const;
171 * Return true if another object is semantically unequal to this one.
173 * @param other the DateFormatSymbols object to be compared with.
174 * @return true if other is semantically unequal to this.
177 UBool
operator!=(const DateFormatSymbols
& other
) const { return !operator==(other
); }
180 * Gets era strings. For example: "AD" and "BC".
182 * @param count Filled in with length of the array.
183 * @return the era strings.
186 const UnicodeString
* getEras(int32_t& count
) const;
189 * Sets era strings. For example: "AD" and "BC".
190 * @param eras Array of era strings (DateFormatSymbols retains ownership.)
191 * @param count Filled in with length of the array.
194 void setEras(const UnicodeString
* eras
, int32_t count
);
197 * Gets era name strings. For example: "Anno Domini" and "Before Christ".
199 * @param count Filled in with length of the array.
200 * @return the era name strings.
203 const UnicodeString
* getEraNames(int32_t& count
) const;
206 * Sets era name strings. For example: "Anno Domini" and "Before Christ".
207 * @param eraNames Array of era name strings (DateFormatSymbols retains ownership.)
208 * @param count Filled in with length of the array.
211 void setEraNames(const UnicodeString
* eraNames
, int32_t count
);
214 * Gets month strings. For example: "January", "February", etc.
215 * @param count Filled in with length of the array.
216 * @return the month strings. (DateFormatSymbols retains ownership.)
219 const UnicodeString
* getMonths(int32_t& count
) const;
222 * Sets month strings. For example: "January", "February", etc.
224 * @param months the new month strings. (not adopted; caller retains ownership)
225 * @param count Filled in with length of the array.
228 void setMonths(const UnicodeString
* months
, int32_t count
);
231 * Gets short month strings. For example: "Jan", "Feb", etc.
233 * @param count Filled in with length of the array.
234 * @return the short month strings. (DateFormatSymbols retains ownership.)
237 const UnicodeString
* getShortMonths(int32_t& count
) const;
240 * Sets short month strings. For example: "Jan", "Feb", etc.
241 * @param count Filled in with length of the array.
242 * @param shortMonths the new short month strings. (not adopted; caller retains ownership)
245 void setShortMonths(const UnicodeString
* shortMonths
, int32_t count
);
248 * Selector for date formatting context
258 * Selector for date formatting width
269 * Gets month strings by width and context. For example: "January", "February", etc.
270 * @param count Filled in with length of the array.
271 * @param context The formatting context, either FORMAT or STANDALONE
272 * @param width The width of returned strings, either WIDE, ABBREVIATED, or NARROW.
273 * @return the month strings. (DateFormatSymbols retains ownership.)
276 const UnicodeString
* getMonths(int32_t& count
, DtContextType context
, DtWidthType width
) const;
279 * Sets month strings by width and context. For example: "January", "February", etc.
281 * @param months The new month strings. (not adopted; caller retains ownership)
282 * @param count Filled in with length of the array.
283 * @param context The formatting context, either FORMAT or STANDALONE
284 * @param width The width of returned strings, either WIDE, ABBREVIATED, or NARROW.
287 void setMonths(const UnicodeString
* months
, int32_t count
, DtContextType context
, DtWidthType width
);
290 * Gets weekday strings. For example: "Sunday", "Monday", etc.
291 * @param count Filled in with length of the array.
292 * @return the weekday strings. (DateFormatSymbols retains ownership.)
295 const UnicodeString
* getWeekdays(int32_t& count
) const;
299 * Sets weekday strings. For example: "Sunday", "Monday", etc.
300 * @param weekdays the new weekday strings. (not adopted; caller retains ownership)
301 * @param count Filled in with length of the array.
304 void setWeekdays(const UnicodeString
* weekdays
, int32_t count
);
307 * Gets short weekday strings. For example: "Sun", "Mon", etc.
308 * @param count Filled in with length of the array.
309 * @return the short weekday strings. (DateFormatSymbols retains ownership.)
312 const UnicodeString
* getShortWeekdays(int32_t& count
) const;
315 * Sets short weekday strings. For example: "Sun", "Mon", etc.
316 * @param shortWeekdays the new short weekday strings. (not adopted; caller retains ownership)
317 * @param count Filled in with length of the array.
320 void setShortWeekdays(const UnicodeString
* shortWeekdays
, int32_t count
);
323 * Gets weekday strings by width and context. For example: "Sunday", "Monday", etc.
324 * @param count Filled in with length of the array.
325 * @param context The formatting context, either FORMAT or STANDALONE
326 * @param width The width of returned strings, either WIDE, ABBREVIATED, or NARROW
327 * @return the month strings. (DateFormatSymbols retains ownership.)
330 const UnicodeString
* getWeekdays(int32_t& count
, DtContextType context
, DtWidthType width
) const;
333 * Sets weekday strings by width and context. For example: "Sunday", "Monday", etc.
334 * @param weekdays The new weekday strings. (not adopted; caller retains ownership)
335 * @param count Filled in with length of the array.
336 * @param context The formatting context, either FORMAT or STANDALONE
337 * @param width The width of returned strings, either WIDE, ABBREVIATED, or NARROW
340 void setWeekdays(const UnicodeString
* weekdays
, int32_t count
, DtContextType context
, DtWidthType width
);
343 * Gets quarter strings by width and context. For example: "1st Quarter", "2nd Quarter", 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 or ABBREVIATED. There
347 * are no NARROW quarters.
348 * @return the quarter strings. (DateFormatSymbols retains ownership.)
351 const UnicodeString
* getQuarters(int32_t& count
, DtContextType context
, DtWidthType width
) const;
354 * Sets quarter strings by width and context. For example: "1st Quarter", "2nd Quarter", etc.
356 * @param quarters The new quarter strings. (not adopted; caller retains ownership)
357 * @param count Filled in with length of the array.
358 * @param context The formatting context, either FORMAT or STANDALONE
359 * @param width The width of returned strings, either WIDE or ABBREVIATED. There
360 * are no NARROW quarters.
363 void setQuarters(const UnicodeString
* quarters
, int32_t count
, DtContextType context
, DtWidthType width
);
366 * Gets AM/PM strings. For example: "AM" and "PM".
367 * @param count Filled in with length of the array.
368 * @return the weekday strings. (DateFormatSymbols retains ownership.)
371 const UnicodeString
* getAmPmStrings(int32_t& count
) const;
374 * Sets ampm strings. For example: "AM" and "PM".
375 * @param ampms the new ampm strings. (not adopted; caller retains ownership)
376 * @param count Filled in with length of the array.
379 void setAmPmStrings(const UnicodeString
* ampms
, int32_t count
);
382 * Gets timezone strings. These strings are stored in a 2-dimensional array.
383 * @param rowCount Output param to receive number of rows.
384 * @param columnCount Output param to receive number of columns.
385 * @return The timezone strings as a 2-d array. (DateFormatSymbols retains ownership.)
386 * @deprecated ICU 3.6
388 const UnicodeString
** getZoneStrings(int32_t& rowCount
, int32_t& columnCount
) const;
391 * Sets timezone strings. These strings are stored in a 2-dimensional array.
392 * @param strings The timezone strings as a 2-d array to be copied. (not adopted; caller retains ownership)
393 * @param rowCount The number of rows (count of first index).
394 * @param columnCount The number of columns (count of second index).
397 void setZoneStrings(const UnicodeString
* const* strings
, int32_t rowCount
, int32_t columnCount
);
400 * Get the non-localized date-time pattern characters.
401 * @return the non-localized date-time pattern characters
404 static const UChar
* U_EXPORT2
getPatternUChars(void);
407 * Gets localized date-time pattern characters. For example: 'u', 't', etc.
408 * @param result Output param which will receive the localized date-time pattern characters.
409 * @return A reference to 'result'.
412 UnicodeString
& getLocalPatternChars(UnicodeString
& result
) const;
415 * Sets localized date-time pattern characters. For example: 'u', 't', etc.
416 * @param newLocalPatternChars the new localized date-time
417 * pattern characters.
420 void setLocalPatternChars(const UnicodeString
& newLocalPatternChars
);
423 * Returns the locale for this object. Two flavors are available:
424 * valid and actual locale.
427 Locale
getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const;
430 * ICU "poor man's RTTI", returns a UClassID for the actual class.
434 virtual UClassID
getDynamicClassID() const;
437 * ICU "poor man's RTTI", returns a UClassID for this class.
441 static UClassID U_EXPORT2
getStaticClassID();
444 * The translation type of the translated zone strings
447 enum TimeZoneTranslationType
{
448 TIMEZONE_SHORT_GENERIC
,
449 TIMEZONE_SHORT_STANDARD
,
450 TIMEZONE_SHORT_DAYLIGHT
,
451 TIMEZONE_LONG_GENERIC
,
452 TIMEZONE_LONG_STANDARD
,
453 TIMEZONE_LONG_DAYLIGHT
,
454 TIMEZONE_EXEMPLAR_CITY
,
459 * Creates an enumeration of time zone IDs. The object is owned by the caller and should delete it after use.
460 * The time zone IDs are just for programmatic lookup. NOT LOCALIZED!!!
461 * @param status Input/output parameter, set to success or
462 * failure code upon return.
463 * @return A new StringEnumeration object
466 virtual StringEnumeration
* createZoneStringIDs(UErrorCode
&status
);
469 * Gets timezone string give the key and translation type
470 * @param ID The ID of zone strings, e.g: "America/Los_Angeles".
471 * The time zone ID is for programmatic lookup.
472 * @param type The translation type requested
473 * @param result Output parameter to recieve the translation string
474 * @param status Input/output parameter, set to success or
475 * failure code upon return.
476 * @return the input UnicodeString parameter for chaining
479 UnicodeString
& getZoneString(const UnicodeString
&ID
, const TimeZoneTranslationType type
, UnicodeString
&result
, UErrorCode
&status
);
482 * Sets timezone string for the given the ID and translation type
483 * @param ID The ID of zone strings, e.g: "America/Los_Angeles".
484 * The time zone ID is for programmatic lookup.
485 * @param type The translation type to set the value for
486 * @param value The string with which current translation needs to be replaced
487 * @param status Input/output parameter, set to success or
490 void setZoneString(const UnicodeString
&ID
, const TimeZoneTranslationType type
, const UnicodeString
&value
, UErrorCode
&status
);
494 friend class SimpleDateFormat
;
495 friend class DateFormatSymbolsSingleSetter
; // see udat.cpp
498 * Era strings. For example: "AD" and "BC".
500 UnicodeString
* fEras
;
504 * Era name strings. For example: "Anno Domini" and "Before Christ".
506 UnicodeString
* fEraNames
;
507 int32_t fEraNamesCount
;
510 * Month strings. For example: "January", "February", etc.
512 UnicodeString
* fMonths
;
513 int32_t fMonthsCount
;
516 * Short month strings. For example: "Jan", "Feb", etc.
518 UnicodeString
* fShortMonths
;
519 int32_t fShortMonthsCount
;
522 * Narrow month strings. For example: "J", "F", etc.
524 UnicodeString
* fNarrowMonths
;
525 int32_t fNarrowMonthsCount
;
528 * Standalone Month strings. For example: "January", "February", etc.
530 UnicodeString
* fStandaloneMonths
;
531 int32_t fStandaloneMonthsCount
;
534 * Standalone Short month strings. For example: "Jan", "Feb", etc.
536 UnicodeString
* fStandaloneShortMonths
;
537 int32_t fStandaloneShortMonthsCount
;
540 * Standalone Narrow month strings. For example: "J", "F", etc.
542 UnicodeString
* fStandaloneNarrowMonths
;
543 int32_t fStandaloneNarrowMonthsCount
;
546 * Weekday strings. For example: "Sunday", "Monday", etc.
548 UnicodeString
* fWeekdays
;
549 int32_t fWeekdaysCount
;
552 * Short weekday strings. For example: "Sun", "Mon", etc.
554 UnicodeString
* fShortWeekdays
;
555 int32_t fShortWeekdaysCount
;
558 * Narrow weekday strings. For example: "Sun", "Mon", etc.
560 UnicodeString
* fNarrowWeekdays
;
561 int32_t fNarrowWeekdaysCount
;
564 * Standalone Weekday strings. For example: "Sunday", "Monday", etc.
566 UnicodeString
* fStandaloneWeekdays
;
567 int32_t fStandaloneWeekdaysCount
;
570 * Standalone Short weekday strings. For example: "Sun", "Mon", etc.
572 UnicodeString
* fStandaloneShortWeekdays
;
573 int32_t fStandaloneShortWeekdaysCount
;
576 * Standalone Narrow weekday strings. For example: "Sun", "Mon", etc.
578 UnicodeString
* fStandaloneNarrowWeekdays
;
579 int32_t fStandaloneNarrowWeekdaysCount
;
582 * Ampm strings. For example: "AM" and "PM".
584 UnicodeString
* fAmPms
;
588 * Quarter strings. For example: "1st quarter", "2nd quarter", etc.
590 UnicodeString
*fQuarters
;
591 int32_t fQuartersCount
;
594 * Short quarters. For example: "Q1", "Q2", etc.
596 UnicodeString
*fShortQuarters
;
597 int32_t fShortQuartersCount
;
600 * Standalone quarter strings. For example: "1st quarter", "2nd quarter", etc.
602 UnicodeString
*fStandaloneQuarters
;
603 int32_t fStandaloneQuartersCount
;
606 * Standalone short quarter strings. For example: "Q1", "Q2", etc.
608 UnicodeString
*fStandaloneShortQuarters
;
609 int32_t fStandaloneShortQuartersCount
;
612 * The format data of all the timezones in this locale.
614 UnicodeString
** fZoneStrings
;
615 int32_t fZoneStringsRowCount
;
616 int32_t fZoneStringsColCount
;
617 StringEnumeration
* fZoneIDEnumeration
;
618 Hashtable
* fZoneStringsHash
;
619 UResourceBundle
* fResourceBundle
;
622 * Localized date-time pattern characters. For example: use 'u' as 'y'.
624 UnicodeString fLocalPatternChars
;
627 /** valid/actual locale information
628 * these are always ICU locales, so the length should not be a problem
630 char validLocale
[ULOC_FULLNAME_CAPACITY
];
631 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
633 DateFormatSymbols(); // default constructor not implemented
636 * Called by the constructors to actually load data from the resources
638 * @param locale The locale to get symbols for.
639 * @param type Calendar Type (as from Calendar::getType())
640 * @param status Input/output parameter, set to success or
641 * failure code upon return.
642 * @param useLastResortData determine if use last resort data
644 void initializeData(const Locale
&, const char *type
, UErrorCode
& status
, UBool useLastResortData
= FALSE
);
647 * Copy or alias an array in another object, as appropriate.
649 * @param dstArray the copy destination array.
650 * @param dstCount fill in with the lenth of 'dstArray'.
651 * @param srcArray the source array to be copied.
652 * @param srcCount the length of items to be copied from the 'srcArray'.
654 static void assignArray(UnicodeString
*& dstArray
,
656 const UnicodeString
* srcArray
,
660 * Return true if the given arrays' contents are equal, or if the arrays are
661 * identical (pointers are equal).
663 * @param array1 one array to be compared with.
664 * @param array2 another array to be compared with.
665 * @param count the length of items to be copied.
666 * @return true if the given arrays' contents are equal, or if the arrays are
667 * identical (pointers are equal).
669 static UBool
arrayCompare(const UnicodeString
* array1
,
670 const UnicodeString
* array2
,
674 * Create a copy, in fZoneStrings, of the given zone strings array. The
675 * member variables fZoneStringsRowCount and fZoneStringsColCount should be
676 * set already by the caller.
678 void createZoneStrings(const UnicodeString
*const * otherStrings
);
681 * Package private: used by SimpleDateFormat
682 * Gets the index for the given time zone ID to obtain the timezone
683 * strings for formatting. The time zone ID is just for programmatic
684 * lookup. NOT LOCALIZED!!!
685 * @param ID the given time zone ID.
686 * @return the index of the given time zone ID. Returns -1 if
687 * the given time zone ID can't be located in the DateFormatSymbols object.
688 * @see java.util.SimpleTimeZone
690 int32_t getZoneIndex(const UnicodeString
& ID
) const;
692 // Internal method; see source for documentation
693 int32_t _getZoneIndex(const UnicodeString
& id
) const;
696 * Delete all the storage owned by this object.
701 * Copy all of the other's data to this.
702 * @param other the object to be copied.
704 void copyData(const DateFormatSymbols
& other
);
707 * Delete just the zone strings.
709 void disposeZoneStrings(void);
711 * Initializes the zoneStrings hash and keys StringEnumeration after reading the zoneStrings resource
713 void initZoneStrings(UErrorCode
&status
);
715 * initialzes the zoneStrings has and keys enumeration after reading the strings[][]. Required for backwards
716 * compatibility of setZoneStrings method
718 void initZoneStrings(const UnicodeString
** strings
, int32_t rowCount
, int32_t collumnCount
, UErrorCode
& status
);
720 * initialization of the fZoneStrings data member
722 void initZoneStringsArray(UErrorCode
& status
);
724 * Creates a deep clone of the Hashtable
726 Hashtable
* createZoneStringsHash(const Hashtable
* otherHash
);
729 * Fetches the key from the hashtable for a given ID.
730 * e.g: for a given ID such as PST returns "Americal/Los_Angeles"
731 * Used by SimpleDateFormat class.
732 * @param ID The id of the time zone for which the key needs to be fetched
733 * @param result Output parameter to recieve the key.
734 * @return the input UnicodeString object for chaining
736 UnicodeString
& getZoneID(const UnicodeString
& zid
, UnicodeString
& result
, UErrorCode
& status
);
739 * Fetches the zone type and zone string from the hashtable for a given key.
740 * e.g: for key: "Americal/Los_Angeles", text: "2004/1/1 PT 1:00" and start:9
741 * returns TIMEZONE_SHORT_GENERIC and "PT".
742 * Used by SimpleDateFormat class.
743 * @param ID the name of the timezone
744 * @param text the string containing the time zone translation
745 * @param start The position in string where time zone string starts
746 * @param type output parameter to recieve the type of time zone string
747 * @param value output parameter to recieve the the acutal time zone string
749 void getZoneType(const UnicodeString
& zid
, const UnicodeString
& text
, int32_t start
, TimeZoneTranslationType
& type
, UnicodeString
& value
, UErrorCode
& status
);
752 * Fetches the zone type and zone string from the hashtable by cycling through all elements in the hashtable.
753 * e.g: text: "2004/1/1 PT 1:00" and start:9
754 * returns "Americal/Los_Angeles", TIMEZONE_SHORT_GENERIC and "PT". Used by SimpleDateFormat class.
755 * Used by SimpleDateFormat class.
756 * @param ID output parameter to recieve the key name of the time zone
757 * @param text the string containing the time zone translation
758 * @param start The position in string where time zone string starts
759 * @param type output parameter to recieve the type of time zone string
760 * @param value output parameter to recieve the the acutal time zone string
761 * @param status output parameter to recive the error information
763 void findZoneIDTypeValue(UnicodeString
& zid
, const UnicodeString
& text
, int32_t start
, TimeZoneTranslationType
& type
, UnicodeString
& value
, UErrorCode
& status
);
769 #endif /* #if !UCONFIG_NO_FORMATTING */