rbtz.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2007-2013, International Business Machines Corporation and *
  6. * others. All Rights Reserved. *
  7. *******************************************************************************
  8. */
  9. #ifndef RBTZ_H
  10. #define RBTZ_H
  11. #include "unicode/utypes.h"
  12. #if U_SHOW_CPLUSPLUS_API
  13. /**
  14. * \file
  15. * \brief C++ API: Rule based customizable time zone
  16. */
  17. #if !UCONFIG_NO_FORMATTING
  18. #include "unicode/basictz.h"
  19. #include "unicode/unistr.h"
  20. U_NAMESPACE_BEGIN
  21. // forward declaration
  22. class UVector;
  23. struct Transition;
  24. /**
  25. * a BasicTimeZone subclass implemented in terms of InitialTimeZoneRule and TimeZoneRule instances
  26. * @see BasicTimeZone
  27. * @see InitialTimeZoneRule
  28. * @see TimeZoneRule
  29. */
  30. class U_I18N_API RuleBasedTimeZone : public BasicTimeZone {
  31. public:
  32. /**
  33. * Constructs a <code>RuleBasedTimeZone</code> object with the ID and the
  34. * <code>InitialTimeZoneRule</code>. The input <code>InitialTimeZoneRule</code>
  35. * is adopted by this <code>RuleBasedTimeZone</code>, thus the caller must not
  36. * delete it.
  37. * @param id The time zone ID.
  38. * @param initialRule The initial time zone rule.
  39. * @stable ICU 3.8
  40. */
  41. RuleBasedTimeZone(const UnicodeString& id, InitialTimeZoneRule* initialRule);
  42. /**
  43. * Copy constructor.
  44. * @param source The RuleBasedTimeZone object to be copied.
  45. * @stable ICU 3.8
  46. */
  47. RuleBasedTimeZone(const RuleBasedTimeZone& source);
  48. /**
  49. * Destructor.
  50. * @stable ICU 3.8
  51. */
  52. virtual ~RuleBasedTimeZone();
  53. /**
  54. * Assignment operator.
  55. * @param right The object to be copied.
  56. * @stable ICU 3.8
  57. */
  58. RuleBasedTimeZone& operator=(const RuleBasedTimeZone& right);
  59. /**
  60. * Return true if the given <code>TimeZone</code> objects are
  61. * semantically equal. Objects of different subclasses are considered unequal.
  62. * @param that The object to be compared with.
  63. * @return true if the given <code>TimeZone</code> objects are
  64. *semantically equal.
  65. * @stable ICU 3.8
  66. */
  67. virtual bool operator==(const TimeZone& that) const override;
  68. /**
  69. * Return true if the given <code>TimeZone</code> objects are
  70. * semantically unequal. Objects of different subclasses are considered unequal.
  71. * @param that The object to be compared with.
  72. * @return true if the given <code>TimeZone</code> objects are
  73. * semantically unequal.
  74. * @stable ICU 3.8
  75. */
  76. virtual bool operator!=(const TimeZone& that) const;
  77. /**
  78. * Adds the `TimeZoneRule` which represents time transitions.
  79. * The `TimeZoneRule` must have start times, that is, the result
  80. * of `isTransitionRule()` must be true. Otherwise, U_ILLEGAL_ARGUMENT_ERROR
  81. * is set to the error code.
  82. * The input `TimeZoneRule` is adopted by this `RuleBasedTimeZone`;
  83. * the caller must not delete it. Should an error condition prevent
  84. * the successful adoption of the rule, this function will delete it.
  85. *
  86. * After all rules are added, the caller must call `complete()` method to
  87. * make this `RuleBasedTimeZone` ready to handle common time
  88. * zone functions.
  89. * @param rule The `TimeZoneRule`.
  90. * @param status Output param to filled in with a success or an error.
  91. * @stable ICU 3.8
  92. */
  93. void addTransitionRule(TimeZoneRule* rule, UErrorCode& status);
  94. /**
  95. * Makes the <code>TimeZoneRule</code> ready to handle actual timezone
  96. * calculation APIs. This method collects time zone rules specified
  97. * by the caller via the constructor and addTransitionRule() and
  98. * builds internal structure for making the object ready to support
  99. * time zone APIs such as getOffset(), getNextTransition() and others.
  100. * @param status Output param to filled in with a success or an error.
  101. * @stable ICU 3.8
  102. */
  103. void complete(UErrorCode& status);
  104. /**
  105. * Clones TimeZone objects polymorphically. Clients are responsible for deleting
  106. * the TimeZone object cloned.
  107. *
  108. * @return A new copy of this TimeZone object.
  109. * @stable ICU 3.8
  110. */
  111. virtual RuleBasedTimeZone* clone() const override;
  112. /**
  113. * Returns the TimeZone's adjusted GMT offset (i.e., the number of milliseconds to add
  114. * to GMT to get local time in this time zone, taking daylight savings time into
  115. * account) as of a particular reference date. The reference date is used to determine
  116. * whether daylight savings time is in effect and needs to be figured into the offset
  117. * that is returned (in other words, what is the adjusted GMT offset in this time zone
  118. * at this particular date and time?). For the time zones produced by createTimeZone(),
  119. * the reference data is specified according to the Gregorian calendar, and the date
  120. * and time fields are local standard time.
  121. *
  122. * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
  123. * which returns both the raw and the DST offset for a given time. This method
  124. * is retained only for backward compatibility.
  125. *
  126. * @param era The reference date's era
  127. * @param year The reference date's year
  128. * @param month The reference date's month (0-based; 0 is January)
  129. * @param day The reference date's day-in-month (1-based)
  130. * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday)
  131. * @param millis The reference date's milliseconds in day, local standard time
  132. * @param status Output param to filled in with a success or an error.
  133. * @return The offset in milliseconds to add to GMT to get local time.
  134. * @stable ICU 3.8
  135. */
  136. virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
  137. uint8_t dayOfWeek, int32_t millis, UErrorCode& status) const override;
  138. /**
  139. * Gets the time zone offset, for current date, modified in case of
  140. * daylight savings. This is the offset to add *to* UTC to get local time.
  141. *
  142. * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
  143. * which returns both the raw and the DST offset for a given time. This method
  144. * is retained only for backward compatibility.
  145. *
  146. * @param era The reference date's era
  147. * @param year The reference date's year
  148. * @param month The reference date's month (0-based; 0 is January)
  149. * @param day The reference date's day-in-month (1-based)
  150. * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday)
  151. * @param millis The reference date's milliseconds in day, local standard time
  152. * @param monthLength The length of the given month in days.
  153. * @param status Output param to filled in with a success or an error.
  154. * @return The offset in milliseconds to add to GMT to get local time.
  155. * @stable ICU 3.8
  156. */
  157. virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
  158. uint8_t dayOfWeek, int32_t millis,
  159. int32_t monthLength, UErrorCode& status) const override;
  160. /**
  161. * Returns the time zone raw and GMT offset for the given moment
  162. * in time. Upon return, local-millis = GMT-millis + rawOffset +
  163. * dstOffset. All computations are performed in the proleptic
  164. * Gregorian calendar. The default implementation in the TimeZone
  165. * class delegates to the 8-argument getOffset().
  166. *
  167. * @param date moment in time for which to return offsets, in
  168. * units of milliseconds from January 1, 1970 0:00 GMT, either GMT
  169. * time or local wall time, depending on `local'.
  170. * @param local if true, `date' is local wall time; otherwise it
  171. * is in GMT time.
  172. * @param rawOffset output parameter to receive the raw offset, that
  173. * is, the offset not including DST adjustments
  174. * @param dstOffset output parameter to receive the DST offset,
  175. * that is, the offset to be added to `rawOffset' to obtain the
  176. * total offset between local and GMT time. If DST is not in
  177. * effect, this value is zero; otherwise it is a positive value,
  178. * typically one hour.
  179. * @param ec input-output error code
  180. * @stable ICU 3.8
  181. */
  182. virtual void getOffset(UDate date, UBool local, int32_t& rawOffset,
  183. int32_t& dstOffset, UErrorCode& ec) const override;
  184. /**
  185. * Sets the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
  186. * to GMT to get local time, before taking daylight savings time into account).
  187. *
  188. * @param offsetMillis The new raw GMT offset for this time zone.
  189. * @stable ICU 3.8
  190. */
  191. virtual void setRawOffset(int32_t offsetMillis) override;
  192. /**
  193. * Returns the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
  194. * to GMT to get local time, before taking daylight savings time into account).
  195. *
  196. * @return The TimeZone's raw GMT offset.
  197. * @stable ICU 3.8
  198. */
  199. virtual int32_t getRawOffset() const override;
  200. /**
  201. * Queries if this time zone uses daylight savings time.
  202. * @return true if this time zone uses daylight savings time,
  203. * false, otherwise.
  204. * @stable ICU 3.8
  205. */
  206. virtual UBool useDaylightTime() const override;
  207. #ifndef U_FORCE_HIDE_DEPRECATED_API
  208. /**
  209. * Queries if the given date is in daylight savings time in
  210. * this time zone.
  211. * This method is wasteful since it creates a new GregorianCalendar and
  212. * deletes it each time it is called. This is a deprecated method
  213. * and provided only for Java compatibility.
  214. *
  215. * @param date the given UDate.
  216. * @param status Output param filled in with success/error code.
  217. * @return true if the given date is in daylight savings time,
  218. * false, otherwise.
  219. * @deprecated ICU 2.4. Use Calendar::inDaylightTime() instead.
  220. */
  221. virtual UBool inDaylightTime(UDate date, UErrorCode& status) const override;
  222. #endif // U_FORCE_HIDE_DEPRECATED_API
  223. /**
  224. * Returns true if this zone has the same rule and offset as another zone.
  225. * That is, if this zone differs only in ID, if at all.
  226. * @param other the <code>TimeZone</code> object to be compared with
  227. * @return true if the given zone is the same as this one,
  228. * with the possible exception of the ID
  229. * @stable ICU 3.8
  230. */
  231. virtual UBool hasSameRules(const TimeZone& other) const override;
  232. /**
  233. * Gets the first time zone transition after the base time.
  234. * @param base The base time.
  235. * @param inclusive Whether the base time is inclusive or not.
  236. * @param result Receives the first transition after the base time.
  237. * @return true if the transition is found.
  238. * @stable ICU 3.8
  239. */
  240. virtual UBool getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const override;
  241. /**
  242. * Gets the most recent time zone transition before the base time.
  243. * @param base The base time.
  244. * @param inclusive Whether the base time is inclusive or not.
  245. * @param result Receives the most recent transition before the base time.
  246. * @return true if the transition is found.
  247. * @stable ICU 3.8
  248. */
  249. virtual UBool getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const override;
  250. /**
  251. * Returns the number of <code>TimeZoneRule</code>s which represents time transitions,
  252. * for this time zone, that is, all <code>TimeZoneRule</code>s for this time zone except
  253. * <code>InitialTimeZoneRule</code>. The return value range is 0 or any positive value.
  254. * @param status Receives error status code.
  255. * @return The number of <code>TimeZoneRule</code>s representing time transitions.
  256. * @stable ICU 3.8
  257. */
  258. virtual int32_t countTransitionRules(UErrorCode& status) const override;
  259. /**
  260. * Gets the <code>InitialTimeZoneRule</code> and the set of <code>TimeZoneRule</code>
  261. * which represent time transitions for this time zone. On successful return,
  262. * the argument initial points to non-nullptr <code>InitialTimeZoneRule</code> and
  263. * the array trsrules is filled with 0 or multiple <code>TimeZoneRule</code>
  264. * instances up to the size specified by trscount. The results are referencing the
  265. * rule instance held by this time zone instance. Therefore, after this time zone
  266. * is destructed, they are no longer available.
  267. * @param initial Receives the initial timezone rule
  268. * @param trsrules Receives the timezone transition rules
  269. * @param trscount On input, specify the size of the array 'transitions' receiving
  270. * the timezone transition rules. On output, actual number of
  271. * rules filled in the array will be set.
  272. * @param status Receives error status code.
  273. * @stable ICU 3.8
  274. */
  275. virtual void getTimeZoneRules(const InitialTimeZoneRule*& initial,
  276. const TimeZoneRule* trsrules[], int32_t& trscount, UErrorCode& status) const override;
  277. /**
  278. * Get time zone offsets from local wall time.
  279. * @stable ICU 69
  280. */
  281. virtual void getOffsetFromLocal(
  282. UDate date, UTimeZoneLocalOption nonExistingTimeOpt,
  283. UTimeZoneLocalOption duplicatedTimeOpt,
  284. int32_t& rawOffset, int32_t& dstOffset, UErrorCode& status) const override;
  285. private:
  286. void deleteRules();
  287. void deleteTransitions();
  288. UVector* copyRules(UVector* source);
  289. TimeZoneRule* findRuleInFinal(UDate date, UBool local,
  290. int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const;
  291. UBool findNext(UDate base, UBool inclusive, UDate& time, TimeZoneRule*& from, TimeZoneRule*& to) const;
  292. UBool findPrev(UDate base, UBool inclusive, UDate& time, TimeZoneRule*& from, TimeZoneRule*& to) const;
  293. int32_t getLocalDelta(int32_t rawBefore, int32_t dstBefore, int32_t rawAfter, int32_t dstAfter,
  294. int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const;
  295. UDate getTransitionTime(Transition* transition, UBool local,
  296. int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const;
  297. void getOffsetInternal(UDate date, UBool local, int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt,
  298. int32_t& rawOffset, int32_t& dstOffset, UErrorCode& ec) const;
  299. void completeConst(UErrorCode &status) const;
  300. InitialTimeZoneRule *fInitialRule;
  301. UVector *fHistoricRules;
  302. UVector *fFinalRules;
  303. UVector *fHistoricTransitions;
  304. UBool fUpToDate;
  305. public:
  306. /**
  307. * Return the class ID for this class. This is useful only for comparing to
  308. * a return value from getDynamicClassID(). For example:
  309. * <pre>
  310. * . Base* polymorphic_pointer = createPolymorphicObject();
  311. * . if (polymorphic_pointer->getDynamicClassID() ==
  312. * . erived::getStaticClassID()) ...
  313. * </pre>
  314. * @return The class ID for all objects of this class.
  315. * @stable ICU 3.8
  316. */
  317. static UClassID U_EXPORT2 getStaticClassID();
  318. /**
  319. * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
  320. * method is to implement a simple version of RTTI, since not all C++
  321. * compilers support genuine RTTI. Polymorphic operator==() and clone()
  322. * methods call this method.
  323. *
  324. * @return The class ID for this object. All objects of a
  325. * given class have the same class ID. Objects of
  326. * other classes have different class IDs.
  327. * @stable ICU 3.8
  328. */
  329. virtual UClassID getDynamicClassID() const override;
  330. };
  331. U_NAMESPACE_END
  332. #endif /* #if !UCONFIG_NO_FORMATTING */
  333. #endif /* U_SHOW_CPLUSPLUS_API */
  334. #endif // RBTZ_H
  335. //eof