rbtz.h 15 KB

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