Skip to content

Commit 8599846

Browse files
Elyahu41claude
andcommitted
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015Er4Sx7LnKzzCbgDEatxkU
1 parent afadabc commit 8599846

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ public String formatXSDateTime(Instant instant) {
282282
}
283283

284284
/**
285-
* This returns the xml representation of an xsd:duration object. This simply uses {@link Duration#toString()} that formats it
286-
* in the standard XSD ISO-8601 (e.g., "{@code PT1H30M}")
285+
* This returns the xml representation of an xsd:duration object, in the standard XSD ISO-8601 format (e.g., "{@code PT1H30M}",
286+
* or "{@code -PT1H30M}" for a negative duration).
287287
*
288288
* @param duration the duration to format
289289
* @return the xsd:duration ISO-8601 formatted String
@@ -292,6 +292,11 @@ public String formatXSDDurationTime(Duration duration) {
292292
if (duration == null) {
293293
return "";
294294
}
295+
if (duration.isNegative()) {
296+
// Duration.toString() writes a negative duration with a sign on every field ("PT-1H-30M"), which isn't a valid
297+
// xsd:duration. XSD puts a single sign in front of the whole value.
298+
return "-" + duration.negated().toString();
299+
}
295300
return duration.toString(); // Java's native toString() outputs a standard XSD ISO-8601 (e.g., "PT1H30M")
296301
}
297302

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Zmanim Java API
3+
* Copyright © 2004-2026 Eliyahu Hershfeld
4+
*
5+
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
6+
* Public License as published by the Free Software Foundation; version 2.1 of the License.
7+
*
8+
* This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied
9+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
10+
* details.
11+
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
12+
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA,
13+
* or connect to: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
14+
*/
15+
package com.kosherjava.zmanim.util;
16+
17+
import static org.junit.Assert.assertEquals;
18+
19+
import java.time.Duration;
20+
import java.time.ZoneId;
21+
22+
import javax.xml.datatype.DatatypeFactory;
23+
24+
import org.junit.Test;
25+
26+
/**
27+
* {@link ZmanimFormatter#formatXSDDurationTime(Duration)} must produce a valid {@code xsd:duration}, including for negative
28+
* durations (for example a negative offset, or a <em>sha'ah zmanis</em> computed from a start after the end).
29+
*/
30+
public class ZmanimFormatterXsdDurationTest {
31+
private final ZmanimFormatter formatter = new ZmanimFormatter(ZoneId.of("America/New_York"));
32+
33+
@Test
34+
public void positiveDurationsAreUnchanged() {
35+
assertEquals("PT1H30M", formatter.formatXSDDurationTime(Duration.ofMinutes(90)));
36+
assertEquals("PT0S", formatter.formatXSDDurationTime(Duration.ZERO));
37+
assertEquals("", formatter.formatXSDDurationTime(null));
38+
}
39+
40+
@Test
41+
public void negativeDurationsHaveALeadingSign() throws Exception {
42+
assertEquals("-PT1H15M3S", formatter.formatXSDDurationTime(Duration.ofMinutes(-75).minusSeconds(3)));
43+
assertEquals("-PT0.5S", formatter.formatXSDDurationTime(Duration.ofMillis(-500)));
44+
45+
// and the result is a valid xsd:duration with the same value
46+
Duration negative = Duration.ofHours(-1).minusMinutes(15).minusMillis(250);
47+
String text = formatter.formatXSDDurationTime(negative);
48+
assertEquals(negative.toMillis(), DatatypeFactory.newInstance().newDuration(text).getTimeInMillis(new java.util.Date(0)));
49+
}
50+
51+
@Test
52+
public void formatUsesTheXsdFormatForNegativeDurations() {
53+
ZmanimFormatter xsd = new ZmanimFormatter(ZmanimFormatter.XSD_DURATION_FORMAT,
54+
java.time.format.DateTimeFormatter.ofPattern("h:mm:ss"), ZoneId.of("America/New_York"));
55+
assertEquals("-PT30M", xsd.format(Duration.ofMinutes(-30)));
56+
}
57+
}

0 commit comments

Comments
 (0)