1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2007-2013, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
12 #include "unicode/utypes.h"
16 * \brief C++ API: Rule based customizable time zone
19 #if !UCONFIG_NO_FORMATTING
21 #include "unicode/basictz.h"
22 #include "unicode/unistr.h"
24 #if U_SHOW_CPLUSPLUS_API
27 // forward declaration
32 * a BasicTimeZone subclass implemented in terms of InitialTimeZoneRule and TimeZoneRule instances
34 * @see InitialTimeZoneRule
37 class U_I18N_API RuleBasedTimeZone
: public BasicTimeZone
{
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
44 * @param id The time zone ID.
45 * @param initialRule The initial time zone rule.
48 RuleBasedTimeZone(const UnicodeString
& id
, InitialTimeZoneRule
* initialRule
);
52 * @param source The RuleBasedTimeZone object to be copied.
55 RuleBasedTimeZone(const RuleBasedTimeZone
& source
);
61 virtual ~RuleBasedTimeZone();
64 * Assignment operator.
65 * @param right The object to be copied.
68 RuleBasedTimeZone
& operator=(const RuleBasedTimeZone
& right
);
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
78 virtual UBool
operator==(const TimeZone
& that
) const;
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.
88 virtual UBool
operator!=(const TimeZone
& that
) const;
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
101 * @param rule The <code>TimeZoneRule</code>.
102 * @param status Output param to filled in with a success or an error.
105 void addTransitionRule(TimeZoneRule
* rule
, UErrorCode
& status
);
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.
116 void complete(UErrorCode
& status
);
119 * Clones TimeZone objects polymorphically. Clients are responsible for deleting
120 * the TimeZone object cloned.
122 * @return A new copy of this TimeZone object.
125 virtual TimeZone
* clone(void) const;
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.
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.
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.
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;
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.
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.
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.
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;
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().
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
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
199 virtual void getOffset(UDate date
, UBool local
, int32_t& rawOffset
,
200 int32_t& dstOffset
, UErrorCode
& ec
) const;
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).
206 * @param offsetMillis The new raw GMT offset for this time zone.
209 virtual void setRawOffset(int32_t offsetMillis
);
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).
215 * @return The TimeZone's raw GMT offset.
218 virtual int32_t getRawOffset(void) const;
221 * Queries if this time zone uses daylight savings time.
222 * @return true if this time zone uses daylight savings time,
226 virtual UBool
useDaylightTime(void) const;
229 * Queries if the given date is in daylight savings time in
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.
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,
239 * @deprecated ICU 2.4. Use Calendar::inDaylightTime() instead.
241 virtual UBool
inDaylightTime(UDate date
, UErrorCode
& status
) const;
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
251 virtual UBool
hasSameRules(const TimeZone
& other
) const;
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.
261 virtual UBool
getNextTransition(UDate base
, UBool inclusive
, TimeZoneTransition
& result
) const;
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.
271 virtual UBool
getPreviousTransition(UDate base
, UBool inclusive
, TimeZoneTransition
& result
) const;
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.
281 virtual int32_t countTransitionRules(UErrorCode
& status
) const;
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.
299 virtual void getTimeZoneRules(const InitialTimeZoneRule
*& initial
,
300 const TimeZoneRule
* trsrules
[], int32_t& trscount
, UErrorCode
& status
) const;
303 * Get time zone offsets from local wall time.
306 virtual void getOffsetFromLocal(UDate date
, int32_t nonExistingTimeOpt
, int32_t duplicatedTimeOpt
,
307 int32_t& rawOffset
, int32_t& dstOffset
, UErrorCode
& status
) const;
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;
323 void completeConst(UErrorCode
&status
) const;
325 InitialTimeZoneRule
*fInitialRule
;
326 UVector
*fHistoricRules
;
327 UVector
*fFinalRules
;
328 UVector
*fHistoricTransitions
;
333 * Return the class ID for this class. This is useful only for comparing to
334 * a return value from getDynamicClassID(). For example:
336 * . Base* polymorphic_pointer = createPolymorphicObject();
337 * . if (polymorphic_pointer->getDynamicClassID() ==
338 * . erived::getStaticClassID()) ...
340 * @return The class ID for all objects of this class.
343 static UClassID U_EXPORT2
getStaticClassID(void);
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.
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.
356 virtual UClassID
getDynamicClassID(void) const;
360 #endif // U_SHOW_CPLUSPLUS_API
362 #endif /* #if !UCONFIG_NO_FORMATTING */