tznames.h 17 KB

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