vtzone.h 20 KB

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