rbtz.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 UBool operator==(const TimeZone& that) const;
  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 UBool operator!=(const TimeZone& that) const;
  77. /**
  78. * Adds the <code>TimeZoneRule</code> which represents time transitions.
  79. * The <code>TimeZoneRule</code> 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 <code>TimeZoneRule</code> is adopted by this
  83. * <code>RuleBasedTimeZone</code> on successful completion of this method,
  84. * thus, the caller must not delete it when no error is returned.
  85. * After all rules are added, the caller must call complete() method to
  86. * make this <code>RuleBasedTimeZone</code> ready to handle common time
  87. * zone functions.
  88. * @param rule The <code>TimeZoneRule</code>.
  89. * @param status Output param to filled in with a success or an error.
  90. * @stable ICU 3.8
  91. */
  92. void addTransitionRule(TimeZoneRule* rule, UErrorCode& status);
  93. /**
  94. * Makes the <code>TimeZoneRule</code> ready to handle actual timezone
  95. * calcuation APIs. This method collects time zone rules specified
  96. * by the caller via the constructor and addTransitionRule() and
  97. * builds internal structure for making the object ready to support
  98. * time zone APIs such as getOffset(), getNextTransition() and others.
  99. * @param status Output param to filled in with a success or an error.
  100. * @stable ICU 3.8
  101. */
  102. void complete(UErrorCode& status);
  103. /**
  104. * Clones TimeZone objects polymorphically. Clients are responsible for deleting
  105. * the TimeZone object cloned.
  106. *
  107. * @return A new copy of this TimeZone object.
  108. * @stable ICU 3.8
  109. */
  110. virtual RuleBasedTimeZone* clone() const;
  111. /**
  112. * Returns the TimeZone's adjusted GMT offset (i.e., the number of milliseconds to add
  113. * to GMT to get local time in this time zone, taking daylight savings time into
  114. * account) as of a particular reference date. The reference date is used to determine
  115. * whether daylight savings time is in effect and needs to be figured into the offset
  116. * that is returned (in other words, what is the adjusted GMT offset in this time zone
  117. * at this particular date and time?). For the time zones produced by createTimeZone(),
  118. * the reference data is specified according to the Gregorian calendar, and the date
  119. * and time fields are local standard time.
  120. *
  121. * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
  122. * which returns both the raw and the DST offset for a given time. This method
  123. * is retained only for backward compatibility.
  124. *
  125. * @param era The reference date's era
  126. * @param year The reference date's year
  127. * @param month The reference date's month (0-based; 0 is January)
  128. * @param day The reference date's day-in-month (1-based)
  129. * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday)
  130. * @param millis The reference date's milliseconds in day, local standard time
  131. * @param status Output param to filled in with a success or an error.
  132. * @return The offset in milliseconds to add to GMT to get local time.
  133. * @stable ICU 3.8
  134. */
  135. virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
  136. uint8_t dayOfWeek, int32_t millis, UErrorCode& status) const;
  137. /**
  138. * Gets the time zone offset, for current date, modified in case of
  139. * daylight savings. This is the offset to add *to* UTC to get local time.
  140. *
  141. * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
  142. * which returns both the raw and the DST offset for a given time. This method
  143. * is retained only for backward compatibility.
  144. *
  145. * @param era The reference date's era
  146. * @param year The reference date's year
  147. * @param month The reference date's month (0-based; 0 is January)
  148. * @param day The reference date's day-in-month (1-based)
  149. * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday)
  150. * @param millis The reference date's milliseconds in day, local standard time
  151. * @param monthLength The length of the given month in days.
  152. * @param status Output param to filled in with a success or an error.
  153. * @return The offset in milliseconds to add to GMT to get local time.
  154. * @stable ICU 3.8
  155. */
  156. virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
  157. uint8_t dayOfWeek, int32_t millis,
  158. int32_t monthLength, UErrorCode& status) const;
  159. /**
  160. * Returns the time zone raw and GMT offset for the given moment
  161. * in time. Upon return, local-millis = GMT-millis + rawOffset +
  162. * dstOffset. All computations are performed in the proleptic
  163. * Gregorian calendar. The default implementation in the TimeZone
  164. * class delegates to the 8-argument getOffset().
  165. *
  166. * @param date moment in time for which to return offsets, in
  167. * units of milliseconds from January 1, 1970 0:00 GMT, either GMT
  168. * time or local wall time, depending on `local'.
  169. * @param local if true, `date' is local wall time; otherwise it
  170. * is in GMT time.
  171. * @param rawOffset output parameter to receive the raw offset, that
  172. * is, the offset not including DST adjustments
  173. * @param dstOffset output parameter to receive the DST offset,
  174. * that is, the offset to be added to `rawOffset' to obtain the
  175. * total offset between local and GMT time. If DST is not in
  176. * effect, this value is zero; otherwise it is a positive value,
  177. * typically one hour.
  178. * @param ec input-output error code
  179. * @stable ICU 3.8
  180. */
  181. virtual void getOffset(UDate date, UBool local, int32_t& rawOffset,
  182. int32_t& dstOffset, UErrorCode& ec) const;
  183. /**
  184. * Sets the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
  185. * to GMT to get local time, before taking daylight savings time into account).
  186. *
  187. * @param offsetMillis The new raw GMT offset for this time zone.
  188. * @stable ICU 3.8
  189. */
  190. virtual void setRawOffset(int32_t offsetMillis);
  191. /**
  192. * Returns the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
  193. * to GMT to get local time, before taking daylight savings time into account).
  194. *
  195. * @return The TimeZone's raw GMT offset.
  196. * @stable ICU 3.8
  197. */
  198. virtual int32_t getRawOffset(void) const;
  199. /**
  200. * Queries if this time zone uses daylight savings time.
  201. * @return true if this time zone uses daylight savings time,
  202. * false, otherwise.
  203. * @stable ICU 3.8
  204. */
  205. virtual UBool useDaylightTime(void) const;
  206. #ifndef U_FORCE_HIDE_DEPRECATED_API
  207. /**
  208. * Queries if the given date is in daylight savings time in
  209. * this time zone.
  210. * This method is wasteful since it creates a new GregorianCalendar and
  211. * deletes it each time it is called. This is a deprecated method
  212. * and provided only for Java compatibility.
  213. *
  214. * @param date the given UDate.
  215. * @param status Output param filled in with success/error code.
  216. * @return true if the given date is in daylight savings time,
  217. * false, otherwise.
  218. * @deprecated ICU 2.4. Use Calendar::inDaylightTime() instead.
  219. */
  220. virtual UBool inDaylightTime(UDate date, UErrorCode& status) const;
  221. #endif // U_FORCE_HIDE_DEPRECATED_API
  222. /**
  223. * Returns true if this zone has the same rule and offset as another zone.
  224. * That is, if this zone differs only in ID, if at all.
  225. * @param other the <code>TimeZone</code> object to be compared with
  226. * @return true if the given zone is the same as this one,
  227. * with the possible exception of the ID
  228. * @stable ICU 3.8
  229. */
  230. virtual UBool hasSameRules(const TimeZone& other) const;
  231. /**
  232. * Gets the first time zone transition after the base time.
  233. * @param base The base time.
  234. * @param inclusive Whether the base time is inclusive or not.
  235. * @param result Receives the first transition after the base time.
  236. * @return TRUE if the transition is found.
  237. * @stable ICU 3.8
  238. */
  239. virtual UBool getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const;
  240. /**
  241. * Gets the most recent time zone transition before the base time.
  242. * @param base The base time.
  243. * @param inclusive Whether the base time is inclusive or not.
  244. * @param result Receives the most recent transition before the base time.
  245. * @return TRUE if the transition is found.
  246. * @stable ICU 3.8
  247. */
  248. virtual UBool getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const;
  249. /**
  250. * Returns the number of <code>TimeZoneRule</code>s which represents time transitions,
  251. * for this time zone, that is, all <code>TimeZoneRule</code>s for this time zone except
  252. * <code>InitialTimeZoneRule</code>. The return value range is 0 or any positive value.
  253. * @param status Receives error status code.
  254. * @return The number of <code>TimeZoneRule</code>s representing time transitions.
  255. * @stable ICU 3.8
  256. */
  257. virtual int32_t countTransitionRules(UErrorCode& status) const;
  258. /**
  259. * Gets the <code>InitialTimeZoneRule</code> and the set of <code>TimeZoneRule</code>
  260. * which represent time transitions for this time zone. On successful return,
  261. * the argument initial points to non-NULL <code>InitialTimeZoneRule</code> and
  262. * the array trsrules is filled with 0 or multiple <code>TimeZoneRule</code>
  263. * instances up to the size specified by trscount. The results are referencing the
  264. * rule instance held by this time zone instance. Therefore, after this time zone
  265. * is destructed, they are no longer available.
  266. * @param initial Receives the initial timezone rule
  267. * @param trsrules Receives the timezone transition rules
  268. * @param trscount On input, specify the size of the array 'transitions' receiving
  269. * the timezone transition rules. On output, actual number of
  270. * rules filled in the array will be set.
  271. * @param status Receives error status code.
  272. * @stable ICU 3.8
  273. */
  274. virtual void getTimeZoneRules(const InitialTimeZoneRule*& initial,
  275. const TimeZoneRule* trsrules[], int32_t& trscount, UErrorCode& status) const;
  276. /**
  277. * Get time zone offsets from local wall time.
  278. * @internal
  279. */
  280. virtual void getOffsetFromLocal(UDate date, int32_t nonExistingTimeOpt, int32_t duplicatedTimeOpt,
  281. int32_t& rawOffset, int32_t& dstOffset, UErrorCode& status) const;
  282. private:
  283. void deleteRules(void);
  284. void deleteTransitions(void);
  285. UVector* copyRules(UVector* source);
  286. TimeZoneRule* findRuleInFinal(UDate date, UBool local,
  287. int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const;
  288. UBool findNext(UDate base, UBool inclusive, UDate& time, TimeZoneRule*& from, TimeZoneRule*& to) const;
  289. UBool findPrev(UDate base, UBool inclusive, UDate& time, TimeZoneRule*& from, TimeZoneRule*& to) const;
  290. int32_t getLocalDelta(int32_t rawBefore, int32_t dstBefore, int32_t rawAfter, int32_t dstAfter,
  291. int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const;
  292. UDate getTransitionTime(Transition* transition, UBool local,
  293. int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const;
  294. void getOffsetInternal(UDate date, UBool local, int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt,
  295. int32_t& rawOffset, int32_t& dstOffset, UErrorCode& ec) const;
  296. void completeConst(UErrorCode &status) const;
  297. InitialTimeZoneRule *fInitialRule;
  298. UVector *fHistoricRules;
  299. UVector *fFinalRules;
  300. UVector *fHistoricTransitions;
  301. UBool fUpToDate;
  302. public:
  303. /**
  304. * Return the class ID for this class. This is useful only for comparing to
  305. * a return value from getDynamicClassID(). For example:
  306. * <pre>
  307. * . Base* polymorphic_pointer = createPolymorphicObject();
  308. * . if (polymorphic_pointer->getDynamicClassID() ==
  309. * . erived::getStaticClassID()) ...
  310. * </pre>
  311. * @return The class ID for all objects of this class.
  312. * @stable ICU 3.8
  313. */
  314. static UClassID U_EXPORT2 getStaticClassID(void);
  315. /**
  316. * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
  317. * method is to implement a simple version of RTTI, since not all C++
  318. * compilers support genuine RTTI. Polymorphic operator==() and clone()
  319. * methods call this method.
  320. *
  321. * @return The class ID for this object. All objects of a
  322. * given class have the same class ID. Objects of
  323. * other classes have different class IDs.
  324. * @stable ICU 3.8
  325. */
  326. virtual UClassID getDynamicClassID(void) const;
  327. };
  328. U_NAMESPACE_END
  329. #endif /* #if !UCONFIG_NO_FORMATTING */
  330. #endif /* U_SHOW_CPLUSPLUS_API */
  331. #endif // RBTZ_H
  332. //eof