timezone.h 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*************************************************************************
  4. * Copyright (c) 1997-2016, International Business Machines Corporation
  5. * and others. All Rights Reserved.
  6. **************************************************************************
  7. *
  8. * File TIMEZONE.H
  9. *
  10. * Modification History:
  11. *
  12. * Date Name Description
  13. * 04/21/97 aliu Overhauled header.
  14. * 07/09/97 helena Changed createInstance to createDefault.
  15. * 08/06/97 aliu Removed dependency on internal header for Hashtable.
  16. * 08/10/98 stephen Changed getDisplayName() API conventions to match
  17. * 08/19/98 stephen Changed createTimeZone() to never return 0
  18. * 09/02/98 stephen Sync to JDK 1.2 8/31
  19. * - Added getOffset(... monthlen ...)
  20. * - Added hasSameRules()
  21. * 09/15/98 stephen Added getStaticClassID
  22. * 12/03/99 aliu Moved data out of static table into icudata.dll.
  23. * Hashtable replaced by new static data structures.
  24. * 12/14/99 aliu Made GMT public.
  25. * 08/15/01 grhoten Made GMT private and added the getGMT() function
  26. **************************************************************************
  27. */
  28. #ifndef TIMEZONE_H
  29. #define TIMEZONE_H
  30. #include "unicode/utypes.h"
  31. #if U_SHOW_CPLUSPLUS_API
  32. /**
  33. * \file
  34. * \brief C++ API: TimeZone object
  35. */
  36. #if !UCONFIG_NO_FORMATTING
  37. #include "unicode/uobject.h"
  38. #include "unicode/unistr.h"
  39. #include "unicode/ures.h"
  40. #include "unicode/ucal.h"
  41. U_NAMESPACE_BEGIN
  42. class StringEnumeration;
  43. /**
  44. *
  45. * <code>TimeZone</code> represents a time zone offset, and also figures out daylight
  46. * savings.
  47. *
  48. * <p>
  49. * Typically, you get a <code>TimeZone</code> using <code>createDefault</code>
  50. * which creates a <code>TimeZone</code> based on the time zone where the program
  51. * is running. For example, for a program running in Japan, <code>createDefault</code>
  52. * creates a <code>TimeZone</code> object based on Japanese Standard Time.
  53. *
  54. * <p>
  55. * You can also get a <code>TimeZone</code> using <code>createTimeZone</code> along
  56. * with a time zone ID. For instance, the time zone ID for the US Pacific
  57. * Time zone is "America/Los_Angeles". So, you can get a Pacific Time <code>TimeZone</code> object
  58. * with:
  59. * \htmlonly<blockquote>\endhtmlonly
  60. * <pre>
  61. * TimeZone *tz = TimeZone::createTimeZone("America/Los_Angeles");
  62. * </pre>
  63. * \htmlonly</blockquote>\endhtmlonly
  64. * You can use the <code>createEnumeration</code> method to iterate through
  65. * all the supported time zone IDs, or the <code>getCanonicalID</code> method to check
  66. * if a time zone ID is supported or not. You can then choose a
  67. * supported ID to get a <code>TimeZone</code>.
  68. * If the time zone you want is not represented by one of the
  69. * supported IDs, then you can create a custom time zone ID with
  70. * the following syntax:
  71. *
  72. * \htmlonly<blockquote>\endhtmlonly
  73. * <pre>
  74. * GMT[+|-]hh[[:]mm]
  75. * </pre>
  76. * \htmlonly</blockquote>\endhtmlonly
  77. *
  78. * For example, you might specify GMT+14:00 as a custom
  79. * time zone ID. The <code>TimeZone</code> that is returned
  80. * when you specify a custom time zone ID uses the specified
  81. * offset from GMT(=UTC) and does not observe daylight saving
  82. * time. For example, you might specify GMT+14:00 as a custom
  83. * time zone ID to create a TimeZone representing 14 hours ahead
  84. * of GMT (with no daylight saving time). In addition,
  85. * <code>getCanonicalID</code> can also be used to
  86. * normalize a custom time zone ID.
  87. *
  88. * TimeZone is an abstract class representing a time zone. A TimeZone is needed for
  89. * Calendar to produce local time for a particular time zone. A TimeZone comprises
  90. * three basic pieces of information:
  91. * <ul>
  92. * <li>A time zone offset; that, is the number of milliseconds to add or subtract
  93. * from a time expressed in terms of GMT to convert it to the same time in that
  94. * time zone (without taking daylight savings time into account).</li>
  95. * <li>Logic necessary to take daylight savings time into account if daylight savings
  96. * time is observed in that time zone (e.g., the days and hours on which daylight
  97. * savings time begins and ends).</li>
  98. * <li>An ID. This is a text string that uniquely identifies the time zone.</li>
  99. * </ul>
  100. *
  101. * (Only the ID is actually implemented in TimeZone; subclasses of TimeZone may handle
  102. * daylight savings time and GMT offset in different ways. Currently we have the following
  103. * TimeZone subclasses: RuleBasedTimeZone, SimpleTimeZone, and VTimeZone.)
  104. * <P>
  105. * The TimeZone class contains a static list containing a TimeZone object for every
  106. * combination of GMT offset and daylight-savings time rules currently in use in the
  107. * world, each with a unique ID. Each ID consists of a region (usually a continent or
  108. * ocean) and a city in that region, separated by a slash, (for example, US Pacific
  109. * Time is "America/Los_Angeles.") Because older versions of this class used
  110. * three- or four-letter abbreviations instead, there is also a table that maps the older
  111. * abbreviations to the newer ones (for example, "PST" maps to "America/Los_Angeles").
  112. * Anywhere the API requires an ID, you can use either form.
  113. * <P>
  114. * To create a new TimeZone, you call the factory function TimeZone::createTimeZone()
  115. * and pass it a time zone ID. You can use the createEnumeration() function to
  116. * obtain a list of all the time zone IDs recognized by createTimeZone().
  117. * <P>
  118. * You can also use TimeZone::createDefault() to create a TimeZone. This function uses
  119. * platform-specific APIs to produce a TimeZone for the time zone corresponding to
  120. * the client's computer's physical location. For example, if you're in Japan (assuming
  121. * your machine is set up correctly), TimeZone::createDefault() will return a TimeZone
  122. * for Japanese Standard Time ("Asia/Tokyo").
  123. */
  124. class U_I18N_API TimeZone : public UObject {
  125. public:
  126. /**
  127. * @stable ICU 2.0
  128. */
  129. virtual ~TimeZone();
  130. /**
  131. * Returns the "unknown" time zone.
  132. * It behaves like the GMT/UTC time zone but has the
  133. * <code>UCAL_UNKNOWN_ZONE_ID</code> = "Etc/Unknown".
  134. * createTimeZone() returns a mutable clone of this time zone if the input ID is not recognized.
  135. *
  136. * @return the "unknown" time zone.
  137. * @see UCAL_UNKNOWN_ZONE_ID
  138. * @see createTimeZone
  139. * @see getGMT
  140. * @stable ICU 49
  141. */
  142. static const TimeZone& U_EXPORT2 getUnknown();
  143. /**
  144. * The GMT (=UTC) time zone has a raw offset of zero and does not use daylight
  145. * savings time. This is a commonly used time zone.
  146. *
  147. * <p>Note: For backward compatibility reason, the ID used by the time
  148. * zone returned by this method is "GMT", although the ICU's canonical
  149. * ID for the GMT time zone is "Etc/GMT".
  150. *
  151. * @return the GMT/UTC time zone.
  152. * @see getUnknown
  153. * @stable ICU 2.0
  154. */
  155. static const TimeZone* U_EXPORT2 getGMT();
  156. /**
  157. * Creates a <code>TimeZone</code> for the given ID.
  158. * @param ID the ID for a <code>TimeZone</code>, such as "America/Los_Angeles",
  159. * or a custom ID such as "GMT-8:00".
  160. * @return the specified <code>TimeZone</code>, or a mutable clone of getUnknown()
  161. * if the given ID cannot be understood or if the given ID is "Etc/Unknown".
  162. * The return result is guaranteed to be non-nullptr.
  163. * If you require that the specific zone asked for be returned,
  164. * compare the result with getUnknown() or check the ID of the return result.
  165. * @stable ICU 2.0
  166. */
  167. static TimeZone* U_EXPORT2 createTimeZone(const UnicodeString& ID);
  168. /**
  169. * Returns an enumeration over system time zone IDs with the given
  170. * filter conditions.
  171. * @param zoneType The system time zone type.
  172. * @param region The ISO 3166 two-letter country code or UN M.49
  173. * three-digit area code. When nullptr, no filtering
  174. * done by region.
  175. * @param rawOffset An offset from GMT in milliseconds, ignoring
  176. * the effect of daylight savings time, if any.
  177. * When nullptr, no filtering done by zone offset.
  178. * @param ec Output param to filled in with a success or
  179. * an error.
  180. * @return an enumeration object, owned by the caller.
  181. * @stable ICU 4.8
  182. */
  183. static StringEnumeration* U_EXPORT2 createTimeZoneIDEnumeration(
  184. USystemTimeZoneType zoneType,
  185. const char* region,
  186. const int32_t* rawOffset,
  187. UErrorCode& ec);
  188. #ifndef U_HIDE_DEPRECATED_API
  189. /**
  190. * Returns an enumeration over all recognized time zone IDs. (i.e.,
  191. * all strings that createTimeZone() accepts)
  192. *
  193. * @return an enumeration object, owned by the caller.
  194. * @deprecated ICU 70 Use createEnumeration(UErrorCode&) instead.
  195. */
  196. static StringEnumeration* U_EXPORT2 createEnumeration();
  197. #endif // U_HIDE_DEPRECATED_API
  198. /**
  199. * Returns an enumeration over all recognized time zone IDs. (i.e.,
  200. * all strings that createTimeZone() accepts)
  201. *
  202. * @param status Receives the status.
  203. * @return an enumeration object, owned by the caller.
  204. * @stable ICU 70
  205. */
  206. static StringEnumeration* U_EXPORT2 createEnumeration(UErrorCode& status);
  207. #ifndef U_HIDE_DEPRECATED_API
  208. /**
  209. * Returns an enumeration over time zone IDs with a given raw
  210. * offset from GMT. There may be several times zones with the
  211. * same GMT offset that differ in the way they handle daylight
  212. * savings time. For example, the state of Arizona doesn't
  213. * observe daylight savings time. If you ask for the time zone
  214. * IDs corresponding to GMT-7:00, you'll get back an enumeration
  215. * over two time zone IDs: "America/Denver," which corresponds to
  216. * Mountain Standard Time in the winter and Mountain Daylight Time
  217. * in the summer, and "America/Phoenix", which corresponds to
  218. * Mountain Standard Time year-round, even in the summer.
  219. *
  220. * @param rawOffset an offset from GMT in milliseconds, ignoring
  221. * the effect of daylight savings time, if any
  222. * @return an enumeration object, owned by the caller
  223. * @deprecated ICU 70 Use createEnumerationForRawOffset(int32_t,UErrorCode&) instead.
  224. */
  225. static StringEnumeration* U_EXPORT2 createEnumeration(int32_t rawOffset);
  226. #endif // U_HIDE_DEPRECATED_API
  227. /**
  228. * Returns an enumeration over time zone IDs with a given raw
  229. * offset from GMT. There may be several times zones with the
  230. * same GMT offset that differ in the way they handle daylight
  231. * savings time. For example, the state of Arizona doesn't
  232. * observe daylight savings time. If you ask for the time zone
  233. * IDs corresponding to GMT-7:00, you'll get back an enumeration
  234. * over two time zone IDs: "America/Denver," which corresponds to
  235. * Mountain Standard Time in the winter and Mountain Daylight Time
  236. * in the summer, and "America/Phoenix", which corresponds to
  237. * Mountain Standard Time year-round, even in the summer.
  238. *
  239. * @param rawOffset an offset from GMT in milliseconds, ignoring
  240. * the effect of daylight savings time, if any
  241. * @param status Receives the status.
  242. * @return an enumeration object, owned by the caller
  243. * @stable ICU 70
  244. */
  245. static StringEnumeration* U_EXPORT2 createEnumerationForRawOffset(int32_t rawOffset, UErrorCode& status);
  246. #ifndef U_HIDE_DEPRECATED_API
  247. /**
  248. * Returns an enumeration over time zone IDs associated with the
  249. * given region. Some zones are affiliated with no region
  250. * (e.g., "UTC"); these may also be retrieved, as a group.
  251. *
  252. * @param region The ISO 3166 two-letter country code, or nullptr to
  253. * retrieve zones not affiliated with any region.
  254. * @return an enumeration object, owned by the caller
  255. * @deprecated ICU 70 Use createEnumerationForRegion(const char*,UErrorCode&) instead.
  256. */
  257. static StringEnumeration* U_EXPORT2 createEnumeration(const char* region);
  258. #endif // U_HIDE_DEPRECATED_API
  259. /**
  260. * Returns an enumeration over time zone IDs associated with the
  261. * given region. Some zones are affiliated with no region
  262. * (e.g., "UTC"); these may also be retrieved, as a group.
  263. *
  264. * @param region The ISO 3166 two-letter country code, or nullptr to
  265. * retrieve zones not affiliated with any region.
  266. * @param status Receives the status.
  267. * @return an enumeration object, owned by the caller
  268. * @stable ICU 70
  269. */
  270. static StringEnumeration* U_EXPORT2 createEnumerationForRegion(const char* region, UErrorCode& status);
  271. /**
  272. * Returns the number of IDs in the equivalency group that
  273. * includes the given ID. An equivalency group contains zones
  274. * that have the same GMT offset and rules.
  275. *
  276. * <p>The returned count includes the given ID; it is always >= 1.
  277. * The given ID must be a system time zone. If it is not, returns
  278. * zero.
  279. * @param id a system time zone ID
  280. * @return the number of zones in the equivalency group containing
  281. * 'id', or zero if 'id' is not a valid system ID
  282. * @see #getEquivalentID
  283. * @stable ICU 2.0
  284. */
  285. static int32_t U_EXPORT2 countEquivalentIDs(const UnicodeString& id);
  286. /**
  287. * Returns an ID in the equivalency group that
  288. * includes the given ID. An equivalency group contains zones
  289. * that have the same GMT offset and rules.
  290. *
  291. * <p>The given index must be in the range 0..n-1, where n is the
  292. * value returned by <code>countEquivalentIDs(id)</code>. For
  293. * some value of 'index', the returned value will be equal to the
  294. * given id. If the given id is not a valid system time zone, or
  295. * if 'index' is out of range, then returns an empty string.
  296. * @param id a system time zone ID
  297. * @param index a value from 0 to n-1, where n is the value
  298. * returned by <code>countEquivalentIDs(id)</code>
  299. * @return the ID of the index-th zone in the equivalency group
  300. * containing 'id', or an empty string if 'id' is not a valid
  301. * system ID or 'index' is out of range
  302. * @see #countEquivalentIDs
  303. * @stable ICU 2.0
  304. */
  305. static const UnicodeString U_EXPORT2 getEquivalentID(const UnicodeString& id,
  306. int32_t index);
  307. /**
  308. * Creates an instance of TimeZone detected from the current host
  309. * system configuration. If the host system detection routines fail,
  310. * or if they specify a TimeZone or TimeZone offset which is not
  311. * recognized, then the special TimeZone "Etc/Unknown" is returned.
  312. *
  313. * Note that ICU4C does not change the default time zone unless
  314. * `TimeZone::adoptDefault(TimeZone*)` or
  315. * `TimeZone::setDefault(const TimeZone&)` is explicitly called by a
  316. * user. This method does not update the current ICU's default,
  317. * and may return a different TimeZone from the one returned by
  318. * `TimeZone::createDefault()`.
  319. *
  320. * <p>This function is not thread safe.</p>
  321. *
  322. * @return A new instance of TimeZone detected from the current host system
  323. * configuration.
  324. * @see adoptDefault
  325. * @see setDefault
  326. * @see createDefault
  327. * @see getUnknown
  328. * @stable ICU 55
  329. */
  330. static TimeZone* U_EXPORT2 detectHostTimeZone();
  331. /**
  332. * Creates a new copy of the default TimeZone for this host. Unless the default time
  333. * zone has already been set using adoptDefault() or setDefault(), the default is
  334. * determined by querying the host system configuration. If the host system detection
  335. * routines fail, or if they specify a TimeZone or TimeZone offset which is not
  336. * recognized, then the special TimeZone "Etc/Unknown" is instantiated and made the
  337. * default.
  338. *
  339. * @return A default TimeZone. Clients are responsible for deleting the time zone
  340. * object returned.
  341. * @see getUnknown
  342. * @stable ICU 2.0
  343. */
  344. static TimeZone* U_EXPORT2 createDefault();
  345. #ifndef U_HIDE_INTERNAL_API
  346. /**
  347. * If the locale contains the timezone keyword, creates a copy of that TimeZone.
  348. * Otherwise, create the default timezone.
  349. *
  350. * @param locale a locale which may contains 'timezone' keyword/value.
  351. * @return A TimeZone. Clients are responsible for deleting the time zone
  352. * object returned.
  353. * @internal
  354. */
  355. static TimeZone* U_EXPORT2 forLocaleOrDefault(const Locale& locale);
  356. #endif /* U_HIDE_INTERNAL_API */
  357. /**
  358. * Sets the default time zone (i.e., what's returned by createDefault()) to be the
  359. * specified time zone. If nullptr is specified for the time zone, the default time
  360. * zone is set to the default host time zone. This call adopts the TimeZone object
  361. * passed in; the client is no longer responsible for deleting it.
  362. *
  363. * @param zone A pointer to the new TimeZone object to use as the default.
  364. * @stable ICU 2.0
  365. */
  366. static void U_EXPORT2 adoptDefault(TimeZone* zone);
  367. #ifndef U_HIDE_SYSTEM_API
  368. /**
  369. * Same as adoptDefault(), except that the TimeZone object passed in is NOT adopted;
  370. * the caller remains responsible for deleting it.
  371. *
  372. * @param zone The given timezone.
  373. * @system
  374. * @stable ICU 2.0
  375. */
  376. static void U_EXPORT2 setDefault(const TimeZone& zone);
  377. #endif /* U_HIDE_SYSTEM_API */
  378. /**
  379. * Returns the timezone data version currently used by ICU.
  380. * @param status Output param to filled in with a success or an error.
  381. * @return the version string, such as "2007f"
  382. * @stable ICU 3.8
  383. */
  384. static const char* U_EXPORT2 getTZDataVersion(UErrorCode& status);
  385. /**
  386. * Returns the canonical system timezone ID or the normalized
  387. * custom time zone ID for the given time zone ID.
  388. * @param id The input time zone ID to be canonicalized.
  389. * @param canonicalID Receives the canonical system time zone ID
  390. * or the custom time zone ID in normalized format.
  391. * @param status Receives the status. When the given time zone ID
  392. * is neither a known system time zone ID nor a
  393. * valid custom time zone ID, U_ILLEGAL_ARGUMENT_ERROR
  394. * is set.
  395. * @return A reference to the result.
  396. * @stable ICU 4.0
  397. */
  398. static UnicodeString& U_EXPORT2 getCanonicalID(const UnicodeString& id,
  399. UnicodeString& canonicalID, UErrorCode& status);
  400. /**
  401. * Returns the canonical system time zone ID or the normalized
  402. * custom time zone ID for the given time zone ID.
  403. * @param id The input time zone ID to be canonicalized.
  404. * @param canonicalID Receives the canonical system time zone ID
  405. * or the custom time zone ID in normalized format.
  406. * @param isSystemID Receives if the given ID is a known system
  407. * time zone ID.
  408. * @param status Receives the status. When the given time zone ID
  409. * is neither a known system time zone ID nor a
  410. * valid custom time zone ID, U_ILLEGAL_ARGUMENT_ERROR
  411. * is set.
  412. * @return A reference to the result.
  413. * @stable ICU 4.0
  414. */
  415. static UnicodeString& U_EXPORT2 getCanonicalID(const UnicodeString& id,
  416. UnicodeString& canonicalID, UBool& isSystemID, UErrorCode& status);
  417. /**
  418. * Returns the preferred time zone ID in the IANA time zone database for the given time zone ID.
  419. * There are two types of preferred IDs. The first type is the one defined in zone.tab file,
  420. * such as "America/Los_Angeles". The second types is the one defined for zones not associated
  421. * with a specific region, but not defined with "Link" syntax such as "Etc/GMT+10".
  422. *
  423. * <p>Note: For most of valid time zone IDs, this method returns an ID same as getCanonicalID().
  424. * getCanonicalID() is based on canonical time zone IDs defined in Unicode CLDR.
  425. * These canonical time zone IDs in CLDR were based on very old version of the time zone database.
  426. * In the IANA time zone database, some IDs were updated since then. This API returns a newer
  427. * time zone ID. For example, CLDR defines "Asia/Calcutta" as the canonical time zone ID. This
  428. * method returns "Asia/Kolkata" instead.
  429. * <p> "Etc/Unknown" is a special time zone ID defined by CLDR. There are no corresponding zones
  430. * in the IANA time zone database. Therefore, this API returns U_ILLEGAL_ARGUMENT_ERROR when the
  431. * input ID is "Etc/Unknown".
  432. *
  433. * @param id The input time zone ID.
  434. * @param ianaID Receives the preferred time zone ID in the IANA time zone database. When
  435. * the given time zone ID is not a known time zone ID, this method sets an
  436. * invalid (bogus) string.
  437. * @param status Receives the status. When the given time zone ID is not a known time zone
  438. * ID, U_ILLEGAL_ARGUMENT_ERROR is set.
  439. * @return A reference to the result.
  440. * @stable ICU 74
  441. */
  442. static UnicodeString& U_EXPORT2 getIanaID(const UnicodeString&id, UnicodeString& ianaID,
  443. UErrorCode& status);
  444. /**
  445. * Converts a system time zone ID to an equivalent Windows time zone ID. For example,
  446. * Windows time zone ID "Pacific Standard Time" is returned for input "America/Los_Angeles".
  447. *
  448. * <p>There are system time zones that cannot be mapped to Windows zones. When the input
  449. * system time zone ID is unknown or unmappable to a Windows time zone, then the result will be
  450. * empty, but the operation itself remains successful (no error status set on return).
  451. *
  452. * <p>This implementation utilizes <a href="http://unicode.org/cldr/charts/supplemental/zone_tzid.html">
  453. * Zone-Tzid mapping data</a>. The mapping data is updated time to time. To get the latest changes,
  454. * please read the ICU user guide section <a href="https://unicode-org.github.io/icu/userguide/datetime/timezone#updating-the-time-zone-data">
  455. * Updating the Time Zone Data</a>.
  456. *
  457. * @param id A system time zone ID.
  458. * @param winid Receives a Windows time zone ID. When the input system time zone ID is unknown
  459. * or unmappable to a Windows time zone ID, then an empty string is set on return.
  460. * @param status Receives the status.
  461. * @return A reference to the result (<code>winid</code>).
  462. * @see getIDForWindowsID
  463. *
  464. * @stable ICU 52
  465. */
  466. static UnicodeString& U_EXPORT2 getWindowsID(const UnicodeString& id,
  467. UnicodeString& winid, UErrorCode& status);
  468. /**
  469. * Converts a Windows time zone ID to an equivalent system time zone ID
  470. * for a region. For example, system time zone ID "America/Los_Angeles" is returned
  471. * for input Windows ID "Pacific Standard Time" and region "US" (or <code>null</code>),
  472. * "America/Vancouver" is returned for the same Windows ID "Pacific Standard Time" and
  473. * region "CA".
  474. *
  475. * <p>Not all Windows time zones can be mapped to system time zones. When the input
  476. * Windows time zone ID is unknown or unmappable to a system time zone, then the result
  477. * will be empty, but the operation itself remains successful (no error status set on return).
  478. *
  479. * <p>This implementation utilizes <a href="http://unicode.org/cldr/charts/supplemental/zone_tzid.html">
  480. * Zone-Tzid mapping data</a>. The mapping data is updated time to time. To get the latest changes,
  481. * please read the ICU user guide section <a href="https://unicode-org.github.io/icu/userguide/datetime/timezone#updating-the-time-zone-data">
  482. * Updating the Time Zone Data</a>.
  483. *
  484. * @param winid A Windows time zone ID.
  485. * @param region A NUL-terminated region code, or <code>nullptr</code> if no regional preference.
  486. * @param id Receives a system time zone ID. When the input Windows time zone ID is unknown
  487. * or unmappable to a system time zone ID, then an empty string is set on return.
  488. * @param status Receives the status.
  489. * @return A reference to the result (<code>id</code>).
  490. * @see getWindowsID
  491. *
  492. * @stable ICU 52
  493. */
  494. static UnicodeString& U_EXPORT2 getIDForWindowsID(const UnicodeString& winid, const char* region,
  495. UnicodeString& id, UErrorCode& status);
  496. /**
  497. * Returns true if the two TimeZones are equal. (The TimeZone version only compares
  498. * IDs, but subclasses are expected to also compare the fields they add.)
  499. *
  500. * @param that The TimeZone object to be compared with.
  501. * @return true if the given TimeZone is equal to this TimeZone; false
  502. * otherwise.
  503. * @stable ICU 2.0
  504. */
  505. virtual bool operator==(const TimeZone& that) const;
  506. /**
  507. * Returns true if the two TimeZones are NOT equal; that is, if operator==() returns
  508. * false.
  509. *
  510. * @param that The TimeZone object to be compared with.
  511. * @return true if the given TimeZone is not equal to this TimeZone; false
  512. * otherwise.
  513. * @stable ICU 2.0
  514. */
  515. bool operator!=(const TimeZone& that) const {return !operator==(that);}
  516. /**
  517. * Returns the TimeZone's adjusted GMT offset (i.e., the number of milliseconds to add
  518. * to GMT to get local time in this time zone, taking daylight savings time into
  519. * account) as of a particular reference date. The reference date is used to determine
  520. * whether daylight savings time is in effect and needs to be figured into the offset
  521. * that is returned (in other words, what is the adjusted GMT offset in this time zone
  522. * at this particular date and time?). For the time zones produced by createTimeZone(),
  523. * the reference data is specified according to the Gregorian calendar, and the date
  524. * and time fields are local standard time.
  525. *
  526. * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
  527. * which returns both the raw and the DST offset for a given time. This method
  528. * is retained only for backward compatibility.
  529. *
  530. * @param era The reference date's era
  531. * @param year The reference date's year
  532. * @param month The reference date's month (0-based; 0 is January)
  533. * @param day The reference date's day-in-month (1-based)
  534. * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday)
  535. * @param millis The reference date's milliseconds in day, local standard time
  536. * @param status Output param to filled in with a success or an error.
  537. * @return The offset in milliseconds to add to GMT to get local time.
  538. * @stable ICU 2.0
  539. */
  540. virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
  541. uint8_t dayOfWeek, int32_t millis, UErrorCode& status) const = 0;
  542. /**
  543. * Gets the time zone offset, for current date, modified in case of
  544. * daylight savings. This is the offset to add *to* UTC to get local time.
  545. *
  546. * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
  547. * which returns both the raw and the DST offset for a given time. This method
  548. * is retained only for backward compatibility.
  549. *
  550. * @param era the era of the given date.
  551. * @param year the year in the given date.
  552. * @param month the month in the given date.
  553. * Month is 0-based. e.g., 0 for January.
  554. * @param day the day-in-month of the given date.
  555. * @param dayOfWeek the day-of-week of the given date.
  556. * @param milliseconds the millis in day in <em>standard</em> local time.
  557. * @param monthLength the length of the given month in days.
  558. * @param status Output param to filled in with a success or an error.
  559. * @return the offset to add *to* GMT to get local time.
  560. * @stable ICU 2.0
  561. */
  562. virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
  563. uint8_t dayOfWeek, int32_t milliseconds,
  564. int32_t monthLength, UErrorCode& status) const = 0;
  565. /**
  566. * Returns the time zone raw and GMT offset for the given moment
  567. * in time. Upon return, local-millis = GMT-millis + rawOffset +
  568. * dstOffset. All computations are performed in the proleptic
  569. * Gregorian calendar. The default implementation in the TimeZone
  570. * class delegates to the 8-argument getOffset().
  571. *
  572. * @param date moment in time for which to return offsets, in
  573. * units of milliseconds from January 1, 1970 0:00 GMT, either GMT
  574. * time or local wall time, depending on `local'.
  575. * @param local if true, `date' is local wall time; otherwise it
  576. * is in GMT time.
  577. * @param rawOffset output parameter to receive the raw offset, that
  578. * is, the offset not including DST adjustments
  579. * @param dstOffset output parameter to receive the DST offset,
  580. * that is, the offset to be added to `rawOffset' to obtain the
  581. * total offset between local and GMT time. If DST is not in
  582. * effect, this value is zero; otherwise it is a positive value,
  583. * typically one hour.
  584. * @param ec input-output error code
  585. *
  586. * @stable ICU 2.8
  587. */
  588. virtual void getOffset(UDate date, UBool local, int32_t& rawOffset,
  589. int32_t& dstOffset, UErrorCode& ec) const;
  590. /**
  591. * Sets the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
  592. * to GMT to get local time, before taking daylight savings time into account).
  593. *
  594. * @param offsetMillis The new raw GMT offset for this time zone.
  595. * @stable ICU 2.0
  596. */
  597. virtual void setRawOffset(int32_t offsetMillis) = 0;
  598. /**
  599. * Returns the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
  600. * to GMT to get local time, before taking daylight savings time into account).
  601. *
  602. * @return The TimeZone's raw GMT offset.
  603. * @stable ICU 2.0
  604. */
  605. virtual int32_t getRawOffset() const = 0;
  606. /**
  607. * Fills in "ID" with the TimeZone's ID.
  608. *
  609. * @param ID Receives this TimeZone's ID.
  610. * @return A reference to 'ID'
  611. * @stable ICU 2.0
  612. */
  613. UnicodeString& getID(UnicodeString& ID) const;
  614. /**
  615. * Sets the TimeZone's ID to the specified value. This doesn't affect any other
  616. * fields (for example, if you say<
  617. * blockquote><pre>
  618. * . TimeZone* foo = TimeZone::createTimeZone("America/New_York");
  619. * . foo.setID("America/Los_Angeles");
  620. * </pre>\htmlonly</blockquote>\endhtmlonly
  621. * the time zone's GMT offset and daylight-savings rules don't change to those for
  622. * Los Angeles. They're still those for New York. Only the ID has changed.)
  623. *
  624. * @param ID The new time zone ID.
  625. * @stable ICU 2.0
  626. */
  627. void setID(const UnicodeString& ID);
  628. /**
  629. * Enum for use with getDisplayName
  630. * @stable ICU 2.4
  631. */
  632. enum EDisplayType {
  633. /**
  634. * Selector for short display name
  635. * @stable ICU 2.4
  636. */
  637. SHORT = 1,
  638. /**
  639. * Selector for long display name
  640. * @stable ICU 2.4
  641. */
  642. LONG,
  643. /**
  644. * Selector for short generic display name
  645. * @stable ICU 4.4
  646. */
  647. SHORT_GENERIC,
  648. /**
  649. * Selector for long generic display name
  650. * @stable ICU 4.4
  651. */
  652. LONG_GENERIC,
  653. /**
  654. * Selector for short display name derived
  655. * from time zone offset
  656. * @stable ICU 4.4
  657. */
  658. SHORT_GMT,
  659. /**
  660. * Selector for long display name derived
  661. * from time zone offset
  662. * @stable ICU 4.4
  663. */
  664. LONG_GMT,
  665. /**
  666. * Selector for short display name derived
  667. * from the time zone's fallback name
  668. * @stable ICU 4.4
  669. */
  670. SHORT_COMMONLY_USED,
  671. /**
  672. * Selector for long display name derived
  673. * from the time zone's fallback name
  674. * @stable ICU 4.4
  675. */
  676. GENERIC_LOCATION
  677. };
  678. /**
  679. * Returns a name of this time zone suitable for presentation to the user
  680. * in the default locale.
  681. * This method returns the long name, not including daylight savings.
  682. * If the display name is not available for the locale,
  683. * then this method returns a string in the localized GMT offset format
  684. * such as <code>GMT[+-]HH:mm</code>.
  685. * @param result the human-readable name of this time zone in the default locale.
  686. * @return A reference to 'result'.
  687. * @stable ICU 2.0
  688. */
  689. UnicodeString& getDisplayName(UnicodeString& result) const;
  690. /**
  691. * Returns a name of this time zone suitable for presentation to the user
  692. * in the specified locale.
  693. * This method returns the long name, not including daylight savings.
  694. * If the display name is not available for the locale,
  695. * then this method returns a string in the localized GMT offset format
  696. * such as <code>GMT[+-]HH:mm</code>.
  697. * @param locale the locale in which to supply the display name.
  698. * @param result the human-readable name of this time zone in the given locale
  699. * or in the default locale if the given locale is not recognized.
  700. * @return A reference to 'result'.
  701. * @stable ICU 2.0
  702. */
  703. UnicodeString& getDisplayName(const Locale& locale, UnicodeString& result) const;
  704. /**
  705. * Returns a name of this time zone suitable for presentation to the user
  706. * in the default locale.
  707. * If the display name is not available for the locale,
  708. * then this method returns a string in the localized GMT offset format
  709. * such as <code>GMT[+-]HH:mm</code>.
  710. * @param inDaylight if true, return the daylight savings name.
  711. * @param style
  712. * @param result the human-readable name of this time zone in the default locale.
  713. * @return A reference to 'result'.
  714. * @stable ICU 2.0
  715. */
  716. UnicodeString& getDisplayName(UBool inDaylight, EDisplayType style, UnicodeString& result) const;
  717. /**
  718. * Returns a name of this time zone suitable for presentation to the user
  719. * in the specified locale.
  720. * If the display name is not available for the locale,
  721. * then this method returns a string in the localized GMT offset format
  722. * such as <code>GMT[+-]HH:mm</code>.
  723. * @param inDaylight if true, return the daylight savings name.
  724. * @param style
  725. * @param locale the locale in which to supply the display name.
  726. * @param result the human-readable name of this time zone in the given locale
  727. * or in the default locale if the given locale is not recognized.
  728. * @return A reference to 'result'.
  729. * @stable ICU 2.0
  730. */
  731. UnicodeString& getDisplayName(UBool inDaylight, EDisplayType style, const Locale& locale, UnicodeString& result) const;
  732. /**
  733. * Queries if this time zone uses daylight savings time.
  734. * @return true if this time zone uses daylight savings time,
  735. * false, otherwise.
  736. * <p><strong>Note:</strong>The default implementation of
  737. * ICU TimeZone uses the tz database, which supports historic
  738. * rule changes, for system time zones. With the implementation,
  739. * there are time zones that used daylight savings time in the
  740. * past, but no longer used currently. For example, Asia/Tokyo has
  741. * never used daylight savings time since 1951. Most clients would
  742. * expect that this method to return <code>false</code> for such case.
  743. * The default implementation of this method returns <code>true</code>
  744. * when the time zone uses daylight savings time in the current
  745. * (Gregorian) calendar year.
  746. * <p>In Java 7, <code>observesDaylightTime()</code> was added in
  747. * addition to <code>useDaylightTime()</code>. In Java, <code>useDaylightTime()</code>
  748. * only checks if daylight saving time is observed by the last known
  749. * rule. This specification might not be what most users would expect
  750. * if daylight saving time is currently observed, but not scheduled
  751. * in future. In this case, Java's <code>userDaylightTime()</code> returns
  752. * <code>false</code>. To resolve the issue, Java 7 added <code>observesDaylightTime()</code>,
  753. * which takes the current rule into account. The method <code>observesDaylightTime()</code>
  754. * was added in ICU4J for supporting API signature compatibility with JDK.
  755. * In general, ICU4C also provides JDK compatible methods, but the current
  756. * implementation <code>userDaylightTime()</code> serves the purpose
  757. * (takes the current rule into account), <code>observesDaylightTime()</code>
  758. * is not added in ICU4C. In addition to <code>useDaylightTime()</code>, ICU4C
  759. * <code>BasicTimeZone</code> class (Note that <code>TimeZone::createTimeZone(const UnicodeString &ID)</code>
  760. * always returns a <code>BasicTimeZone</code>) provides a series of methods allowing
  761. * historic and future time zone rule iteration, so you can check if daylight saving
  762. * time is observed or not within a given period.
  763. *
  764. * @stable ICU 2.0
  765. */
  766. virtual UBool useDaylightTime() const = 0;
  767. #ifndef U_FORCE_HIDE_DEPRECATED_API
  768. /**
  769. * Queries if the given date is in daylight savings time in
  770. * this time zone.
  771. * This method is wasteful since it creates a new GregorianCalendar and
  772. * deletes it each time it is called. This is a deprecated method
  773. * and provided only for Java compatibility.
  774. *
  775. * @param date the given UDate.
  776. * @param status Output param filled in with success/error code.
  777. * @return true if the given date is in daylight savings time,
  778. * false, otherwise.
  779. * @deprecated ICU 2.4. Use Calendar::inDaylightTime() instead.
  780. */
  781. virtual UBool inDaylightTime(UDate date, UErrorCode& status) const = 0;
  782. #endif // U_FORCE_HIDE_DEPRECATED_API
  783. /**
  784. * Returns true if this zone has the same rule and offset as another zone.
  785. * That is, if this zone differs only in ID, if at all.
  786. * @param other the <code>TimeZone</code> object to be compared with
  787. * @return true if the given zone is the same as this one,
  788. * with the possible exception of the ID
  789. * @stable ICU 2.0
  790. */
  791. virtual UBool hasSameRules(const TimeZone& other) const;
  792. /**
  793. * Clones TimeZone objects polymorphically. Clients are responsible for deleting
  794. * the TimeZone object cloned.
  795. *
  796. * @return A new copy of this TimeZone object.
  797. * @stable ICU 2.0
  798. */
  799. virtual TimeZone* clone() const = 0;
  800. /**
  801. * Return the class ID for this class. This is useful only for
  802. * comparing to a return value from getDynamicClassID().
  803. * @return The class ID for all objects of this class.
  804. * @stable ICU 2.0
  805. */
  806. static UClassID U_EXPORT2 getStaticClassID();
  807. /**
  808. * Returns a unique class ID POLYMORPHICALLY. This method is to
  809. * implement a simple version of RTTI, since not all C++ compilers support genuine
  810. * RTTI. Polymorphic operator==() and clone() methods call this method.
  811. * <P>
  812. * Concrete subclasses of TimeZone must use the UOBJECT_DEFINE_RTTI_IMPLEMENTATION
  813. * macro from uobject.h in their implementation to provide correct RTTI information.
  814. * @return The class ID for this object. All objects of a given class have the
  815. * same class ID. Objects of other classes have different class IDs.
  816. * @stable ICU 2.0
  817. */
  818. virtual UClassID getDynamicClassID() const override = 0;
  819. /**
  820. * Returns the amount of time to be added to local standard time
  821. * to get local wall clock time.
  822. * <p>
  823. * The default implementation always returns 3600000 milliseconds
  824. * (i.e., one hour) if this time zone observes Daylight Saving
  825. * Time. Otherwise, 0 (zero) is returned.
  826. * <p>
  827. * If an underlying TimeZone implementation subclass supports
  828. * historical Daylight Saving Time changes, this method returns
  829. * the known latest daylight saving value.
  830. *
  831. * @return the amount of saving time in milliseconds
  832. * @stable ICU 3.6
  833. */
  834. virtual int32_t getDSTSavings() const;
  835. /**
  836. * Gets the region code associated with the given
  837. * system time zone ID. The region code is either ISO 3166
  838. * 2-letter country code or UN M.49 3-digit area code.
  839. * When the time zone is not associated with a specific location,
  840. * for example - "Etc/UTC", "EST5EDT", then this method returns
  841. * "001" (UN M.49 area code for World).
  842. *
  843. * @param id The system time zone ID.
  844. * @param region Output buffer for receiving the region code.
  845. * @param capacity The size of the output buffer.
  846. * @param status Receives the status. When the given time zone ID
  847. * is not a known system time zone ID,
  848. * U_ILLEGAL_ARGUMENT_ERROR is set.
  849. * @return The length of the output region code.
  850. * @stable ICU 4.8
  851. */
  852. static int32_t U_EXPORT2 getRegion(const UnicodeString& id,
  853. char *region, int32_t capacity, UErrorCode& status);
  854. protected:
  855. /**
  856. * Default constructor. ID is initialized to the empty string.
  857. * @stable ICU 2.0
  858. */
  859. TimeZone();
  860. /**
  861. * Construct a TimeZone with a given ID.
  862. * @param id a system time zone ID
  863. * @stable ICU 2.0
  864. */
  865. TimeZone(const UnicodeString &id);
  866. /**
  867. * Copy constructor.
  868. * @param source the object to be copied.
  869. * @stable ICU 2.0
  870. */
  871. TimeZone(const TimeZone& source);
  872. /**
  873. * Default assignment operator.
  874. * @param right the object to be copied.
  875. * @stable ICU 2.0
  876. */
  877. TimeZone& operator=(const TimeZone& right);
  878. #ifndef U_HIDE_INTERNAL_API
  879. /**
  880. * Utility function. For internally loading rule data.
  881. * @param top Top resource bundle for tz data
  882. * @param ruleid ID of rule to load
  883. * @param oldbundle Old bundle to reuse or nullptr
  884. * @param status Status parameter
  885. * @return either a new bundle or *oldbundle
  886. * @internal
  887. */
  888. static UResourceBundle* loadRule(const UResourceBundle* top, const UnicodeString& ruleid, UResourceBundle* oldbundle, UErrorCode&status);
  889. #endif /* U_HIDE_INTERNAL_API */
  890. private:
  891. friend class ZoneMeta;
  892. static TimeZone* createCustomTimeZone(const UnicodeString&); // Creates a time zone based on the string.
  893. /**
  894. * Finds the given ID in the Olson tzdata. If the given ID is found in the tzdata,
  895. * returns the pointer to the ID resource. This method is exposed through ZoneMeta class
  896. * for ICU internal implementation and useful for building hashtable using a time zone
  897. * ID as a key.
  898. * @param id zone id string
  899. * @return the pointer of the ID resource, or nullptr.
  900. */
  901. static const char16_t* findID(const UnicodeString& id);
  902. /**
  903. * Resolve a link in Olson tzdata. When the given id is known and it's not a link,
  904. * the id itself is returned. When the given id is known and it is a link, then
  905. * dereferenced zone id is returned. When the given id is unknown, then it returns
  906. * nullptr.
  907. * @param id zone id string
  908. * @return the dereferenced zone or nullptr
  909. */
  910. static const char16_t* dereferOlsonLink(const UnicodeString& id);
  911. /**
  912. * Returns the region code associated with the given zone,
  913. * or nullptr if the zone is not known.
  914. * @param id zone id string
  915. * @return the region associated with the given zone
  916. */
  917. static const char16_t* getRegion(const UnicodeString& id);
  918. public:
  919. #ifndef U_HIDE_INTERNAL_API
  920. /**
  921. * Returns the region code associated with the given zone,
  922. * or nullptr if the zone is not known.
  923. * @param id zone id string
  924. * @param status Status parameter
  925. * @return the region associated with the given zone
  926. * @internal
  927. */
  928. static const char16_t* getRegion(const UnicodeString& id, UErrorCode& status);
  929. #endif /* U_HIDE_INTERNAL_API */
  930. private:
  931. /**
  932. * Parses the given custom time zone identifier
  933. * @param id id A string of the form GMT[+-]hh:mm, GMT[+-]hhmm, or
  934. * GMT[+-]hh.
  935. * @param sign Receives parsed sign, 1 for positive, -1 for negative.
  936. * @param hour Receives parsed hour field
  937. * @param minute Receives parsed minute field
  938. * @param second Receives parsed second field
  939. * @return Returns true when the given custom id is valid.
  940. */
  941. static UBool parseCustomID(const UnicodeString& id, int32_t& sign, int32_t& hour,
  942. int32_t& minute, int32_t& second);
  943. /**
  944. * Parse a custom time zone identifier and return the normalized
  945. * custom time zone identifier for the given custom id string.
  946. * @param id a string of the form GMT[+-]hh:mm, GMT[+-]hhmm, or
  947. * GMT[+-]hh.
  948. * @param normalized Receives the normalized custom ID
  949. * @param status Receives the status. When the input ID string is invalid,
  950. * U_ILLEGAL_ARGUMENT_ERROR is set.
  951. * @return The normalized custom id string.
  952. */
  953. static UnicodeString& getCustomID(const UnicodeString& id, UnicodeString& normalized,
  954. UErrorCode& status);
  955. /**
  956. * Returns the normalized custom time zone ID for the given offset fields.
  957. * @param hour offset hours
  958. * @param min offset minutes
  959. * @param sec offset seconds
  960. * @param negative sign of the offset, true for negative offset.
  961. * @param id Receives the format result (normalized custom ID)
  962. * @return The reference to id
  963. */
  964. static UnicodeString& formatCustomID(int32_t hour, int32_t min, int32_t sec,
  965. UBool negative, UnicodeString& id);
  966. UnicodeString fID; // this time zone's ID
  967. friend class TZEnumeration;
  968. };
  969. // -------------------------------------
  970. inline UnicodeString&
  971. TimeZone::getID(UnicodeString& ID) const
  972. {
  973. ID = fID;
  974. return ID;
  975. }
  976. // -------------------------------------
  977. inline void
  978. TimeZone::setID(const UnicodeString& ID)
  979. {
  980. fID = ID;
  981. }
  982. U_NAMESPACE_END
  983. #endif /* #if !UCONFIG_NO_FORMATTING */
  984. #endif /* U_SHOW_CPLUSPLUS_API */
  985. #endif //_TIMEZONE
  986. //eof