1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.util.Date;
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10
11
12
13
14
15
16
17
18
19
20
21
22 public class Time implements Field {
23
24
25 private static final long serialVersionUID = -4529219101991941664L;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public static final long NTP_CONSTANT = 2208988800L;
49
50
51 private static final Pattern fieldPattern = Pattern.compile("(([1-9](\\d){0,9})|0) (([1-9](\\d){0,9})|0)");
52
53
54 private static final Pattern ntpPattern = Pattern.compile("[1-9](\\d){0,9}");
55
56
57 protected long start;
58
59
60 protected long stop;
61
62
63
64
65
66 public Time() {
67
68 super();
69 this.setZero();
70 }
71
72
73
74
75
76
77
78
79
80
81
82 public Time(final Date start, final Date stop) throws SDPException {
83
84 super();
85
86 setStartTime(start);
87 setStopTime(stop);
88 }
89
90
91
92
93
94
95
96
97
98
99 public Time(final long start, final long stop) throws SDPException {
100
101 super();
102
103 setStartTime(start);
104 setStopTime(stop);
105 }
106
107
108
109
110
111
112
113
114
115 public static Date getDateFromNtp(final long ntpTime) {
116
117 return new Date((ntpTime - NTP_CONSTANT) * 1000);
118 }
119
120
121
122
123
124
125
126
127
128 public static long getNTP(final Date date) {
129
130 long result = 0;
131
132 if (date == null) {
133 result = -1;
134 }
135 else {
136 result = (date.getTime() / 1000) + NTP_CONSTANT;
137 }
138
139 return result;
140 }
141
142
143
144
145
146
147
148
149
150 public static boolean isValidNTP(final String input) {
151
152 return ntpPattern.matcher(input).matches();
153 }
154
155
156
157
158
159
160
161
162
163
164 public static Time parse(final String field) throws SDPParseException {
165
166 if (!field.startsWith("t=")) {
167 throw new SDPParseException("The string \"" + field + "\" isn't a time field");
168 }
169
170 Time t = null;
171
172
173 Matcher matcher = fieldPattern.matcher(field.substring(2));
174
175
176 if (matcher.matches()) {
177
178 try {
179
180 t = new Time(Long.parseLong(matcher.group(1)), Long.parseLong(matcher.group(4)));
181 }
182 catch (SDPException parseException) {
183 throw new SDPParseException("The string \"" + field + "\" isn't a valid time field", parseException);
184 }
185 }
186 else {
187 throw new SDPParseException("The string \"" + field + "\" isn't a valid time field");
188 }
189
190 return t;
191 }
192
193
194
195
196
197
198 public Object clone() {
199
200 Time field = new Time();
201 field.start = this.start;
202 field.stop = this.stop;
203
204 return field;
205 }
206
207
208
209
210
211
212 public Date getStartTime() {
213
214 return getDateFromNtp(start);
215 }
216
217
218
219
220
221
222 public Date getStopTime() {
223
224 return getDateFromNtp(stop);
225 }
226
227
228
229
230
231
232 public char getType() {
233
234 return Field.TIME_FIELD;
235 }
236
237
238
239
240
241
242
243 public boolean isZero() {
244
245 return ((stop == 0) && (start == 0));
246 }
247
248
249
250
251
252
253
254
255
256 public void setStartTime(final Date start) throws SDPException {
257
258 if (start == null) {
259 throw new SDPException("The start time cannot be null");
260 }
261
262 setStartTime(getNTP(start));
263 }
264
265
266
267
268
269
270
271
272 public void setStartTime(final long start) throws SDPException {
273
274 if (start < 0) {
275 throw new SDPException("The session start time cannot be negative");
276 }
277
278 this.start = start;
279 }
280
281
282
283
284
285
286
287
288
289 public void setStopTime(final Date stop) throws SDPException {
290
291 if (stop == null) {
292 throw new SDPException("The stop time cannot be null");
293 }
294
295 setStopTime(getNTP(stop));
296 }
297
298
299
300
301
302
303
304
305 public void setStopTime(final long stop) throws SDPException {
306
307 if (stop < 0) {
308 throw new SDPException("The session stop time cannot be negative");
309 }
310
311 this.stop = stop;
312 }
313
314
315
316
317 public void setZero() {
318
319 this.start = 0;
320 this.stop = 0;
321 }
322
323
324
325
326
327
328
329 public String toString() {
330
331 StringBuffer result = new StringBuffer(getType() + "=");
332 result.append(start);
333 result.append(" ");
334 result.append(stop);
335
336 return result.toString();
337 }
338 }