tznames.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. *******************************************************************************
  3. * Copyright (C) 2011-2014, 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. #ifndef U_HIDE_DRAFT_API
  157. /**
  158. * Returns an instance of <code>TimeZoneNames</code> containing only short specific
  159. * zone names (SHORT_STANDARD and SHORT_DAYLIGHT),
  160. * compatible with the IANA tz database's zone abbreviations (not localized).
  161. * <br>
  162. * Note: The input locale is used for resolving ambiguous names (e.g. "IST" is parsed
  163. * as Israel Standard Time for Israel, while it is parsed as India Standard Time for
  164. * all other regions). The zone names returned by this instance are not localized.
  165. * @draft ICU 54
  166. */
  167. static TimeZoneNames* U_EXPORT2 createTZDBInstance(const Locale& locale, UErrorCode& status);
  168. #endif /* U_HIDE_DRAFT_API */
  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. * <code>MatchInfoCollection</code> represents a collection of time zone name matches used by
  272. * {@link TimeZoneNames#find}.
  273. * @internal
  274. */
  275. class U_I18N_API MatchInfoCollection : public UMemory {
  276. public:
  277. /**
  278. * Constructor.
  279. * @internal
  280. */
  281. MatchInfoCollection();
  282. /**
  283. * Destructor.
  284. * @internal
  285. */
  286. virtual ~MatchInfoCollection();
  287. #ifndef U_HIDE_INTERNAL_API
  288. /**
  289. * Adds a zone match.
  290. * @param nameType The name type.
  291. * @param matchLength The match length.
  292. * @param tzID The time zone ID.
  293. * @param status Receives the status
  294. * @internal
  295. */
  296. void addZone(UTimeZoneNameType nameType, int32_t matchLength,
  297. const UnicodeString& tzID, UErrorCode& status);
  298. /**
  299. * Adds a meata zone match.
  300. * @param nameType The name type.
  301. * @param matchLength The match length.
  302. * @param mzID The metazone ID.
  303. * @param status Receives the status
  304. * @internal
  305. */
  306. void addMetaZone(UTimeZoneNameType nameType, int32_t matchLength,
  307. const UnicodeString& mzID, UErrorCode& status);
  308. /**
  309. * Returns the number of entries available in this object.
  310. * @return The number of entries.
  311. * @internal
  312. */
  313. int32_t size() const;
  314. /**
  315. * Returns the time zone name type of a match at the specified index.
  316. * @param idx The index
  317. * @return The time zone name type. If the specified idx is out of range,
  318. * it returns UTZNM_UNKNOWN.
  319. * @see UTimeZoneNameType
  320. * @internal
  321. */
  322. UTimeZoneNameType getNameTypeAt(int32_t idx) const;
  323. /**
  324. * Returns the match length of a match at the specified index.
  325. * @param idx The index
  326. * @return The match length. If the specified idx is out of range,
  327. * it returns 0.
  328. * @internal
  329. */
  330. int32_t getMatchLengthAt(int32_t idx) const;
  331. /**
  332. * Gets the zone ID of a match at the specified index.
  333. * @param idx The index
  334. * @param tzID Receives the zone ID.
  335. * @return TRUE if the zone ID was set to tzID.
  336. * @internal
  337. */
  338. UBool getTimeZoneIDAt(int32_t idx, UnicodeString& tzID) const;
  339. /**
  340. * Gets the metazone ID of a match at the specified index.
  341. * @param idx The index
  342. * @param mzID Receives the metazone ID
  343. * @return TRUE if the meta zone ID was set to mzID.
  344. * @internal
  345. */
  346. UBool getMetaZoneIDAt(int32_t idx, UnicodeString& mzID) const;
  347. #endif /* U_HIDE_INTERNAL_API */
  348. private:
  349. UVector* fMatches; // vector of MatchEntry
  350. UVector* matches(UErrorCode& status);
  351. };
  352. /**
  353. * Finds time zone name prefix matches for the input text at the
  354. * given offset and returns a collection of the matches.
  355. * @param text The text.
  356. * @param start The starting offset within the text.
  357. * @param types The set of name types represented by bitwise flags of UTimeZoneNameType enums,
  358. * or UTZNM_UNKNOWN for all name types.
  359. * @param status Receives the status.
  360. * @return A collection of matches (owned by the caller), or NULL if no matches are found.
  361. * @see UTimeZoneNameType
  362. * @see MatchInfoCollection
  363. * @internal
  364. */
  365. virtual MatchInfoCollection* find(const UnicodeString& text, int32_t start, uint32_t types, UErrorCode& status) const = 0;
  366. };
  367. U_NAMESPACE_END
  368. #endif
  369. #endif