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