1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.util.Date;
8 import java.util.Enumeration;
9 import java.util.Hashtable;
10
11 import net.sourceforge.jsdp.util.TypedTime;
12 import net.sourceforge.jsdp.util.ZoneAdjustment;
13
14
15
16
17
18
19
20
21
22
23
24
25 public class TimeZone implements Field {
26
27
28 private static final long serialVersionUID = -87926730273093503L;
29
30
31 protected boolean isTyped;
32
33
34 protected Hashtable adjustments;
35
36
37
38
39 protected TimeZone() {
40
41 super();
42
43 adjustments = new Hashtable(3);
44 }
45
46
47
48
49
50
51
52
53
54
55
56 public TimeZone(final Date time, final long offset) throws SDPException {
57
58 this(new ZoneAdjustment(time, offset));
59 }
60
61
62
63
64
65
66 public TimeZone(final ZoneAdjustment adjustment) {
67
68 this();
69
70 addZoneAdjustment(adjustment);
71 this.isTyped = true;
72 }
73
74
75
76
77
78
79
80
81
82
83 public static TimeZone parse(final String field) throws SDPParseException {
84
85 if (!field.startsWith("z=")) {
86 throw new SDPParseException("The string \"" + field + "\" isn't a timezone field");
87 }
88
89 String[] values = field.substring(2).split(" ");
90
91 if (values.length < 2 || (values.length % 2 != 0)) {
92 throw new SDPParseException("The string \"" + field + "\" isn't a valid timezone field");
93 }
94
95 TimeZone z = new TimeZone();
96
97 for (int i = 0; i < values.length; i += 2) {
98 try {
99 z.addZoneAdjustment(new ZoneAdjustment(Long.parseLong(values[i]), TypedTime.getTime(values[i + 1])));
100 }
101 catch (Exception parseException) {
102 throw new SDPParseException("The string \"" + field + "\" isn't a valid timezone field", parseException);
103 }
104 }
105
106 return z;
107 }
108
109
110
111
112
113
114
115
116
117
118 public void addZoneAdjustment(final Date time, final long offset) throws SDPException {
119
120 adjustments.put(time, new ZoneAdjustment(time, offset));
121 }
122
123
124
125
126
127
128 public void addZoneAdjustment(final ZoneAdjustment adjustment) {
129
130 adjustments.put(new Date(adjustment.getTime()), adjustment);
131 }
132
133
134
135
136
137
138 public Object clone() {
139
140 TimeZone field = new TimeZone();
141
142 field.isTyped = this.isTyped;
143 field.adjustments = (Hashtable) this.adjustments.clone();
144
145 return field;
146 }
147
148
149
150
151
152
153 public char getType() {
154
155 return Field.TIMEZONE_FIELD;
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
183
184
185
186
187
188
189
190 public boolean isTypedTime() {
191
192 return isTyped;
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 public void setTypedTime(final boolean typedTime) {
211
212 this.isTyped = typedTime;
213 }
214
215
216
217
218
219
220
221 public String toString() {
222
223 StringBuffer result = new StringBuffer(getType() + "=");
224 ZoneAdjustment adjustment;
225 Enumeration values = adjustments.elements();
226 while (values.hasMoreElements()) {
227
228 adjustment = (ZoneAdjustment) values.nextElement();
229
230 result.append(adjustment.getTime() + " ");
231 if (this.isTyped) {
232 result.append(TypedTime.toString(adjustment.getOffset()) + " ");
233 }
234 else {
235 result.append(adjustment.getOffset() + " ");
236 }
237 }
238 result.deleteCharAt(result.length() - 1);
239
240 return result.toString();
241 }
242 }