From 859984691382ec6e639db756a68aa0e11e4a0d41 Mon Sep 17 00:00:00 2001 From: Elyahu41 Date: Fri, 6 Nov 2026 10:51:08 -0500 Subject: [PATCH] ZmanimFormatter.formatXSDDurationTime: write negative durations as valid xsd:duration Duration.toString() writes a negative duration as "PT-1H-15M-3S", which isn't a valid xsd:duration (XSD only allows one leading sign: "-PT1H15M3S"). This also affected format() with XSD_DURATION_FORMAT and the durations in toXML() / toJSON(). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015Er4Sx7LnKzzCbgDEatxkU --- .../zmanim/util/ZmanimFormatter.java | 9 ++- .../util/ZmanimFormatterXsdDurationTest.java | 57 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/kosherjava/zmanim/util/ZmanimFormatterXsdDurationTest.java diff --git a/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java b/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java index 7608ab01..79741d74 100644 --- a/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java +++ b/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java @@ -282,8 +282,8 @@ public String formatXSDateTime(Instant instant) { } /** - * This returns the xml representation of an xsd:duration object. This simply uses {@link Duration#toString()} that formats it - * in the standard XSD ISO-8601 (e.g., "{@code PT1H30M}") + * This returns the xml representation of an xsd:duration object, in the standard XSD ISO-8601 format (e.g., "{@code PT1H30M}", + * or "{@code -PT1H30M}" for a negative duration). * * @param duration the duration to format * @return the xsd:duration ISO-8601 formatted String @@ -292,6 +292,11 @@ public String formatXSDDurationTime(Duration duration) { if (duration == null) { return ""; } + if (duration.isNegative()) { + // Duration.toString() writes a negative duration with a sign on every field ("PT-1H-30M"), which isn't a valid + // xsd:duration. XSD puts a single sign in front of the whole value. + return "-" + duration.negated().toString(); + } return duration.toString(); // Java's native toString() outputs a standard XSD ISO-8601 (e.g., "PT1H30M") } diff --git a/src/test/java/com/kosherjava/zmanim/util/ZmanimFormatterXsdDurationTest.java b/src/test/java/com/kosherjava/zmanim/util/ZmanimFormatterXsdDurationTest.java new file mode 100644 index 00000000..6ec806f9 --- /dev/null +++ b/src/test/java/com/kosherjava/zmanim/util/ZmanimFormatterXsdDurationTest.java @@ -0,0 +1,57 @@ +/* + * Zmanim Java API + * Copyright © 2004-2026 Eliyahu Hershfeld + * + * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA, + * or connect to: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html + */ +package com.kosherjava.zmanim.util; + +import static org.junit.Assert.assertEquals; + +import java.time.Duration; +import java.time.ZoneId; + +import javax.xml.datatype.DatatypeFactory; + +import org.junit.Test; + +/** + * {@link ZmanimFormatter#formatXSDDurationTime(Duration)} must produce a valid {@code xsd:duration}, including for negative + * durations (for example a negative offset, or a sha'ah zmanis computed from a start after the end). + */ +public class ZmanimFormatterXsdDurationTest { + private final ZmanimFormatter formatter = new ZmanimFormatter(ZoneId.of("America/New_York")); + + @Test + public void positiveDurationsAreUnchanged() { + assertEquals("PT1H30M", formatter.formatXSDDurationTime(Duration.ofMinutes(90))); + assertEquals("PT0S", formatter.formatXSDDurationTime(Duration.ZERO)); + assertEquals("", formatter.formatXSDDurationTime(null)); + } + + @Test + public void negativeDurationsHaveALeadingSign() throws Exception { + assertEquals("-PT1H15M3S", formatter.formatXSDDurationTime(Duration.ofMinutes(-75).minusSeconds(3))); + assertEquals("-PT0.5S", formatter.formatXSDDurationTime(Duration.ofMillis(-500))); + + // and the result is a valid xsd:duration with the same value + Duration negative = Duration.ofHours(-1).minusMinutes(15).minusMillis(250); + String text = formatter.formatXSDDurationTime(negative); + assertEquals(negative.toMillis(), DatatypeFactory.newInstance().newDuration(text).getTimeInMillis(new java.util.Date(0))); + } + + @Test + public void formatUsesTheXsdFormatForNegativeDurations() { + ZmanimFormatter xsd = new ZmanimFormatter(ZmanimFormatter.XSD_DURATION_FORMAT, + java.time.format.DateTimeFormatter.ofPattern("h:mm:ss"), ZoneId.of("America/New_York")); + assertEquals("-PT30M", xsd.format(Duration.ofMinutes(-30))); + } +}