This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 2434a3e5 fold 400 year cycles into the xs:duration value hash (#157)
2434a3e5 is described below

commit 2434a3e54decc107439c5ecbd0b25ff0a26a7ce6
Author: PJ Fanning <[email protected]>
AuthorDate: Tue Nov 3 11:19:49 2026 +0100

    fold 400 year cycles into the xs:duration value hash (#157)
    
    Every 400 Gregorian years is exactly 146097 days from any start date, so
    compareToGDuration finds P400Y equal to P146097D through the reference
    date check, but the month totals differ and so did the hash codes.
    Whole 4800 month blocks are the only month offsets that span the same
    number of days from all four reference dates; fold them into seconds
    before hashing. Strip trailing zeros after the fold, since
    BigDecimal.hashCode depends on scale.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../xmlbeans/impl/values/JavaGDurationHolderEx.java   |  8 ++++++--
 .../DurationIdentityConstraintValidateTest.java       | 19 +++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/apache/xmlbeans/impl/values/JavaGDurationHolderEx.java 
b/src/main/java/org/apache/xmlbeans/impl/values/JavaGDurationHolderEx.java
index 91e0b275..b8aa3f41 100644
--- a/src/main/java/org/apache/xmlbeans/impl/values/JavaGDurationHolderEx.java
+++ b/src/main/java/org/apache/xmlbeans/impl/values/JavaGDurationHolderEx.java
@@ -163,12 +163,16 @@ public abstract class JavaGDurationHolderEx extends 
XmlObjectBase {
         long months = (long) _value.getYear() * 12 + _value.getMonth();
         BigDecimal seconds = BigDecimal.valueOf(
             (((long) _value.getDay() * 24 + _value.getHour()) * 60 + 
_value.getMinute()) * 60 + _value.getSecond())
-            .add(_value.getFraction()).stripTrailingZeros();
+            .add(_value.getFraction());
         if (_value.getSign() < 0) {
             months = -months;
             seconds = seconds.negate();
         }
-        return Long.hashCode(months) * 31 + seconds.hashCode();
+        // every 400 years is exactly 146097 days from any start date, so 
equal_to
+        // finds P400Y equal to P146097D; fold whole 400 year blocks into 
seconds
+        seconds = seconds.add(BigDecimal.valueOf(Math.floorDiv(months, 4800L) 
* 146097L * 86400L));
+        months = Math.floorMod(months, 4800L);
+        return Long.hashCode(months) * 31 + 
seconds.stripTrailingZeros().hashCode();
     }
 
     protected void validate_simpleval(String lexical, ValidationContext ctx) {
diff --git 
a/src/test/java/misc/checkin/DurationIdentityConstraintValidateTest.java 
b/src/test/java/misc/checkin/DurationIdentityConstraintValidateTest.java
index d0070c7e..c4637633 100644
--- a/src/test/java/misc/checkin/DurationIdentityConstraintValidateTest.java
+++ b/src/test/java/misc/checkin/DurationIdentityConstraintValidateTest.java
@@ -109,4 +109,23 @@ public class DurationIdentityConstraintValidateTest {
         assertEquals(a.valueHashCode(), b.valueHashCode());
         assertFalse(a.valueEquals(XmlDuration.Factory.newValue("PT26H")));
     }
+
+    @Test
+    void fourHundredYearCycleHashesEqual() throws Exception {
+        // 400 years is 146097 days from any start date, so these are equal
+        String[][] same = {
+            {"P400Y", "P146097D"}, {"-P400Y", "-P146097D"}, {"P401Y1M", 
"P1Y1M146097D"},
+            {"P400YT0.5S", "P146097DT0.5S"}, {"P800Y", "P292194D"}
+        };
+        for (String[] pair : same) {
+            XmlDuration x = XmlDuration.Factory.newValue(pair[0]);
+            XmlDuration y = XmlDuration.Factory.newValue(pair[1]);
+            assertTrue(x.valueEquals(y), pair[0] + " " + pair[1]);
+            assertEquals(x.valueHashCode(), y.valueHashCode(), pair[0] + " " + 
pair[1]);
+            
assertEquals(Collections.singletonList("cvc-identity-constraint.4.1"),
+                unique(pair[0], pair[1]), pair[0] + " " + pair[1]);
+        }
+        assertFalse(XmlDuration.Factory.newValue("P400Y").valueEquals(
+            XmlDuration.Factory.newValue("P146096D")));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to