From 29c3a132977f233d5af51593d55a9130f75c85a0 Mon Sep 17 00:00:00 2001 From: kaklakariada Date: Mon, 18 Sep 2023 09:00:58 +0200 Subject: [PATCH] #132: Fix formatting of negative zero duration --- CHANGELOG.md | 1 + .../DurationStringConverterTest.java | 24 ++++++++++----- .../logic/service/FormatterService.java | 13 ++++---- .../logic/service/FormatterServiceTest.java | 30 +++++++++++++++++-- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3785240e..3c956eff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ See [Release](https://github.com/itsallcode/white-rabbit/releases/tag/v1.9.0) / * [#260](https://github.com/itsallcode/white-rabbit/pull/260): Upgraded dependencies. * Added build with Java 20 * UI-Tests run only in non-headless mode due to restrictions with the latest JavaFX version, see #261 +* [#132](https://github.com/itsallcode/white-rabbit/issues/132): Fix display of negative zero duration ## [1.8.0] - 2022-01-22 diff --git a/jfxui/src/test/java/org/itsallcode/whiterabbit/jfxui/table/converter/DurationStringConverterTest.java b/jfxui/src/test/java/org/itsallcode/whiterabbit/jfxui/table/converter/DurationStringConverterTest.java index 4ec26b57..4f861d97 100644 --- a/jfxui/src/test/java/org/itsallcode/whiterabbit/jfxui/table/converter/DurationStringConverterTest.java +++ b/jfxui/src/test/java/org/itsallcode/whiterabbit/jfxui/table/converter/DurationStringConverterTest.java @@ -15,8 +15,13 @@ class DurationStringConverterTest @ParameterizedTest @CsvSource({ "PT0S, 00:00", + "PT-20S, 00:00", "PT20S, 00:00", "PT4M, 00:04", + "PT4M20S, 00:04", + "PT-4M, -00:04", + "PT-4M-20S, -00:04", + "PT-4M20S, -00:03", "PT14M, 00:14", "PT100M, 01:40", "PT3H, 03:00", @@ -31,7 +36,8 @@ void testToString(Duration duration, String expectedValue) } @ParameterizedTest - @CsvSource({ + @CsvSource( + { "00:00, PT0S", "00:04, PT4M", "00:14, PT14M", @@ -67,14 +73,16 @@ void fromStringManuallyEntered(String input, Duration expectedDuration) } @ParameterizedTest - @CsvSource(value = { - "string, NULL", - "1.2, NULL", - "1:2:3, NULL", - }, nullValues = "NULL") - void fromStringHandlesInvalidInput(String input, Duration expectedDuration) + @CsvSource( { - assertThat(converter().fromString(input)).isEqualTo(expectedDuration); + "string", + "1.2", + "-00:01", + "1:2:3", + }) + void fromStringHandlesInvalidInput(String input) + { + assertThat(converter().fromString(input)).isNull(); } private DurationStringConverter converter() diff --git a/logic/src/main/java/org/itsallcode/whiterabbit/logic/service/FormatterService.java b/logic/src/main/java/org/itsallcode/whiterabbit/logic/service/FormatterService.java index 21383eba..a0a12269 100644 --- a/logic/src/main/java/org/itsallcode/whiterabbit/logic/service/FormatterService.java +++ b/logic/src/main/java/org/itsallcode/whiterabbit/logic/service/FormatterService.java @@ -1,10 +1,7 @@ package org.itsallcode.whiterabbit.logic.service; import java.text.MessageFormat; -import java.time.Duration; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneId; +import java.time.*; import java.time.format.DateTimeFormatter; import java.util.Locale; @@ -26,9 +23,14 @@ public FormatterService(Locale locale, ZoneId timeZoneId) public String format(Duration duration) { - final String sign = duration.isNegative() ? "-" : ""; + String sign = duration.isNegative() ? "-" : ""; final long hours = Math.abs(duration.toHours()); final int minutes = Math.abs(duration.toMinutesPart()); + if (hours == 0 && minutes == 0) + { + sign = ""; + } + System.out.println(duration + " -> " + " sign=" + sign + ", h=" + hours + ", min=" + minutes); return format("{0}{1,number,00}:{2,number,00}", sign, hours, minutes); } @@ -53,5 +55,4 @@ public DayOfWeekWithoutDotFormatter getCustomShortDateFormatter() { return new DayOfWeekWithoutDotFormatter(DateTimeFormatter.ofPattern("E dd.MM.", locale)); } - } diff --git a/logic/src/test/java/org/itsallcode/whiterabbit/logic/service/FormatterServiceTest.java b/logic/src/test/java/org/itsallcode/whiterabbit/logic/service/FormatterServiceTest.java index 99be6377..c95a8cfa 100644 --- a/logic/src/test/java/org/itsallcode/whiterabbit/logic/service/FormatterServiceTest.java +++ b/logic/src/test/java/org/itsallcode/whiterabbit/logic/service/FormatterServiceTest.java @@ -2,13 +2,13 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.time.Duration; -import java.time.Instant; -import java.time.ZoneId; +import java.time.*; import java.util.Locale; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; class FormatterServiceTest { @@ -26,6 +26,30 @@ void testFormatDuration() assertThat(formatter.format(Duration.ofHours(4).plusMinutes(42).plusSeconds(31))).isEqualTo("04:42"); } + @ParameterizedTest + @CsvSource( + { + "PT0S, 00:00", + "PT-20S, 00:00", + "PT20S, 00:00", + "PT4M, 00:04", + "PT4M20S, 00:04", + "PT-4M, -00:04", + "PT-4M-20S, -00:04", + "PT-4M20S, -00:03", + "PT14M, 00:14", + "PT100M, 01:40", + "PT3H, 03:00", + "PT13H, 13:00", + "PT2H34M, 02:34", + "PT25H34M, 25:34", + "PT101H34M, 101:34", + }) + void testFormatDuration2(Duration duration, String expectedValue) + { + assertThat(formatter.format(duration)).isEqualTo(expectedValue); + } + @Test void testFormatDateAndTime() {