1. /*
  2. * @(#)SimpleTimeZone.java 1.49 04/01/12
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  9. * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  10. *
  11. * The original version of this source code and documentation is copyrighted
  12. * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  13. * materials are provided under terms of a License Agreement between Taligent
  14. * and Sun. This technology is protected by multiple US and International
  15. * patents. This notice and attribution to Taligent may not be removed.
  16. * Taligent is a registered trademark of Taligent, Inc.
  17. *
  18. */
  19. package java.util;
  20. import java.io.ObjectInputStream;
  21. import java.io.ObjectOutputStream;
  22. import java.io.IOException;
  23. import sun.util.calendar.CalendarSystem;
  24. import sun.util.calendar.CalendarUtils;
  25. import sun.util.calendar.BaseCalendar;
  26. import sun.util.calendar.Gregorian;
  27. /**
  28. * <code>SimpleTimeZone</code> is a concrete subclass of <code>TimeZone</code>
  29. * that represents a time zone for use with a Gregorian calendar.
  30. * The class holds an offset from GMT, called <em>raw offset</em>, and start
  31. * and end rules for a daylight saving time schedule. Since it only holds
  32. * single values for each, it cannot handle historical changes in the offset
  33. * from GMT and the daylight saving schedule, except that the {@link
  34. * #setStartYear setStartYear} method can specify the year when the daylight
  35. * saving time schedule starts in effect.
  36. * <p>
  37. * To construct a <code>SimpleTimeZone</code> with a daylight saving time
  38. * schedule, the schedule can be described with a set of rules,
  39. * <em>start-rule</em> and <em>end-rule</em>. A day when daylight saving time
  40. * starts or ends is specified by a combination of <em>month</em>,
  41. * <em>day-of-month</em>, and <em>day-of-week</em> values. The <em>month</em>
  42. * value is represented by a Calendar {@link Calendar#MONTH MONTH} field
  43. * value, such as {@link Calendar#MARCH}. The <em>day-of-week</em> value is
  44. * represented by a Calendar {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} value,
  45. * such as {@link Calendar#SUNDAY SUNDAY}. The meanings of value combinations
  46. * are as follows.
  47. *
  48. * <ul>
  49. * <li><b>Exact day of month</b><br>
  50. * To specify an exact day of month, set the <em>month</em> and
  51. * <em>day-of-month</em> to an exact value, and <em>day-of-week</em> to zero. For
  52. * example, to specify March 1, set the <em>month</em> to {@link Calendar#MARCH
  53. * MARCH}, <em>day-of-month</em> to 1, and <em>day-of-week</em> to 0.</li>
  54. *
  55. * <li><b>Day of week on or after day of month</b><br>
  56. * To specify a day of week on or after an exact day of month, set the
  57. * <em>month</em> to an exact month value, <em>day-of-month</em> to the day on
  58. * or after which the rule is applied, and <em>day-of-week</em> to a negative {@link
  59. * Calendar#DAY_OF_WEEK DAY_OF_WEEK} field value. For example, to specify the
  60. * second Sunday of April, set <em>month</em> to {@link Calendar#APRIL APRIL},
  61. * <em>day-of-month</em> to 8, and <em>day-of-week</em> to <code>-</code>{@link
  62. * Calendar#SUNDAY SUNDAY}.</li>
  63. *
  64. * <li><b>Day of week on or before day of month</b><br>
  65. * To specify a day of the week on or before an exact day of the month, set
  66. * <em>day-of-month</em> and <em>day-of-week</em> to a negative value. For
  67. * example, to specify the last Wednesday on or before the 21st of March, set
  68. * <em>month</em> to {@link Calendar#MARCH MARCH}, <em>day-of-month</em> is -21
  69. * and <em>day-of-week</em> is <code>-</code>{@link Calendar#WEDNESDAY WEDNESDAY}. </li>
  70. *
  71. * <li><b>Last day-of-week of month</b><br>
  72. * To specify, the last day-of-week of the month, set <em>day-of-week</em> to a
  73. * {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} value and <em>day-of-month</em> to
  74. * -1. For example, to specify the last Sunday of October, set <em>month</em>
  75. * to {@link Calendar#OCTOBER OCTOBER}, <em>day-of-week</em> to {@link
  76. * Calendar#SUNDAY SUNDAY} and <em>day-of-month</em> to -1. </li>
  77. *
  78. * </ul>
  79. * The time of the day at which daylight saving time starts or ends is
  80. * specified by a millisecond value within the day. There are three kinds of
  81. * <em>mode</em>s to specify the time: {@link #WALL_TIME}, {@link
  82. * #STANDARD_TIME} and {@link #UTC_TIME}. For example, if daylight
  83. * saving time ends
  84. * at 2:00 am in the wall clock time, it can be specified by 7200000
  85. * milliseconds in the {@link #WALL_TIME} mode. In this case, the wall clock time
  86. * for an <em>end-rule</em> means the same thing as the daylight time.
  87. * <p>
  88. * The following are examples of parameters for constructing time zone objects.
  89. * <pre><code>
  90. * // Base GMT offset: -8:00
  91. * // DST starts: at 2:00am in standard time
  92. * // on the first Sunday in April
  93. * // DST ends: at 2:00am in daylight time
  94. * // on the last Sunday in October
  95. * // Save: 1 hour
  96. * SimpleTimeZone(-28800000,
  97. * "America/Los_Angeles",
  98. * Calendar.APRIL, 1, -Calendar.SUNDAY,
  99. * 7200000,
  100. * Calendar.OCTOBER, -1, Calendar.SUNDAY,
  101. * 7200000,
  102. * 3600000)
  103. *
  104. * // Base GMT offset: +1:00
  105. * // DST starts: at 1:00am in UTC time
  106. * // on the last Sunday in March
  107. * // DST ends: at 1:00am in UTC time
  108. * // on the last Sunday in October
  109. * // Save: 1 hour
  110. * SimpleTimeZone(3600000,
  111. * "Europe/Paris",
  112. * Calendar.MARCH, -1, Calendar.SUNDAY,
  113. * 3600000, SimpleTimeZone.UTC_TIME,
  114. * Calendar.OCTOBER, -1, Calendar.SUNDAY,
  115. * 3600000, SimpleTimeZone.UTC_TIME,
  116. * 3600000)
  117. * </code></pre>
  118. * These parameter rules are also applicable to the set rule methods, such as
  119. * <code>setStartRule</code>.
  120. *
  121. * @since 1.1
  122. * @see Calendar
  123. * @see GregorianCalendar
  124. * @see TimeZone
  125. * @version 1.49 01/12/04
  126. * @author David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu
  127. */
  128. public class SimpleTimeZone extends TimeZone {
  129. /**
  130. * Constructs a SimpleTimeZone with the given base time zone offset from GMT
  131. * and time zone ID with no daylight saving time schedule.
  132. *
  133. * @param rawOffset The base time zone offset in milliseconds to GMT.
  134. * @param ID The time zone name that is given to this instance.
  135. */
  136. public SimpleTimeZone(int rawOffset, String ID)
  137. {
  138. this.rawOffset = rawOffset;
  139. setID (ID);
  140. dstSavings = millisPerHour; // In case user sets rules later
  141. }
  142. /**
  143. * Constructs a SimpleTimeZone with the given base time zone offset from
  144. * GMT, time zone ID, and rules for starting and ending the daylight
  145. * time.
  146. * Both <code>startTime</code> and <code>endTime</code> are specified to be
  147. * represented in the wall clock time. The amount of daylight saving is
  148. * assumed to be 3600000 milliseconds (i.e., one hour). This constructor is
  149. * equivalent to:
  150. * <pre><code>
  151. * SimpleTimeZone(rawOffset,
  152. * ID,
  153. * startMonth,
  154. * startDay,
  155. * startDayOfWeek,
  156. * startTime,
  157. * SimpleTimeZone.{@link #WALL_TIME},
  158. * endMonth,
  159. * endDay,
  160. * endDayOfWeek,
  161. * endTime,
  162. * SimpleTimeZone.{@link #WALL_TIME},
  163. * 3600000)
  164. * </code></pre>
  165. *
  166. * @param rawOffset The given base time zone offset from GMT.
  167. * @param ID The time zone ID which is given to this object.
  168. * @param startMonth The daylight saving time starting month. Month is
  169. * a {@link Calendar#MONTH MONTH} field value (0-based. e.g., 0
  170. * for January).
  171. * @param startDay The day of the month on which the daylight saving time starts.
  172. * See the class description for the special cases of this parameter.
  173. * @param startDayOfWeek The daylight saving time starting day-of-week.
  174. * See the class description for the special cases of this parameter.
  175. * @param startTime The daylight saving time starting time in local wall clock
  176. * time (in milliseconds within the day), which is local
  177. * standard time in this case.
  178. * @param endMonth The daylight saving time ending month. Month is
  179. * a {@link Calendar#MONTH MONTH} field
  180. * value (0-based. e.g., 9 for October).
  181. * @param endDay The day of the month on which the daylight saving time ends.
  182. * See the class description for the special cases of this parameter.
  183. * @param endDayOfWeek The daylight saving time ending day-of-week.
  184. * See the class description for the special cases of this parameter.
  185. * @param endTime The daylight saving ending time in local wall clock time,
  186. * (in milliseconds within the day) which is local daylight
  187. * time in this case.
  188. * @exception IllegalArgumentException if the month, day, dayOfWeek, or time
  189. * parameters are out of range for the start or end rule
  190. */
  191. public SimpleTimeZone(int rawOffset, String ID,
  192. int startMonth, int startDay, int startDayOfWeek, int startTime,
  193. int endMonth, int endDay, int endDayOfWeek, int endTime)
  194. {
  195. this(rawOffset, ID,
  196. startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,
  197. endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,
  198. millisPerHour);
  199. }
  200. /**
  201. * Constructs a SimpleTimeZone with the given base time zone offset from
  202. * GMT, time zone ID, and rules for starting and ending the daylight
  203. * time.
  204. * Both <code>startTime</code> and <code>endTime</code> are assumed to be
  205. * represented in the wall clock time. This constructor is equivalent to:
  206. * <pre><code>
  207. * SimpleTimeZone(rawOffset,
  208. * ID,
  209. * startMonth,
  210. * startDay,
  211. * startDayOfWeek,
  212. * startTime,
  213. * SimpleTimeZone.{@link #WALL_TIME},
  214. * endMonth,
  215. * endDay,
  216. * endDayOfWeek,
  217. * endTime,
  218. * SimpleTimeZone.{@link #WALL_TIME},
  219. * dstSavings)
  220. * </code></pre>
  221. *
  222. * @param rawOffset The given base time zone offset from GMT.
  223. * @param ID The time zone ID which is given to this object.
  224. * @param startMonth The daylight saving time starting month. Month is
  225. * a {@link Calendar#MONTH MONTH} field
  226. * value (0-based. e.g., 0 for January).
  227. * @param startDay The day of the month on which the daylight saving time starts.
  228. * See the class description for the special cases of this parameter.
  229. * @param startDayOfWeek The daylight saving time starting day-of-week.
  230. * See the class description for the special cases of this parameter.
  231. * @param startTime The daylight saving time starting time in local wall clock
  232. * time, which is local standard time in this case.
  233. * @param endMonth The daylight saving time ending month. Month is
  234. * a {@link Calendar#MONTH MONTH} field
  235. * value (0-based. e.g., 9 for October).
  236. * @param endDay The day of the month on which the daylight saving time ends.
  237. * See the class description for the special cases of this parameter.
  238. * @param endDayOfWeek The daylight saving time ending day-of-week.
  239. * See the class description for the special cases of this parameter.
  240. * @param endTime The daylight saving ending time in local wall clock time,
  241. * which is local daylight time in this case.
  242. * @param dstSavings The amount of time in milliseconds saved during
  243. * daylight saving time.
  244. * @exception IllegalArgumentException if the month, day, dayOfWeek, or time
  245. * parameters are out of range for the start or end rule
  246. * @since 1.2
  247. */
  248. public SimpleTimeZone(int rawOffset, String ID,
  249. int startMonth, int startDay, int startDayOfWeek, int startTime,
  250. int endMonth, int endDay, int endDayOfWeek, int endTime,
  251. int dstSavings)
  252. {
  253. this(rawOffset, ID,
  254. startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,
  255. endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,
  256. dstSavings);
  257. }
  258. /**
  259. * Constructs a SimpleTimeZone with the given base time zone offset from
  260. * GMT, time zone ID, and rules for starting and ending the daylight
  261. * time.
  262. * This constructor takes the full set of the start and end rules
  263. * parameters, including modes of <code>startTime</code> and
  264. * <code>endTime</code>. The mode specifies either {@link #WALL_TIME wall
  265. * time} or {@link #STANDARD_TIME standard time} or {@link #UTC_TIME UTC
  266. * time}.
  267. *
  268. * @param rawOffset The given base time zone offset from GMT.
  269. * @param ID The time zone ID which is given to this object.
  270. * @param startMonth The daylight saving time starting month. Month is
  271. * a {@link Calendar#MONTH MONTH} field
  272. * value (0-based. e.g., 0 for January).
  273. * @param startDay The day of the month on which the daylight saving time starts.
  274. * See the class description for the special cases of this parameter.
  275. * @param startDayOfWeek The daylight saving time starting day-of-week.
  276. * See the class description for the special cases of this parameter.
  277. * @param startTime The daylight saving time starting time in the time mode
  278. * specified by <code>startTimeMode</code>.
  279. * @param startTimeMode The mode of the start time specified by startTime.
  280. * @param endMonth The daylight saving time ending month. Month is
  281. * a {@link Calendar#MONTH MONTH} field
  282. * value (0-based. e.g., 9 for October).
  283. * @param endDay The day of the month on which the daylight saving time ends.
  284. * See the class description for the special cases of this parameter.
  285. * @param endDayOfWeek The daylight saving time ending day-of-week.
  286. * See the class description for the special cases of this parameter.
  287. * @param endTime The daylight saving ending time in time time mode
  288. * specified by <code>endTimeMode</code>.
  289. * @param endTimeMode The mode of the end time specified by endTime
  290. * @param dstSavings The amount of time in milliseconds saved during
  291. * daylight saving time.
  292. *
  293. * @exception IllegalArgumentException if the month, day, dayOfWeek, time more, or
  294. * time parameters are out of range for the start or end rule, or if a time mode
  295. * value is invalid.
  296. *
  297. * @see #WALL_TIME
  298. * @see #STANDARD_TIME
  299. * @see #UTC_TIME
  300. *
  301. * @since 1.4
  302. */
  303. public SimpleTimeZone(int rawOffset, String ID,
  304. int startMonth, int startDay, int startDayOfWeek,
  305. int startTime, int startTimeMode,
  306. int endMonth, int endDay, int endDayOfWeek,
  307. int endTime, int endTimeMode,
  308. int dstSavings) {
  309. setID(ID);
  310. this.rawOffset = rawOffset;
  311. this.startMonth = startMonth;
  312. this.startDay = startDay;
  313. this.startDayOfWeek = startDayOfWeek;
  314. this.startTime = startTime;
  315. this.startTimeMode = startTimeMode;
  316. this.endMonth = endMonth;
  317. this.endDay = endDay;
  318. this.endDayOfWeek = endDayOfWeek;
  319. this.endTime = endTime;
  320. this.endTimeMode = endTimeMode;
  321. this.dstSavings = dstSavings;
  322. // this.useDaylight is set by decodeRules
  323. decodeRules();
  324. if (dstSavings <= 0) {
  325. throw new IllegalArgumentException("Illegal daylight saving value: " + dstSavings);
  326. }
  327. }
  328. /**
  329. * Sets the daylight saving time starting year.
  330. *
  331. * @param year The daylight saving starting year.
  332. */
  333. public void setStartYear(int year)
  334. {
  335. startYear = year;
  336. invalidateCache();
  337. }
  338. /**
  339. * Sets the daylight saving time start rule. For example, if daylight saving
  340. * time starts on the first Sunday in April at 2 am in local wall clock
  341. * time, you can set the start rule by calling:
  342. * <pre><code>setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);</code></pre>
  343. *
  344. * @param startMonth The daylight saving time starting month. Month is
  345. * a {@link Calendar#MONTH MONTH} field
  346. * value (0-based. e.g., 0 for January).
  347. * @param startDay The day of the month on which the daylight saving time starts.
  348. * See the class description for the special cases of this parameter.
  349. * @param startDayOfWeek The daylight saving time starting day-of-week.
  350. * See the class description for the special cases of this parameter.
  351. * @param startTime The daylight saving time starting time in local wall clock
  352. * time, which is local standard time in this case.
  353. * @exception IllegalArgumentException if the <code>startMonth</code>, <code>startDay</code>,
  354. * <code>startDayOfWeek</code>, or <code>startTime</code> parameters are out of range
  355. */
  356. public void setStartRule(int startMonth, int startDay, int startDayOfWeek, int startTime)
  357. {
  358. this.startMonth = startMonth;
  359. this.startDay = startDay;
  360. this.startDayOfWeek = startDayOfWeek;
  361. this.startTime = startTime;
  362. startTimeMode = WALL_TIME;
  363. decodeStartRule();
  364. invalidateCache();
  365. }
  366. /**
  367. * Sets the daylight saving time start rule to a fixed date within a month.
  368. * This method is equivalent to:
  369. * <pre><code>setStartRule(startMonth, startDay, 0, startTime)</code></pre>
  370. *
  371. * @param startMonth The daylight saving time starting month. Month is
  372. * a {@link Calendar#MONTH MONTH} field
  373. * value (0-based. e.g., 0 for January).
  374. * @param startDay The day of the month on which the daylight saving time starts.
  375. * @param startTime The daylight saving time starting time in local wall clock
  376. * time, which is local standard time in this case.
  377. * See the class description for the special cases of this parameter.
  378. * @exception IllegalArgumentException if the <code>startMonth</code>,
  379. * <code>startDayOfMonth</code>, or <code>startTime</code> parameters are out of range
  380. * @since 1.2
  381. */
  382. public void setStartRule(int startMonth, int startDay, int startTime) {
  383. setStartRule(startMonth, startDay, 0, startTime);
  384. }
  385. /**
  386. * Sets the daylight saving time start rule to a weekday before or after the given date within
  387. * a month, e.g., the first Monday on or after the 8th.
  388. *
  389. * @param startMonth The daylight saving time starting month. Month is
  390. * a {@link Calendar#MONTH MONTH} field
  391. * value (0-based. e.g., 0 for January).
  392. * @param startDay The day of the month on which the daylight saving time starts.
  393. * @param startDayOfWeek The daylight saving time starting day-of-week.
  394. * @param startTime The daylight saving time starting time in local wall clock
  395. * time, which is local standard time in this case.
  396. * @param after If true, this rule selects the first <code>dayOfWeek</code> on or
  397. * <em>after</em> <code>dayOfMonth</code>. If false, this rule
  398. * selects the last <code>dayOfWeek</code> on or <em>before</em>
  399. * <code>dayOfMonth</code>.
  400. * @exception IllegalArgumentException if the <code>startMonth</code>, <code>startDay</code>,
  401. * <code>startDayOfWeek</code>, or <code>startTime</code> parameters are out of range
  402. * @since 1.2
  403. */
  404. public void setStartRule(int startMonth, int startDay, int startDayOfWeek,
  405. int startTime, boolean after)
  406. {
  407. // TODO: this method doesn't check the initial values of dayOfMonth or dayOfWeek.
  408. if (after) {
  409. setStartRule(startMonth, startDay, -startDayOfWeek, startTime);
  410. } else {
  411. setStartRule(startMonth, -startDay, -startDayOfWeek, startTime);
  412. }
  413. }
  414. /**
  415. * Sets the daylight saving time end rule. For example, if daylight saving time
  416. * ends on the last Sunday in October at 2 am in wall clock time,
  417. * you can set the end rule by calling:
  418. * <code>setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);</code>
  419. *
  420. * @param endMonth The daylight saving time ending month. Month is
  421. * a {@link Calendar#MONTH MONTH} field
  422. * value (0-based. e.g., 9 for October).
  423. * @param endDay The day of the month on which the daylight saving time ends.
  424. * See the class description for the special cases of this parameter.
  425. * @param endDayOfWeek The daylight saving time ending day-of-week.
  426. * See the class description for the special cases of this parameter.
  427. * @param endTime The daylight saving ending time in local wall clock time,
  428. * (in milliseconds within the day) which is local daylight
  429. * time in this case.
  430. * @exception IllegalArgumentException if the <code>endMonth</code>, <code>endDay</code>,
  431. * <code>endDayOfWeek</code>, or <code>endTime</code> parameters are out of range
  432. */
  433. public void setEndRule(int endMonth, int endDay, int endDayOfWeek,
  434. int endTime)
  435. {
  436. this.endMonth = endMonth;
  437. this.endDay = endDay;
  438. this.endDayOfWeek = endDayOfWeek;
  439. this.endTime = endTime;
  440. this.endTimeMode = WALL_TIME;
  441. decodeEndRule();
  442. invalidateCache();
  443. }
  444. /**
  445. * Sets the daylight saving time end rule to a fixed date within a month.
  446. * This method is equivalent to:
  447. * <pre><code>setEndRule(endMonth, endDay, 0, endTime)</code></pre>
  448. *
  449. * @param endMonth The daylight saving time ending month. Month is
  450. * a {@link Calendar#MONTH MONTH} field
  451. * value (0-based. e.g., 9 for October).
  452. * @param endDay The day of the month on which the daylight saving time ends.
  453. * @param endTime The daylight saving ending time in local wall clock time,
  454. * (in milliseconds within the day) which is local daylight
  455. * time in this case.
  456. * @exception IllegalArgumentException the <code>endMonth</code>, <code>endDay</code>,
  457. * or <code>endTime</code> parameters are out of range
  458. * @since 1.2
  459. */
  460. public void setEndRule(int endMonth, int endDay, int endTime)
  461. {
  462. setEndRule(endMonth, endDay, 0, endTime);
  463. }
  464. /**
  465. * Sets the daylight saving time end rule to a weekday before or after the given date within
  466. * a month, e.g., the first Monday on or after the 8th.
  467. *
  468. * @param endMonth The daylight saving time ending month. Month is
  469. * a {@link Calendar#MONTH MONTH} field
  470. * value (0-based. e.g., 9 for October).
  471. * @param endDay The day of the month on which the daylight saving time ends.
  472. * @param endDayOfWeek The daylight saving time ending day-of-week.
  473. * @param endTime The daylight saving ending time in local wall clock time,
  474. * (in milliseconds within the day) which is local daylight
  475. * time in this case.
  476. * @param after If true, this rule selects the first <code>endDayOfWeek</code> on
  477. * or <em>after</em> <code>endDay</code>. If false, this rule
  478. * selects the last <code>endDayOfWeek</code> on or before
  479. * <code>endDay</code> of the month.
  480. * @exception IllegalArgumentException the <code>endMonth</code>, <code>endDay</code>,
  481. * <code>endDayOfWeek</code>, or <code>endTime</code> parameters are out of range
  482. * @since 1.2
  483. */
  484. public void setEndRule(int endMonth, int endDay, int endDayOfWeek, int endTime, boolean after)
  485. {
  486. if (after) {
  487. setEndRule(endMonth, endDay, -endDayOfWeek, endTime);
  488. } else {
  489. setEndRule(endMonth, -endDay, -endDayOfWeek, endTime);
  490. }
  491. }
  492. /**
  493. * Returns the offset of this time zone from UTC at the given
  494. * time. If daylight saving time is in effect at the given time,
  495. * the offset value is adjusted with the amount of daylight
  496. * saving.
  497. *
  498. * @param date the time at which the time zone offset is found
  499. * @return the amount of time in milliseconds to add to UTC to get
  500. * local time.
  501. * @since 1.4
  502. */
  503. public int getOffset(long date) {
  504. return getOffsets(date, null);
  505. }
  506. /**
  507. * @see TimeZone#getOffsets
  508. */
  509. int getOffsets(long date, int[] offsets) {
  510. int offset = rawOffset;
  511. computeOffset:
  512. if (useDaylight) {
  513. synchronized (this) {
  514. if (cacheStart != 0) {
  515. if (date >= cacheStart && date < cacheEnd) {
  516. offset += dstSavings;
  517. break computeOffset;
  518. }
  519. }
  520. }
  521. BaseCalendar cal = date >= GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER ?
  522. gcal : (BaseCalendar) CalendarSystem.forName("julian");
  523. BaseCalendar.Date cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
  524. // Get the year in local time
  525. cal.getCalendarDate(date + rawOffset, cdate);
  526. int year = cdate.getNormalizedYear();
  527. if (year >= startYear) {
  528. // Clear time elements for the transition calculations
  529. cdate.setTimeOfDay(0, 0, 0, 0);
  530. offset = getOffset(cal, cdate, year, date);
  531. }
  532. }
  533. if (offsets != null) {
  534. offsets[0] = rawOffset;
  535. offsets[1] = offset - rawOffset;
  536. }
  537. return offset;
  538. }
  539. /**
  540. * Returns the difference in milliseconds between local time and
  541. * UTC, taking into account both the raw offset and the effect of
  542. * daylight saving, for the specified date and time. This method
  543. * assumes that the start and end month are distinct. It also
  544. * uses a default {@link GregorianCalendar} object as its
  545. * underlying calendar, such as for determining leap years. Do
  546. * not use the result of this method with a calendar other than a
  547. * default <code>GregorianCalendar</code>.
  548. *
  549. * <p><em>Note: In general, clients should use
  550. * <code>Calendar.get(ZONE_OFFSET) + Calendar.get(DST_OFFSET)</code>
  551. * instead of calling this method.</em>
  552. *
  553. * @param era The era of the given date.
  554. * @param year The year in the given date.
  555. * @param month The month in the given date. Month is 0-based. e.g.,
  556. * 0 for January.
  557. * @param day The day-in-month of the given date.
  558. * @param dayOfWeek The day-of-week of the given date.
  559. * @param millis The milliseconds in day in <em>standard</em> local time.
  560. * @return The milliseconds to add to UTC to get local time.
  561. * @exception IllegalArgumentException the <code>era</code>,
  562. * <code>month</code>, <code>day</code>, <code>dayOfWeek</code>,
  563. * or <code>millis</code> parameters are out of range
  564. */
  565. public int getOffset(int era, int year, int month, int day, int dayOfWeek,
  566. int millis)
  567. {
  568. if (era != GregorianCalendar.AD && era != GregorianCalendar.BC) {
  569. throw new IllegalArgumentException("Illegal era " + era);
  570. }
  571. int y = year;
  572. if (era == GregorianCalendar.BC) {
  573. // adjust y with the GregorianCalendar-style year numbering.
  574. y = 1 - y;
  575. }
  576. // If the year isn't representable with the 64-bit long
  577. // integer in milliseconds, convert the year to an
  578. // equivalent year. This is required to pass some JCK test cases
  579. // which are actually useless though because the specified years
  580. // can't be supported by the Java time system.
  581. if (y >= 292278994) {
  582. y = 2800 + y % 2800;
  583. } else if (y <= -292269054) {
  584. // y %= 28 also produces an equivalent year, but positive
  585. // year numbers would be convenient to use the UNIX cal
  586. // command.
  587. y = (int) CalendarUtils.mod((long) y, 28);
  588. }
  589. // convert year to its 1-based month value
  590. int m = month + 1;
  591. // First, calculate time as a Gregorian date.
  592. BaseCalendar cal = gcal;
  593. BaseCalendar.Date cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
  594. cdate.setDate(y, m, day);
  595. long time = cal.getTime(cdate); // normalize cdate
  596. time += millis - rawOffset; // UTC time
  597. // If the time value represents a time before the default
  598. // Gregorian cutover, recalculate time using the Julian
  599. // calendar system. For the Julian calendar system, the
  600. // normalized year numbering is ..., -2 (BCE 2), -1 (BCE 1),
  601. // 1, 2 ... which is different from the GregorianCalendar
  602. // style year numbering (..., -1, 0 (BCE 1), 1, 2, ...).
  603. if (time < GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER) {
  604. cal = (BaseCalendar) CalendarSystem.forName("julian");
  605. cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
  606. cdate.setNormalizedDate(y, m, day);
  607. time = cal.getTime(cdate) + millis - rawOffset;
  608. }
  609. if ((cdate.getNormalizedYear() != y)
  610. || (cdate.getMonth() != m)
  611. || (cdate.getDayOfMonth() != day)
  612. // The validation should be cdate.getDayOfWeek() ==
  613. // dayOfWeek. However, we don't check dayOfWeek for
  614. // compatibility.
  615. || (dayOfWeek < Calendar.SUNDAY || dayOfWeek > Calendar.SATURDAY)
  616. || (millis < 0 || millis >= (24*60*60*1000))) {
  617. throw new IllegalArgumentException();
  618. }
  619. if (!useDaylight || year < startYear || era != GregorianCalendar.CE) {
  620. return rawOffset;
  621. }
  622. return getOffset(cal, cdate, y, time);
  623. }
  624. private int getOffset(BaseCalendar cal, BaseCalendar.Date cdate, int year, long time) {
  625. synchronized (this) {
  626. if (cacheStart != 0) {
  627. if (time >= cacheStart && time < cacheEnd) {
  628. return rawOffset + dstSavings;
  629. }
  630. if (year == cacheYear) {
  631. return rawOffset;
  632. }
  633. }
  634. }
  635. long start = getStart(cal, cdate, year);
  636. long end = getEnd(cal, cdate, year);
  637. int offset = rawOffset;
  638. if (start <= end) {
  639. if (time >= start && time < end) {
  640. offset += dstSavings;
  641. }
  642. synchronized (this) {
  643. cacheYear = year;
  644. cacheStart = start;
  645. cacheEnd = end;
  646. }
  647. } else {
  648. if (time < end) {
  649. // TODO: support Gregorian cutover. The previous year
  650. // may be in the other calendar system.
  651. start = getStart(cal, cdate, year - 1);
  652. if (time >= start) {
  653. offset += dstSavings;
  654. }
  655. } else if (time >= start) {
  656. // TODO: support Gregorian cutover. The next year
  657. // may be in the other calendar system.
  658. end = getEnd(cal, cdate, year + 1);
  659. if (time < end) {
  660. offset += dstSavings;
  661. }
  662. }
  663. if (start <= end) {
  664. synchronized (this) {
  665. // The start and end transitions are in multiple years.
  666. cacheYear = (long) startYear - 1;
  667. cacheStart = start;
  668. cacheEnd = end;
  669. }
  670. }
  671. }
  672. return offset;
  673. }
  674. private long getStart(BaseCalendar cal, BaseCalendar.Date cdate, int year) {
  675. int time = startTime;
  676. if (startTimeMode != UTC_TIME) {
  677. time -= rawOffset;
  678. }
  679. return getTransition(cal, cdate, startMode, year, startMonth, startDay,
  680. startDayOfWeek, time);
  681. }
  682. private long getEnd(BaseCalendar cal, BaseCalendar.Date cdate, int year) {
  683. int time = endTime;
  684. if (startTimeMode != UTC_TIME) {
  685. time -= rawOffset;
  686. }
  687. if (startTimeMode == WALL_TIME) {
  688. time -= dstSavings;
  689. }
  690. return getTransition(cal, cdate, endMode, year, endMonth, endDay,
  691. endDayOfWeek, time);
  692. }
  693. private long getTransition(BaseCalendar cal, BaseCalendar.Date cdate,
  694. int mode, int year, int month, int dayOfMonth,
  695. int dayOfWeek, int timeOfDay) {
  696. cdate.setNormalizedYear(year);
  697. cdate.setMonth(month + 1);
  698. switch (mode) {
  699. case DOM_MODE:
  700. cdate.setDayOfMonth(dayOfMonth);
  701. break;
  702. case DOW_IN_MONTH_MODE:
  703. cdate.setDayOfMonth(1);
  704. if (dayOfMonth < 0) {
  705. cdate.setDayOfMonth(cal.getMonthLength(cdate));
  706. }
  707. cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(dayOfMonth, dayOfWeek, cdate);
  708. break;
  709. case DOW_GE_DOM_MODE:
  710. cdate.setDayOfMonth(dayOfMonth);
  711. cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(1, dayOfWeek, cdate);
  712. break;
  713. case DOW_LE_DOM_MODE:
  714. cdate.setDayOfMonth(dayOfMonth);
  715. cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(-1, dayOfWeek, cdate);
  716. break;
  717. }
  718. return cal.getTime(cdate) + timeOfDay;
  719. }
  720. /**
  721. * Gets the GMT offset for this time zone.
  722. * @return the GMT offset value in milliseconds
  723. * @see #setRawOffset
  724. */
  725. public int getRawOffset()
  726. {
  727. // The given date will be taken into account while
  728. // we have the historical time zone data in place.
  729. return rawOffset;
  730. }
  731. /**
  732. * Sets the base time zone offset to GMT.
  733. * This is the offset to add to UTC to get local time.
  734. * @see #getRawOffset
  735. */
  736. public void setRawOffset(int offsetMillis)
  737. {
  738. this.rawOffset = offsetMillis;
  739. }
  740. /**
  741. * Sets the amount of time in milliseconds that the clock is advanced
  742. * during daylight saving time.
  743. * @param millisSavedDuringDST the number of milliseconds the time is
  744. * advanced with respect to standard time when the daylight saving time rules
  745. * are in effect. A positive number, typically one hour (3600000).
  746. * @see #getDSTSavings
  747. * @since 1.2
  748. */
  749. public void setDSTSavings(int millisSavedDuringDST) {
  750. if (millisSavedDuringDST <= 0) {
  751. throw new IllegalArgumentException("Illegal daylight saving value: "
  752. + millisSavedDuringDST);
  753. }
  754. dstSavings = millisSavedDuringDST;
  755. }
  756. /**
  757. * Returns the amount of time in milliseconds that the clock is
  758. * advanced during daylight saving time.
  759. *
  760. * @return the number of milliseconds the time is advanced with
  761. * respect to standard time when the daylight saving rules are in
  762. * effect, or 0 (zero) if this time zone doesn't observe daylight
  763. * saving time.
  764. *
  765. * @see #setDSTSavings
  766. * @since 1.2
  767. */
  768. public int getDSTSavings() {
  769. if (useDaylight) {
  770. return dstSavings;
  771. }
  772. return 0;
  773. }
  774. /**
  775. * Queries if this time zone uses daylight saving time.
  776. * @return true if this time zone uses daylight saving time;
  777. * false otherwise.
  778. */
  779. public boolean useDaylightTime()
  780. {
  781. return useDaylight;
  782. }
  783. /**
  784. * Queries if the given date is in daylight saving time.
  785. * @return true if daylight saving time is in effective at the
  786. * given date; false otherwise.
  787. */
  788. public boolean inDaylightTime(Date date)
  789. {
  790. return (getOffset(date.getTime()) != rawOffset);
  791. }
  792. /**
  793. * Returns a clone of this <code>SimpleTimeZone</code> instance.
  794. * @return a clone of this instance.
  795. */
  796. public Object clone()
  797. {
  798. return super.clone();
  799. }
  800. /**
  801. * Generates the hash code for the SimpleDateFormat object.
  802. * @return the hash code for this object
  803. */
  804. public synchronized int hashCode()
  805. {
  806. return startMonth ^ startDay ^ startDayOfWeek ^ startTime ^
  807. endMonth ^ endDay ^ endDayOfWeek ^ endTime ^ rawOffset;
  808. }
  809. /**
  810. * Compares the equality of two <code>SimpleTimeZone</code> objects.
  811. *
  812. * @param obj The <code>SimpleTimeZone</code> object to be compared with.
  813. * @return True if the given <code>obj</code> is the same as this
  814. * <code>SimpleTimeZone</code> object; false otherwise.
  815. */
  816. public boolean equals(Object obj)
  817. {
  818. if (this == obj) {
  819. return true;
  820. }
  821. if (!(obj instanceof SimpleTimeZone)) {
  822. return false;
  823. }
  824. SimpleTimeZone that = (SimpleTimeZone) obj;
  825. return getID().equals(that.getID()) &&
  826. hasSameRules(that);
  827. }
  828. /**
  829. * Returns <code>true</code> if this zone has the same rules and offset as another zone.
  830. * @param other the TimeZone object to be compared with
  831. * @return <code>true</code> if the given zone is a SimpleTimeZone and has the
  832. * same rules and offset as this one
  833. * @since 1.2
  834. */
  835. public boolean hasSameRules(TimeZone other) {
  836. if (this == other) {
  837. return true;
  838. }
  839. if (!(other instanceof SimpleTimeZone)) {
  840. return false;
  841. }
  842. SimpleTimeZone that = (SimpleTimeZone) other;
  843. return rawOffset == that.rawOffset &&
  844. useDaylight == that.useDaylight &&
  845. (!useDaylight
  846. // Only check rules if using DST
  847. || (dstSavings == that.dstSavings &&
  848. startMode == that.startMode &&
  849. startMonth == that.startMonth &&
  850. startDay == that.startDay &&
  851. startDayOfWeek == that.startDayOfWeek &&
  852. startTime == that.startTime &&
  853. startTimeMode == that.startTimeMode &&
  854. endMode == that.endMode &&
  855. endMonth == that.endMonth &&
  856. endDay == that.endDay &&
  857. endDayOfWeek == that.endDayOfWeek &&
  858. endTime == that.endTime &&
  859. endTimeMode == that.endTimeMode &&
  860. startYear == that.startYear));
  861. }
  862. /**
  863. * Returns a string representation of this time zone.
  864. * @return a string representation of this time zone.
  865. */
  866. public String toString() {
  867. return getClass().getName() +
  868. "[id=" + getID() +
  869. ",offset=" + rawOffset +
  870. ",dstSavings=" + dstSavings +
  871. ",useDaylight=" + useDaylight +
  872. ",startYear=" + startYear +
  873. ",startMode=" + startMode +
  874. ",startMonth=" + startMonth +
  875. ",startDay=" + startDay +
  876. ",startDayOfWeek=" + startDayOfWeek +
  877. ",startTime=" + startTime +
  878. ",startTimeMode=" + startTimeMode +
  879. ",endMode=" + endMode +
  880. ",endMonth=" + endMonth +
  881. ",endDay=" + endDay +
  882. ",endDayOfWeek=" + endDayOfWeek +
  883. ",endTime=" + endTime +
  884. ",endTimeMode=" + endTimeMode + ']';
  885. }
  886. // =======================privates===============================
  887. /**
  888. * The month in which daylight saving time starts. This value must be
  889. * between <code>Calendar.JANUARY</code> and
  890. * <code>Calendar.DECEMBER</code> inclusive. This value must not equal
  891. * <code>endMonth</code>.
  892. * <p>If <code>useDaylight</code> is false, this value is ignored.
  893. * @serial
  894. */
  895. private int startMonth;
  896. /**
  897. * This field has two possible interpretations:
  898. * <dl>
  899. * <dt><code>startMode == DOW_IN_MONTH</code></dt>
  900. * <dd>
  901. * <code>startDay</code> indicates the day of the month of
  902. * <code>startMonth</code> on which daylight
  903. * saving time starts, from 1 to 28, 30, or 31, depending on the
  904. * <code>startMonth</code>.
  905. * </dd>
  906. * <dt><code>startMode != DOW_IN_MONTH</code></dt>
  907. * <dd>
  908. * <code>startDay</code> indicates which <code>startDayOfWeek</code> in th
  909. * month <code>startMonth</code> daylight
  910. * saving time starts on. For example, a value of +1 and a
  911. * <code>startDayOfWeek</code> of <code>Calendar.SUNDAY</code> indicates the
  912. * first Sunday of <code>startMonth</code>. Likewise, +2 would indicate the
  913. * second Sunday, and -1 the last Sunday. A value of 0 is illegal.
  914. * </dd>
  915. * </ul>
  916. * <p>If <code>useDaylight</code> is false, this value is ignored.
  917. * @serial
  918. */
  919. private int startDay;
  920. /**
  921. * The day of the week on which daylight saving time starts. This value
  922. * must be between <code>Calendar.SUNDAY</code> and
  923. * <code>Calendar.SATURDAY</code> inclusive.
  924. * <p>If <code>useDaylight</code> is false or
  925. * <code>startMode == DAY_OF_MONTH</code>, this value is ignored.
  926. * @serial
  927. */
  928. private int startDayOfWeek;
  929. /**
  930. * The time in milliseconds after midnight at which daylight saving
  931. * time starts. This value is expressed as wall time, standard time,
  932. * or UTC time, depending on the setting of <code>startTimeMode</code>.
  933. * <p>If <code>useDaylight</code> is false, this value is ignored.
  934. * @serial
  935. */
  936. private int startTime;
  937. /**
  938. * The format of startTime, either WALL_TIME, STANDARD_TIME, or UTC_TIME.
  939. * @serial
  940. * @since 1.3
  941. */
  942. private int startTimeMode;
  943. /**
  944. * The month in which daylight saving time ends. This value must be
  945. * between <code>Calendar.JANUARY</code> and
  946. * <code>Calendar.UNDECIMBER</code>. This value must not equal
  947. * <code>startMonth</code>.
  948. * <p>If <code>useDaylight</code> is false, this value is ignored.
  949. * @serial
  950. */
  951. private int endMonth;
  952. /**
  953. * This field has two possible interpretations:
  954. * <dl>
  955. * <dt><code>endMode == DOW_IN_MONTH</code></dt>
  956. * <dd>
  957. * <code>endDay</code> indicates the day of the month of
  958. * <code>endMonth</code> on which daylight
  959. * saving time ends, from 1 to 28, 30, or 31, depending on the
  960. * <code>endMonth</code>.
  961. * </dd>
  962. * <dt><code>endMode != DOW_IN_MONTH</code></dt>
  963. * <dd>
  964. * <code>endDay</code> indicates which <code>endDayOfWeek</code> in th
  965. * month <code>endMonth</code> daylight
  966. * saving time ends on. For example, a value of +1 and a
  967. * <code>endDayOfWeek</code> of <code>Calendar.SUNDAY</code> indicates the
  968. * first Sunday of <code>endMonth</code>. Likewise, +2 would indicate the
  969. * second Sunday, and -1 the last Sunday. A value of 0 is illegal.
  970. * </dd>
  971. * </ul>
  972. * <p>If <code>useDaylight</code> is false, this value is ignored.
  973. * @serial
  974. */
  975. private int endDay;
  976. /**
  977. * The day of the week on which daylight saving time ends. This value
  978. * must be between <code>Calendar.SUNDAY</code> and
  979. * <code>Calendar.SATURDAY</code> inclusive.
  980. * <p>If <code>useDaylight</code> is false or
  981. * <code>endMode == DAY_OF_MONTH</code>, this value is ignored.
  982. * @serial
  983. */
  984. private int endDayOfWeek;
  985. /**
  986. * The time in milliseconds after midnight at which daylight saving
  987. * time ends. This value is expressed as wall time, standard time,
  988. * or UTC time, depending on the setting of <code>endTimeMode</code>.
  989. * <p>If <code>useDaylight</code> is false, this value is ignored.
  990. * @serial
  991. */
  992. private int endTime;
  993. /**
  994. * The format of endTime, either <code>WALL_TIME</code>,
  995. * <code>STANDARD_TIME</code>, or <code>UTC_TIME</code>.
  996. * @serial
  997. * @since 1.3
  998. */
  999. private int endTimeMode;
  1000. /**
  1001. * The year in which daylight saving time is first observed. This is an {@link GregorianCalendar#AD AD}
  1002. * value. If this value is less than 1 then daylight saving time is observed
  1003. * for all <code>AD</code> years.
  1004. * <p>If <code>useDaylight</code> is false, this value is ignored.
  1005. * @serial
  1006. */
  1007. private int startYear;
  1008. /**
  1009. * The offset in milliseconds between this zone and GMT. Negative offsets
  1010. * are to the west of Greenwich. To obtain local <em>standard</em> time,
  1011. * add the offset to GMT time. To obtain local wall time it may also be
  1012. * necessary to add <code>dstSavings</code>.
  1013. * @serial
  1014. */
  1015. private int rawOffset;
  1016. /**
  1017. * A boolean value which is true if and only if this zone uses daylight
  1018. * saving time. If this value is false, several other fields are ignored.
  1019. * @serial
  1020. */
  1021. private boolean useDaylight=false; // indicate if this time zone uses DST
  1022. private static final int millisPerHour = 60*60*1000;
  1023. private static final int millisPerDay = 24*millisPerHour;
  1024. /**
  1025. * This field was serialized in JDK 1.1, so we have to keep it that way
  1026. * to maintain serialization compatibility. However, there's no need to
  1027. * recreate the array each time we create a new time zone.
  1028. * @serial An array of bytes containing the values {31, 28, 31, 30, 31, 30,
  1029. * 31, 31, 30, 31, 30, 31}. This is ignored as of the Java 2 platform v1.2, however, it must
  1030. * be streamed out for compatibility with JDK 1.1.
  1031. */
  1032. private final byte monthLength[] = staticMonthLength;
  1033. private final static byte staticMonthLength[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  1034. private final static byte staticLeapMonthLength[] = {31,29,31,30,31,30,31,31,30,31,30,31};
  1035. /**
  1036. * Variables specifying the mode of the start rule. Takes the following
  1037. * values:
  1038. * <dl>
  1039. * <dt><code>DOM_MODE</code></dt>
  1040. * <dd>
  1041. * Exact day of week; e.g., March 1.
  1042. * </dd>
  1043. * <dt><code>DOW_IN_MONTH_MODE</code></dt>
  1044. * <dd>
  1045. * Day of week in month; e.g., last Sunday in March.
  1046. * </dd>
  1047. * <dt><code>DOW_GE_DOM_MODE</code></dt>
  1048. * <dd>
  1049. * Day of week after day of month; e.g., Sunday on or after March 15.
  1050. * </dd>
  1051. * <dt><code>DOW_LE_DOM_MODE</code></dt>
  1052. * <dd>
  1053. * Day of week before day of month; e.g., Sunday on or before March 15.
  1054. * </dd>
  1055. * </dl>
  1056. * The setting of this field affects the interpretation of the
  1057. * <code>startDay</code> field.
  1058. * <p>If <code>useDaylight</code> is false, this value is ignored.
  1059. * @serial
  1060. * @since 1.1.4
  1061. */
  1062. private int startMode;
  1063. /**
  1064. * Variables specifying the mode of the end rule. Takes the following
  1065. * values:
  1066. * <dl>
  1067. * <dt><code>DOM_MODE</code></dt>
  1068. * <dd>
  1069. * Exact day of week; e.g., March 1.
  1070. * </dd>
  1071. * <dt><code>DOW_IN_MONTH_MODE</code></dt>
  1072. * <dd>
  1073. * Day of week in month; e.g., last Sunday in March.
  1074. * </dd>
  1075. * <dt><code>DOW_GE_DOM_MODE</code></dt>
  1076. * <dd>
  1077. * Day of week after day of month; e.g., Sunday on or after March 15.
  1078. * </dd>
  1079. * <dt><code>DOW_LE_DOM_MODE</code></dt>
  1080. * <dd>
  1081. * Day of week before day of month; e.g., Sunday on or before March 15.
  1082. * </dd>
  1083. * </dl>
  1084. * The setting of this field affects the interpretation of the
  1085. * <code>endDay</code> field.
  1086. * <p>If <code>useDaylight</code> is false, this value is ignored.
  1087. * @serial
  1088. * @since 1.1.4
  1089. */
  1090. private int endMode;
  1091. /**
  1092. * A positive value indicating the amount of time saved during DST in
  1093. * milliseconds.
  1094. * Typically one hour (3600000); sometimes 30 minutes (1800000).
  1095. * <p>If <code>useDaylight</code> is false, this value is ignored.
  1096. * @serial
  1097. * @since 1.1.4
  1098. */
  1099. private int dstSavings;
  1100. private static final Gregorian gcal = CalendarSystem.getGregorianCalendar();
  1101. /**
  1102. * Cache values representing a single period of daylight saving
  1103. * time. When the cache values are valid, cacheStart is the start
  1104. * time (inclusive) of daylight saving time and cacheEnd is the
  1105. * end time (exclusive).
  1106. *
  1107. * cacheYear has a year value if both cacheStart and cacheEnd are
  1108. * in the same year. cacheYear is set to startYear - 1 if
  1109. * cacheStart and cacheEnd are in different years. cacheStart is 0
  1110. * if the cache values are void. cacheYear is a long to support
  1111. * Integer.MIN_VALUE - 1 (JCK requirement).
  1112. */
  1113. private transient long cacheYear;
  1114. private transient long cacheStart;
  1115. private transient long cacheEnd;
  1116. /**
  1117. * Constants specifying values of startMode and endMode.
  1118. */
  1119. private static final int DOM_MODE = 1; // Exact day of month, "Mar 1"
  1120. private static final int DOW_IN_MONTH_MODE = 2; // Day of week in month, "lastSun"
  1121. private static final int DOW_GE_DOM_MODE = 3; // Day of week after day of month, "Sun>=15"
  1122. private static final int DOW_LE_DOM_MODE = 4; // Day of week before day of month, "Sun<=21"
  1123. /**
  1124. * Constant for a mode of start or end time specified as wall clock
  1125. * time. Wall clock time is standard time for the onset rule, and
  1126. * daylight time for the end rule.
  1127. * @since 1.4
  1128. */
  1129. public static final int WALL_TIME = 0; // Zero for backward compatibility
  1130. /**
  1131. * Constant for a mode of start or end time specified as standard time.
  1132. * @since 1.4
  1133. */
  1134. public static final int STANDARD_TIME = 1;
  1135. /**
  1136. * Constant for a mode of start or end time specified as UTC. European
  1137. * Union rules are specified as UTC time, for example.
  1138. * @since 1.4
  1139. */
  1140. public static final int UTC_TIME = 2;
  1141. // Proclaim compatibility with 1.1
  1142. static final long serialVersionUID = -403250971215465050L;
  1143. // the internal serial version which says which version was written
  1144. // - 0 (default) for version up to JDK 1.1.3
  1145. // - 1 for version from JDK 1.1.4, which includes 3 new fields
  1146. // - 2 for JDK 1.3, which includes 2 new fields
  1147. static final int currentSerialVersion = 2;
  1148. /**
  1149. * The version of the serialized data on the stream. Possible values:
  1150. * <dl>
  1151. * <dt><b>0</b> or not present on stream</dt>
  1152. * <dd>
  1153. * JDK 1.1.3 or earlier.
  1154. * </dd>
  1155. * <dt><b>1</b></dt>
  1156. * <dd>
  1157. * JDK 1.1.4 or later. Includes three new fields: <code>startMode</code>,
  1158. * <code>endMode</code>, and <code>dstSavings</code>.
  1159. * </dd>
  1160. * <dt><b>2</b></dt>
  1161. * <dd>
  1162. * JDK 1.3 or later. Includes two new fields: <code>startTimeMode</code>
  1163. * and <code>endTimeMode</code>.
  1164. * </dd>
  1165. * </dl>
  1166. * When streaming out this class, the most recent format
  1167. * and the highest allowable <code>serialVersionOnStream</code>
  1168. * is written.
  1169. * @serial
  1170. * @since 1.1.4
  1171. */
  1172. private int serialVersionOnStream = currentSerialVersion;
  1173. synchronized private void invalidateCache() {
  1174. cacheYear = startYear - 1;
  1175. cacheStart = cacheEnd = 0;
  1176. }
  1177. //----------------------------------------------------------------------
  1178. // Rule representation
  1179. //
  1180. // We represent the following flavors of rules:
  1181. // 5 the fifth of the month
  1182. // lastSun the last Sunday in the month
  1183. // lastMon the last Monday in the month
  1184. // Sun>=8 first Sunday on or after the eighth
  1185. // Sun<=25 last Sunday on or before the 25th
  1186. // This is further complicated by the fact that we need to remain
  1187. // backward compatible with the 1.1 FCS. Finally, we need to minimize
  1188. // API changes. In order to satisfy these requirements, we support
  1189. // three representation systems, and we translate between them.
  1190. //
  1191. // INTERNAL REPRESENTATION
  1192. // This is the format SimpleTimeZone objects take after construction or
  1193. // streaming in is complete. Rules are represented directly, using an
  1194. // unencoded format. We will discuss the start rule only below; the end
  1195. // rule is analogous.
  1196. // startMode Takes on enumerated values DAY_OF_MONTH,
  1197. // DOW_IN_MONTH, DOW_AFTER_DOM, or DOW_BEFORE_DOM.
  1198. // startDay The day of the month, or for DOW_IN_MONTH mode, a
  1199. // value indicating which DOW, such as +1 for first,
  1200. // +2 for second, -1 for last, etc.
  1201. // startDayOfWeek The day of the week. Ignored for DAY_OF_MONTH.
  1202. //
  1203. // ENCODED REPRESENTATION
  1204. // This is the format accepted by the constructor and by setStartRule()
  1205. // and setEndRule(). It uses various combinations of positive, negative,
  1206. // and zero values to encode the different rules. This representation
  1207. // allows us to specify all the different rule flavors without altering
  1208. // the API.
  1209. // MODE startMonth startDay startDayOfWeek
  1210. // DOW_IN_MONTH_MODE >=0 !=0 >0
  1211. // DOM_MODE >=0 >0 ==0
  1212. // DOW_GE_DOM_MODE >=0 >0 <0
  1213. // DOW_LE_DOM_MODE >=0 <0 <0
  1214. // (no DST) don't care ==0 don't care
  1215. //
  1216. // STREAMED REPRESENTATION
  1217. // We must retain binary compatibility with the 1.1 FCS. The 1.1 code only
  1218. // handles DOW_IN_MONTH_MODE and non-DST mode, the latter indicated by the
  1219. // flag useDaylight. When we stream an object out, we translate into an
  1220. // approximate DOW_IN_MONTH_MODE representation so the object can be parsed
  1221. // and used by 1.1 code. Following that, we write out the full
  1222. // representation separately so that contemporary code can recognize and
  1223. // parse it. The full representation is written in a "packed" format,
  1224. // consisting of a version number, a length, and an array of bytes. Future
  1225. // versions of this class may specify different versions. If they wish to
  1226. // include additional data, they should do so by storing them after the
  1227. // packed representation below.
  1228. //----------------------------------------------------------------------
  1229. /**
  1230. * Given a set of encoded rules in startDay and startDayOfMonth, decode
  1231. * them and set the startMode appropriately. Do the same for endDay and
  1232. * endDayOfMonth. Upon entry, the day of week variables may be zero or
  1233. * negative, in order to indicate special modes. The day of month
  1234. * variables may also be negative. Upon exit, the mode variables will be
  1235. * set, and the day of week and day of month variables will be positive.
  1236. * This method also recognizes a startDay or endDay of zero as indicating
  1237. * no DST.
  1238. */
  1239. private void decodeRules()
  1240. {
  1241. decodeStartRule();
  1242. decodeEndRule();
  1243. }
  1244. /**
  1245. * Decode the start rule and validate the parameters. The parameters are
  1246. * expected to be in encoded form, which represents the various rule modes
  1247. * by negating or zeroing certain values. Representation formats are:
  1248. * <p>
  1249. * <pre>
  1250. * DOW_IN_MONTH DOM DOW>=DOM DOW<=DOM no DST
  1251. * ------------ ----- -------- -------- ----------
  1252. * month 0..11 same same same don't care
  1253. * day -5..5 1..31 1..31 -1..-31 0
  1254. * dayOfWeek 1..7 0 -1..-7 -1..-7 don't care
  1255. * time 0..ONEDAY same same same don't care
  1256. * </pre>
  1257. * The range for month does not include UNDECIMBER since this class is
  1258. * really specific to GregorianCalendar, which does not use that month.
  1259. * The range for time includes ONEDAY (vs. ending at ONEDAY-1) because the
  1260. * end rule is an exclusive limit point. That is, the range of times that
  1261. * are in DST include those >= the start and < the end. For this reason,
  1262. * it should be possible to specify an end of ONEDAY in order to include the
  1263. * entire day. Although this is equivalent to time 0 of the following day,
  1264. * it's not always possible to specify that, for example, on December 31.
  1265. * While arguably the start range should still be 0..ONEDAY-1, we keep
  1266. * the start and end ranges the same for consistency.
  1267. */
  1268. private void decodeStartRule() {
  1269. useDaylight = (startDay != 0) && (endDay != 0);
  1270. if (startDay != 0) {
  1271. if (startMonth < Calendar.JANUARY || startMonth > Calendar.DECEMBER) {
  1272. throw new IllegalArgumentException(
  1273. "Illegal start month " + startMonth);
  1274. }
  1275. if (startTime < 0 || startTime >= millisPerDay) {
  1276. throw new IllegalArgumentException(
  1277. "Illegal start time " + startTime);
  1278. }
  1279. if (startDayOfWeek == 0) {
  1280. startMode = DOM_MODE;
  1281. } else {
  1282. if (startDayOfWeek > 0) {
  1283. startMode = DOW_IN_MONTH_MODE;
  1284. } else {
  1285. startDayOfWeek = -startDayOfWeek;
  1286. if (startDay > 0) {
  1287. startMode = DOW_GE_DOM_MODE;
  1288. } else {
  1289. startDay = -startDay;
  1290. startMode = DOW_LE_DOM_MODE;
  1291. }
  1292. }
  1293. if (startDayOfWeek > Calendar.SATURDAY) {
  1294. throw new IllegalArgumentException(
  1295. "Illegal start day of week " + startDayOfWeek);
  1296. }
  1297. }
  1298. if (startMode == DOW_IN_MONTH_MODE) {
  1299. if (startDay < -5 || startDay > 5) {
  1300. throw new IllegalArgumentException(
  1301. "Illegal start day of week in month " + startDay);
  1302. }
  1303. } else if (startDay < 1 || startDay > staticMonthLength[startMonth]) {
  1304. throw new IllegalArgumentException(
  1305. "Illegal start day " + startDay);
  1306. }
  1307. }
  1308. }
  1309. /**
  1310. * Decode the end rule and validate the parameters. This method is exactly
  1311. * analogous to decodeStartRule().
  1312. * @see decodeStartRule
  1313. */
  1314. private void decodeEndRule() {
  1315. useDaylight = (startDay != 0) && (endDay != 0);
  1316. if (endDay != 0) {
  1317. if (endMonth < Calendar.JANUARY || endMonth > Calendar.DECEMBER) {
  1318. throw new IllegalArgumentException(
  1319. "Illegal end month " + endMonth);
  1320. }
  1321. if (endTime < 0 || endTime >= millisPerDay) {
  1322. throw new IllegalArgumentException(
  1323. "Illegal end time " + endTime);
  1324. }
  1325. if (endDayOfWeek == 0) {
  1326. endMode = DOM_MODE;
  1327. } else {
  1328. if (endDayOfWeek > 0) {
  1329. endMode = DOW_IN_MONTH_MODE;
  1330. } else {
  1331. endDayOfWeek = -endDayOfWeek;
  1332. if (endDay > 0) {
  1333. endMode = DOW_GE_DOM_MODE;
  1334. } else {
  1335. endDay = -endDay;
  1336. endMode = DOW_LE_DOM_MODE;
  1337. }
  1338. }
  1339. if (endDayOfWeek > Calendar.SATURDAY) {
  1340. throw new IllegalArgumentException(
  1341. "Illegal end day of week " + endDayOfWeek);
  1342. }
  1343. }
  1344. if (endMode == DOW_IN_MONTH_MODE) {
  1345. if (endDay < -5 || endDay > 5) {
  1346. throw new IllegalArgumentException(
  1347. "Illegal end day of week in month " + endDay);
  1348. }
  1349. } else if (endDay < 1 || endDay > staticMonthLength[endMonth]) {
  1350. throw new IllegalArgumentException(
  1351. "Illegal end day " + endDay);
  1352. }
  1353. }
  1354. }
  1355. /**
  1356. * Make rules compatible to 1.1 FCS code. Since 1.1 FCS code only understands
  1357. * day-of-week-in-month rules, we must modify other modes of rules to their
  1358. * approximate equivalent in 1.1 FCS terms. This method is used when streaming
  1359. * out objects of this class. After it is called, the rules will be modified,
  1360. * with a possible loss of information. startMode and endMode will NOT be
  1361. * altered, even though semantically they should be set to DOW_IN_MONTH_MODE,
  1362. * since the rule modification is only intended to be temporary.
  1363. */
  1364. private void makeRulesCompatible()
  1365. {
  1366. switch (startMode) {
  1367. case DOM_MODE:
  1368. startDay = 1 + (startDay / 7);
  1369. startDayOfWeek = Calendar.SUNDAY;
  1370. break;
  1371. case DOW_GE_DOM_MODE:
  1372. // A day-of-month of 1 is equivalent to DOW_IN_MONTH_MODE
  1373. // that is, Sun>=1 == firstSun.
  1374. if (startDay != 1) {
  1375. startDay = 1 + (startDay / 7);
  1376. }
  1377. break;
  1378. case DOW_LE_DOM_MODE:
  1379. if (startDay >= 30) {
  1380. startDay = -1;
  1381. } else {
  1382. startDay = 1 + (startDay / 7);
  1383. }
  1384. break;
  1385. }
  1386. switch (endMode) {
  1387. case DOM_MODE:
  1388. endDay = 1 + (endDay / 7);
  1389. endDayOfWeek = Calendar.SUNDAY;
  1390. break;
  1391. case DOW_GE_DOM_MODE:
  1392. // A day-of-month of 1 is equivalent to DOW_IN_MONTH_MODE
  1393. // that is, Sun>=1 == firstSun.
  1394. if (endDay != 1) {
  1395. endDay = 1 + (endDay / 7);
  1396. }
  1397. break;
  1398. case DOW_LE_DOM_MODE:
  1399. if (endDay >= 30) {
  1400. endDay = -1;
  1401. } else {
  1402. endDay = 1 + (endDay / 7);
  1403. }
  1404. break;
  1405. }
  1406. /*
  1407. * Adjust the start and end times to wall time. This works perfectly
  1408. * well unless it pushes into the next or previous day. If that
  1409. * happens, we attempt to adjust the day rule somewhat crudely. The day
  1410. * rules have been forced into DOW_IN_MONTH mode already, so we change
  1411. * the day of week to move forward or back by a day. It's possible to
  1412. * make a more refined adjustment of the original rules first, but in
  1413. * most cases this extra effort will go to waste once we adjust the day
  1414. * rules anyway.
  1415. */
  1416. switch (startTimeMode) {
  1417. case UTC_TIME:
  1418. startTime += rawOffset;
  1419. break;
  1420. }
  1421. while (startTime < 0) {
  1422. startTime += millisPerDay;
  1423. startDayOfWeek = 1 + ((startDayOfWeek+5) % 7); // Back 1 day
  1424. }
  1425. while (startTime >= millisPerDay) {
  1426. startTime -= millisPerDay;
  1427. startDayOfWeek = 1 + (startDayOfWeek % 7); // Forward 1 day
  1428. }
  1429. switch (endTimeMode) {
  1430. case UTC_TIME:
  1431. endTime += rawOffset + dstSavings;
  1432. break;
  1433. case STANDARD_TIME:
  1434. endTime += dstSavings;
  1435. }
  1436. while (endTime < 0) {
  1437. endTime += millisPerDay;
  1438. endDayOfWeek = 1 + ((endDayOfWeek+5) % 7); // Back 1 day
  1439. }
  1440. while (endTime >= millisPerDay) {
  1441. endTime -= millisPerDay;
  1442. endDayOfWeek = 1 + (endDayOfWeek % 7); // Forward 1 day
  1443. }
  1444. }
  1445. /**
  1446. * Pack the start and end rules into an array of bytes. Only pack
  1447. * data which is not preserved by makeRulesCompatible.
  1448. */
  1449. private byte[] packRules()
  1450. {
  1451. byte[] rules = new byte[6];
  1452. rules[0] = (byte)startDay;
  1453. rules[1] = (byte)startDayOfWeek;
  1454. rules[2] = (byte)endDay;
  1455. rules[3] = (byte)endDayOfWeek;
  1456. // As of serial version 2, include time modes
  1457. rules[4] = (byte)startTimeMode;
  1458. rules[5] = (byte)endTimeMode;
  1459. return rules;
  1460. }
  1461. /**
  1462. * Given an array of bytes produced by packRules, interpret them
  1463. * as the start and end rules.
  1464. */
  1465. private void unpackRules(byte[] rules)
  1466. {
  1467. startDay = rules[0];
  1468. startDayOfWeek = rules[1];
  1469. endDay = rules[2];
  1470. endDayOfWeek = rules[3];
  1471. // As of serial version 2, include time modes
  1472. if (rules.length >= 6) {
  1473. startTimeMode = rules[4];
  1474. endTimeMode = rules[5];
  1475. }
  1476. }
  1477. /**
  1478. * Pack the start and end times into an array of bytes. This is required
  1479. * as of serial version 2.
  1480. */
  1481. private int[] packTimes() {
  1482. int[] times = new int[2];
  1483. times[0] = startTime;
  1484. times[1] = endTime;
  1485. return times;
  1486. }
  1487. /**
  1488. * Unpack the start and end times from an array of bytes. This is required
  1489. * as of serial version 2.
  1490. */
  1491. private void unpackTimes(int[] times) {
  1492. startTime = times[0];
  1493. endTime = times[1];
  1494. }
  1495. /**
  1496. * Save the state of this object to a stream (i.e., serialize it).
  1497. *
  1498. * @serialData We write out two formats, a JDK 1.1 compatible format, using
  1499. * <code>DOW_IN_MONTH_MODE</code> rules, in the required section, followed
  1500. * by the full rules, in packed format, in the optional section. The
  1501. * optional section will be ignored by JDK 1.1 code upon stream in.
  1502. * <p> Contents of the optional section: The length of a byte array is
  1503. * emitted (int); this is 4 as of this release. The byte array of the given
  1504. * length is emitted. The contents of the byte array are the true values of
  1505. * the fields <code>startDay</code>, <code>startDayOfWeek</code>,
  1506. * <code>endDay</code>, and <code>endDayOfWeek</code>. The values of these
  1507. * fields in the required section are approximate values suited to the rule
  1508. * mode <code>DOW_IN_MONTH_MODE</code>, which is the only mode recognized by
  1509. * JDK 1.1.
  1510. */
  1511. private void writeObject(ObjectOutputStream stream)
  1512. throws IOException
  1513. {
  1514. // Construct a binary rule
  1515. byte[] rules = packRules();
  1516. int[] times = packTimes();
  1517. // Convert to 1.1 FCS rules. This step may cause us to lose information.
  1518. makeRulesCompatible();
  1519. // Write out the 1.1 FCS rules
  1520. stream.defaultWriteObject();
  1521. // Write out the binary rules in the optional data area of the stream.
  1522. stream.writeInt(rules.length);
  1523. stream.write(rules);
  1524. stream.writeObject(times);
  1525. // Recover the original rules. This recovers the information lost
  1526. // by makeRulesCompatible.
  1527. unpackRules(rules);
  1528. unpackTimes(times);
  1529. }
  1530. /**
  1531. * Reconstitute this object from a stream (i.e., deserialize it).
  1532. *
  1533. * We handle both JDK 1.1
  1534. * binary formats and full formats with a packed byte array.
  1535. */
  1536. private void readObject(ObjectInputStream stream)
  1537. throws IOException, ClassNotFoundException
  1538. {
  1539. stream.defaultReadObject();
  1540. if (serialVersionOnStream < 1) {
  1541. // Fix a bug in the 1.1 SimpleTimeZone code -- namely,
  1542. // startDayOfWeek and endDayOfWeek were usually uninitialized. We can't do
  1543. // too much, so we assume SUNDAY, which actually works most of the time.
  1544. if (startDayOfWeek == 0) {
  1545. startDayOfWeek = Calendar.SUNDAY;
  1546. }
  1547. if (endDayOfWeek == 0) {
  1548. endDayOfWeek = Calendar.SUNDAY;
  1549. }
  1550. // The variables dstSavings, startMode, and endMode are post-1.1, so they
  1551. // won't be present if we're reading from a 1.1 stream. Fix them up.
  1552. startMode = endMode = DOW_IN_MONTH_MODE;
  1553. dstSavings = millisPerHour;
  1554. } else {
  1555. // For 1.1.4, in addition to the 3 new instance variables, we also
  1556. // store the actual rules (which have not be made compatible with 1.1)
  1557. // in the optional area. Read them in here and parse them.
  1558. int length = stream.readInt();
  1559. byte[] rules = new byte[length];
  1560. stream.readFully(rules);
  1561. unpackRules(rules);
  1562. }
  1563. if (serialVersionOnStream >= 2) {
  1564. int[] times = (int[]) stream.readObject();
  1565. unpackTimes(times);
  1566. }
  1567. serialVersionOnStream = currentSerialVersion;
  1568. }
  1569. }