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"
14 #if U_SHOW_CPLUSPLUS_API
18 * \brief C++ API: RFC2445 VTIMEZONE support
21 #if !UCONFIG_NO_FORMATTING
23 #include "unicode/basictz.h"
32 * <code>VTimeZone</code> is a class implementing RFC2445 VTIMEZONE. You can create a
33 * <code>VTimeZone</code> instance from a time zone ID supported by <code>TimeZone</code>.
34 * With the <code>VTimeZone</code> instance created from the ID, you can write out the rule
35 * in RFC2445 VTIMEZONE format. Also, you can create a <code>VTimeZone</code> instance
36 * from RFC2445 VTIMEZONE data stream, which allows you to calculate time
37 * zone offset by the rules defined by the data. Or, you can create a
38 * <code>VTimeZone</code> from any other ICU <code>BasicTimeZone</code>.
40 * Note: The consumer of this class reading or writing VTIMEZONE data is responsible to
41 * decode or encode Non-ASCII text. Methods reading/writing VTIMEZONE data in this class
42 * do nothing with MIME encoding.
45 class U_I18N_API VTimeZone : public BasicTimeZone {
49 * @param source The <code>VTimeZone</code> object to be copied.
52 VTimeZone(const VTimeZone& source);
61 * Assignment operator.
62 * @param right The object to be copied.
65 VTimeZone& operator=(const VTimeZone& right);
68 * Return true if the given <code>TimeZone</code> objects are
69 * semantically equal. 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
75 virtual bool operator==(const TimeZone& that) const override;
78 * Return true if the given <code>TimeZone</code> objects are
79 * semantically unequal. Objects of different subclasses are considered unequal.
80 * @param that The object to be compared with.
81 * @return true if the given <code>TimeZone</code> objects are
82 * semantically unequal.
85 virtual bool operator!=(const TimeZone& that) const;
88 * Create a <code>VTimeZone</code> instance by the time zone ID.
89 * @param ID The time zone ID, such as America/New_York
90 * @return A <code>VTimeZone</code> object initialized by the time zone ID,
91 * or NULL when the ID is unknown.
94 static VTimeZone* createVTimeZoneByID(const UnicodeString& ID);
97 * Create a <code>VTimeZone</code> instance using a basic time zone.
98 * @param basicTZ The basic time zone instance
99 * @param status Output param to filled in with a success or an error.
100 * @return A <code>VTimeZone</code> object initialized by the basic time zone.
103 static VTimeZone* createVTimeZoneFromBasicTimeZone(const BasicTimeZone& basicTZ,
107 * Create a <code>VTimeZone</code> instance by RFC2445 VTIMEZONE data
109 * @param vtzdata The string including VTIMEZONE data block
110 * @param status Output param to filled in with a success or an error.
111 * @return A <code>VTimeZone</code> initialized by the VTIMEZONE data or
112 * NULL if failed to load the rule from the VTIMEZONE data.
115 static VTimeZone* createVTimeZone(const UnicodeString& vtzdata, UErrorCode& status);
118 * Gets the RFC2445 TZURL property value. When a <code>VTimeZone</code> instance was
119 * created from VTIMEZONE data, the initial value is set by the TZURL property value
120 * in the data. Otherwise, the initial value is not set.
121 * @param url Receives the RFC2445 TZURL property value.
122 * @return true if TZURL attribute is available and value is set.
125 UBool getTZURL(UnicodeString& url) const;
128 * Sets the RFC2445 TZURL property value.
129 * @param url The TZURL property value.
132 void setTZURL(const UnicodeString& url);
135 * Gets the RFC2445 LAST-MODIFIED property value. When a <code>VTimeZone</code> instance
136 * was created from VTIMEZONE data, the initial value is set by the LAST-MODIFIED property
137 * value in the data. Otherwise, the initial value is not set.
138 * @param lastModified Receives the last modified date.
139 * @return true if lastModified attribute is available and value is set.
142 UBool getLastModified(UDate& lastModified) const;
145 * Sets the RFC2445 LAST-MODIFIED property value.
146 * @param lastModified The LAST-MODIFIED date.
149 void setLastModified(UDate lastModified);
152 * Writes RFC2445 VTIMEZONE data for this time zone
153 * @param result Output param to filled in with the VTIMEZONE data.
154 * @param status Output param to filled in with a success or an error.
157 void write(UnicodeString& result, UErrorCode& status) const;
160 * Writes RFC2445 VTIMEZONE data for this time zone applicable
161 * for dates after the specified start time.
162 * @param start The start date.
163 * @param result Output param to filled in with the VTIMEZONE data.
164 * @param status Output param to filled in with a success or an error.
167 void write(UDate start, UnicodeString& result, UErrorCode& status) const;
170 * Writes RFC2445 VTIMEZONE data applicable for the specified date.
171 * Some common iCalendar implementations can only handle a single time
172 * zone property or a pair of standard and daylight time properties using
173 * BYDAY rule with day of week (such as BYDAY=1SUN). This method produce
174 * the VTIMEZONE data which can be handled these implementations. The rules
175 * produced by this method can be used only for calculating time zone offset
176 * around the specified date.
177 * @param time The date used for rule extraction.
178 * @param result Output param to filled in with the VTIMEZONE data.
179 * @param status Output param to filled in with a success or an error.
182 void writeSimple(UDate time, UnicodeString& result, UErrorCode& status) const;
185 * Clones TimeZone objects polymorphically. Clients are responsible for deleting
186 * the TimeZone object cloned.
187 * @return A new copy of this TimeZone object.
190 virtual VTimeZone* clone() const override;
193 * Returns the TimeZone's adjusted GMT offset (i.e., the number of milliseconds to add
194 * to GMT to get local time in this time zone, taking daylight savings time into
195 * account) as of a particular reference date. The reference date is used to determine
196 * whether daylight savings time is in effect and needs to be figured into the offset
197 * that is returned (in other words, what is the adjusted GMT offset in this time zone
198 * at this particular date and time?). For the time zones produced by createTimeZone(),
199 * the reference data is specified according to the Gregorian calendar, and the date
200 * and time fields are local standard time.
202 * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
203 * which returns both the raw and the DST offset for a given time. This method
204 * is retained only for backward compatibility.
206 * @param era The reference date's era
207 * @param year The reference date's year
208 * @param month The reference date's month (0-based; 0 is January)
209 * @param day The reference date's day-in-month (1-based)
210 * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday)
211 * @param millis The reference date's milliseconds in day, local standard time
212 * @param status Output param to filled in with a success or an error.
213 * @return The offset in milliseconds to add to GMT to get local time.
216 virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
217 uint8_t dayOfWeek, int32_t millis, UErrorCode& status) const override;
220 * Gets the time zone offset, for current date, modified in case of
221 * daylight savings. This is the offset to add *to* UTC to get local time.
223 * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
224 * which returns both the raw and the DST offset for a given time. This method
225 * is retained only for backward compatibility.
227 * @param era The reference date's era
228 * @param year The reference date's year
229 * @param month The reference date's month (0-based; 0 is January)
230 * @param day The reference date's day-in-month (1-based)
231 * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday)
232 * @param millis The reference date's milliseconds in day, local standard time
233 * @param monthLength The length of the given month in days.
234 * @param status Output param to filled in with a success or an error.
235 * @return The offset in milliseconds to add to GMT to get local time.
238 virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
239 uint8_t dayOfWeek, int32_t millis,
240 int32_t monthLength, UErrorCode& status) const override;
243 * Returns the time zone raw and GMT offset for the given moment
244 * in time. Upon return, local-millis = GMT-millis + rawOffset +
245 * dstOffset. All computations are performed in the proleptic
246 * Gregorian calendar. The default implementation in the TimeZone
247 * class delegates to the 8-argument getOffset().
249 * @param date moment in time for which to return offsets, in
250 * units of milliseconds from January 1, 1970 0:00 GMT, either GMT
251 * time or local wall time, depending on `local'.
252 * @param local if true, `date' is local wall time; otherwise it
254 * @param rawOffset output parameter to receive the raw offset, that
255 * is, the offset not including DST adjustments
256 * @param dstOffset output parameter to receive the DST offset,
257 * that is, the offset to be added to `rawOffset' to obtain the
258 * total offset between local and GMT time. If DST is not in
259 * effect, this value is zero; otherwise it is a positive value,
260 * typically one hour.
261 * @param ec input-output error code
264 virtual void getOffset(UDate date, UBool local, int32_t& rawOffset,
265 int32_t& dstOffset, UErrorCode& ec) const override;
267 #ifndef U_FORCE_HIDE_DRAFT_API
269 * Get time zone offsets from local wall time.
272 virtual void getOffsetFromLocal(
273 UDate date, UTimeZoneLocalOption nonExistingTimeOpt,
274 UTimeZoneLocalOption duplicatedTimeOpt,
275 int32_t& rawOffset, int32_t& dstOffset, UErrorCode& status) const override;
276 #endif /* U_FORCE_HIDE_DRAFT_API */
279 * Sets the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
280 * to GMT to get local time, before taking daylight savings time into account).
282 * @param offsetMillis The new raw GMT offset for this time zone.
285 virtual void setRawOffset(int32_t offsetMillis) override;
288 * Returns the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
289 * to GMT to get local time, before taking daylight savings time into account).
291 * @return The TimeZone's raw GMT offset.
294 virtual int32_t getRawOffset(void) const override;
297 * Queries if this time zone uses daylight savings time.
298 * @return true if this time zone uses daylight savings time,
302 virtual UBool useDaylightTime(void) const override;
304 #ifndef U_FORCE_HIDE_DEPRECATED_API
306 * Queries if the given date is in daylight savings time in
308 * This method is wasteful since it creates a new GregorianCalendar and
309 * deletes it each time it is called. This is a deprecated method
310 * and provided only for Java compatibility.
312 * @param date the given UDate.
313 * @param status Output param filled in with success/error code.
314 * @return true if the given date is in daylight savings time,
316 * @deprecated ICU 2.4. Use Calendar::inDaylightTime() instead.
318 virtual UBool inDaylightTime(UDate date, UErrorCode& status) const override;
319 #endif // U_FORCE_HIDE_DEPRECATED_API
322 * Returns true if this zone has the same rule and offset as another zone.
323 * That is, if this zone differs only in ID, if at all.
324 * @param other the <code>TimeZone</code> object to be compared with
325 * @return true if the given zone is the same as this one,
326 * with the possible exception of the ID
329 virtual UBool hasSameRules(const TimeZone& other) const override;
332 * Gets the first time zone transition after the base time.
333 * @param base The base time.
334 * @param inclusive Whether the base time is inclusive or not.
335 * @param result Receives the first transition after the base time.
336 * @return true if the transition is found.
339 virtual UBool getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const override;
342 * Gets the most recent time zone transition before the base time.
343 * @param base The base time.
344 * @param inclusive Whether the base time is inclusive or not.
345 * @param result Receives the most recent transition before the base time.
346 * @return true if the transition is found.
349 virtual UBool getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const override;
352 * Returns the number of <code>TimeZoneRule</code>s which represents time transitions,
353 * for this time zone, that is, all <code>TimeZoneRule</code>s for this time zone except
354 * <code>InitialTimeZoneRule</code>. The return value range is 0 or any positive value.
355 * @param status Receives error status code.
356 * @return The number of <code>TimeZoneRule</code>s representing time transitions.
359 virtual int32_t countTransitionRules(UErrorCode& status) const override;
362 * Gets the <code>InitialTimeZoneRule</code> and the set of <code>TimeZoneRule</code>
363 * which represent time transitions for this time zone. On successful return,
364 * the argument initial points to non-NULL <code>InitialTimeZoneRule</code> and
365 * the array trsrules is filled with 0 or multiple <code>TimeZoneRule</code>
366 * instances up to the size specified by trscount. The results are referencing the
367 * rule instance held by this time zone instance. Therefore, after this time zone
368 * is destructed, they are no longer available.
369 * @param initial Receives the initial timezone rule
370 * @param trsrules Receives the timezone transition rules
371 * @param trscount On input, specify the size of the array 'transitions' receiving
372 * the timezone transition rules. On output, actual number of
373 * rules filled in the array will be set.
374 * @param status Receives error status code.
377 virtual void getTimeZoneRules(const InitialTimeZoneRule*& initial,
378 const TimeZoneRule* trsrules[], int32_t& trscount, UErrorCode& status) const override;
381 enum { DEFAULT_VTIMEZONE_LINES = 100 };
384 * Default constructor.
387 static VTimeZone* createVTimeZone(VTZReader* reader);
388 void write(VTZWriter& writer, UErrorCode& status) const;
389 void write(UDate start, VTZWriter& writer, UErrorCode& status) const;
390 void writeSimple(UDate time, VTZWriter& writer, UErrorCode& status) const;
391 void load(VTZReader& reader, UErrorCode& status);
392 void parse(UErrorCode& status);
394 void writeZone(VTZWriter& w, BasicTimeZone& basictz, UVector* customProps,
395 UErrorCode& status) const;
397 void writeHeaders(VTZWriter& w, UErrorCode& status) const;
398 void writeFooter(VTZWriter& writer, UErrorCode& status) const;
400 void writeZonePropsByTime(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
401 int32_t fromOffset, int32_t toOffset, UDate time, UBool withRDATE,
402 UErrorCode& status) const;
403 void writeZonePropsByDOM(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
404 int32_t fromOffset, int32_t toOffset,
405 int32_t month, int32_t dayOfMonth, UDate startTime, UDate untilTime,
406 UErrorCode& status) const;
407 void writeZonePropsByDOW(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
408 int32_t fromOffset, int32_t toOffset,
409 int32_t month, int32_t weekInMonth, int32_t dayOfWeek,
410 UDate startTime, UDate untilTime, UErrorCode& status) const;
411 void writeZonePropsByDOW_GEQ_DOM(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
412 int32_t fromOffset, int32_t toOffset,
413 int32_t month, int32_t dayOfMonth, int32_t dayOfWeek,
414 UDate startTime, UDate untilTime, UErrorCode& status) const;
415 void writeZonePropsByDOW_GEQ_DOM_sub(VTZWriter& writer, int32_t month, int32_t dayOfMonth,
416 int32_t dayOfWeek, int32_t numDays,
417 UDate untilTime, int32_t fromOffset, UErrorCode& status) const;
418 void writeZonePropsByDOW_LEQ_DOM(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
419 int32_t fromOffset, int32_t toOffset,
420 int32_t month, int32_t dayOfMonth, int32_t dayOfWeek,
421 UDate startTime, UDate untilTime, UErrorCode& status) const;
422 void writeFinalRule(VTZWriter& writer, UBool isDst, const AnnualTimeZoneRule* rule,
423 int32_t fromRawOffset, int32_t fromDSTSavings,
424 UDate startTime, UErrorCode& status) const;
426 void beginZoneProps(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
427 int32_t fromOffset, int32_t toOffset, UDate startTime, UErrorCode& status) const;
428 void endZoneProps(VTZWriter& writer, UBool isDst, UErrorCode& status) const;
429 void beginRRULE(VTZWriter& writer, int32_t month, UErrorCode& status) const;
430 void appendUNTIL(VTZWriter& writer, const UnicodeString& until, UErrorCode& status) const;
436 UnicodeString olsonzid;
437 UnicodeString icutzver;
441 * Return the class ID for this class. This is useful only for comparing to
442 * a return value from getDynamicClassID(). For example:
444 * . Base* polymorphic_pointer = createPolymorphicObject();
445 * . if (polymorphic_pointer->getDynamicClassID() ==
446 * . erived::getStaticClassID()) ...
448 * @return The class ID for all objects of this class.
451 static UClassID U_EXPORT2 getStaticClassID(void);
454 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
455 * method is to implement a simple version of RTTI, since not all C++
456 * compilers support genuine RTTI. Polymorphic operator==() and clone()
457 * methods call this method.
459 * @return The class ID for this object. All objects of a
460 * given class have the same class ID. Objects of
461 * other classes have different class IDs.
464 virtual UClassID getDynamicClassID(void) const override;
469 #endif /* #if !UCONFIG_NO_FORMATTING */
471 #endif /* U_SHOW_CPLUSPLUS_API */