|
29 | 29 | import org.exist.xquery.XPathException;
|
30 | 30 |
|
31 | 31 | import javax.xml.datatype.DatatypeConstants;
|
| 32 | +import javax.xml.datatype.DatatypeFactory; |
32 | 33 | import javax.xml.datatype.Duration;
|
33 | 34 | import javax.xml.datatype.XMLGregorianCalendar;
|
34 | 35 | import java.math.BigDecimal;
|
| 36 | +import java.time.LocalDate; |
| 37 | +import java.time.ZoneId; |
| 38 | +import java.util.GregorianCalendar; |
35 | 39 |
|
36 | 40 | /**
|
37 | 41 | * @author <a href="mailto:[email protected]">Piotr Kaminski</a>
|
@@ -155,12 +159,28 @@ public ComputableValue plus(ComputableValue other) throws XPathException {
|
155 | 159 | case Type.DATE:
|
156 | 160 | final AbstractDateTimeValue date = (AbstractDateTimeValue) other;
|
157 | 161 | 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(javax.xml.datatype.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; |
162 | 181 | }
|
163 |
| - return date.createSameKind(gc); |
| 182 | + |
| 183 | + return date.createSameKind(updatedGc); |
164 | 184 | default:
|
165 | 185 | throw new XPathException(getExpression(), ErrorCodes.XPTY0004, "cannot add " +
|
166 | 186 | Type.getTypeName(other.getType()) + "('" + other.getStringValue() + "') from " +
|
|
0 commit comments