1 | package schedframe.scheduling.plugin.local; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import org.joda.time.DateTime; |
---|
5 | |
---|
6 | /** |
---|
7 | * @author Ariel Oleksiak |
---|
8 | * |
---|
9 | */ |
---|
10 | public class TimeOperations { |
---|
11 | |
---|
12 | long timeDelta; |
---|
13 | |
---|
14 | public static final long TWENTY_FOUR_HOURS = 24*60*60*1000; //24h in millis |
---|
15 | |
---|
16 | /** |
---|
17 | * Constructor of DateOperations class |
---|
18 | * @param timeDelta - parameter used to express potential time overheads, e.g. |
---|
19 | * while checking if certain period fits into another |
---|
20 | */ |
---|
21 | public TimeOperations(long timeDelta) { |
---|
22 | this.timeDelta = timeDelta; |
---|
23 | } |
---|
24 | |
---|
25 | //Getters/setters |
---|
26 | |
---|
27 | public long getTimeDelta() { |
---|
28 | return timeDelta; |
---|
29 | } |
---|
30 | |
---|
31 | public void setTimeDelta(long timeDelta) { |
---|
32 | this.timeDelta = timeDelta; |
---|
33 | } |
---|
34 | |
---|
35 | //end of getters/setters |
---|
36 | |
---|
37 | |
---|
38 | //Comparisons |
---|
39 | |
---|
40 | /** |
---|
41 | * Compares two Calendar objects taking into account date only, i.e. |
---|
42 | * year, month, and day (without hour, minute, second, milis) |
---|
43 | * @param date1 |
---|
44 | * @param date2 |
---|
45 | * @return true if date1 is before date2; false otherwise |
---|
46 | */ |
---|
47 | public boolean beforeDate(DateTime date1, DateTime date2) { |
---|
48 | |
---|
49 | if (date1 == null || date2 == null) |
---|
50 | return false; |
---|
51 | |
---|
52 | if (date1.getYear() < date2.getYear()) |
---|
53 | return true; |
---|
54 | if (date1.getYear() > date2.getYear()) |
---|
55 | return false; |
---|
56 | |
---|
57 | if (date1.getMonthOfYear() < date2.getMonthOfYear()) |
---|
58 | return true; |
---|
59 | if (date1.getMonthOfYear() > date2.getMonthOfYear()) |
---|
60 | return false; |
---|
61 | |
---|
62 | if (date1.getDayOfMonth() < date2.getDayOfMonth()) |
---|
63 | return true; |
---|
64 | if (date1.getDayOfMonth() > date2.getDayOfMonth()) |
---|
65 | return false; |
---|
66 | |
---|
67 | return false; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * Compares two Calendar objects taking into account date only, i.e. |
---|
73 | * year, month, and day (without hour, minute, second, milis) |
---|
74 | * @param date1 |
---|
75 | * @param date2 |
---|
76 | * @return true if dates are equal; false otherwise |
---|
77 | */ |
---|
78 | public boolean sameDate(DateTime date1, DateTime date2) { |
---|
79 | |
---|
80 | if (date1 == null || date2 == null) |
---|
81 | return false; |
---|
82 | |
---|
83 | if (date1.getYear() != date2.getYear()) |
---|
84 | return false; |
---|
85 | |
---|
86 | if (date1.getMonthOfYear() != date2.getMonthOfYear()) |
---|
87 | return false; |
---|
88 | |
---|
89 | if (date1.getDayOfMonth() != date2.getDayOfMonth()) |
---|
90 | return false; |
---|
91 | |
---|
92 | return true; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Check whether one date is before another date. Only time is taken into account, i.e. |
---|
97 | * hour, minute, second, milis (without year, month, and day) |
---|
98 | * @param date1 |
---|
99 | * @param date2 |
---|
100 | * @return true if date1 is before date2 |
---|
101 | */ |
---|
102 | public boolean beforeDayTime(DateTime date1, DateTime date2) { |
---|
103 | |
---|
104 | if (date1 == null || date2 == null) |
---|
105 | return false; |
---|
106 | |
---|
107 | if(date1.getHourOfDay() < date2.getHourOfDay()) |
---|
108 | return true; |
---|
109 | if(date1.getHourOfDay() > date2.getHourOfDay()) |
---|
110 | return false; |
---|
111 | |
---|
112 | if(date1.getMinuteOfHour() < date2.getMinuteOfHour()) |
---|
113 | return true; |
---|
114 | if(date1.getMinuteOfHour() > date2.getMinuteOfHour()) |
---|
115 | return false; |
---|
116 | |
---|
117 | if(date1.getSecondOfMinute() < date2.getSecondOfMinute()) |
---|
118 | return true; |
---|
119 | if(date1.getSecondOfMinute() > date2.getSecondOfMinute()) |
---|
120 | return false; |
---|
121 | |
---|
122 | if(date1.getMillisOfSecond() < date2.getMillisOfSecond()) |
---|
123 | return true; |
---|
124 | if(date1.getMillisOfSecond() > date2.getMillisOfSecond()) |
---|
125 | return false; |
---|
126 | |
---|
127 | return false; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * Check whether a period, whose length is equal duration, is before specific date. Only time is taken into account, i.e. |
---|
132 | * hour, minute, second, milis (without year, month, and day) |
---|
133 | * @param date1 |
---|
134 | * @param duration - duration of a period to be checked |
---|
135 | * @param date2 |
---|
136 | * @return true if date1 + duration is before date2 |
---|
137 | */ |
---|
138 | public boolean beforeDayTime(DateTime date1, long duration, DateTime date2) { |
---|
139 | if (date1 == null || date2 == null) |
---|
140 | return false; |
---|
141 | |
---|
142 | date1 = date1.plus(duration + timeDelta); |
---|
143 | |
---|
144 | return beforeDate(date1, date2); |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * Check whether dates are equal. Only time is taken into account, i.e. |
---|
149 | * hour, minute, second, milis (without year, month, and day) |
---|
150 | * @param date1 |
---|
151 | * @param date2 |
---|
152 | * @return true if dates are equal |
---|
153 | */ |
---|
154 | public boolean sameDayTime(DateTime date1, DateTime date2) { |
---|
155 | |
---|
156 | if (date1 == null || date2 == null) |
---|
157 | return false; |
---|
158 | |
---|
159 | if (date1.getMillisOfDay() != date2.getMillisOfDay()) |
---|
160 | return false; |
---|
161 | |
---|
162 | return true; |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * Check whether a period, whose length is equal duration, is after specific date. Only time is taken into account, i.e. |
---|
167 | * hour, minute, second, milis (without year, month, and day) |
---|
168 | * @param date1 |
---|
169 | * @param date2 |
---|
170 | * @return true if date1 is after date2 |
---|
171 | */ |
---|
172 | public boolean afterDayTime(DateTime date1, DateTime date2) { |
---|
173 | return beforeDayTime(date2, date1); |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * Check whether a period, whose length is equal duration, is after specific date. Only time is taken into account, i.e. |
---|
178 | * hour, minute, second, milis (without year, month, and day) |
---|
179 | * @param date1 |
---|
180 | * @param duration - duration of a period to be checked |
---|
181 | * @param date2 |
---|
182 | * @return true if date1 + duration is after date2 |
---|
183 | */ |
---|
184 | public boolean afterDayTime(DateTime date1, long duration, DateTime date2) { |
---|
185 | |
---|
186 | if (date1 == null || date2 == null) |
---|
187 | return false; |
---|
188 | |
---|
189 | date1 = date1.plus(duration + timeDelta); |
---|
190 | |
---|
191 | return afterDayTime(date1, date2); |
---|
192 | } |
---|
193 | |
---|
194 | //end of comparisons |
---|
195 | |
---|
196 | //Operations on date and time |
---|
197 | |
---|
198 | /** |
---|
199 | * Copy date from newDate DateTime object into date DateTime object. |
---|
200 | * Time (hour, minutes, seconds, milis) is not copied. |
---|
201 | * @return DateTime object representing new date |
---|
202 | */ |
---|
203 | public DateTime setDate(DateTime date, DateTime newDate) { |
---|
204 | |
---|
205 | if (date == null || newDate == null) |
---|
206 | return date; |
---|
207 | |
---|
208 | return new DateTime(newDate.getYear(), |
---|
209 | newDate.getMonthOfYear(), |
---|
210 | newDate.getDayOfMonth(), |
---|
211 | date.getHourOfDay(), |
---|
212 | date.getMinuteOfHour(), |
---|
213 | date.getSecondOfMinute(), |
---|
214 | date.getMillisOfSecond()); |
---|
215 | |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | /** |
---|
220 | * Set time within a day from newTime Calendar object into date Calendar object. |
---|
221 | * Date (year, month, day) is not set. |
---|
222 | * @return Calendar object representing new time |
---|
223 | */ |
---|
224 | public DateTime setDayTime(DateTime date, DateTime newTime) { |
---|
225 | |
---|
226 | if (date == null || newTime == null) |
---|
227 | return date; |
---|
228 | |
---|
229 | return new DateTime(date.getYear(), |
---|
230 | date.getMonthOfYear(), |
---|
231 | date.getDayOfMonth(), |
---|
232 | newTime.getHourOfDay(), |
---|
233 | newTime.getMinuteOfHour(), |
---|
234 | newTime.getSecondOfMinute(), |
---|
235 | newTime.getMillisOfSecond()); |
---|
236 | |
---|
237 | } |
---|
238 | |
---|
239 | //end of operations on date and time |
---|
240 | |
---|
241 | |
---|
242 | //Operations on periods |
---|
243 | |
---|
244 | /** |
---|
245 | * Calculate period duration in milis |
---|
246 | * @param start |
---|
247 | * @param end |
---|
248 | * @return period duration in milis |
---|
249 | */ |
---|
250 | public long periodDuration(DateTime start, DateTime end) { |
---|
251 | |
---|
252 | if (start == null || end == null) |
---|
253 | return 0; |
---|
254 | |
---|
255 | return end.getMillis() - start.getMillis(); |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | * Calculate period duration in milis taking into account time only, i.e. |
---|
260 | * hour, minute, second, milis (without year, month, and day) |
---|
261 | * @param start |
---|
262 | * @param end |
---|
263 | * @return period duration in milis |
---|
264 | */ |
---|
265 | public long periodDayTimeDuration(DateTime start, DateTime end) { |
---|
266 | |
---|
267 | if (start == null && end == null) |
---|
268 | return TWENTY_FOUR_HOURS; |
---|
269 | |
---|
270 | if (start == null) { |
---|
271 | start = new DateTime(end.getYear(), |
---|
272 | end.getMonthOfYear(), |
---|
273 | end.getDayOfMonth(), |
---|
274 | 0, 0, 0, 0); |
---|
275 | } |
---|
276 | |
---|
277 | if (end == null) { |
---|
278 | end = new DateTime(start.getYear(), |
---|
279 | start.getMonthOfYear(), |
---|
280 | start.getDayOfMonth(), |
---|
281 | 23, 59, 59, 999); |
---|
282 | } |
---|
283 | |
---|
284 | //setting the same date |
---|
285 | DateTime newEnd = new DateTime(start.getYear(), |
---|
286 | start.getMonthOfYear(), |
---|
287 | start.getDayOfMonth(), |
---|
288 | end.getHourOfDay(), |
---|
289 | end.getMinuteOfHour(), |
---|
290 | end.getSecondOfMinute(), |
---|
291 | end.getMillisOfSecond() |
---|
292 | ); |
---|
293 | |
---|
294 | if (beforeDayTime(start, newEnd)) |
---|
295 | return newEnd.getMillis() - start.getMillis(); |
---|
296 | else |
---|
297 | return TWENTY_FOUR_HOURS - (start.getMillis() - newEnd.getMillis()); |
---|
298 | } |
---|
299 | |
---|
300 | /** |
---|
301 | * Check whether date is before given period. Only time is taken into account, i.e. |
---|
302 | * hour, minute, second, milis (without year, month, and day) |
---|
303 | * @param date - date to be checked |
---|
304 | * @param start - begin of period |
---|
305 | * @param end - end of period |
---|
306 | * @return true if date is before a period; false otherwise |
---|
307 | */ |
---|
308 | public boolean beforePeriodDayTime(DateTime date, DateTime start, DateTime end) { |
---|
309 | |
---|
310 | if (date == null) |
---|
311 | return false; |
---|
312 | |
---|
313 | if (start == null) |
---|
314 | return false; |
---|
315 | |
---|
316 | if (end == null) |
---|
317 | return beforeDayTime(date, start); |
---|
318 | |
---|
319 | //date is not before start |
---|
320 | if(!beforeDayTime(date, start)) |
---|
321 | return false; |
---|
322 | |
---|
323 | //end of period is next day |
---|
324 | if(afterDayTime(start, end)) |
---|
325 | if(!afterDayTime(date, end)) |
---|
326 | return false; |
---|
327 | |
---|
328 | return true; |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | * Check whether a period, whose length is given by duration, is within a period |
---|
333 | * defined by start and end. |
---|
334 | * @param date - date to be checked |
---|
335 | * @param duration - duration of a period to be checked |
---|
336 | * @param start - begin of period |
---|
337 | * @param end - end of period |
---|
338 | * @return true if a period is within the period <start, end>; false otherwise |
---|
339 | */ |
---|
340 | public boolean withinPeriod(DateTime date, long duration, DateTime start, DateTime end) { |
---|
341 | |
---|
342 | if (date == null) |
---|
343 | return false; |
---|
344 | |
---|
345 | if (start == null || end == null) |
---|
346 | return true; |
---|
347 | |
---|
348 | if (date.isBefore(start) || (date.getMillis() + duration + timeDelta > end.getMillis())) |
---|
349 | return false; |
---|
350 | |
---|
351 | return true; |
---|
352 | } |
---|
353 | |
---|
354 | /** |
---|
355 | * Check whether a period, whose length is given by duration, is within a period |
---|
356 | * defined by start and end. |
---|
357 | * @param duration - duration of a period to be checked |
---|
358 | * @param start - begin of period |
---|
359 | * @param end - end of period |
---|
360 | * @return true if a period is within the period <start, end>; false otherwise |
---|
361 | */ |
---|
362 | public boolean withinPeriod(long duration, DateTime start, DateTime end) { |
---|
363 | |
---|
364 | if (start == null || end == null) |
---|
365 | return true; |
---|
366 | |
---|
367 | if (start.getMillis() + duration + timeDelta > end.getMillis()) |
---|
368 | return false; |
---|
369 | |
---|
370 | return true; |
---|
371 | } |
---|
372 | |
---|
373 | /** |
---|
374 | * Check whether date is within a period |
---|
375 | * defined by start and end. Only time is taken into account, i.e. |
---|
376 | * hour, minute, second, milis (without year, month, and day) |
---|
377 | * @param date - date to be checked |
---|
378 | * @param start - begin of period |
---|
379 | * @param end - end of period |
---|
380 | * @return true if date is within the period <start, end>; false otherwise |
---|
381 | */ |
---|
382 | public boolean withinPeriodDayTime(DateTime date, DateTime start, DateTime end) { |
---|
383 | |
---|
384 | if (date == null) |
---|
385 | return false; |
---|
386 | |
---|
387 | if (start == null && end == null) |
---|
388 | return false; |
---|
389 | |
---|
390 | if (start == null) |
---|
391 | return !afterDayTime(date, end); |
---|
392 | |
---|
393 | if (end == null) |
---|
394 | return !beforeDayTime(date, start); |
---|
395 | |
---|
396 | if (afterDayTime(start, end)){ |
---|
397 | if (beforeDayTime(date, start) && afterDayTime(date, end)) |
---|
398 | return false; |
---|
399 | } |
---|
400 | else { |
---|
401 | if (beforeDayTime(date, start) || afterDayTime(date, end)) |
---|
402 | return false; |
---|
403 | } |
---|
404 | |
---|
405 | return true; |
---|
406 | } |
---|
407 | |
---|
408 | /** |
---|
409 | * Check whether a period, whose length is given by duration, is within a period |
---|
410 | * defined by start and end. Only time is taken into account, i.e. |
---|
411 | * hour, minute, second, milis (without year, month, and day) |
---|
412 | * @param date - date to be checked |
---|
413 | * @param duration - duration of a period to be checked |
---|
414 | * @param start - begin of period |
---|
415 | * @param end - end of period |
---|
416 | * @return true if a period is within the period <start, end>; false otherwise |
---|
417 | */ |
---|
418 | public boolean withinPeriodDayTime(DateTime date, long duration, DateTime start, DateTime end) { |
---|
419 | |
---|
420 | if (date == null) |
---|
421 | return false; |
---|
422 | |
---|
423 | if (start == null && end == null) |
---|
424 | return true; |
---|
425 | |
---|
426 | if (start == null) |
---|
427 | return !afterDayTime(date, duration, end); |
---|
428 | |
---|
429 | if (end == null) |
---|
430 | return !beforeDayTime(date, duration, start); |
---|
431 | |
---|
432 | if (afterDayTime(start, end)){ |
---|
433 | if (beforeDate(date, start) && afterDayTime(date, duration, end)) |
---|
434 | return false; |
---|
435 | } |
---|
436 | else { |
---|
437 | if (beforeDate(date, start) || afterDayTime(date, duration, end)) |
---|
438 | return false; |
---|
439 | } |
---|
440 | |
---|
441 | return true; |
---|
442 | } |
---|
443 | |
---|
444 | /** |
---|
445 | * Check whether a period, whose length is given by duration, overlaps a period |
---|
446 | * defined by start and end. Only time is taken into account, i.e. |
---|
447 | * hour, minute, second, milis (without year, month, and day) |
---|
448 | * @param date - date to be checked |
---|
449 | * @param duration - duration of a period to be checked |
---|
450 | * @param start - begin of period |
---|
451 | * @param end - end of period |
---|
452 | * @return true if a period overlaps with the period <start, end>; false otherwise |
---|
453 | */ |
---|
454 | public boolean overlapPeriodDayTime(DateTime date, long duration, DateTime start, DateTime end) { |
---|
455 | |
---|
456 | if (date == null) |
---|
457 | return false; |
---|
458 | |
---|
459 | if (start == null && end == null) |
---|
460 | return true; |
---|
461 | |
---|
462 | if (start == null) |
---|
463 | return beforeDayTime(date, end); |
---|
464 | |
---|
465 | if (end == null) |
---|
466 | return !beforeDayTime(date, duration, start); |
---|
467 | |
---|
468 | if (afterDayTime(start, end)){ |
---|
469 | if (!beforeDate(date, end) && !afterDayTime(date, duration, start)) |
---|
470 | return false; |
---|
471 | } |
---|
472 | else { |
---|
473 | if (!beforeDate(date, end) || !afterDayTime(date, duration, start)) |
---|
474 | return false; |
---|
475 | } |
---|
476 | |
---|
477 | return true; |
---|
478 | } |
---|
479 | |
---|
480 | /** |
---|
481 | * Check whether date is after given period. Only time is taken into account, i.e. |
---|
482 | * hour, minute, second, milis (without year, month, and day) |
---|
483 | * @param date - date to be checked |
---|
484 | * @param start - begin of period |
---|
485 | * @param end - end of period |
---|
486 | * @return true if date is after a period; false otherwise |
---|
487 | */ |
---|
488 | public boolean afterPeriodDayTime(DateTime date, DateTime start, DateTime end) { |
---|
489 | |
---|
490 | if (date == null) |
---|
491 | return false; |
---|
492 | |
---|
493 | if (end == null) |
---|
494 | return false; |
---|
495 | |
---|
496 | if (start == null) |
---|
497 | return afterDayTime(date, end); |
---|
498 | |
---|
499 | //date is not after end |
---|
500 | if (!afterDayTime(date, end)) |
---|
501 | return false; |
---|
502 | |
---|
503 | //end of period is next day |
---|
504 | if (afterDayTime(start, end)) |
---|
505 | if (!beforeDate(date, start)) |
---|
506 | return false; |
---|
507 | |
---|
508 | return true; |
---|
509 | } |
---|
510 | |
---|
511 | //end of operations on periods |
---|
512 | |
---|
513 | |
---|
514 | //Operations on lists of dates |
---|
515 | |
---|
516 | /** |
---|
517 | * Find date in list of dates |
---|
518 | * @param date - date to be found |
---|
519 | * @param days - Vector containing Calendar objects |
---|
520 | * @return true if data is found; false otherwise |
---|
521 | */ |
---|
522 | public boolean findDate(DateTime date, ArrayList<DateTime> days) { |
---|
523 | |
---|
524 | if (days == null) |
---|
525 | return false; |
---|
526 | |
---|
527 | for (int i=0; i < days.size(); i++) { |
---|
528 | DateTime vDate = days.get(i); |
---|
529 | |
---|
530 | if (!sameDate(date, vDate)) |
---|
531 | continue; |
---|
532 | |
---|
533 | return true; |
---|
534 | } |
---|
535 | |
---|
536 | return false; |
---|
537 | } |
---|
538 | |
---|
539 | /** |
---|
540 | * Find the latest date in inclDates that is not found in exclDates |
---|
541 | * @param inclDates |
---|
542 | * @param exclDates |
---|
543 | * @return the latest date |
---|
544 | */ |
---|
545 | public DateTime findLatestDate(ArrayList <DateTime>inclDates, ArrayList<DateTime> exclDates) { |
---|
546 | |
---|
547 | if (inclDates == null) |
---|
548 | return null; |
---|
549 | |
---|
550 | DateTime latest = new DateTime(); //Calendar.getInstance(); |
---|
551 | |
---|
552 | for (int i=0; i < inclDates.size(); i++) |
---|
553 | if (beforeDate(latest, inclDates.get(i))) |
---|
554 | if (!findDate(inclDates.get(i), exclDates)) |
---|
555 | latest = inclDates.get(i); |
---|
556 | |
---|
557 | return latest; |
---|
558 | } |
---|
559 | |
---|
560 | //end of operations on lists of dates |
---|
561 | |
---|
562 | /** |
---|
563 | * Get DateTime object representing current time |
---|
564 | * This method can be overloaded to return logic time, e.g. in simulations |
---|
565 | * @return current time |
---|
566 | */ |
---|
567 | public DateTime getTime() { |
---|
568 | return new DateTime(); |
---|
569 | } |
---|
570 | |
---|
571 | } |
---|
572 | |
---|