|
| 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