gcc/
[official-gcc.git] / libjava / java / util / SimpleTimeZone.java
blob648b6753267d7cfc41e47a41b57afc6fd713e30b
1 /* java.util.SimpleTimeZone
2 Copyright (C) 1998, 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.util;
41 /**
42 * This class represents a simple time zone offset and handles
43 * daylight savings. It can only handle one daylight savings rule, so
44 * it can't represent historical changes.
46 * This object is tightly bound to the Gregorian calendar. It assumes
47 * a regular seven days week, and the month lengths are that of the
48 * Gregorian Calendar. It can only handle daylight savings for years
49 * lying in the AD era.
51 * @see Calendar
52 * @see GregorianCalender
53 * @author Jochen Hoenicke
55 public class SimpleTimeZone extends TimeZone
57 /**
58 * The raw time zone offset in milliseconds to GMT, ignoring
59 * daylight savings.
60 * @serial
62 private int rawOffset;
64 /**
65 * True, if this timezone uses daylight savings, false otherwise.
66 * @serial
68 private boolean useDaylight;
70 /**
71 * The daylight savings offset. This is a positive offset in
72 * milliseconds with respect to standard time. Typically this
73 * is one hour, but for some time zones this may be half an our.
74 * @serial
75 * @since JDK1.1.4
77 private int dstSavings = 60 * 60 * 1000;
79 /**
80 * The first year, in which daylight savings rules applies.
81 * @serial
83 private int startYear;
85 private static final int DOM_MODE = 1;
86 private static final int DOW_IN_MONTH_MODE = 2;
87 private static final int DOW_GE_DOM_MODE = 3;
88 private static final int DOW_LE_DOM_MODE = 4;
90 /**
91 * The mode of the start rule. This takes one of the following values:
92 * <dl>
93 * <dt>DOM_MODE (1)</dt>
94 * <dd> startDay contains the day in month of the start date,
95 * startDayOfWeek is unused. </dd>
96 * <dt>DOW_IN_MONTH_MODE (2)</dt>
97 * <dd> The startDay gives the day of week in month, and
98 * startDayOfWeek the day of week. For example startDay=2 and
99 * startDayOfWeek=Calender.SUNDAY specifies that the change is on
100 * the second sunday in that month. You must make sure, that this
101 * day always exists (ie. don't specify the 5th sunday).
102 * </dd>
103 * <dt>DOW_GE_DOM_MODE (3)</dt>
104 * <dd> The start is on the first startDayOfWeek on or after
105 * startDay. For example startDay=13 and
106 * startDayOfWeek=Calendar.FRIDAY specifies that the daylight
107 * savings start on the first FRIDAY on or after the 13th of that
108 * Month. Make sure that the change is always in the given month, or
109 * the result is undefined.
110 * </dd>
111 * <dt>DOW_LE_DOM_MONTH (4)</dt>
112 * <dd> The start is on the first startDayOfWeek on or before the
113 * startDay. Make sure that the change is always in the given
114 * month, or the result is undefined.
115 </dd>
116 * </dl>
117 * @serial */
118 private int startMode;
121 * The month in which daylight savings start. This is one of the
122 * constants Calendar.JANUARY, ..., Calendar.DECEMBER.
123 * @serial
125 private int startMonth;
128 * This variable can have different meanings. See startMode for details
129 * @see #startMode;
130 * @serial
132 private int startDay;
135 * This variable specifies the day of week the change takes place. If
136 * startMode == DOM_MODE, this is undefined.
137 * @serial
138 * @see #startMode;
140 private int startDayOfWeek;
143 * This variable specifies the time of change to daylight savings.
144 * This time is given in milliseconds after midnight local
145 * standard time.
146 * @serial
148 private int startTime;
151 * This variable specifies the mode that startTime is specified in. By
152 * default it is WALL_TIME, but can also be STANDARD_TIME or UTC_TIME. For
153 * startTime, STANDARD_TIME and WALL_TIME are equivalent.
154 * @serial
156 private int startTimeMode = WALL_TIME;
159 * The month in which daylight savings ends. This is one of the
160 * constants Calendar.JANUARY, ..., Calendar.DECEMBER.
161 * @serial
163 private int endMonth;
166 * This variable gives the mode for the end of daylight savings rule.
167 * It can take the same values as startMode.
168 * @serial
169 * @see #startMode
171 private int endMode;
174 * This variable can have different meanings. See startMode for details
175 * @serial
176 * @see #startMode;
178 private int endDay;
181 * This variable specifies the day of week the change takes place. If
182 * endMode == DOM_MODE, this is undefined.
183 * @serial
184 * @see #startMode;
186 private int endDayOfWeek;
189 * This variable specifies the time of change back to standard time.
190 * This time is given in milliseconds after midnight local
191 * standard time.
192 * @serial
194 private int endTime;
197 * This variable specifies the mode that endTime is specified in. By
198 * default it is WALL_TIME, but can also be STANDARD_TIME or UTC_TIME.
199 * @serial
201 private int endTimeMode = WALL_TIME;
204 * This variable points to a deprecated array from JDK 1.1. It is
205 * ignored in JDK 1.2 but streamed out for compatibility with JDK 1.1.
206 * The array contains the lengths of the months in the year and is
207 * assigned from a private static final field to avoid allocating
208 * the array for every instance of the object.
209 * Note that static final fields are not serialized.
210 * @serial
212 private byte[] monthLength = monthArr;
213 private static final byte[] monthArr =
214 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
217 * The version of the serialized data on the stream.
218 * <dl>
219 * <dt>0 or not present on stream</dt>
220 * <dd> JDK 1.1.3 or earlier, only provides this fields:
221 * rawOffset, startDay, startDayOfWeek, startMonth, startTime,
222 * startYear, endDay, endDayOfWeek, endMonth, endTime
223 * </dd>
224 * <dd> JDK 1.1.4 or later. This includes three new fields, namely
225 * startMode, endMode and dstSavings. And there is a optional section
226 * as described in writeObject.
227 * </dd>
229 * XXX - JDK 1.2 Beta 4 docu states 1.1.4, but my 1.1.5 has the old
230 * version.
232 * When streaming out this class it is always written in the latest
233 * version.
234 * @serial
235 * @since JDK1.1.4
237 private int serialVersionOnStream = 2;
239 private static final long serialVersionUID = -403250971215465050L;
242 * Constant to indicate that start and end times are specified in standard
243 * time, without adjusting for daylight savings.
245 public static final int STANDARD_TIME = 1;
248 * Constant to indicate that start and end times are specified in wall
249 * time, adjusting for daylight savings. This is the default.
251 public static final int WALL_TIME = 0;
254 * Constant to indicate that start and end times are specified in UTC.
256 public static final int UTC_TIME = 2;
259 * Create a <code>SimpleTimeZone</code> with the given time offset
260 * from GMT and without daylight savings.
261 * @param rawOffset the time offset from GMT in milliseconds.
262 * @param id The identifier of this time zone.
264 public SimpleTimeZone(int rawOffset, String id)
266 this.rawOffset = rawOffset;
267 setID(id);
268 useDaylight = false;
269 startYear = 0;
273 * Create a <code>SimpleTimeZone</code> with the given time offset
274 * from GMT and with daylight savings. The start/end parameters
275 * can have different meaning (replace WEEKDAY with a real day of
276 * week). Only the first two meanings were supported by earlier
277 * versions of jdk.
279 * <dl>
280 * <dt><code>day &gt; 0, dayOfWeek = Calendar.WEEKDAY</code></dt>
281 * <dd>The start/end of daylight savings is on the <code>day</code>-th
282 * <code>WEEKDAY</code> in the given month. </dd>
283 * <dt><code>day &lt; 0, dayOfWeek = Calendar.WEEKDAY</code></dt>
284 * <dd>The start/end of daylight savings is on the <code>-day</code>-th
285 * <code>WEEKDAY</code> counted from the <i>end</i> of the month. </dd>
286 * <dt><code>day &gt; 0, dayOfWeek = 0</code></dt>
287 * <dd>The start/end of daylight is on the <code>day</code>-th day of
288 * the month. </dd>
289 * <dt><code>day &gt; 0, dayOfWeek = -Calendar.WEEKDAY</code></dt>
290 * <dd>The start/end of daylight is on the first WEEKDAY on or after
291 * the <code>day</code>-th day of the month. You must make sure that
292 * this day lies in the same month. </dd>
293 * <dt><code>day &lt; 0, dayOfWeek = -Calendar.WEEKDAY</code></dt>
294 * <dd>The start/end of daylight is on the first WEEKDAY on or
295 * <i>before</i> the <code>-day</code>-th day of the month. You
296 * must make sure that this day lies in the same month. </dd>
297 * </dl>
299 * If you give a non existing month, a day that is zero, or too big,
300 * or a dayOfWeek that is too big, the result is undefined.
302 * The start rule must have a different month than the end rule.
303 * This restriction shouldn't hurt for all possible time zones.
305 * @param rawOffset The time offset from GMT in milliseconds.
306 * @param id The identifier of this time zone.
307 * @param startMonth The start month of daylight savings; use the
308 * constants in Calendar.
309 * @param startday A day in month or a day of week number, as
310 * described above.
311 * @param startDayOfWeek The start rule day of week; see above.
312 * @param startTime A time in millis in standard time.
313 * @param endMonth The end month of daylight savings; use the
314 * constants in Calendar.
315 * @param endday A day in month or a day of week number, as
316 * described above.
317 * @param endDayOfWeek The end rule day of week; see above.
318 * @param endTime A time in millis in standard time.
319 * @throws IllegalArgumentException if parameters are invalid or out of
320 * range.
322 public SimpleTimeZone(int rawOffset, String id,
323 int startMonth, int startDayOfWeekInMonth,
324 int startDayOfWeek, int startTime,
325 int endMonth, int endDayOfWeekInMonth,
326 int endDayOfWeek, int endTime)
328 this.rawOffset = rawOffset;
329 setID(id);
330 useDaylight = true;
332 setStartRule(startMonth, startDayOfWeekInMonth,
333 startDayOfWeek, startTime);
334 setEndRule(endMonth, endDayOfWeekInMonth, endDayOfWeek, endTime);
335 if (startMonth == endMonth)
336 throw new IllegalArgumentException
337 ("startMonth and endMonth must be different");
338 this.startYear = 0;
342 * This constructs a new SimpleTimeZone that supports a daylight savings
343 * rule. The parameter are the same as for the constructor above, except
344 * there is the additional dstSavaings parameter.
346 * @param dstSavings the amount of savings for daylight savings
347 * time in milliseconds. This must be positive.
348 * @since 1.2
350 public SimpleTimeZone(int rawOffset, String id,
351 int startMonth, int startDayOfWeekInMonth,
352 int startDayOfWeek, int startTime,
353 int endMonth, int endDayOfWeekInMonth,
354 int endDayOfWeek, int endTime, int dstSavings)
356 this(rawOffset, id,
357 startMonth, startDayOfWeekInMonth, startDayOfWeek, startTime,
358 endMonth, endDayOfWeekInMonth, endDayOfWeek, endTime);
360 this.dstSavings = dstSavings;
364 * This constructs a new SimpleTimeZone that supports a daylight savings
365 * rule. The parameter are the same as for the constructor above, except
366 * there are the additional startTimeMode, endTimeMode, and dstSavings
367 * parameters.
369 * @param startTimeMode the mode that start times are specified in. One of
370 * WALL_TIME, STANDARD_TIME, or UTC_TIME.
371 * @param endTimeMode the mode that end times are specified in. One of
372 * WALL_TIME, STANDARD_TIME, or UTC_TIME.
373 * @param dstSavings the amount of savings for daylight savings
374 * time in milliseconds. This must be positive.
375 * @throws IllegalArgumentException if parameters are invalid or out of
376 * range.
377 * @since 1.4
379 public SimpleTimeZone(int rawOffset, String id,
380 int startMonth, int startDayOfWeekInMonth,
381 int startDayOfWeek, int startTime, int startTimeMode,
382 int endMonth, int endDayOfWeekInMonth,
383 int endDayOfWeek, int endTime, int endTimeMode,
384 int dstSavings)
386 this.rawOffset = rawOffset;
387 setID(id);
388 useDaylight = true;
390 if (startTimeMode < WALL_TIME || startTimeMode > UTC_TIME)
391 throw new IllegalArgumentException("startTimeMode must be one of WALL_TIME, STANDARD_TIME, or UTC_TIME");
392 if (endTimeMode < WALL_TIME || endTimeMode > UTC_TIME)
393 throw new IllegalArgumentException("endTimeMode must be one of WALL_TIME, STANDARD_TIME, or UTC_TIME");
394 this.startTimeMode = startTimeMode;
395 this.endTimeMode = endTimeMode;
397 setStartRule(startMonth, startDayOfWeekInMonth,
398 startDayOfWeek, startTime);
399 setEndRule(endMonth, endDayOfWeekInMonth, endDayOfWeek, endTime);
400 if (startMonth == endMonth)
401 throw new IllegalArgumentException
402 ("startMonth and endMonth must be different");
403 this.startYear = 0;
405 this.dstSavings = dstSavings;
409 * Sets the first year, where daylight savings applies. The daylight
410 * savings rule never apply for years in the BC era. Note that this
411 * is gregorian calendar specific.
412 * @param year the start year.
414 public void setStartYear(int year)
416 startYear = year;
417 useDaylight = true;
421 * Checks if the month, day, dayOfWeek arguments are in range and
422 * returns the mode of the rule.
423 * @param month the month parameter as in the constructor
424 * @param day the day parameter as in the constructor
425 * @param dayOfWeek the day of week parameter as in the constructor
426 * @return the mode of this rule see startMode.
427 * @exception IllegalArgumentException if parameters are out of range.
428 * @see #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)
429 * @see #startMode
431 private int checkRule(int month, int day, int dayOfWeek)
433 int daysInMonth = getDaysInMonth(month, 1);
434 if (dayOfWeek == 0)
436 if (day <= 0 || day > daysInMonth)
437 throw new IllegalArgumentException("day out of range");
438 return DOM_MODE;
440 else if (dayOfWeek > 0)
442 if (Math.abs(day) > (daysInMonth + 6) / 7)
443 throw new IllegalArgumentException("dayOfWeekInMonth out of range");
444 if (dayOfWeek > Calendar.SATURDAY)
445 throw new IllegalArgumentException("dayOfWeek out of range");
446 return DOW_IN_MONTH_MODE;
448 else
450 if (day == 0 || Math.abs(day) > daysInMonth)
451 throw new IllegalArgumentException("day out of range");
452 if (dayOfWeek < -Calendar.SATURDAY)
453 throw new IllegalArgumentException("dayOfWeek out of range");
454 if (day < 0)
455 return DOW_LE_DOM_MODE;
456 else
457 return DOW_GE_DOM_MODE;
463 * Sets the daylight savings start rule. You must also set the
464 * end rule with <code>setEndRule</code> or the result of
465 * getOffset is undefined. For the parameters see the ten-argument
466 * constructor above.
468 * @param month The month where daylight savings start, zero
469 * based. You should use the constants in Calendar.
470 * @param day A day of month or day of week in month.
471 * @param dayOfWeek The day of week where daylight savings start.
472 * @param time The time in milliseconds standard time where daylight
473 * savings start.
474 * @see SimpleTimeZone
476 public void setStartRule(int month, int day, int dayOfWeek, int time)
478 this.startMode = checkRule(month, day, dayOfWeek);
479 this.startMonth = month;
480 this.startDay = day;
481 this.startDayOfWeek = Math.abs(dayOfWeek);
482 if (this.startTimeMode == WALL_TIME || this.startTimeMode == STANDARD_TIME)
483 this.startTime = time;
484 else
485 // Convert from UTC to STANDARD
486 this.startTime = time + this.rawOffset;
487 useDaylight = true;
491 * Sets the daylight savings start rule. You must also set the
492 * end rule with <code>setEndRule</code> or the result of
493 * getOffset is undefined. For the parameters see the ten-argument
494 * constructor above.
496 * Note that this API isn't incredibly well specified. It appears that the
497 * after flag must override the parameters, since normally, the day and
498 * dayofweek can select this. I.e., if day < 0 and dayOfWeek < 0, on or
499 * before mode is chosen. But if after == true, this implementation
500 * overrides the signs of the other arguments. And if dayOfWeek == 0, it
501 * falls back to the behavior in the other APIs. I guess this should be
502 * checked against Sun's implementation.
504 * @param month The month where daylight savings start, zero
505 * based. You should use the constants in Calendar.
506 * @param day A day of month or day of week in month.
507 * @param dayOfWeek The day of week where daylight savings start.
508 * @param time The time in milliseconds standard time where daylight
509 * savings start.
510 * @param after If true, day and dayOfWeek specify first day of week on or
511 * after day, else first day of week on or before.
512 * @since 1.2
513 * @see SimpleTimeZone
515 public void setStartRule(int month, int day, int dayOfWeek, int time, boolean after)
517 // FIXME: XXX: Validate that checkRule and offset processing work with on
518 // or before mode.
519 this.startDay = after ? Math.abs(day) : -Math.abs(day);
520 this.startDayOfWeek = after ? Math.abs(dayOfWeek) : -Math.abs(dayOfWeek);
521 this.startMode = (dayOfWeek != 0) ? (after ? DOW_GE_DOM_MODE : DOW_LE_DOM_MODE)
522 : checkRule(month, day, dayOfWeek);
523 this.startDay = Math.abs(this.startDay);
524 this.startDayOfWeek = Math.abs(this.startDayOfWeek);
526 this.startMonth = month;
528 if (this.startTimeMode == WALL_TIME || this.startTimeMode == STANDARD_TIME)
529 this.startTime = time;
530 else
531 // Convert from UTC to STANDARD
532 this.startTime = time + this.rawOffset;
533 useDaylight = true;
537 * Sets the daylight savings start rule. You must also set the
538 * end rule with <code>setEndRule</code> or the result of
539 * getOffset is undefined. For the parameters see the ten-argument
540 * constructor above.
542 * @param month The month where daylight savings start, zero
543 * based. You should use the constants in Calendar.
544 * @param day A day of month or day of week in month.
545 * @param time The time in milliseconds standard time where daylight
546 * savings start.
547 * @see SimpleTimeZone
548 * @since 1.2
550 public void setStartRule(int month, int day, int time)
552 setStartRule(month, day, 0, time);
556 * Sets the daylight savings end rule. You must also set the
557 * start rule with <code>setStartRule</code> or the result of
558 * getOffset is undefined. For the parameters see the ten-argument
559 * constructor above.
561 * @param month The end month of daylight savings.
562 * @param day A day in month, or a day of week in month.
563 * @param dayOfWeek A day of week, when daylight savings ends.
564 * @param time A time in millis in standard time.
565 * @see #setStartRule
567 public void setEndRule(int month, int day, int dayOfWeek, int time)
569 this.endMode = checkRule(month, day, dayOfWeek);
570 this.endMonth = month;
571 this.endDay = day;
572 this.endDayOfWeek = Math.abs(dayOfWeek);
573 if (this.endTimeMode == WALL_TIME)
574 this.endTime = time;
575 else if (this.endTimeMode == STANDARD_TIME)
576 // Convert from STANDARD to DST
577 this.endTime = time + this.dstSavings;
578 else
579 // Convert from UTC to DST
580 this.endTime = time + this.rawOffset + this.dstSavings;
581 useDaylight = true;
585 * Sets the daylight savings end rule. You must also set the
586 * start rule with <code>setStartRule</code> or the result of
587 * getOffset is undefined. For the parameters see the ten-argument
588 * constructor above.
590 * Note that this API isn't incredibly well specified. It appears that the
591 * after flag must override the parameters, since normally, the day and
592 * dayofweek can select this. I.e., if day < 0 and dayOfWeek < 0, on or
593 * before mode is chosen. But if after == true, this implementation
594 * overrides the signs of the other arguments. And if dayOfWeek == 0, it
595 * falls back to the behavior in the other APIs. I guess this should be
596 * checked against Sun's implementation.
598 * @param month The end month of daylight savings.
599 * @param day A day in month, or a day of week in month.
600 * @param dayOfWeek A day of week, when daylight savings ends.
601 * @param time A time in millis in standard time.
602 * @param after If true, day and dayOfWeek specify first day of week on or
603 * after day, else first day of week on or before.
604 * @since 1.2
605 * @see #setStartRule
607 public void setEndRule(int month, int day, int dayOfWeek, int time, boolean after)
609 // FIXME: XXX: Validate that checkRule and offset processing work with on
610 // or before mode.
611 this.endDay = after ? Math.abs(day) : -Math.abs(day);
612 this.endDayOfWeek = after ? Math.abs(dayOfWeek) : -Math.abs(dayOfWeek);
613 this.endMode = (dayOfWeek != 0) ? (after ? DOW_GE_DOM_MODE : DOW_LE_DOM_MODE)
614 : checkRule(month, day, dayOfWeek);
615 this.endDay = Math.abs(this.endDay);
616 this.endDayOfWeek = Math.abs(endDayOfWeek);
618 this.endMonth = month;
620 if (this.endTimeMode == WALL_TIME)
621 this.endTime = time;
622 else if (this.endTimeMode == STANDARD_TIME)
623 // Convert from STANDARD to DST
624 this.endTime = time + this.dstSavings;
625 else
626 // Convert from UTC to DST
627 this.endTime = time + this.rawOffset + this.dstSavings;
628 useDaylight = true;
632 * Sets the daylight savings end rule. You must also set the
633 * start rule with <code>setStartRule</code> or the result of
634 * getOffset is undefined. For the parameters see the ten-argument
635 * constructor above.
637 * @param month The end month of daylight savings.
638 * @param day A day in month, or a day of week in month.
639 * @param dayOfWeek A day of week, when daylight savings ends.
640 * @param time A time in millis in standard time.
641 * @see #setStartRule
643 public void setEndRule(int month, int day, int time)
645 setEndRule(month, day, 0, time);
649 * Gets the time zone offset, for current date, modified in case of
650 * daylight savings. This is the offset to add to UTC to get the local
651 * time.
653 * In the standard JDK the results given by this method may result in
654 * inaccurate results at the end of February or the beginning of March.
655 * To avoid this, you should use Calendar instead:
656 * <code>offset = cal.get(Calendar.ZONE_OFFSET)
657 * + cal.get(Calendar.DST_OFFSET);</code>
659 * This version doesn't suffer this inaccuracy.
661 * The arguments don't follow the approach for setting start and end rules.
662 * The day must be a positive number and dayOfWeek must be a positive value
663 * from Calendar. dayOfWeek is redundant, but must match the other values
664 * or an inaccurate result may be returned.
666 * @param era the era of the given date
667 * @param year the year of the given date
668 * @param month the month of the given date, 0 for January.
669 * @param day the day of month
670 * @param dayOfWeek the day of week; this must match the other fields.
671 * @param millis the millis in the day (in local standard time)
672 * @return the time zone offset in milliseconds.
673 * @throws IllegalArgumentException if arguments are incorrect.
675 public int getOffset(int era, int year, int month,
676 int day, int dayOfWeek, int millis)
678 int daysInMonth = getDaysInMonth(month, year);
679 if (day < 1 || day > daysInMonth)
680 throw new IllegalArgumentException("day out of range");
681 if (dayOfWeek < Calendar.SUNDAY || dayOfWeek > Calendar.SATURDAY)
682 throw new IllegalArgumentException("dayOfWeek out of range");
683 if (month < Calendar.JANUARY || month > Calendar.DECEMBER)
684 throw new IllegalArgumentException("month out of range");
686 // This method is called by Calendar, so we mustn't use that class.
687 int daylightSavings = 0;
688 if (useDaylight && era == GregorianCalendar.AD && year >= startYear)
690 // This does only work for Gregorian calendars :-(
691 // This is mainly because setStartYear doesn't take an era.
693 boolean afterStart = !isBefore(year, month, day, dayOfWeek, millis,
694 startMode, startMonth,
695 startDay, startDayOfWeek, startTime);
696 boolean beforeEnd = isBefore(year, month, day, dayOfWeek, millis,
697 endMode, endMonth,
698 endDay, endDayOfWeek, endTime);
700 if (startMonth < endMonth)
702 // use daylight savings, if the date is after the start of
703 // savings, and before the end of savings.
704 daylightSavings = afterStart && beforeEnd ? dstSavings : 0;
706 else
708 // use daylight savings, if the date is before the end of
709 // savings, or after the start of savings.
710 daylightSavings = beforeEnd || afterStart ? dstSavings : 0;
713 return rawOffset + daylightSavings;
717 * Returns the time zone offset to GMT in milliseconds, ignoring
718 * day light savings.
719 * @return the time zone offset.
721 public int getRawOffset()
723 return rawOffset;
727 * Sets the standard time zone offset to GMT.
728 * @param rawOffset The time offset from GMT in milliseconds.
730 public void setRawOffset(int rawOffset)
732 this.rawOffset = rawOffset;
736 * Gets the daylight savings offset. This is a positive offset in
737 * milliseconds with respect to standard time. Typically this
738 * is one hour, but for some time zones this may be half an our.
739 * @return the daylight savings offset in milliseconds.
741 * @since 1.2
743 public int getDSTSavings()
745 return dstSavings;
749 * Sets the daylight savings offset. This is a positive offset in
750 * milliseconds with respect to standard time.
752 * @param dstSavings the daylight savings offset in milliseconds.
754 * @since 1.2
756 public void setDSTSavings(int dstSavings)
758 if (dstSavings <= 0)
759 throw new IllegalArgumentException("illegal value for dstSavings");
761 this.dstSavings = dstSavings;
765 * Returns if this time zone uses daylight savings time.
766 * @return true, if we use daylight savings time, false otherwise.
768 public boolean useDaylightTime()
770 return useDaylight;
774 * Returns the number of days in the given month. It does always
775 * use the Gregorian leap year rule.
776 * @param month The month, zero based; use one of the Calendar constants.
777 * @param year The year.
779 private int getDaysInMonth(int month, int year)
781 // Most of this is copied from GregorianCalendar.getActualMaximum()
782 if (month == Calendar.FEBRUARY)
784 return ((year & 3) == 0 && (year % 100 != 0 || year % 400 == 0))
785 ? 29 : 28;
787 else if (month < Calendar.AUGUST)
788 return 31 - (month & 1);
789 else
790 return 30 + (month & 1);
794 * Checks if the date given in calXXXX, is before the change between
795 * dst and standard time.
796 * @param calYear the year of the date to check (for leap day checking).
797 * @param calMonth the month of the date to check.
798 * @param calDay the day of month of the date to check.
799 * @param calDayOfWeek the day of week of the date to check.
800 * @param calMillis the millis of day of the date to check (standard time).
801 * @param mode the change mode; same semantic as startMode.
802 * @param month the change month; same semantic as startMonth.
803 * @param day the change day; same semantic as startDay.
804 * @param dayOfWeek the change day of week;
805 * @param millis the change time in millis since midnight standard time.
806 * same semantic as startDayOfWeek.
807 * @return true, if cal is before the change, false if cal is on
808 * or after the change.
810 private boolean isBefore(int calYear,
811 int calMonth, int calDayOfMonth, int calDayOfWeek,
812 int calMillis, int mode, int month,
813 int day, int dayOfWeek, int millis)
816 // This method is called by Calendar, so we mustn't use that class.
817 // We have to do all calculations by hand.
819 // check the months:
821 // XXX - this is not correct:
822 // for the DOW_GE_DOM and DOW_LE_DOM modes the change date may
823 // be in a different month.
824 if (calMonth != month)
825 return calMonth < month;
827 // check the day:
828 switch (mode)
830 case DOM_MODE:
831 if (calDayOfMonth != day)
832 return calDayOfMonth < day;
833 break;
834 case DOW_IN_MONTH_MODE:
836 // This computes the day of month of the day of type
837 // "dayOfWeek" that lies in the same (sunday based) week as cal.
838 calDayOfMonth += (dayOfWeek - calDayOfWeek);
840 // Now we convert it to 7 based number (to get a one based offset
841 // after dividing by 7). If we count from the end of the
842 // month, we get want a -7 based number counting the days from
843 // the end:
845 if (day < 0)
846 calDayOfMonth -= getDaysInMonth(calMonth, calYear) + 7;
847 else
848 calDayOfMonth += 6;
850 // day > 0 day < 0
851 // S M T W T F S S M T W T F S
852 // 7 8 9 10 11 12 -36-35-34-33-32-31
853 // 13 14 15 16 17 18 19 -30-29-28-27-26-25-24
854 // 20 21 22 23 24 25 26 -23-22-21-20-19-18-17
855 // 27 28 29 30 31 32 33 -16-15-14-13-12-11-10
856 // 34 35 36 -9 -8 -7
858 // Now we calculate the day of week in month:
859 int week = calDayOfMonth / 7;
860 // day > 0 day < 0
861 // S M T W T F S S M T W T F S
862 // 1 1 1 1 1 1 -5 -5 -4 -4 -4 -4
863 // 1 2 2 2 2 2 2 -4 -4 -4 -3 -3 -3 -3
864 // 2 3 3 3 3 3 3 -3 -3 -3 -2 -2 -2 -2
865 // 3 4 4 4 4 4 4 -2 -2 -2 -1 -1 -1 -1
866 // 4 5 5 -1 -1 -1
868 if (week != day)
869 return week < day;
871 if (calDayOfWeek != dayOfWeek)
872 return calDayOfWeek < dayOfWeek;
874 // daylight savings starts/ends on the given day.
875 break;
878 case DOW_LE_DOM_MODE:
879 // The greatest sunday before or equal December, 12
880 // is the same as smallest sunday after or equal December, 6.
881 day = Math.abs(day) - 6;
883 case DOW_GE_DOM_MODE:
885 // Calculate the day of month of the day of type
886 // "dayOfWeek" that lies before (or on) the given date.
887 calDayOfMonth -= (calDayOfWeek < dayOfWeek ? 7 : 0)
888 + calDayOfWeek - dayOfWeek;
889 if (calDayOfMonth < day)
890 return true;
891 if (calDayOfWeek != dayOfWeek || calDayOfMonth >= day + 7)
892 return false;
893 // now we have the same day
894 break;
896 // the millis decides:
897 return (calMillis < millis);
901 * Determines if the given date is in daylight savings time.
902 * @return true, if it is in daylight savings time, false otherwise.
904 public boolean inDaylightTime(Date date)
906 Calendar cal = Calendar.getInstance(this);
907 cal.setTime(date);
908 return (cal.get(Calendar.DST_OFFSET) != 0);
912 * Generates the hashCode for the SimpleDateFormat object. It is
913 * the rawOffset, possibly, if useDaylightSavings is true, xored
914 * with startYear, startMonth, startDayOfWeekInMonth, ..., endTime.
916 public synchronized int hashCode()
918 return rawOffset ^
919 (useDaylight ?
920 startMonth ^ startDay ^ startDayOfWeek ^ startTime
921 ^ endMonth ^ endDay ^ endDayOfWeek ^ endTime : 0);
924 public synchronized boolean equals(Object o)
926 if (this == o)
927 return true;
928 if (!(o instanceof SimpleTimeZone))
929 return false;
930 SimpleTimeZone zone = (SimpleTimeZone) o;
931 if (zone.hashCode() != hashCode()
932 || !getID().equals(zone.getID())
933 || rawOffset != zone.rawOffset || useDaylight != zone.useDaylight)
934 return false;
935 if (!useDaylight)
936 return true;
937 return (startYear == zone.startYear
938 && startMonth == zone.startMonth
939 && startDay == zone.startDay
940 && startDayOfWeek == zone.startDayOfWeek
941 && startTime == zone.startTime
942 && startTimeMode == zone.startTimeMode
943 && endMonth == zone.endMonth
944 && endDay == zone.endDay
945 && endDayOfWeek == zone.endDayOfWeek
946 && endTime == zone.endTime
947 && endTimeMode == zone.endTimeMode);
951 * Test if the other time zone uses the same rule and only
952 * possibly differs in ID. This implementation for this particular
953 * class will return true if the other object is a SimpleTimeZone,
954 * the raw offsets and useDaylight are identical and if useDaylight
955 * is true, also the start and end datas are identical.
956 * @return true if this zone uses the same rule.
958 public boolean hasSameRules(TimeZone other)
960 if (this == other)
961 return true;
962 if (!(other instanceof SimpleTimeZone))
963 return false;
964 SimpleTimeZone zone = (SimpleTimeZone) other;
965 if (zone.hashCode() != hashCode()
966 || rawOffset != zone.rawOffset || useDaylight != zone.useDaylight)
967 return false;
968 if (!useDaylight)
969 return true;
970 return (startYear == zone.startYear
971 && startMonth == zone.startMonth
972 && startDay == zone.startDay
973 && startDayOfWeek == zone.startDayOfWeek
974 && startTime == zone.startTime
975 && startTimeMode == zone.startTimeMode
976 && endMonth == zone.endMonth
977 && endDay == zone.endDay
978 && endDayOfWeek == zone.endDayOfWeek
979 && endTime == zone.endTime
980 && endTimeMode == zone.endTimeMode);
984 * Returns a string representation of this SimpleTimeZone object.
985 * @return a string representation of this SimpleTimeZone object.
987 public String toString()
989 // the test for useDaylight is an incompatibility to jdk1.2, but
990 // I think this shouldn't hurt.
991 return getClass().getName() + "["
992 + "id=" + getID()
993 + ",offset=" + rawOffset
994 + ",dstSavings=" + dstSavings
995 + ",useDaylight=" + useDaylight
996 + (useDaylight ?
997 ",startYear=" + startYear
998 + ",startMode=" + startMode
999 + ",startMonth=" + startMonth
1000 + ",startDay=" + startDay
1001 + ",startDayOfWeek=" + startDayOfWeek
1002 + ",startTime=" + startTime
1003 + ",startTimeMode=" + startTimeMode
1004 + ",endMode=" + endMode
1005 + ",endMonth=" + endMonth
1006 + ",endDay=" + endDay
1007 + ",endDayOfWeek=" + endDayOfWeek
1008 + ",endTime=" + endTime
1009 + ",endTimeMode=" + endTimeMode
1010 : "") + "]";
1014 * Reads a serialized simple time zone from stream.
1015 * @see #writeObject
1017 private void readObject(java.io.ObjectInputStream input)
1018 throws java.io.IOException, ClassNotFoundException
1020 input.defaultReadObject();
1021 if (serialVersionOnStream == 0)
1023 // initialize the new fields to default values.
1024 dstSavings = 60 * 60 * 1000;
1025 endMode = DOW_IN_MONTH_MODE;
1026 startMode = DOW_IN_MONTH_MODE;
1027 startTimeMode = WALL_TIME;
1028 endTimeMode = WALL_TIME;
1029 serialVersionOnStream = 2; }
1030 else
1032 int length = input.readInt();
1033 byte[] byteArray = new byte[length];
1034 input.read(byteArray, 0, length);
1035 if (length >= 4)
1037 // Lets hope that Sun does extensions to the serialized
1038 // form in a sane manner.
1039 startDay = byteArray[0];
1040 startDayOfWeek = byteArray[1];
1041 endDay = byteArray[2];
1042 endDayOfWeek = byteArray[3];
1048 * Serializes this object to a stream. @serialdata The object is
1049 * first written in the old JDK 1.1 format, so that it can be read
1050 * by by the old classes. This means, that the
1051 * <code>start/endDay(OfWeek)</code>-Fields are written in the
1052 * DOW_IN_MONTH_MODE rule, since this was the only supported rule
1053 * in 1.1.
1055 * In the optional section, we write first the length of an byte
1056 * array as int and afterwards the byte array itself. The byte
1057 * array contains in this release four elements, namely the real
1058 * startDay, startDayOfWeek endDay, endDayOfWeek in that Order.
1059 * These fields are needed, because for compatibility reasons only
1060 * approximative values are written to the required section, as
1061 * described above.
1063 private void writeObject(java.io.ObjectOutputStream output)
1064 throws java.io.IOException
1066 byte[] byteArray = new byte[]
1068 (byte) startDay, (byte) startDayOfWeek,
1069 (byte) endDay, (byte) endDayOfWeek};
1071 /* calculate the approximation for JDK 1.1 */
1072 switch (startMode)
1074 case DOM_MODE:
1075 startDayOfWeek = Calendar.SUNDAY; // random day of week
1076 // fall through
1077 case DOW_GE_DOM_MODE:
1078 case DOW_LE_DOM_MODE:
1079 startDay = (startDay + 6) / 7;
1081 switch (endMode)
1083 case DOM_MODE:
1084 endDayOfWeek = Calendar.SUNDAY;
1085 // fall through
1086 case DOW_GE_DOM_MODE:
1087 case DOW_LE_DOM_MODE:
1088 endDay = (endDay + 6) / 7;
1091 // the required part:
1092 output.defaultWriteObject();
1093 // the optional part:
1094 output.writeInt(byteArray.length);
1095 output.write(byteArray, 0, byteArray.length);