* @param slaWorkingHours
* @return
*/
public Duration calculateElapsedTime(DateTime lastCalculated, DateTime now,
int slaWorkingHours) {
DateMidnight dateBeingProcessed = new DateMidnight(lastCalculated);
Interval processingInterval = getEntireDayIntervalForDate(dateBeingProcessed);
Duration duration = new Duration(null);
while (processingInterval.contains(now)
|| processingInterval.isBefore(now)) {
Interval workingHours = getWorkingIntervalForDate(
dateBeingProcessed, slaWorkingHours);
// process the day
// if the last calculated date is within the current 24hours being
// processed.
if (processingInterval.contains(lastCalculated)) {
// set start to last calc'd or start or working period if after
// last calced
if (workingHours.getStart().isAfter(lastCalculated)) {
// leave start of working hours as is
} else if (workingHours.contains(lastCalculated)) {
workingHours = workingHours.withStart(lastCalculated);
} else if (workingHours.getEnd().isBefore(lastCalculated)) {
workingHours = new Interval(lastCalculated, lastCalculated); // zero
// time
// duration
}
}
if (processingInterval.contains(now)) {
// set end to now if now in working period
if (workingHours.getStart().isAfter(now)) {
workingHours = new Interval(now, now); // zero time duration
} else if (workingHours.contains(now)) {
workingHours = workingHours.withEnd(now);
} else if (workingHours.getEnd().isBefore(now)) {
// leave end of working hours as is
}
}
duration = duration.plus(workingHours.toDuration());
dateBeingProcessed = dateBeingProcessed.plusDays(1);
processingInterval = getEntireDayIntervalForDate(dateBeingProcessed);
}
System.out.println(duration.toPeriod());
return duration;
}