SimpleTimeZone.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /* java.util.SimpleTimeZone
  2. Copyright (C) 1998, 1999, 2000, 2003, 2004, 2005, 2007
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util;
  33. /**
  34. * This class represents a simple time zone offset and handles
  35. * daylight savings. It can only handle one daylight savings rule, so
  36. * it can't represent historical changes.
  37. *
  38. * This object is tightly bound to the Gregorian calendar. It assumes
  39. * a regular seven days week, and the month lengths are that of the
  40. * Gregorian Calendar. It can only handle daylight savings for years
  41. * lying in the AD era.
  42. *
  43. * @see Calendar
  44. * @see GregorianCalendar
  45. * @author Jochen Hoenicke
  46. */
  47. public class SimpleTimeZone extends TimeZone
  48. {
  49. /**
  50. * The raw time zone offset in milliseconds to GMT, ignoring
  51. * daylight savings.
  52. * @serial
  53. */
  54. private int rawOffset;
  55. /**
  56. * True, if this timezone uses daylight savings, false otherwise.
  57. * @serial
  58. */
  59. private boolean useDaylight;
  60. /**
  61. * The daylight savings offset. This is a positive offset in
  62. * milliseconds with respect to standard time. Typically this
  63. * is one hour, but for some time zones this may be half an hour.
  64. * @serial
  65. * @since JDK1.1.4
  66. */
  67. private int dstSavings = 60 * 60 * 1000;
  68. /**
  69. * The first year, in which daylight savings rules applies.
  70. * @serial
  71. */
  72. private int startYear;
  73. private static final int DOM_MODE = 1;
  74. private static final int DOW_IN_MONTH_MODE = 2;
  75. private static final int DOW_GE_DOM_MODE = 3;
  76. private static final int DOW_LE_DOM_MODE = 4;
  77. /**
  78. * The mode of the start rule. This takes one of the following values:
  79. * <dl>
  80. * <dt>DOM_MODE (1)</dt>
  81. * <dd> startDay contains the day in month of the start date,
  82. * startDayOfWeek is unused. </dd>
  83. * <dt>DOW_IN_MONTH_MODE (2)</dt>
  84. * <dd> The startDay gives the day of week in month, and
  85. * startDayOfWeek the day of week. For example startDay=2 and
  86. * startDayOfWeek=Calender.SUNDAY specifies that the change is on
  87. * the second sunday in that month. You must make sure, that this
  88. * day always exists (ie. don't specify the 5th sunday).
  89. * </dd>
  90. * <dt>DOW_GE_DOM_MODE (3)</dt>
  91. * <dd> The start is on the first startDayOfWeek on or after
  92. * startDay. For example startDay=13 and
  93. * startDayOfWeek=Calendar.FRIDAY specifies that the daylight
  94. * savings start on the first FRIDAY on or after the 13th of that
  95. * Month. Make sure that the change is always in the given month, or
  96. * the result is undefined.
  97. * </dd>
  98. * <dt>DOW_LE_DOM_MONTH (4)</dt>
  99. * <dd> The start is on the first startDayOfWeek on or before the
  100. * startDay. Make sure that the change is always in the given
  101. * month, or the result is undefined.
  102. </dd>
  103. * </dl>
  104. * @serial */
  105. private int startMode;
  106. /**
  107. * The month in which daylight savings start. This is one of the
  108. * constants Calendar.JANUARY, ..., Calendar.DECEMBER.
  109. * @serial
  110. */
  111. private int startMonth;
  112. /**
  113. * This variable can have different meanings. See startMode for details
  114. * @see #startMode
  115. * @serial
  116. */
  117. private int startDay;
  118. /**
  119. * This variable specifies the day of week the change takes place. If
  120. * startMode == DOM_MODE, this is undefined.
  121. * @serial
  122. * @see #startMode
  123. */
  124. private int startDayOfWeek;
  125. /**
  126. * This variable specifies the time of change to daylight savings.
  127. * This time is given in milliseconds after midnight in startTimeMode
  128. * chosen time mode.
  129. * @serial
  130. */
  131. private int startTime;
  132. /**
  133. * This variable specifies the mode that startTime is specified in. By
  134. * default it is WALL_TIME, but can also be STANDARD_TIME or UTC_TIME. For
  135. * startTime, STANDARD_TIME and WALL_TIME are equivalent.
  136. * @serial
  137. */
  138. private int startTimeMode = WALL_TIME;
  139. /**
  140. * The month in which daylight savings ends. This is one of the
  141. * constants Calendar.JANUARY, ..., Calendar.DECEMBER.
  142. * @serial
  143. */
  144. private int endMonth;
  145. /**
  146. * This variable gives the mode for the end of daylight savings rule.
  147. * It can take the same values as startMode.
  148. * @serial
  149. * @see #startMode
  150. */
  151. private int endMode;
  152. /**
  153. * This variable can have different meanings. See startMode for details
  154. * @serial
  155. * @see #startMode
  156. */
  157. private int endDay;
  158. /**
  159. * This variable specifies the day of week the change takes place. If
  160. * endMode == DOM_MODE, this is undefined.
  161. * @serial
  162. * @see #startMode
  163. */
  164. private int endDayOfWeek;
  165. /**
  166. * This variable specifies the time of change back to standard time.
  167. * This time is given in milliseconds after midnight in endTimeMode
  168. * chosen time mode.
  169. * @serial
  170. */
  171. private int endTime;
  172. /**
  173. * This variable specifies the mode that endTime is specified in. By
  174. * default it is WALL_TIME, but can also be STANDARD_TIME or UTC_TIME.
  175. * @serial
  176. */
  177. private int endTimeMode = WALL_TIME;
  178. /**
  179. * This variable points to a deprecated array from JDK 1.1. It is
  180. * ignored in JDK 1.2 but streamed out for compatibility with JDK 1.1.
  181. * The array contains the lengths of the months in the year and is
  182. * assigned from a private static final field to avoid allocating
  183. * the array for every instance of the object.
  184. * Note that static final fields are not serialized.
  185. * @serial
  186. */
  187. private byte[] monthLength = monthArr;
  188. private static final byte[] monthArr =
  189. {
  190. 31, 28, 31, 30, 31, 30, 31, 31, 30,
  191. 31, 30, 31
  192. };
  193. /**
  194. * The version of the serialized data on the stream.
  195. * <dl>
  196. * <dt>0 or not present on stream</dt>
  197. * <dd> JDK 1.1.3 or earlier, only provides this fields:
  198. * rawOffset, startDay, startDayOfWeek, startMonth, startTime,
  199. * startYear, endDay, endDayOfWeek, endMonth, endTime
  200. * </dd>
  201. * <dd> JDK 1.1.4 or later. This includes three new fields, namely
  202. * startMode, endMode and dstSavings. And there is a optional section
  203. * as described in writeObject.
  204. * </dd>
  205. * </dl>
  206. *
  207. * XXX - JDK 1.2 Beta 4 docu states 1.1.4, but my 1.1.5 has the old
  208. * version.
  209. *
  210. * When streaming out this class it is always written in the latest
  211. * version.
  212. * @serial
  213. * @since JDK1.1.4
  214. */
  215. private int serialVersionOnStream = 2;
  216. private static final long serialVersionUID = -403250971215465050L;
  217. /**
  218. * Constant to indicate that start and end times are specified in standard
  219. * time, without adjusting for daylight savings.
  220. */
  221. public static final int STANDARD_TIME = 1;
  222. /**
  223. * Constant to indicate that start and end times are specified in wall
  224. * time, adjusting for daylight savings. This is the default.
  225. */
  226. public static final int WALL_TIME = 0;
  227. /**
  228. * Constant to indicate that start and end times are specified in UTC.
  229. */
  230. public static final int UTC_TIME = 2;
  231. /**
  232. * Create a <code>SimpleTimeZone</code> with the given time offset
  233. * from GMT and without daylight savings.
  234. * @param rawOffset the time offset from GMT in milliseconds.
  235. * @param id The identifier of this time zone.
  236. */
  237. public SimpleTimeZone(int rawOffset, String id)
  238. {
  239. this.rawOffset = rawOffset;
  240. setID(id);
  241. useDaylight = false;
  242. startYear = 0;
  243. }
  244. /**
  245. * Create a <code>SimpleTimeZone</code> with the given time offset
  246. * from GMT and with daylight savings. The start/end parameters
  247. * can have different meaning (replace WEEKDAY with a real day of
  248. * week). Only the first two meanings were supported by earlier
  249. * versions of jdk.
  250. *
  251. * <dl>
  252. * <dt><code>day &gt; 0, dayOfWeek = Calendar.WEEKDAY</code></dt>
  253. * <dd>The start/end of daylight savings is on the <code>day</code>-th
  254. * <code>WEEKDAY</code> in the given month. </dd>
  255. * <dt><code>day &lt; 0, dayOfWeek = Calendar.WEEKDAY</code></dt>
  256. * <dd>The start/end of daylight savings is on the <code>-day</code>-th
  257. * <code>WEEKDAY</code> counted from the <i>end</i> of the month. </dd>
  258. * <dt><code>day &gt; 0, dayOfWeek = 0</code></dt>
  259. * <dd>The start/end of daylight is on the <code>day</code>-th day of
  260. * the month. </dd>
  261. * <dt><code>day &gt; 0, dayOfWeek = -Calendar.WEEKDAY</code></dt>
  262. * <dd>The start/end of daylight is on the first WEEKDAY on or after
  263. * the <code>day</code>-th day of the month. You must make sure that
  264. * this day lies in the same month. </dd>
  265. * <dt><code>day &lt; 0, dayOfWeek = -Calendar.WEEKDAY</code></dt>
  266. * <dd>The start/end of daylight is on the first WEEKDAY on or
  267. * <i>before</i> the <code>-day</code>-th day of the month. You
  268. * must make sure that this day lies in the same month. </dd>
  269. * </dl>
  270. *
  271. * If you give a non existing month, a day that is zero, or too big,
  272. * or a dayOfWeek that is too big, the result is undefined.
  273. *
  274. * The start rule must have a different month than the end rule.
  275. * This restriction shouldn't hurt for all possible time zones.
  276. *
  277. * @param rawOffset The time offset from GMT in milliseconds.
  278. * @param id The identifier of this time zone.
  279. * @param startMonth The start month of daylight savings; use the
  280. * constants in Calendar.
  281. * @param startDayOfWeekInMonth A day in month or a day of week number, as
  282. * described above.
  283. * @param startDayOfWeek The start rule day of week; see above.
  284. * @param startTime A time in millis in standard time.
  285. * @param endMonth The end month of daylight savings; use the
  286. * constants in Calendar.
  287. * @param endDayOfWeekInMonth A day in month or a day of week number, as
  288. * described above.
  289. * @param endDayOfWeek The end rule day of week; see above.
  290. * @param endTime A time in millis in standard time.
  291. * @throws IllegalArgumentException if parameters are invalid or out of
  292. * range.
  293. */
  294. public SimpleTimeZone(int rawOffset, String id, int startMonth,
  295. int startDayOfWeekInMonth, int startDayOfWeek,
  296. int startTime, int endMonth, int endDayOfWeekInMonth,
  297. int endDayOfWeek, int endTime)
  298. {
  299. this.rawOffset = rawOffset;
  300. setID(id);
  301. useDaylight = true;
  302. setStartRule(startMonth, startDayOfWeekInMonth, startDayOfWeek, startTime);
  303. setEndRule(endMonth, endDayOfWeekInMonth, endDayOfWeek, endTime);
  304. if (startMonth == endMonth)
  305. throw new IllegalArgumentException("startMonth and endMonth must be different");
  306. this.startYear = 0;
  307. }
  308. /**
  309. * This constructs a new SimpleTimeZone that supports a daylight savings
  310. * rule. The parameter are the same as for the constructor above, except
  311. * there is the additional dstSavaings parameter.
  312. *
  313. * @param dstSavings the amount of savings for daylight savings
  314. * time in milliseconds. This must be positive.
  315. * @since 1.2
  316. */
  317. public SimpleTimeZone(int rawOffset, String id, int startMonth,
  318. int startDayOfWeekInMonth, int startDayOfWeek,
  319. int startTime, int endMonth, int endDayOfWeekInMonth,
  320. int endDayOfWeek, int endTime, int dstSavings)
  321. {
  322. this(rawOffset, id, startMonth, startDayOfWeekInMonth, startDayOfWeek,
  323. startTime, endMonth, endDayOfWeekInMonth, endDayOfWeek, endTime);
  324. this.dstSavings = dstSavings;
  325. }
  326. /**
  327. * This constructs a new SimpleTimeZone that supports a daylight savings
  328. * rule. The parameter are the same as for the constructor above, except
  329. * there are the additional startTimeMode, endTimeMode, and dstSavings
  330. * parameters.
  331. *
  332. * @param startTimeMode the mode that start times are specified in. One of
  333. * WALL_TIME, STANDARD_TIME, or UTC_TIME.
  334. * @param endTimeMode the mode that end times are specified in. One of
  335. * WALL_TIME, STANDARD_TIME, or UTC_TIME.
  336. * @param dstSavings the amount of savings for daylight savings
  337. * time in milliseconds. This must be positive.
  338. * @throws IllegalArgumentException if parameters are invalid or out of
  339. * range.
  340. * @since 1.4
  341. */
  342. public SimpleTimeZone(int rawOffset, String id, int startMonth,
  343. int startDayOfWeekInMonth, int startDayOfWeek,
  344. int startTime, int startTimeMode, int endMonth,
  345. int endDayOfWeekInMonth, int endDayOfWeek,
  346. int endTime, int endTimeMode, int dstSavings)
  347. {
  348. this(rawOffset, id, startMonth, startDayOfWeekInMonth, startDayOfWeek,
  349. startTime, endMonth, endDayOfWeekInMonth, endDayOfWeek, endTime);
  350. if (startTimeMode < WALL_TIME || startTimeMode > UTC_TIME)
  351. throw new IllegalArgumentException("startTimeMode must be one of WALL_TIME, STANDARD_TIME, or UTC_TIME");
  352. if (endTimeMode < WALL_TIME || endTimeMode > UTC_TIME)
  353. throw new IllegalArgumentException("endTimeMode must be one of WALL_TIME, STANDARD_TIME, or UTC_TIME");
  354. this.dstSavings = dstSavings;
  355. this.startTimeMode = startTimeMode;
  356. this.endTimeMode = endTimeMode;
  357. }
  358. /**
  359. * Sets the first year, where daylight savings applies. The daylight
  360. * savings rule never apply for years in the BC era. Note that this
  361. * is gregorian calendar specific.
  362. * @param year the start year.
  363. */
  364. public void setStartYear(int year)
  365. {
  366. startYear = year;
  367. useDaylight = true;
  368. }
  369. /**
  370. * Checks if the month, day, dayOfWeek arguments are in range and
  371. * returns the mode of the rule.
  372. * @param month the month parameter as in the constructor
  373. * @param day the day parameter as in the constructor
  374. * @param dayOfWeek the day of week parameter as in the constructor
  375. * @return the mode of this rule see startMode.
  376. * @exception IllegalArgumentException if parameters are out of range.
  377. * @see #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)
  378. * @see #startMode
  379. */
  380. private int checkRule(int month, int day, int dayOfWeek)
  381. {
  382. if (month < 0 || month > 11)
  383. throw new IllegalArgumentException("month out of range");
  384. int daysInMonth = getDaysInMonth(month, 1);
  385. if (dayOfWeek == 0)
  386. {
  387. if (day <= 0 || day > daysInMonth)
  388. throw new IllegalArgumentException("day out of range");
  389. return DOM_MODE;
  390. }
  391. else if (dayOfWeek > 0)
  392. {
  393. if (Math.abs(day) > (daysInMonth + 6) / 7)
  394. throw new IllegalArgumentException("dayOfWeekInMonth out of range");
  395. if (dayOfWeek > Calendar.SATURDAY)
  396. throw new IllegalArgumentException("dayOfWeek out of range");
  397. return DOW_IN_MONTH_MODE;
  398. }
  399. else
  400. {
  401. if (day == 0 || Math.abs(day) > daysInMonth)
  402. throw new IllegalArgumentException("day out of range");
  403. if (dayOfWeek < -Calendar.SATURDAY)
  404. throw new IllegalArgumentException("dayOfWeek out of range");
  405. if (day < 0)
  406. return DOW_LE_DOM_MODE;
  407. else
  408. return DOW_GE_DOM_MODE;
  409. }
  410. }
  411. /**
  412. * Sets the daylight savings start rule. You must also set the
  413. * end rule with <code>setEndRule</code> or the result of
  414. * getOffset is undefined. For the parameters see the ten-argument
  415. * constructor above.
  416. *
  417. * @param month The month where daylight savings start, zero
  418. * based. You should use the constants in Calendar.
  419. * @param day A day of month or day of week in month.
  420. * @param dayOfWeek The day of week where daylight savings start.
  421. * @param time The time in milliseconds standard time where daylight
  422. * savings start.
  423. * @exception IllegalArgumentException if parameters are out of range.
  424. * @see SimpleTimeZone
  425. */
  426. public void setStartRule(int month, int day, int dayOfWeek, int time)
  427. {
  428. this.startMode = checkRule(month, day, dayOfWeek);
  429. this.startMonth = month;
  430. this.startDay = day;
  431. this.startDayOfWeek = Math.abs(dayOfWeek);
  432. this.startTime = time;
  433. this.startTimeMode = WALL_TIME;
  434. }
  435. /**
  436. * Sets the daylight savings start rule. You must also set the
  437. * end rule with <code>setEndRule</code> or the result of
  438. * getOffset is undefined. For the parameters see the ten-argument
  439. * constructor above.
  440. *
  441. * Note that this API isn't incredibly well specified. It appears that the
  442. * after flag must override the parameters, since normally, the day and
  443. * dayofweek can select this. I.e., if day < 0 and dayOfWeek < 0, on or
  444. * before mode is chosen. But if after == true, this implementation
  445. * overrides the signs of the other arguments. And if dayOfWeek == 0, it
  446. * falls back to the behavior in the other APIs. I guess this should be
  447. * checked against Sun's implementation.
  448. *
  449. * @param month The month where daylight savings start, zero
  450. * based. You should use the constants in Calendar.
  451. * @param day A day of month or day of week in month.
  452. * @param dayOfWeek The day of week where daylight savings start.
  453. * @param time The time in milliseconds standard time where daylight
  454. * savings start.
  455. * @param after If true, day and dayOfWeek specify first day of week on or
  456. * after day, else first day of week on or before.
  457. * @since 1.2
  458. * @see SimpleTimeZone
  459. */
  460. public void setStartRule(int month, int day, int dayOfWeek, int time,
  461. boolean after)
  462. {
  463. if (after)
  464. setStartRule(month, day, -dayOfWeek, time);
  465. else
  466. setStartRule(month, -day, -dayOfWeek, time);
  467. }
  468. /**
  469. * Sets the daylight savings start rule. You must also set the
  470. * end rule with <code>setEndRule</code> or the result of
  471. * getOffset is undefined. For the parameters see the ten-argument
  472. * constructor above.
  473. *
  474. * @param month The month where daylight savings start, zero
  475. * based. You should use the constants in Calendar.
  476. * @param day A day of month or day of week in month.
  477. * @param time The time in milliseconds standard time where daylight
  478. * savings start.
  479. * @see SimpleTimeZone
  480. * @since 1.2
  481. */
  482. public void setStartRule(int month, int day, int time)
  483. {
  484. setStartRule(month, day, 0, time);
  485. }
  486. /**
  487. * Sets the daylight savings end rule. You must also set the
  488. * start rule with <code>setStartRule</code> or the result of
  489. * getOffset is undefined. For the parameters see the ten-argument
  490. * constructor above.
  491. *
  492. * @param month The end month of daylight savings.
  493. * @param day A day in month, or a day of week in month.
  494. * @param dayOfWeek A day of week, when daylight savings ends.
  495. * @param time A time in millis in standard time.
  496. * @see #setStartRule(int, int, int, int)
  497. */
  498. public void setEndRule(int month, int day, int dayOfWeek, int time)
  499. {
  500. this.endMode = checkRule(month, day, dayOfWeek);
  501. this.endMonth = month;
  502. this.endDay = day;
  503. this.endDayOfWeek = Math.abs(dayOfWeek);
  504. this.endTime = time;
  505. this.endTimeMode = WALL_TIME;
  506. useDaylight = true;
  507. }
  508. /**
  509. * Sets the daylight savings end rule. You must also set the
  510. * start rule with <code>setStartRule</code> or the result of
  511. * getOffset is undefined. For the parameters see the ten-argument
  512. * constructor above.
  513. *
  514. * Note that this API isn't incredibly well specified. It appears that the
  515. * after flag must override the parameters, since normally, the day and
  516. * dayofweek can select this. I.e., if day < 0 and dayOfWeek < 0, on or
  517. * before mode is chosen. But if after == true, this implementation
  518. * overrides the signs of the other arguments. And if dayOfWeek == 0, it
  519. * falls back to the behavior in the other APIs. I guess this should be
  520. * checked against Sun's implementation.
  521. *
  522. * @param month The end month of daylight savings.
  523. * @param day A day in month, or a day of week in month.
  524. * @param dayOfWeek A day of week, when daylight savings ends.
  525. * @param time A time in millis in standard time.
  526. * @param after If true, day and dayOfWeek specify first day of week on or
  527. * after day, else first day of week on or before.
  528. * @since 1.2
  529. * @see #setStartRule(int, int, int, int, boolean)
  530. */
  531. public void setEndRule(int month, int day, int dayOfWeek, int time,
  532. boolean after)
  533. {
  534. if (after)
  535. setEndRule(month, day, -dayOfWeek, time);
  536. else
  537. setEndRule(month, -day, -dayOfWeek, time);
  538. }
  539. /**
  540. * Sets the daylight savings end rule. You must also set the
  541. * start rule with <code>setStartRule</code> or the result of
  542. * getOffset is undefined. For the parameters see the ten-argument
  543. * constructor above.
  544. *
  545. * @param month The end month of daylight savings.
  546. * @param day A day in month, or a day of week in month.
  547. * @param time A time in millis in standard time.
  548. * @see #setStartRule(int, int, int)
  549. */
  550. public void setEndRule(int month, int day, int time)
  551. {
  552. setEndRule(month, day, 0, time);
  553. }
  554. /**
  555. * Gets the time zone offset, for current date, modified in case of
  556. * daylight savings. This is the offset to add to UTC to get the local
  557. * time.
  558. *
  559. * In the standard JDK the results given by this method may result in
  560. * inaccurate results at the end of February or the beginning of March.
  561. * To avoid this, you should use Calendar instead:
  562. * <code>offset = cal.get(Calendar.ZONE_OFFSET)
  563. * + cal.get(Calendar.DST_OFFSET);</code>
  564. *
  565. * This version doesn't suffer this inaccuracy.
  566. *
  567. * The arguments don't follow the approach for setting start and end rules.
  568. * The day must be a positive number and dayOfWeek must be a positive value
  569. * from Calendar. dayOfWeek is redundant, but must match the other values
  570. * or an inaccurate result may be returned.
  571. *
  572. * @param era the era of the given date
  573. * @param year the year of the given date
  574. * @param month the month of the given date, 0 for January.
  575. * @param day the day of month
  576. * @param dayOfWeek the day of week; this must match the other fields.
  577. * @param millis the millis in the day (in local standard time)
  578. * @return the time zone offset in milliseconds.
  579. * @throws IllegalArgumentException if arguments are incorrect.
  580. */
  581. public int getOffset(int era, int year, int month, int day, int dayOfWeek,
  582. int millis)
  583. {
  584. int daysInMonth = getDaysInMonth(month, year);
  585. if (day < 1 || day > daysInMonth)
  586. throw new IllegalArgumentException("day out of range");
  587. if (dayOfWeek < Calendar.SUNDAY || dayOfWeek > Calendar.SATURDAY)
  588. throw new IllegalArgumentException("dayOfWeek out of range");
  589. if (month < Calendar.JANUARY || month > Calendar.DECEMBER)
  590. throw new IllegalArgumentException("month out of range:" + month);
  591. // This method is called by Calendar, so we mustn't use that class.
  592. int daylightSavings = 0;
  593. if (useDaylight && era == GregorianCalendar.AD && year >= startYear)
  594. {
  595. int orig_year = year;
  596. int time = startTime + (startTimeMode == UTC_TIME ? rawOffset : 0);
  597. // This does only work for Gregorian calendars :-(
  598. // This is mainly because setStartYear doesn't take an era.
  599. boolean afterStart = ! isBefore(year, month, day, dayOfWeek, millis,
  600. startMode, startMonth, startDay,
  601. startDayOfWeek, time);
  602. millis += dstSavings;
  603. if (millis >= 24 * 60 * 60 * 1000)
  604. {
  605. millis -= 24 * 60 * 60 * 1000;
  606. dayOfWeek = (dayOfWeek % 7) + 1;
  607. if (++day > daysInMonth)
  608. {
  609. day = 1;
  610. if (month++ == Calendar.DECEMBER)
  611. {
  612. month = Calendar.JANUARY;
  613. year++;
  614. }
  615. }
  616. }
  617. time = endTime + (endTimeMode == UTC_TIME ? rawOffset : 0);
  618. if (endTimeMode != WALL_TIME)
  619. time += dstSavings;
  620. boolean beforeEnd = isBefore(year, month, day, dayOfWeek, millis,
  621. endMode, endMonth, endDay, endDayOfWeek,
  622. time);
  623. if (year != orig_year)
  624. afterStart = false;
  625. if (startMonth < endMonth)
  626. // use daylight savings, if the date is after the start of
  627. // savings, and before the end of savings.
  628. daylightSavings = afterStart && beforeEnd ? dstSavings : 0;
  629. else
  630. // use daylight savings, if the date is before the end of
  631. // savings, or after the start of savings.
  632. daylightSavings = beforeEnd || afterStart ? dstSavings : 0;
  633. }
  634. return rawOffset + daylightSavings;
  635. }
  636. /**
  637. * Returns the time zone offset to GMT in milliseconds, ignoring
  638. * day light savings.
  639. * @return the time zone offset.
  640. */
  641. public int getRawOffset()
  642. {
  643. return rawOffset;
  644. }
  645. /**
  646. * Sets the standard time zone offset to GMT.
  647. * @param rawOffset The time offset from GMT in milliseconds.
  648. */
  649. public void setRawOffset(int rawOffset)
  650. {
  651. this.rawOffset = rawOffset;
  652. }
  653. /**
  654. * Gets the daylight savings offset. This is a positive offset in
  655. * milliseconds with respect to standard time. Typically this
  656. * is one hour, but for some time zones this may be half an our.
  657. * @return the daylight savings offset in milliseconds.
  658. *
  659. * @since 1.2
  660. */
  661. public int getDSTSavings()
  662. {
  663. return dstSavings;
  664. }
  665. /**
  666. * Sets the daylight savings offset. This is a positive offset in
  667. * milliseconds with respect to standard time.
  668. *
  669. * @param dstSavings the daylight savings offset in milliseconds.
  670. *
  671. * @since 1.2
  672. */
  673. public void setDSTSavings(int dstSavings)
  674. {
  675. if (dstSavings <= 0)
  676. throw new IllegalArgumentException("illegal value for dstSavings");
  677. this.dstSavings = dstSavings;
  678. }
  679. /**
  680. * Returns if this time zone uses daylight savings time.
  681. * @return true, if we use daylight savings time, false otherwise.
  682. */
  683. public boolean useDaylightTime()
  684. {
  685. return useDaylight;
  686. }
  687. /**
  688. * Returns the number of days in the given month.
  689. * Uses gregorian rules prior to 1582 (The default and earliest cutover)
  690. * @param month The month, zero based; use one of the Calendar constants.
  691. * @param year The year.
  692. */
  693. private int getDaysInMonth(int month, int year)
  694. {
  695. if (month == Calendar.FEBRUARY)
  696. {
  697. if ((year & 3) != 0)
  698. return 28;
  699. // Assume default Gregorian cutover,
  700. // all years prior to this must be Julian
  701. if (year < 1582)
  702. return 29;
  703. // Gregorian rules
  704. return ((year % 100) != 0 || (year % 400) == 0) ? 29 : 28;
  705. }
  706. else
  707. return monthArr[month];
  708. }
  709. /**
  710. * Checks if the date given in calXXXX, is before the change between
  711. * dst and standard time.
  712. * @param calYear the year of the date to check (for leap day checking).
  713. * @param calMonth the month of the date to check.
  714. * @param calDayOfMonth the day of month of the date to check.
  715. * @param calDayOfWeek the day of week of the date to check.
  716. * @param calMillis the millis of day of the date to check (standard time).
  717. * @param mode the change mode; same semantic as startMode.
  718. * @param month the change month; same semantic as startMonth.
  719. * @param day the change day; same semantic as startDay.
  720. * @param dayOfWeek the change day of week;
  721. * @param millis the change time in millis since midnight standard time.
  722. * same semantic as startDayOfWeek.
  723. * @return true, if cal is before the change, false if cal is on
  724. * or after the change.
  725. */
  726. private boolean isBefore(int calYear, int calMonth, int calDayOfMonth,
  727. int calDayOfWeek, int calMillis, int mode,
  728. int month, int day, int dayOfWeek, int millis)
  729. {
  730. // This method is called by Calendar, so we mustn't use that class.
  731. // We have to do all calculations by hand.
  732. // check the months:
  733. // XXX - this is not correct:
  734. // for the DOW_GE_DOM and DOW_LE_DOM modes the change date may
  735. // be in a different month.
  736. if (calMonth != month)
  737. return calMonth < month;
  738. // check the day:
  739. switch (mode)
  740. {
  741. case DOM_MODE:
  742. if (calDayOfMonth != day)
  743. return calDayOfMonth < day;
  744. break;
  745. case DOW_IN_MONTH_MODE:
  746. {
  747. // This computes the day of month of the day of type
  748. // "dayOfWeek" that lies in the same (sunday based) week as cal.
  749. calDayOfMonth += (dayOfWeek - calDayOfWeek);
  750. // Now we convert it to 7 based number (to get a one based offset
  751. // after dividing by 7). If we count from the end of the
  752. // month, we get want a -7 based number counting the days from
  753. // the end:
  754. if (day < 0)
  755. calDayOfMonth -= getDaysInMonth(calMonth, calYear) + 7;
  756. else
  757. calDayOfMonth += 6;
  758. // day > 0 day < 0
  759. // S M T W T F S S M T W T F S
  760. // 7 8 9 10 11 12 -36-35-34-33-32-31
  761. // 13 14 15 16 17 18 19 -30-29-28-27-26-25-24
  762. // 20 21 22 23 24 25 26 -23-22-21-20-19-18-17
  763. // 27 28 29 30 31 32 33 -16-15-14-13-12-11-10
  764. // 34 35 36 -9 -8 -7
  765. // Now we calculate the day of week in month:
  766. int week = calDayOfMonth / 7;
  767. // day > 0 day < 0
  768. // S M T W T F S S M T W T F S
  769. // 1 1 1 1 1 1 -5 -5 -4 -4 -4 -4
  770. // 1 2 2 2 2 2 2 -4 -4 -4 -3 -3 -3 -3
  771. // 2 3 3 3 3 3 3 -3 -3 -3 -2 -2 -2 -2
  772. // 3 4 4 4 4 4 4 -2 -2 -2 -1 -1 -1 -1
  773. // 4 5 5 -1 -1 -1
  774. if (week != day)
  775. return week < day;
  776. if (calDayOfWeek != dayOfWeek)
  777. return calDayOfWeek < dayOfWeek;
  778. // daylight savings starts/ends on the given day.
  779. break;
  780. }
  781. case DOW_LE_DOM_MODE:
  782. // The greatest sunday before or equal December, 12
  783. // is the same as smallest sunday after or equal December, 6.
  784. day = Math.abs(day) - 6;
  785. case DOW_GE_DOM_MODE:
  786. // Calculate the day of month of the day of type
  787. // "dayOfWeek" that lies before (or on) the given date.
  788. calDayOfMonth -= (calDayOfWeek < dayOfWeek ? 7 : 0) + calDayOfWeek
  789. - dayOfWeek;
  790. if (calDayOfMonth < day)
  791. return true;
  792. if (calDayOfWeek != dayOfWeek || calDayOfMonth >= day + 7)
  793. return false;
  794. // now we have the same day
  795. break;
  796. }
  797. // the millis decides:
  798. return (calMillis < millis);
  799. }
  800. /**
  801. * Determines if the given date is in daylight savings time.
  802. * @return true, if it is in daylight savings time, false otherwise.
  803. */
  804. public boolean inDaylightTime(Date date)
  805. {
  806. Calendar cal = Calendar.getInstance(this);
  807. cal.setTime(date);
  808. return (cal.get(Calendar.DST_OFFSET) != 0);
  809. }
  810. /**
  811. * Generates the hashCode for the SimpleDateFormat object. It is
  812. * the rawOffset, possibly, if useDaylightSavings is true, xored
  813. * with startYear, startMonth, startDayOfWeekInMonth, ..., endTime.
  814. */
  815. public synchronized int hashCode()
  816. {
  817. return rawOffset
  818. ^ (useDaylight
  819. ? startMonth ^ startDay ^ startDayOfWeek ^ startTime ^ endMonth
  820. ^ endDay ^ endDayOfWeek ^ endTime : 0);
  821. }
  822. public synchronized boolean equals(Object o)
  823. {
  824. if (this == o)
  825. return true;
  826. if (! (o instanceof SimpleTimeZone))
  827. return false;
  828. SimpleTimeZone zone = (SimpleTimeZone) o;
  829. if (zone.hashCode() != hashCode() || ! getID().equals(zone.getID())
  830. || rawOffset != zone.rawOffset || useDaylight != zone.useDaylight)
  831. return false;
  832. if (! useDaylight)
  833. return true;
  834. return (startYear == zone.startYear && startMonth == zone.startMonth
  835. && startDay == zone.startDay
  836. && startDayOfWeek == zone.startDayOfWeek
  837. && startTime == zone.startTime
  838. && startTimeMode == zone.startTimeMode && endMonth == zone.endMonth
  839. && endDay == zone.endDay && endDayOfWeek == zone.endDayOfWeek
  840. && endTime == zone.endTime && endTimeMode == zone.endTimeMode);
  841. }
  842. /**
  843. * Test if the other time zone uses the same rule and only
  844. * possibly differs in ID. This implementation for this particular
  845. * class will return true if the other object is a SimpleTimeZone,
  846. * the raw offsets and useDaylight are identical and if useDaylight
  847. * is true, also the start and end datas are identical.
  848. * @return true if this zone uses the same rule.
  849. */
  850. public boolean hasSameRules(TimeZone other)
  851. {
  852. if (this == other)
  853. return true;
  854. if (! (other instanceof SimpleTimeZone))
  855. return false;
  856. SimpleTimeZone zone = (SimpleTimeZone) other;
  857. if (zone.hashCode() != hashCode() || rawOffset != zone.rawOffset
  858. || useDaylight != zone.useDaylight)
  859. return false;
  860. if (! useDaylight)
  861. return true;
  862. return (startYear == zone.startYear && startMonth == zone.startMonth
  863. && startDay == zone.startDay
  864. && startDayOfWeek == zone.startDayOfWeek
  865. && startTime == zone.startTime
  866. && startTimeMode == zone.startTimeMode && endMonth == zone.endMonth
  867. && endDay == zone.endDay && endDayOfWeek == zone.endDayOfWeek
  868. && endTime == zone.endTime && endTimeMode == zone.endTimeMode);
  869. }
  870. /**
  871. * Returns a string representation of this SimpleTimeZone object.
  872. * @return a string representation of this SimpleTimeZone object.
  873. */
  874. public String toString()
  875. {
  876. // the test for useDaylight is an incompatibility to jdk1.2, but
  877. // I think this shouldn't hurt.
  878. return getClass().getName() + "[" + "id=" + getID() + ",offset="
  879. + rawOffset + ",dstSavings=" + dstSavings + ",useDaylight="
  880. + useDaylight
  881. + (useDaylight
  882. ? ",startYear=" + startYear + ",startMode=" + startMode
  883. + ",startMonth=" + startMonth + ",startDay=" + startDay
  884. + ",startDayOfWeek=" + startDayOfWeek + ",startTime="
  885. + startTime + ",startTimeMode=" + startTimeMode + ",endMode="
  886. + endMode + ",endMonth=" + endMonth + ",endDay=" + endDay
  887. + ",endDayOfWeek=" + endDayOfWeek + ",endTime=" + endTime
  888. + ",endTimeMode=" + endTimeMode : "") + "]";
  889. }
  890. /**
  891. * Reads a serialized simple time zone from stream.
  892. * @see #writeObject
  893. */
  894. private void readObject(java.io.ObjectInputStream input)
  895. throws java.io.IOException, ClassNotFoundException
  896. {
  897. input.defaultReadObject();
  898. if (serialVersionOnStream == 0)
  899. {
  900. // initialize the new fields to default values.
  901. dstSavings = 60 * 60 * 1000;
  902. endMode = DOW_IN_MONTH_MODE;
  903. startMode = DOW_IN_MONTH_MODE;
  904. startTimeMode = WALL_TIME;
  905. endTimeMode = WALL_TIME;
  906. serialVersionOnStream = 2;
  907. }
  908. else
  909. {
  910. int length = input.readInt();
  911. byte[] byteArray = new byte[length];
  912. input.read(byteArray, 0, length);
  913. if (length >= 4)
  914. {
  915. // Lets hope that Sun does extensions to the serialized
  916. // form in a sane manner.
  917. startDay = byteArray[0];
  918. startDayOfWeek = byteArray[1];
  919. endDay = byteArray[2];
  920. endDayOfWeek = byteArray[3];
  921. }
  922. }
  923. }
  924. /**
  925. * Serializes this object to a stream. @serialdata The object is
  926. * first written in the old JDK 1.1 format, so that it can be read
  927. * by the old classes. This means, that the
  928. * <code>start/endDay(OfWeek)</code>-Fields are written in the
  929. * DOW_IN_MONTH_MODE rule, since this was the only supported rule
  930. * in 1.1.
  931. *
  932. * In the optional section, we write first the length of an byte
  933. * array as int and afterwards the byte array itself. The byte
  934. * array contains in this release four elements, namely the real
  935. * startDay, startDayOfWeek endDay, endDayOfWeek in that Order.
  936. * These fields are needed, because for compatibility reasons only
  937. * approximative values are written to the required section, as
  938. * described above.
  939. */
  940. private void writeObject(java.io.ObjectOutputStream output)
  941. throws java.io.IOException
  942. {
  943. byte[] byteArray = new byte[]
  944. {
  945. (byte) startDay, (byte) startDayOfWeek, (byte) endDay,
  946. (byte) endDayOfWeek
  947. };
  948. /* calculate the approximation for JDK 1.1 */
  949. switch (startMode)
  950. {
  951. case DOM_MODE:
  952. startDayOfWeek = Calendar.SUNDAY; // random day of week
  953. // fall through
  954. case DOW_GE_DOM_MODE:
  955. case DOW_LE_DOM_MODE:
  956. startDay = (startDay + 6) / 7;
  957. }
  958. switch (endMode)
  959. {
  960. case DOM_MODE:
  961. endDayOfWeek = Calendar.SUNDAY;
  962. // fall through
  963. case DOW_GE_DOM_MODE:
  964. case DOW_LE_DOM_MODE:
  965. endDay = (endDay + 6) / 7;
  966. }
  967. // the required part:
  968. output.defaultWriteObject();
  969. // the optional part:
  970. output.writeInt(byteArray.length);
  971. output.write(byteArray, 0, byteArray.length);
  972. }
  973. }