]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/tznames.h
ICU-59180.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / tznames.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2011-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 */
9 #ifndef __TZNAMES_H
10 #define __TZNAMES_H
11
12 /**
13 * \file
14 * \brief C++ API: TimeZoneNames
15 */
16 #include "unicode/utypes.h"
17
18 #if !UCONFIG_NO_FORMATTING
19
20 #include "unicode/uloc.h"
21 #include "unicode/unistr.h"
22
23 U_CDECL_BEGIN
24
25 /**
26 * Constants for time zone display name types.
27 * @stable ICU 50
28 */
29 typedef enum UTimeZoneNameType {
30 /**
31 * Unknown display name type.
32 * @stable ICU 50
33 */
34 UTZNM_UNKNOWN = 0x00,
35 /**
36 * Long display name, such as "Eastern Time".
37 * @stable ICU 50
38 */
39 UTZNM_LONG_GENERIC = 0x01,
40 /**
41 * Long display name for standard time, such as "Eastern Standard Time".
42 * @stable ICU 50
43 */
44 UTZNM_LONG_STANDARD = 0x02,
45 /**
46 * Long display name for daylight saving time, such as "Eastern Daylight Time".
47 * @stable ICU 50
48 */
49 UTZNM_LONG_DAYLIGHT = 0x04,
50 /**
51 * Short display name, such as "ET".
52 * @stable ICU 50
53 */
54 UTZNM_SHORT_GENERIC = 0x08,
55 /**
56 * Short display name for standard time, such as "EST".
57 * @stable ICU 50
58 */
59 UTZNM_SHORT_STANDARD = 0x10,
60 /**
61 * Short display name for daylight saving time, such as "EDT".
62 * @stable ICU 50
63 */
64 UTZNM_SHORT_DAYLIGHT = 0x20,
65 /**
66 * Exemplar location name, such as "Los Angeles".
67 * @stable ICU 51
68 */
69 UTZNM_EXEMPLAR_LOCATION = 0x40
70 } UTimeZoneNameType;
71
72 U_CDECL_END
73
74 #if U_SHOW_CPLUSPLUS_API
75 U_NAMESPACE_BEGIN
76
77 class UVector;
78 struct MatchInfo;
79
80 /**
81 * <code>TimeZoneNames</code> is an abstract class representing the time zone display name data model defined
82 * by <a href="http://www.unicode.org/reports/tr35/">UTS#35 Unicode Locale Data Markup Language (LDML)</a>.
83 * The model defines meta zone, which is used for storing a set of display names. A meta zone can be shared
84 * by multiple time zones. Also a time zone may have multiple meta zone historic mappings.
85 * <p>
86 * For example, people in the United States refer the zone used by the east part of North America as "Eastern Time".
87 * The tz database contains multiple time zones "America/New_York", "America/Detroit", "America/Montreal" and some
88 * others that belong to "Eastern Time". However, assigning different display names to these time zones does not make
89 * much sense for most of people.
90 * <p>
91 * In <a href="http://cldr.unicode.org/">CLDR</a> (which uses LDML for representing locale data), the display name
92 * "Eastern Time" is stored as long generic display name of a meta zone identified by the ID "America_Eastern".
93 * Then, there is another table maintaining the historic mapping to meta zones for each time zone. The time zones in
94 * the above example ("America/New_York", "America/Detroit"...) are mapped to the meta zone "America_Eastern".
95 * <p>
96 * Sometimes, a time zone is mapped to a different time zone in the past. For example, "America/Indiana/Knox"
97 * had been moving "Eastern Time" and "Central Time" back and forth. Therefore, it is necessary that time zone
98 * to meta zones mapping data are stored by date range.
99 *
100 * <p><b>Note:</b>
101 * The methods in this class assume that time zone IDs are already canonicalized. For example, you may not get proper
102 * result returned by a method with time zone ID "America/Indiana/Indianapolis", because it's not a canonical time zone
103 * ID (the canonical time zone ID for the time zone is "America/Indianapolis". See
104 * {@link TimeZone#getCanonicalID(const UnicodeString& id, UnicodeString& canonicalID, UErrorCode& status)} about ICU
105 * canonical time zone IDs.
106 *
107 * <p>
108 * In CLDR, most of time zone display names except location names are provided through meta zones. But a time zone may
109 * have a specific name that is not shared with other time zones.
110 *
111 * For example, time zone "Europe/London" has English long name for standard time "Greenwich Mean Time", which is also
112 * shared with other time zones. However, the long name for daylight saving time is "British Summer Time", which is only
113 * used for "Europe/London".
114 *
115 * <p>
116 * {@link #getTimeZoneDisplayName} is designed for accessing a name only used by a single time zone.
117 * But is not necessarily mean that a subclass implementation use the same model with CLDR. A subclass implementation
118 * may provide time zone names only through {@link #getTimeZoneDisplayName}, or only through {@link #getMetaZoneDisplayName},
119 * or both.
120 *
121 * <p>
122 * The default <code>TimeZoneNames</code> implementation returned by {@link #createInstance}
123 * uses the locale data imported from CLDR. In CLDR, set of meta zone IDs and mappings between zone IDs and meta zone
124 * IDs are shared by all locales. Therefore, the behavior of {@link #getAvailableMetaZoneIDs},
125 * {@link #getMetaZoneID}, and {@link #getReferenceZoneID} won't be changed no matter
126 * what locale is used for getting an instance of <code>TimeZoneNames</code>.
127 *
128 * @stable ICU 50
129 */
130 class U_I18N_API TimeZoneNames : public UObject {
131 public:
132 /**
133 * Destructor.
134 * @stable ICU 50
135 */
136 virtual ~TimeZoneNames();
137
138 /**
139 * Return true if the given TimeZoneNames objects are semantically equal.
140 * @param other the object to be compared with.
141 * @return Return TRUE if the given Format objects are semantically equal.
142 * @stable ICU 50
143 */
144 virtual UBool operator==(const TimeZoneNames& other) const = 0;
145
146 /**
147 * Return true if the given TimeZoneNames objects are not semantically
148 * equal.
149 * @param other the object to be compared with.
150 * @return Return TRUE if the given Format objects are not semantically equal.
151 * @stable ICU 50
152 */
153 UBool operator!=(const TimeZoneNames& other) const { return !operator==(other); }
154
155 /**
156 * Clone this object polymorphically. The caller is responsible
157 * for deleting the result when done.
158 * @return A copy of the object
159 * @stable ICU 50
160 */
161 virtual TimeZoneNames* clone() const = 0;
162
163 /**
164 * Returns an instance of <code>TimeZoneNames</code> for the specified locale.
165 *
166 * @param locale The locale.
167 * @param status Receives the status.
168 * @return An instance of <code>TimeZoneNames</code>
169 * @stable ICU 50
170 */
171 static TimeZoneNames* U_EXPORT2 createInstance(const Locale& locale, UErrorCode& status);
172
173 /**
174 * Returns an instance of <code>TimeZoneNames</code> containing only short specific
175 * zone names (SHORT_STANDARD and SHORT_DAYLIGHT),
176 * compatible with the IANA tz database's zone abbreviations (not localized).
177 * <br>
178 * Note: The input locale is used for resolving ambiguous names (e.g. "IST" is parsed
179 * as Israel Standard Time for Israel, while it is parsed as India Standard Time for
180 * all other regions). The zone names returned by this instance are not localized.
181 * @stable ICU 54
182 */
183 static TimeZoneNames* U_EXPORT2 createTZDBInstance(const Locale& locale, UErrorCode& status);
184
185 /**
186 * Returns an enumeration of all available meta zone IDs.
187 * @param status Receives the status.
188 * @return an enumeration object, owned by the caller.
189 * @stable ICU 50
190 */
191 virtual StringEnumeration* getAvailableMetaZoneIDs(UErrorCode& status) const = 0;
192
193 /**
194 * Returns an enumeration of all available meta zone IDs used by the given time zone.
195 * @param tzID The canoical tiem zone ID.
196 * @param status Receives the status.
197 * @return an enumeration object, owned by the caller.
198 * @stable ICU 50
199 */
200 virtual StringEnumeration* getAvailableMetaZoneIDs(const UnicodeString& tzID, UErrorCode& status) const = 0;
201
202 /**
203 * Returns the meta zone ID for the given canonical time zone ID at the given date.
204 * @param tzID The canonical time zone ID.
205 * @param date The date.
206 * @param mzID Receives the meta zone ID for the given time zone ID at the given date. If the time zone does not have a
207 * corresponding meta zone at the given date or the implementation does not support meta zones, "bogus" state
208 * is set.
209 * @return A reference to the result.
210 * @stable ICU 50
211 */
212 virtual UnicodeString& getMetaZoneID(const UnicodeString& tzID, UDate date, UnicodeString& mzID) const = 0;
213
214 /**
215 * Returns the reference zone ID for the given meta zone ID for the region.
216 *
217 * Note: Each meta zone must have a reference zone associated with a special region "001" (world).
218 * Some meta zones may have region specific reference zone IDs other than the special region
219 * "001". When a meta zone does not have any region specific reference zone IDs, this method
220 * return the reference zone ID for the special region "001" (world).
221 *
222 * @param mzID The meta zone ID.
223 * @param region The region.
224 * @param tzID Receives the reference zone ID ("golden zone" in the LDML specification) for the given time zone ID for the
225 * region. If the meta zone is unknown or the implementation does not support meta zones, "bogus" state
226 * is set.
227 * @return A reference to the result.
228 * @stable ICU 50
229 */
230 virtual UnicodeString& getReferenceZoneID(const UnicodeString& mzID, const char* region, UnicodeString& tzID) const = 0;
231
232 /**
233 * Returns the display name of the meta zone.
234 * @param mzID The meta zone ID.
235 * @param type The display name type. See {@link #UTimeZoneNameType}.
236 * @param name Receives the display name of the meta zone. When this object does not have a localized display name for the given
237 * meta zone with the specified type or the implementation does not provide any display names associated
238 * with meta zones, "bogus" state is set.
239 * @return A reference to the result.
240 * @stable ICU 50
241 */
242 virtual UnicodeString& getMetaZoneDisplayName(const UnicodeString& mzID, UTimeZoneNameType type, UnicodeString& name) const = 0;
243
244 /**
245 * Returns the display name of the time zone. Unlike {@link #getDisplayName},
246 * this method does not get a name from a meta zone used by the time zone.
247 * @param tzID The canonical time zone ID.
248 * @param type The display name type. See {@link #UTimeZoneNameType}.
249 * @param name Receives the display name for the time zone. When this object does not have a localized display name for the given
250 * time zone with the specified type, "bogus" state is set.
251 * @return A reference to the result.
252 * @stable ICU 50
253 */
254 virtual UnicodeString& getTimeZoneDisplayName(const UnicodeString& tzID, UTimeZoneNameType type, UnicodeString& name) const = 0;
255
256 /**
257 * Returns the exemplar location name for the given time zone. When this object does not have a localized location
258 * name, the default implementation may still returns a programmatically generated name with the logic described
259 * below.
260 * <ol>
261 * <li>Check if the ID contains "/". If not, return null.
262 * <li>Check if the ID does not start with "Etc/" or "SystemV/". If it does, return null.
263 * <li>Extract a substring after the last occurrence of "/".
264 * <li>Replace "_" with " ".
265 * </ol>
266 * For example, "New York" is returned for the time zone ID "America/New_York" when this object does not have the
267 * localized location name.
268 *
269 * @param tzID The canonical time zone ID
270 * @param name Receives the exemplar location name for the given time zone, or "bogus" state is set when a localized
271 * location name is not available and the fallback logic described above cannot extract location from the ID.
272 * @return A reference to the result.
273 * @stable ICU 50
274 */
275 virtual UnicodeString& getExemplarLocationName(const UnicodeString& tzID, UnicodeString& name) const;
276
277 /**
278 * Returns the display name of the time zone at the given date.
279 * <p>
280 * <b>Note:</b> This method calls the subclass's {@link #getTimeZoneDisplayName} first. When the
281 * result is bogus, this method calls {@link #getMetaZoneID} to get the meta zone ID mapped from the
282 * time zone, then calls {@link #getMetaZoneDisplayName}.
283 *
284 * @param tzID The canonical time zone ID.
285 * @param type The display name type. See {@link #UTimeZoneNameType}.
286 * @param date The date.
287 * @param name Receives the display name for the time zone at the given date. When this object does not have a localized display
288 * name for the time zone with the specified type and date, "bogus" state is set.
289 * @return A reference to the result.
290 * @stable ICU 50
291 */
292 virtual UnicodeString& getDisplayName(const UnicodeString& tzID, UTimeZoneNameType type, UDate date, UnicodeString& name) const;
293
294 /**
295 * @internal For specific users only until proposed publicly.
296 * @deprecated This API is ICU internal only.
297 */
298 virtual void loadAllDisplayNames(UErrorCode& status);
299
300 /**
301 * @internal For specific users only until proposed publicly.
302 * @deprecated This API is ICU internal only.
303 */
304 virtual void getDisplayNames(const UnicodeString& tzID, const UTimeZoneNameType types[], int32_t numTypes, UDate date, UnicodeString dest[], UErrorCode& status) const;
305
306 /**
307 * <code>MatchInfoCollection</code> represents a collection of time zone name matches used by
308 * {@link TimeZoneNames#find}.
309 * @internal
310 */
311 class U_I18N_API MatchInfoCollection : public UMemory {
312 public:
313 /**
314 * Constructor.
315 * @internal
316 */
317 MatchInfoCollection();
318 /**
319 * Destructor.
320 * @internal
321 */
322 virtual ~MatchInfoCollection();
323
324 #ifndef U_HIDE_INTERNAL_API
325 /**
326 * Adds a zone match.
327 * @param nameType The name type.
328 * @param matchLength The match length.
329 * @param tzID The time zone ID.
330 * @param status Receives the status
331 * @internal
332 */
333 void addZone(UTimeZoneNameType nameType, int32_t matchLength,
334 const UnicodeString& tzID, UErrorCode& status);
335
336 /**
337 * Adds a meata zone match.
338 * @param nameType The name type.
339 * @param matchLength The match length.
340 * @param mzID The metazone ID.
341 * @param status Receives the status
342 * @internal
343 */
344 void addMetaZone(UTimeZoneNameType nameType, int32_t matchLength,
345 const UnicodeString& mzID, UErrorCode& status);
346
347 /**
348 * Returns the number of entries available in this object.
349 * @return The number of entries.
350 * @internal
351 */
352 int32_t size() const;
353
354 /**
355 * Returns the time zone name type of a match at the specified index.
356 * @param idx The index
357 * @return The time zone name type. If the specified idx is out of range,
358 * it returns UTZNM_UNKNOWN.
359 * @see UTimeZoneNameType
360 * @internal
361 */
362 UTimeZoneNameType getNameTypeAt(int32_t idx) const;
363
364 /**
365 * Returns the match length of a match at the specified index.
366 * @param idx The index
367 * @return The match length. If the specified idx is out of range,
368 * it returns 0.
369 * @internal
370 */
371 int32_t getMatchLengthAt(int32_t idx) const;
372
373 /**
374 * Gets the zone ID of a match at the specified index.
375 * @param idx The index
376 * @param tzID Receives the zone ID.
377 * @return TRUE if the zone ID was set to tzID.
378 * @internal
379 */
380 UBool getTimeZoneIDAt(int32_t idx, UnicodeString& tzID) const;
381
382 /**
383 * Gets the metazone ID of a match at the specified index.
384 * @param idx The index
385 * @param mzID Receives the metazone ID
386 * @return TRUE if the meta zone ID was set to mzID.
387 * @internal
388 */
389 UBool getMetaZoneIDAt(int32_t idx, UnicodeString& mzID) const;
390 #endif /* U_HIDE_INTERNAL_API */
391
392 private:
393 UVector* fMatches; // vector of MatchEntry
394
395 UVector* matches(UErrorCode& status);
396 };
397
398 /**
399 * Finds time zone name prefix matches for the input text at the
400 * given offset and returns a collection of the matches.
401 * @param text The text.
402 * @param start The starting offset within the text.
403 * @param types The set of name types represented by bitwise flags of UTimeZoneNameType enums,
404 * or UTZNM_UNKNOWN for all name types.
405 * @param status Receives the status.
406 * @return A collection of matches (owned by the caller), or NULL if no matches are found.
407 * @see UTimeZoneNameType
408 * @see MatchInfoCollection
409 * @internal
410 */
411 virtual MatchInfoCollection* find(const UnicodeString& text, int32_t start, uint32_t types, UErrorCode& status) const = 0;
412 };
413
414 U_NAMESPACE_END
415 #endif // U_SHOW_CPLUSPLUS_API
416
417 #endif
418 #endif