vtzone.h 21 KB

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