tznames.h 17 KB

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