1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.opencastproject.metadata.mpeg7;
24
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.w3c.dom.Node;
28
29 import java.util.Calendar;
30 import java.util.Formatter;
31 import java.util.TimeZone;
32
33
34
35
36 public class MediaTimePointImpl implements MediaTimePoint {
37
38
39 private MediaTimePoint referenceTimePoint = null;
40
41
42 private static final String TimeSpecDelimiter = "T";
43
44
45 private static final String FractionSpecDelimiter = "F";
46
47
48 private static final String TimeSeparator = ":";
49
50
51 private static final String DateSeparator = "-";
52
53
54 protected int year = 0;
55
56
57 protected int month = 0;
58
59
60 protected int day = 0;
61
62
63 protected int hour = 0;
64
65
66 protected int minute = 0;
67
68
69 protected int second = 0;
70
71
72 protected int fractions = 0;
73
74
75 protected int fractionsPerSecond = 0;
76
77
78 protected String timeZone = "+00:00";
79
80
81 private static final long MS_PER_DAY = 86400000L;
82
83
84 private static final long MS_PER_HOUR = 3600000L;
85
86
87 private static final long MS_PER_MINUTE = 60000L;
88
89
90 private static final long MS_PER_SECOND = 1000L;
91
92
93
94
95 public MediaTimePointImpl() {
96 year = 0;
97 month = 0;
98 day = 0;
99 hour = 0;
100 minute = 0;
101 second = 0;
102 fractions = 0;
103 fractionsPerSecond = 0;
104 }
105
106
107
108
109
110
111
112 public MediaTimePointImpl(long milliseconds) {
113 Calendar calendar = Calendar.getInstance();
114 calendar.setTimeInMillis(milliseconds);
115 calendar.setTimeZone(TimeZone.getTimeZone("UCT"));
116 second = calendar.get(Calendar.SECOND);
117 minute = calendar.get(Calendar.MINUTE);
118 hour = calendar.get(Calendar.HOUR_OF_DAY);
119 day = calendar.get(Calendar.DAY_OF_MONTH) - 1;
120 month = calendar.get(Calendar.MONTH);
121 year = calendar.get(Calendar.YEAR);
122 fractions = Math.round(milliseconds % 1000);
123 fractionsPerSecond = 1000;
124 }
125
126
127
128
129 public int getDay() {
130 return day;
131 }
132
133
134
135
136 public int getHour() {
137 return hour;
138 }
139
140
141
142
143 public int getMinutes() {
144 return minute;
145 }
146
147
148
149
150 public int getMonth() {
151 return month;
152 }
153
154
155
156
157 public int getNFractions() {
158 return fractions;
159 }
160
161
162
163
164 public int getSeconds() {
165 return second;
166 }
167
168
169
170
171 public int getYear() {
172 return year;
173 }
174
175
176
177
178 public int getFractionsPerSecond() {
179 return fractionsPerSecond;
180 }
181
182
183
184
185 public long getTimeInMilliseconds() {
186 long milliseconds = second * MS_PER_SECOND;
187 milliseconds += minute * MS_PER_MINUTE;
188 milliseconds += hour * MS_PER_HOUR;
189 milliseconds += day * MS_PER_DAY;
190 if (fractionsPerSecond > 0)
191 milliseconds += (fractions * 1000L / fractionsPerSecond);
192 return milliseconds;
193 }
194
195
196
197
198 public boolean isRelative() {
199 return referenceTimePoint != null;
200 }
201
202
203
204
205
206
207
208 public void setReferenceTimePoint(MediaTimePoint timePoint) {
209 this.referenceTimePoint = timePoint;
210 }
211
212 public void setFractionsPerSecond(int fractionsPerSecond) {
213 this.fractionsPerSecond = fractionsPerSecond;
214 }
215
216 public String getTimeZone() {
217 return timeZone;
218 }
219
220 public void setTimeZone(String timeZone) {
221 this.timeZone = timeZone;
222 }
223
224 public void setDay(int day) {
225 this.day = day;
226 }
227
228 public void setHour(int hour) {
229 this.hour = hour;
230 }
231
232 public void setMinutes(int minutes) {
233 this.minute = minutes;
234 }
235
236 public void setMonth(int month) {
237 this.month = month;
238 }
239
240 public void setFractions(int fractions) {
241 this.fractions = fractions;
242 }
243
244 public void setSeconds(int seconds) {
245 this.second = seconds;
246 }
247
248 public void setYear(int year) {
249 this.year = year;
250 }
251
252
253
254
255
256
257
258
259
260
261 public static MediaTimePointImpl parseTimePoint(String text) throws IllegalArgumentException {
262 MediaTimePointImpl timePoint = new MediaTimePointImpl();
263 timePoint.parse(text);
264 return timePoint;
265 }
266
267
268
269
270 private void parse(String text) throws IllegalArgumentException {
271 String date = null;
272 String time = null;
273 String fractions = null;
274 date = text.substring(0, text.indexOf(TimeSpecDelimiter));
275 time = text.substring(text.indexOf(TimeSpecDelimiter) + 1, text.indexOf(TimeSpecDelimiter) + 9);
276 fractions = text.substring(text.indexOf(TimeSpecDelimiter) + time.length() + 2);
277 if (fractions.contains(TimeSeparator)) {
278 timeZone = fractions.substring(fractions.length() - 6);
279 fractions = fractions.substring(0, fractions.length() - 7);
280 }
281 parseDate(date);
282 parseTime(time);
283 parseFractions(fractions);
284 }
285
286
287
288
289
290
291
292 protected void parseDate(String date) {
293 int firstDateSeparator = date.indexOf(DateSeparator);
294 int lastDateSeparator = date.lastIndexOf(DateSeparator);
295 if (firstDateSeparator > -1) {
296 year = Integer.parseInt(date.substring(0, firstDateSeparator));
297 month = Short.parseShort(date.substring(firstDateSeparator + 1, lastDateSeparator));
298 day = Short.parseShort(date.substring(lastDateSeparator + 1));
299 } else {
300 year = 0;
301 month = 0;
302 day = 0;
303 }
304 }
305
306
307
308
309
310
311
312 protected void parseTime(String time) {
313 int firstTimeSeparator = time.indexOf(TimeSeparator);
314 int lastTimeSeparator = time.lastIndexOf(TimeSeparator);
315 if (firstTimeSeparator > -1) {
316 hour = Short.parseShort(time.substring(0, firstTimeSeparator));
317 minute = Short.parseShort(time.substring(firstTimeSeparator + 1, lastTimeSeparator));
318 second = Short.parseShort(time.substring(lastTimeSeparator + 1));
319 }
320 }
321
322
323
324
325
326
327
328 private void parseFractions(String fractions) {
329 this.fractions = Integer.parseInt(fractions.substring(0, fractions.indexOf(FractionSpecDelimiter)));
330 this.fractionsPerSecond = Integer.parseInt(fractions.substring(fractions.indexOf(FractionSpecDelimiter) + 1));
331 }
332
333
334
335
336 @Override
337 public String toString() {
338 Formatter f = new Formatter();
339 String result = null;
340 if (referenceTimePoint == null && year != 0) {
341 result = f.format("%04d-%02d-%02dT%02d:%02d:%02d:%dF%d", year, month, day, hour, minute, second, fractions,
342 fractionsPerSecond).toString();
343 } else {
344 result = f.format("T%02d:%02d:%02d:%dF%d", hour, minute, second, fractions, fractionsPerSecond).toString();
345 }
346 f.close();
347 return result;
348 }
349
350
351
352
353 public Node toXml(Document document) {
354 Element node = null;
355 if (referenceTimePoint != null)
356 node = document.createElement("MediaRelTimePoint");
357 else
358 node = document.createElement("MediaTimePoint");
359 node.setTextContent(toString());
360 return node;
361 }
362
363 }