Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] made date calculation efficient #5593

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@
import org.exist.xquery.XPathException;

import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.GregorianCalendar;

/**
* @author <a href="mailto:[email protected]">Piotr Kaminski</a>
Expand Down Expand Up @@ -155,12 +159,28 @@ public ComputableValue plus(ComputableValue other) throws XPathException {
case Type.DATE:
final AbstractDateTimeValue date = (AbstractDateTimeValue) other;
final XMLGregorianCalendar gc = (XMLGregorianCalendar) date.calendar.clone();
gc.add(duration);
//Shift one year
if (gc.getYear() < 0) {
gc.setYear(gc.getYear() - 1);
LocalDate localDate = gc.toGregorianCalendar().toZonedDateTime().toLocalDate();
LocalDate updatedDate = null;

long days = duration.getField(DatatypeConstants.DAYS).longValue();
if (days > Integer.MAX_VALUE) {
while (days > Integer.MAX_VALUE) {
updatedDate = localDate.plusDays(Integer.MAX_VALUE);
days -= Integer.MAX_VALUE;
}
} else {
updatedDate = localDate.plusDays(duration.getDays());
}

GregorianCalendar gregorianCalendar = GregorianCalendar.from(updatedDate.atStartOfDay(ZoneId.systemDefault()));
XMLGregorianCalendar updatedGc = null;
try {
updatedGc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
} catch (final Exception E) {
return null;
}
return date.createSameKind(gc);

return date.createSameKind(updatedGc);
default:
throw new XPathException(getExpression(), ErrorCodes.XPTY0004, "cannot add " +
Type.getTypeName(other.getType()) + "('" + other.getStringValue() + "') from " +
Expand Down
Loading