Initial revision
[gcc.git] / libjava / java / util / SimpleTimeZone.java
1 /* Copyright (C) 1998, 1999 Cygnus Solutions
2
3 This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
8
9 package java.util;
10
11 /**
12 * @author Per Bothner <bothner@cygnus.com>
13 * @date October 24, 1998.
14 */
15
16 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3.
17 * Status: Does not know how to figure out if daylight savings time
18 * is in effect; hence only correct for zones without DST.
19 * No known spec for hashCode.
20 */
21
22 public class SimpleTimeZone extends TimeZone
23 {
24 // The fields are as specified in Sun's "Serialized Form"
25 // in the JDK 1.2 beta 4 API specification.
26
27 int dstSavings = 60 * 60 * 1000;
28
29 int rawOffset;
30
31 // int serialVersionOnStream;
32
33 int startDay;
34 int startDayOfWeek;
35 int startMode; /// Seems to be JDK 1.2 only.
36
37 int startMonth;
38
39 int startTime;
40
41 int startYear;
42
43 int endDay;
44
45 int endDayOfWeek;
46
47 int endMode; // Seems to be JDK 1.2 only.
48
49 int endMonth;
50
51 int endTime;
52
53 // byte[] monthLength;
54
55 boolean useDaylight;
56
57 public SimpleTimeZone (int rawOffset, String ID)
58 {
59 setID(ID);
60 this.rawOffset = rawOffset;
61 }
62
63 public SimpleTimeZone (int rawOffset, String ID,
64 int startMonth, int startDay,
65 int startDayOfWeek, int startTime,
66 int endMonth, int endDay,
67 int endDayOfWeek, int endTime)
68 {
69 this(rawOffset, ID);
70 setStartRule (startMonth, startDay, startDayOfWeek, startTime);
71 setEndRule (endMonth, endDay, endDayOfWeek, endTime);
72 }
73
74 public int getRawOffset() { return rawOffset; }
75 public void setRawOffset (int offsetMillis) { rawOffset = offsetMillis; }
76
77 public int getOffset (int era, int year, int month, int day,
78 int dayOfWeek, int millis)
79 {
80 int offset = getRawOffset();
81 if (useDaylight)
82 {
83 if (startYear != 0
84 && (year < startYear || era == GregorianCalendar.BC))
85 return offset;
86 boolean midYearSummer = startMonth < endMonth;
87 if (midYearSummer ? (month < startMonth || month > endMonth)
88 : (month < startMonth && month > endMonth))
89 return offset; // Definitely not DST.
90 if (midYearSummer ? (month > startMonth && month < endMonth)
91 : (month > startMonth || month < endMonth))
92 return offset + dstSavings; // Definitely DST.
93 // Now it gets more complicated. Bail for now.
94 throw new Error("not implemented - SimpleTimeZone.getOffset");
95 }
96 return offset;
97 }
98
99 public boolean useDaylightTime() { return useDaylight; }
100
101 public boolean inDaylightTime(Date date)
102 {
103 if (! useDaylight)
104 return false;
105 throw new Error("not implemented - SimpleTimeZone.inDaylightTime");
106 }
107
108 public int getDSTSavings () { return dstSavings; }
109
110 public void setDSTSavings (int millisSavedDuringDST)
111 { dstSavings = millisSavedDuringDST; }
112
113 public void setStartRule (int month, int dayOfWeekInMonth,
114 int dayOfWeek, int time)
115 {
116 this.startMonth = month;
117 this.startDay = dayOfWeekInMonth;
118 this.startDayOfWeek = dayOfWeek;
119 this.startTime = time;
120 this.useDaylight = true;
121 }
122
123 public void setEndRule (int month, int dayOfWeekInMonth,
124 int dayOfWeek, int time)
125 {
126 this.endMonth = month;
127 this.endDay = dayOfWeekInMonth;
128 this.endDayOfWeek = dayOfWeek;
129 this.endTime = time;
130 this.useDaylight = true;
131 }
132
133 public void setStartYear (int year)
134 {
135 this.startYear = startYear;
136 }
137
138 public boolean hasSameRules (TimeZone other)
139 {
140 if (this == other)
141 return true;
142 if (! (other instanceof SimpleTimeZone))
143 return false;
144 SimpleTimeZone o = (SimpleTimeZone) other;
145 if (rawOffset != o.rawOffset)
146 return false;
147 if (useDaylight != o.useDaylight)
148 return false;
149 if (! useDaylight)
150 return true;
151 return startDay == o.startDay
152 && startDayOfWeek == o.startDayOfWeek
153 && startMonth == o.startMonth
154 && startTime == o.startTime
155 && endDay == o.endDay
156 && endDayOfWeek == o.endDayOfWeek
157 && endMonth == o.endMonth
158 && endTime == o.endTime
159 && startYear == o.startYear
160 && startMode == o.startMode
161 && endMode == o.endMode;
162 }
163
164 public boolean equals (Object obj)
165 {
166 if (! (obj instanceof SimpleTimeZone))
167 return false;
168 SimpleTimeZone other = (SimpleTimeZone) obj;
169 return getID() == other.getID() && hasSameRules(other);
170 }
171
172 public int hashCode ()
173 {
174 // FIXME - this does not folow any spec (since none is public)!
175 int hash = rawOffset;
176 if (useDaylight)
177 hash += dstSavings + startYear + startMode + endMode
178 + startDay + startDayOfWeek + startMonth + startTime
179 + endDay + endDayOfWeek + endMonth + endTime;
180 return hash;
181 }
182 }