Skip to content

Commit 8ad25ca

Browse files
committed
[bugfix] made date calculation efficient
1 parent fb930ca commit 8ad25ca

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

exist-core/src/main/java/org/exist/xquery/value/OrderedDurationValue.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@
2929
import org.exist.xquery.XPathException;
3030

3131
import javax.xml.datatype.DatatypeConstants;
32+
import javax.xml.datatype.DatatypeFactory;
3233
import javax.xml.datatype.Duration;
3334
import javax.xml.datatype.XMLGregorianCalendar;
3435
import java.math.BigDecimal;
36+
import java.time.LocalDate;
37+
import java.time.ZoneId;
38+
import java.util.GregorianCalendar;
3539

3640
/**
3741
* @author <a href="mailto:[email protected]">Piotr Kaminski</a>
@@ -155,12 +159,28 @@ public ComputableValue plus(ComputableValue other) throws XPathException {
155159
case Type.DATE:
156160
final AbstractDateTimeValue date = (AbstractDateTimeValue) other;
157161
final XMLGregorianCalendar gc = (XMLGregorianCalendar) date.calendar.clone();
158-
gc.add(duration);
159-
//Shift one year
160-
if (gc.getYear() < 0) {
161-
gc.setYear(gc.getYear() - 1);
162+
LocalDate localDate = gc.toGregorianCalendar().toZonedDateTime().toLocalDate();
163+
LocalDate updatedDate = null;
164+
165+
long days = duration.getField(DatatypeConstants.DAYS).longValue();
166+
if (days > Integer.MAX_VALUE) {
167+
while (days > Integer.MAX_VALUE) {
168+
updatedDate = localDate.plusDays(Integer.MAX_VALUE);
169+
days -= Integer.MAX_VALUE;
170+
}
171+
} else {
172+
updatedDate = localDate.plusDays(duration.getDays());
173+
}
174+
175+
GregorianCalendar gregorianCalendar = GregorianCalendar.from(updatedDate.atStartOfDay(ZoneId.systemDefault()));
176+
XMLGregorianCalendar updatedGc = null;
177+
try {
178+
updatedGc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
179+
} catch (final Exception E) {
180+
return null;
162181
}
163-
return date.createSameKind(gc);
182+
183+
return date.createSameKind(updatedGc);
164184
default:
165185
throw new XPathException(getExpression(), ErrorCodes.XPTY0004, "cannot add " +
166186
Type.getTypeName(other.getType()) + "('" + other.getStringValue() + "') from " +

0 commit comments

Comments
 (0)