rbtz.h 15 KB

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