tznames.h 16 KB

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