]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/vtzone.h
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / vtzone.h
1 // © 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 VTZONE_H
10 #define VTZONE_H
11
12 #include "unicode/utypes.h"
13
14 /**
15 * \file
16 * \brief C++ API: RFC2445 VTIMEZONE support
17 */
18
19 #if !UCONFIG_NO_FORMATTING
20
21 #include "unicode/basictz.h"
22
23 #if U_SHOW_CPLUSPLUS_API
24 U_NAMESPACE_BEGIN
25
26 class VTZWriter;
27 class VTZReader;
28 class UVector;
29
30 /**
31 * <code>VTimeZone</code> is a class implementing RFC2445 VTIMEZONE. You can create a
32 * <code>VTimeZone</code> instance from a time zone ID supported by <code>TimeZone</code>.
33 * With the <code>VTimeZone</code> instance created from the ID, you can write out the rule
34 * in RFC2445 VTIMEZONE format. Also, you can create a <code>VTimeZone</code> instance
35 * from RFC2445 VTIMEZONE data stream, which allows you to calculate time
36 * zone offset by the rules defined by the data. Or, you can create a
37 * <code>VTimeZone</code> from any other ICU <code>BasicTimeZone</code>.
38 * <br><br>
39 * Note: The consumer of this class reading or writing VTIMEZONE data is responsible to
40 * decode or encode Non-ASCII text. Methods reading/writing VTIMEZONE data in this class
41 * do nothing with MIME encoding.
42 * @stable ICU 3.8
43 */
44 class U_I18N_API VTimeZone : public BasicTimeZone {
45 public:
46 /**
47 * Copy constructor.
48 * @param source The <code>VTimeZone</code> object to be copied.
49 * @stable ICU 3.8
50 */
51 VTimeZone(const VTimeZone& source);
52
53 /**
54 * Destructor.
55 * @stable ICU 3.8
56 */
57 virtual ~VTimeZone();
58
59 /**
60 * Assignment operator.
61 * @param right The object to be copied.
62 * @stable ICU 3.8
63 */
64 VTimeZone& operator=(const VTimeZone& right);
65
66 /**
67 * Return true if the given <code>TimeZone</code> objects are
68 * semantically equal. Objects of different subclasses are considered unequal.
69 * @param that The object to be compared with.
70 * @return true if the given <code>TimeZone</code> objects are
71 *semantically equal.
72 * @stable ICU 3.8
73 */
74 virtual UBool operator==(const TimeZone& that) const;
75
76 /**
77 * Return true if the given <code>TimeZone</code> objects are
78 * semantically unequal. Objects of different subclasses are considered unequal.
79 * @param that The object to be compared with.
80 * @return true if the given <code>TimeZone</code> objects are
81 * semantically unequal.
82 * @stable ICU 3.8
83 */
84 virtual UBool operator!=(const TimeZone& that) const;
85
86 /**
87 * Create a <code>VTimeZone</code> instance by the time zone ID.
88 * @param ID The time zone ID, such as America/New_York
89 * @return A <code>VTimeZone</code> object initialized by the time zone ID,
90 * or NULL when the ID is unknown.
91 * @stable ICU 3.8
92 */
93 static VTimeZone* createVTimeZoneByID(const UnicodeString& ID);
94
95 /**
96 * Create a <code>VTimeZone</code> instance using a basic time zone.
97 * @param basicTZ The basic time zone instance
98 * @param status Output param to filled in with a success or an error.
99 * @return A <code>VTimeZone</code> object initialized by the basic time zone.
100 * @stable ICU 4.6
101 */
102 static VTimeZone* createVTimeZoneFromBasicTimeZone(const BasicTimeZone& basicTZ,
103 UErrorCode &status);
104
105 /**
106 * Create a <code>VTimeZone</code> instance by RFC2445 VTIMEZONE data
107 *
108 * @param vtzdata The string including VTIMEZONE data block
109 * @param status Output param to filled in with a success or an error.
110 * @return A <code>VTimeZone</code> initialized by the VTIMEZONE data or
111 * NULL if failed to load the rule from the VTIMEZONE data.
112 * @stable ICU 3.8
113 */
114 static VTimeZone* createVTimeZone(const UnicodeString& vtzdata, UErrorCode& status);
115
116 /**
117 * Gets the RFC2445 TZURL property value. When a <code>VTimeZone</code> instance was
118 * created from VTIMEZONE data, the initial value is set by the TZURL property value
119 * in the data. Otherwise, the initial value is not set.
120 * @param url Receives the RFC2445 TZURL property value.
121 * @return TRUE if TZURL attribute is available and value is set.
122 * @stable ICU 3.8
123 */
124 UBool getTZURL(UnicodeString& url) const;
125
126 /**
127 * Sets the RFC2445 TZURL property value.
128 * @param url The TZURL property value.
129 * @stable ICU 3.8
130 */
131 void setTZURL(const UnicodeString& url);
132
133 /**
134 * Gets the RFC2445 LAST-MODIFIED property value. When a <code>VTimeZone</code> instance
135 * was created from VTIMEZONE data, the initial value is set by the LAST-MODIFIED property
136 * value in the data. Otherwise, the initial value is not set.
137 * @param lastModified Receives the last modified date.
138 * @return TRUE if lastModified attribute is available and value is set.
139 * @stable ICU 3.8
140 */
141 UBool getLastModified(UDate& lastModified) const;
142
143 /**
144 * Sets the RFC2445 LAST-MODIFIED property value.
145 * @param lastModified The LAST-MODIFIED date.
146 * @stable ICU 3.8
147 */
148 void setLastModified(UDate lastModified);
149
150 /**
151 * Writes RFC2445 VTIMEZONE data for this time zone
152 * @param result Output param to filled in with the VTIMEZONE data.
153 * @param status Output param to filled in with a success or an error.
154 * @stable ICU 3.8
155 */
156 void write(UnicodeString& result, UErrorCode& status) const;
157
158 /**
159 * Writes RFC2445 VTIMEZONE data for this time zone applicalbe
160 * for dates after the specified start time.
161 * @param start The start date.
162 * @param result Output param to filled in with the VTIMEZONE data.
163 * @param status Output param to filled in with a success or an error.
164 * @stable ICU 3.8
165 */
166 void write(UDate start, UnicodeString& result, UErrorCode& status) const;
167
168 /**
169 * Writes RFC2445 VTIMEZONE data applicalbe for the specified date.
170 * Some common iCalendar implementations can only handle a single time
171 * zone property or a pair of standard and daylight time properties using
172 * BYDAY rule with day of week (such as BYDAY=1SUN). This method produce
173 * the VTIMEZONE data which can be handled these implementations. The rules
174 * produced by this method can be used only for calculating time zone offset
175 * around the specified date.
176 * @param time The date used for rule extraction.
177 * @param result Output param to filled in with the VTIMEZONE data.
178 * @param status Output param to filled in with a success or an error.
179 * @stable ICU 3.8
180 */
181 void writeSimple(UDate time, UnicodeString& result, UErrorCode& status) const;
182
183 /**
184 * Clones TimeZone objects polymorphically. Clients are responsible for deleting
185 * the TimeZone object cloned.
186 * @return A new copy of this TimeZone object.
187 * @stable ICU 3.8
188 */
189 virtual TimeZone* clone(void) const;
190
191 /**
192 * Returns the TimeZone's adjusted GMT offset (i.e., the number of milliseconds to add
193 * to GMT to get local time in this time zone, taking daylight savings time into
194 * account) as of a particular reference date. The reference date is used to determine
195 * whether daylight savings time is in effect and needs to be figured into the offset
196 * that is returned (in other words, what is the adjusted GMT offset in this time zone
197 * at this particular date and time?). For the time zones produced by createTimeZone(),
198 * the reference data is specified according to the Gregorian calendar, and the date
199 * and time fields are local standard time.
200 *
201 * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
202 * which returns both the raw and the DST offset for a given time. This method
203 * is retained only for backward compatibility.
204 *
205 * @param era The reference date's era
206 * @param year The reference date's year
207 * @param month The reference date's month (0-based; 0 is January)
208 * @param day The reference date's day-in-month (1-based)
209 * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday)
210 * @param millis The reference date's milliseconds in day, local standard time
211 * @param status Output param to filled in with a success or an error.
212 * @return The offset in milliseconds to add to GMT to get local time.
213 * @stable ICU 3.8
214 */
215 virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
216 uint8_t dayOfWeek, int32_t millis, UErrorCode& status) const;
217
218 /**
219 * Gets the time zone offset, for current date, modified in case of
220 * daylight savings. This is the offset to add *to* UTC to get local time.
221 *
222 * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
223 * which returns both the raw and the DST offset for a given time. This method
224 * is retained only for backward compatibility.
225 *
226 * @param era The reference date's era
227 * @param year The reference date's year
228 * @param month The reference date's month (0-based; 0 is January)
229 * @param day The reference date's day-in-month (1-based)
230 * @param dayOfWeek The reference date's day-of-week (1-based; 1 is Sunday)
231 * @param millis The reference date's milliseconds in day, local standard time
232 * @param monthLength The length of the given month in days.
233 * @param status Output param to filled in with a success or an error.
234 * @return The offset in milliseconds to add to GMT to get local time.
235 * @stable ICU 3.8
236 */
237 virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
238 uint8_t dayOfWeek, int32_t millis,
239 int32_t monthLength, UErrorCode& status) const;
240
241 /**
242 * Returns the time zone raw and GMT offset for the given moment
243 * in time. Upon return, local-millis = GMT-millis + rawOffset +
244 * dstOffset. All computations are performed in the proleptic
245 * Gregorian calendar. The default implementation in the TimeZone
246 * class delegates to the 8-argument getOffset().
247 *
248 * @param date moment in time for which to return offsets, in
249 * units of milliseconds from January 1, 1970 0:00 GMT, either GMT
250 * time or local wall time, depending on `local'.
251 * @param local if true, `date' is local wall time; otherwise it
252 * is in GMT time.
253 * @param rawOffset output parameter to receive the raw offset, that
254 * is, the offset not including DST adjustments
255 * @param dstOffset output parameter to receive the DST offset,
256 * that is, the offset to be added to `rawOffset' to obtain the
257 * total offset between local and GMT time. If DST is not in
258 * effect, this value is zero; otherwise it is a positive value,
259 * typically one hour.
260 * @param ec input-output error code
261 * @stable ICU 3.8
262 */
263 virtual void getOffset(UDate date, UBool local, int32_t& rawOffset,
264 int32_t& dstOffset, UErrorCode& ec) const;
265
266 /**
267 * Sets the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
268 * to GMT to get local time, before taking daylight savings time into account).
269 *
270 * @param offsetMillis The new raw GMT offset for this time zone.
271 * @stable ICU 3.8
272 */
273 virtual void setRawOffset(int32_t offsetMillis);
274
275 /**
276 * Returns the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
277 * to GMT to get local time, before taking daylight savings time into account).
278 *
279 * @return The TimeZone's raw GMT offset.
280 * @stable ICU 3.8
281 */
282 virtual int32_t getRawOffset(void) const;
283
284 /**
285 * Queries if this time zone uses daylight savings time.
286 * @return true if this time zone uses daylight savings time,
287 * false, otherwise.
288 * @stable ICU 3.8
289 */
290 virtual UBool useDaylightTime(void) const;
291
292 /**
293 * Queries if the given date is in daylight savings time in
294 * this time zone.
295 * This method is wasteful since it creates a new GregorianCalendar and
296 * deletes it each time it is called. This is a deprecated method
297 * and provided only for Java compatibility.
298 *
299 * @param date the given UDate.
300 * @param status Output param filled in with success/error code.
301 * @return true if the given date is in daylight savings time,
302 * false, otherwise.
303 * @deprecated ICU 2.4. Use Calendar::inDaylightTime() instead.
304 */
305 virtual UBool inDaylightTime(UDate date, UErrorCode& status) const;
306
307 /**
308 * Returns true if this zone has the same rule and offset as another zone.
309 * That is, if this zone differs only in ID, if at all.
310 * @param other the <code>TimeZone</code> object to be compared with
311 * @return true if the given zone is the same as this one,
312 * with the possible exception of the ID
313 * @stable ICU 3.8
314 */
315 virtual UBool hasSameRules(const TimeZone& other) const;
316
317 /**
318 * Gets the first time zone transition after the base time.
319 * @param base The base time.
320 * @param inclusive Whether the base time is inclusive or not.
321 * @param result Receives the first transition after the base time.
322 * @return TRUE if the transition is found.
323 * @stable ICU 3.8
324 */
325 virtual UBool getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const;
326
327 /**
328 * Gets the most recent time zone transition before the base time.
329 * @param base The base time.
330 * @param inclusive Whether the base time is inclusive or not.
331 * @param result Receives the most recent transition before the base time.
332 * @return TRUE if the transition is found.
333 * @stable ICU 3.8
334 */
335 virtual UBool getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const;
336
337 /**
338 * Returns the number of <code>TimeZoneRule</code>s which represents time transitions,
339 * for this time zone, that is, all <code>TimeZoneRule</code>s for this time zone except
340 * <code>InitialTimeZoneRule</code>. The return value range is 0 or any positive value.
341 * @param status Receives error status code.
342 * @return The number of <code>TimeZoneRule</code>s representing time transitions.
343 * @stable ICU 3.8
344 */
345 virtual int32_t countTransitionRules(UErrorCode& status) const;
346
347 /**
348 * Gets the <code>InitialTimeZoneRule</code> and the set of <code>TimeZoneRule</code>
349 * which represent time transitions for this time zone. On successful return,
350 * the argument initial points to non-NULL <code>InitialTimeZoneRule</code> and
351 * the array trsrules is filled with 0 or multiple <code>TimeZoneRule</code>
352 * instances up to the size specified by trscount. The results are referencing the
353 * rule instance held by this time zone instance. Therefore, after this time zone
354 * is destructed, they are no longer available.
355 * @param initial Receives the initial timezone rule
356 * @param trsrules Receives the timezone transition rules
357 * @param trscount On input, specify the size of the array 'transitions' receiving
358 * the timezone transition rules. On output, actual number of
359 * rules filled in the array will be set.
360 * @param status Receives error status code.
361 * @stable ICU 3.8
362 */
363 virtual void getTimeZoneRules(const InitialTimeZoneRule*& initial,
364 const TimeZoneRule* trsrules[], int32_t& trscount, UErrorCode& status) const;
365
366 private:
367 enum { DEFAULT_VTIMEZONE_LINES = 100 };
368
369 /**
370 * Default constructor.
371 */
372 VTimeZone();
373 static VTimeZone* createVTimeZone(VTZReader* reader);
374 void write(VTZWriter& writer, UErrorCode& status) const;
375 void write(UDate start, VTZWriter& writer, UErrorCode& status) const;
376 void writeSimple(UDate time, VTZWriter& writer, UErrorCode& status) const;
377 void load(VTZReader& reader, UErrorCode& status);
378 void parse(UErrorCode& status);
379
380 void writeZone(VTZWriter& w, BasicTimeZone& basictz, UVector* customProps,
381 UErrorCode& status) const;
382
383 void writeHeaders(VTZWriter& w, UErrorCode& status) const;
384 void writeFooter(VTZWriter& writer, UErrorCode& status) const;
385
386 void writeZonePropsByTime(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
387 int32_t fromOffset, int32_t toOffset, UDate time, UBool withRDATE,
388 UErrorCode& status) const;
389 void writeZonePropsByDOM(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
390 int32_t fromOffset, int32_t toOffset,
391 int32_t month, int32_t dayOfMonth, UDate startTime, UDate untilTime,
392 UErrorCode& status) const;
393 void writeZonePropsByDOW(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
394 int32_t fromOffset, int32_t toOffset,
395 int32_t month, int32_t weekInMonth, int32_t dayOfWeek,
396 UDate startTime, UDate untilTime, UErrorCode& status) const;
397 void writeZonePropsByDOW_GEQ_DOM(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
398 int32_t fromOffset, int32_t toOffset,
399 int32_t month, int32_t dayOfMonth, int32_t dayOfWeek,
400 UDate startTime, UDate untilTime, UErrorCode& status) const;
401 void writeZonePropsByDOW_GEQ_DOM_sub(VTZWriter& writer, int32_t month, int32_t dayOfMonth,
402 int32_t dayOfWeek, int32_t numDays,
403 UDate untilTime, int32_t fromOffset, UErrorCode& status) const;
404 void writeZonePropsByDOW_LEQ_DOM(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
405 int32_t fromOffset, int32_t toOffset,
406 int32_t month, int32_t dayOfMonth, int32_t dayOfWeek,
407 UDate startTime, UDate untilTime, UErrorCode& status) const;
408 void writeFinalRule(VTZWriter& writer, UBool isDst, const AnnualTimeZoneRule* rule,
409 int32_t fromRawOffset, int32_t fromDSTSavings,
410 UDate startTime, UErrorCode& status) const;
411
412 void beginZoneProps(VTZWriter& writer, UBool isDst, const UnicodeString& zonename,
413 int32_t fromOffset, int32_t toOffset, UDate startTime, UErrorCode& status) const;
414 void endZoneProps(VTZWriter& writer, UBool isDst, UErrorCode& status) const;
415 void beginRRULE(VTZWriter& writer, int32_t month, UErrorCode& status) const;
416 void appendUNTIL(VTZWriter& writer, const UnicodeString& until, UErrorCode& status) const;
417
418 BasicTimeZone *tz;
419 UVector *vtzlines;
420 UnicodeString tzurl;
421 UDate lastmod;
422 UnicodeString olsonzid;
423 UnicodeString icutzver;
424
425 public:
426 /**
427 * Return the class ID for this class. This is useful only for comparing to
428 * a return value from getDynamicClassID(). For example:
429 * <pre>
430 * . Base* polymorphic_pointer = createPolymorphicObject();
431 * . if (polymorphic_pointer->getDynamicClassID() ==
432 * . erived::getStaticClassID()) ...
433 * </pre>
434 * @return The class ID for all objects of this class.
435 * @stable ICU 3.8
436 */
437 static UClassID U_EXPORT2 getStaticClassID(void);
438
439 /**
440 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
441 * method is to implement a simple version of RTTI, since not all C++
442 * compilers support genuine RTTI. Polymorphic operator==() and clone()
443 * methods call this method.
444 *
445 * @return The class ID for this object. All objects of a
446 * given class have the same class ID. Objects of
447 * other classes have different class IDs.
448 * @stable ICU 3.8
449 */
450 virtual UClassID getDynamicClassID(void) const;
451 };
452
453 U_NAMESPACE_END
454 #endif // U_SHOW_CPLUSPLUS_API
455
456 #endif /* #if !UCONFIG_NO_FORMATTING */
457
458 #endif // VTZONE_H
459 //eof