Skip to content
Open
Show file tree
Hide file tree
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 @@ -1699,21 +1699,45 @@ public static NodeValue durGetSign(NodeValue nv) {
return NodeValue.makeInteger(x);
}

// F&O 3.1 section 8.2: the component of the canonical form of the duration, with the sign of the duration.
// Year/month and day/time parts are normalised separately.
private static NodeValue accessDuration(NodeValue nv, Field field) {
Duration dur = nv.getDuration();
// if ( ! nv.isDuration() )
// throw new ExprEvalException("Not a duration: "+nv);
Number x = dur.getField(field);
if ( x == null ) {
x = field.equals(DatatypeConstants.SECONDS)
? BigDecimal.ZERO
: BigInteger.ZERO;
boolean negative = dur.getSign() < 0;

if ( field.equals(YEARS) || field.equals(MONTHS) ) {
BigInteger totalMonths = durFieldInteger(dur, YEARS).multiply(BigInteger.valueOf(12)).add(durFieldInteger(dur, MONTHS));
BigInteger[] yearsMonths = totalMonths.divideAndRemainder(BigInteger.valueOf(12));
BigInteger x = field.equals(YEARS) ? yearsMonths[0] : yearsMonths[1];
return NodeValue.makeInteger(negative ? x.negate() : x);
}

if ( field.equals(DatatypeConstants.SECONDS) )
return NodeValue.makeDecimal((BigDecimal)x);
BigDecimal seconds = (BigDecimal)dur.getField(SECONDS);
if ( seconds == null )
seconds = BigDecimal.ZERO;
BigInteger totalMinutes = durFieldInteger(dur, DAYS).multiply(BigInteger.valueOf(24 * 60))
.add(durFieldInteger(dur, HOURS).multiply(BigInteger.valueOf(60)))
.add(durFieldInteger(dur, MINUTES));
BigDecimal sixty = BigDecimal.valueOf(60);
totalMinutes = totalMinutes.add(seconds.divideToIntegralValue(sixty).toBigInteger());
seconds = seconds.remainder(sixty);

if ( field.equals(SECONDS) )
return NodeValue.makeDecimal(negative ? seconds.negate() : seconds);

BigInteger x;
if ( field.equals(MINUTES) )
x = totalMinutes.mod(BigInteger.valueOf(60));
else if ( field.equals(HOURS) )
x = totalMinutes.divide(BigInteger.valueOf(60)).mod(BigInteger.valueOf(24));
else
x = totalMinutes.divide(BigInteger.valueOf(24 * 60));
return NodeValue.makeInteger(negative ? x.negate() : x);
}

return NodeValue.makeInteger((BigInteger)x);
private static BigInteger durFieldInteger(Duration dur, Field field) {
Number x = dur.getField(field);
return x == null ? BigInteger.ZERO : (BigInteger)x;
}

/** One day */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ public void datetime_7() {
test("fn:seconds-from-duration("+s+")", "6.7");
}

// GH-4437
@Test public void fromDurationNegative() {
test("fn:days-from-duration('-P3DT10H'^^xsd:dayTimeDuration)", "-3");
test("fn:hours-from-duration('-P3DT10H'^^xsd:dayTimeDuration)", "-10");
test("fn:minutes-from-duration('-P5DT12H30M'^^xsd:dayTimeDuration)", "-30");
test("fn:seconds-from-duration('-PT256S'^^xsd:dayTimeDuration)", "-16.0");
test("fn:years-from-duration('-P15M'^^xsd:yearMonthDuration)", "-1");
test("fn:months-from-duration('-P2Y15M'^^xsd:yearMonthDuration)", "-3");
test("fn:days-from-duration('2026-01-01T00:00:00Z'^^xsd:dateTime - '2026-01-04T00:00:00Z'^^xsd:dateTime)", "-3");
}

// GH-4437
@Test public void fromDurationNormalized() {
test("fn:days-from-duration('P3DT55H'^^xsd:dayTimeDuration)", "5");
test("fn:hours-from-duration('P3DT55H'^^xsd:dayTimeDuration)", "7");
test("fn:hours-from-duration('PT123H'^^xsd:dayTimeDuration)", "3");
test("fn:minutes-from-duration('PT90M'^^xsd:dayTimeDuration)", "30");
test("fn:seconds-from-duration('PT150.5S'^^xsd:dayTimeDuration)", "30.5");
test("fn:minutes-from-duration('PT150.5S'^^xsd:dayTimeDuration)", "2");
test("fn:years-from-duration('P20M'^^xsd:yearMonthDuration)", "1");
test("fn:months-from-duration('P20M'^^xsd:yearMonthDuration)", "8");
test("fn:seconds-from-duration('-P3D'^^xsd:dayTimeDuration)", "0.0");
}

@Test public void fromMisc() {
test("fn:hours-from-time('17:01:02'^^xsd:time)", "17");
test("fn:minutes-from-time('17:01:02'^^xsd:time)", "01");
Expand Down