return incrementTime(dt, Calendar.MILLISECOND, "milliseconds", n.negate());
}
public IValue createDurationInternal(IDateTime dStart, IDateTime dEnd) {
// dStart and dEnd both have to be dates, times, or datetimes
Calendar startCal = Calendar.getInstance();
startCal.setTimeInMillis(dStart.getInstant());
Calendar endCal = Calendar.getInstance();
endCal.setTimeInMillis(dEnd.getInstant());
IValue duration = null;
if (dStart.isDate()) {
if (dEnd.isDate()) {
duration = values.tuple(
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.YEAR)),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.MONTH)),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.DAY_OF_MONTH)),
values.integer(0), values.integer(0), values.integer(0),
values.integer(0));
} else if (dEnd.isTime()) {
throw RuntimeExceptionFactory.invalidUseOfTimeException("Cannot determine the duration between a date with no time and a time with no date.", null, null);
} else {
throw RuntimeExceptionFactory.invalidUseOfDateTimeException("Cannot determine the duration between a date with no time and a datetime.", null, null);
}
} else if (dStart.isTime()) {
if (dEnd.isTime()) {
duration = values.tuple(
values.integer(0),
values.integer(0),
values.integer(0),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.HOUR_OF_DAY)),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.MINUTE)),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.SECOND)),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.MILLISECOND)));
} else if (dEnd.isDate()) {
throw RuntimeExceptionFactory.invalidUseOfDateException("Cannot determine the duration between a time with no date and a date with no time.", null, null);
} else {
throw RuntimeExceptionFactory.invalidUseOfDateTimeException("Cannot determine the duration between a time with no date and a datetime.", null, null);
}
} else {
if (dEnd.isDateTime()) {
duration = values.tuple(
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.YEAR)),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.MONTH)),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.DAY_OF_MONTH)),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.HOUR_OF_DAY)),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.MINUTE)),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.SECOND)),
values.integer(startCal.fieldDifference(endCal.getTime(), Calendar.MILLISECOND)));
} else if (dEnd.isDate()) {
throw RuntimeExceptionFactory.invalidUseOfDateException("Cannot determine the duration between a datetime and a date with no time.", null, null);
} else {
throw RuntimeExceptionFactory.invalidUseOfTimeException("Cannot determine the duration between a datetime and a time with no date.", null, null);
}