vtzone.h 20 KB

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