aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/util/SimpleTimeZone.java
blob: 6082b1816e5484106aca70986be6c7e858c82ab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Copyright (C) 1998, 1999  Cygnus Solutions

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.util;

/**
 * @author Per Bothner <bothner@cygnus.com>
 * @date October 24, 1998.
 */

/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3.
 * Status:  Does not know how to figure out if daylight savings time
 *   is in effect;  hence only correct for zones without DST.
 *   No known spec for hashCode.
 */

public class SimpleTimeZone extends TimeZone
{
  // The fields are as specified in Sun's "Serialized Form"
  // in the JDK 1.2 beta 4 API specification.

  int dstSavings = 60 * 60 * 1000;

  int rawOffset;

  // int serialVersionOnStream;

  int startDay;
  int startDayOfWeek;
  int startMode;  /// Seems to be JDK 1.2 only.

  int startMonth;

  int startTime;

  int startYear;

  int endDay;

  int endDayOfWeek;

  int endMode;  // Seems to be JDK 1.2 only.

  int endMonth;

  int endTime;

  // byte[] monthLength;

  boolean useDaylight;

  public SimpleTimeZone (int rawOffset, String ID)
  {
    setID(ID);
    this.rawOffset = rawOffset;
  }

  public SimpleTimeZone (int rawOffset, String ID,
			 int startMonth, int startDay,
			 int startDayOfWeek, int startTime,
			 int endMonth, int endDay,
			 int endDayOfWeek, int endTime)
  {
    this(rawOffset, ID);
    setStartRule (startMonth, startDay, startDayOfWeek, startTime);
    setEndRule (endMonth, endDay, endDayOfWeek, endTime);
  }

  public int getRawOffset() { return rawOffset; }
  public void setRawOffset (int offsetMillis) { rawOffset = offsetMillis; }

  public int getOffset (int era, int year, int month, int day,
			int dayOfWeek, int millis)
  {
    int offset = getRawOffset();
    if (useDaylight)
      {
	if (startYear != 0
	    && (year < startYear || era == GregorianCalendar.BC))
	  return offset;
	boolean midYearSummer = startMonth < endMonth;
	if (midYearSummer ? (month < startMonth || month > endMonth)
	    : (month < startMonth && month > endMonth))
	  return offset; // Definitely not DST.
	if (midYearSummer ? (month > startMonth && month < endMonth)
	    : (month > startMonth || month < endMonth))
	  return offset + dstSavings;  // Definitely DST.
	// Now it gets more complicated.  Bail for now.
	throw new Error("not implemented - SimpleTimeZone.getOffset");
      }
    return offset;
  }

  public boolean useDaylightTime() { return useDaylight; }

  public boolean inDaylightTime(Date date)
  {
    if (! useDaylight)
      return false;
    throw new Error("not implemented - SimpleTimeZone.inDaylightTime");
  }

  public int getDSTSavings () { return dstSavings; }

  public void setDSTSavings (int millisSavedDuringDST)
  { dstSavings = millisSavedDuringDST; }

  public void setStartRule (int month, int dayOfWeekInMonth,
			    int dayOfWeek, int time)
  {
    this.startMonth = month;
    this.startDay = dayOfWeekInMonth;
    this.startDayOfWeek = dayOfWeek;
    this.startTime = time;
    this.useDaylight = true;
  }

  public void setEndRule (int month, int dayOfWeekInMonth,
			    int dayOfWeek, int time)
  {
    this.endMonth = month;
    this.endDay = dayOfWeekInMonth;
    this.endDayOfWeek = dayOfWeek;
    this.endTime = time;
    this.useDaylight = true;
  }

  public void setStartYear (int year)
  {
    this.startYear = startYear;
  }

  public boolean hasSameRules (TimeZone other)
  {
    if (this == other)
      return true;
    if (! (other instanceof SimpleTimeZone))
      return false;
    SimpleTimeZone o = (SimpleTimeZone) other;
    if (rawOffset != o.rawOffset)
      return false;
    if (useDaylight != o.useDaylight)
      return false;
    if (! useDaylight)
      return true;
    return startDay == o.startDay
      && startDayOfWeek == o.startDayOfWeek
      && startMonth == o.startMonth
      && startTime == o.startTime
      && endDay == o.endDay
      && endDayOfWeek == o.endDayOfWeek
      && endMonth == o.endMonth
      && endTime == o.endTime
      && startYear == o.startYear
      && startMode == o.startMode
      && endMode == o.endMode;
  }

  public boolean equals (Object obj)
  {
    if (! (obj instanceof SimpleTimeZone))
      return false;
    SimpleTimeZone other = (SimpleTimeZone) obj;
    return getID() == other.getID() && hasSameRules(other);
  }

  public int hashCode ()
  {
    // FIXME - this does not folow any spec (since none is public)!
    int hash = rawOffset;
    if (useDaylight)
      hash += dstSavings + startYear + startMode + endMode
	+ startDay + startDayOfWeek + startMonth + startTime
	+ endDay + endDayOfWeek + endMonth + endTime;
    return hash;
  }
}