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