Skip to content

Commit

Permalink
#132: Fix formatting of negative zero duration
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Sep 18, 2023
1 parent 758eef5 commit 29c3a13
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -31,7 +36,8 @@ void testToString(Duration duration, String expectedValue)
}

@ParameterizedTest
@CsvSource({
@CsvSource(
{
"00:00, PT0S",
"00:04, PT4M",
"00:14, PT14M",
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}

Expand All @@ -53,5 +55,4 @@ public DayOfWeekWithoutDotFormatter getCustomShortDateFormatter()
{
return new DayOfWeekWithoutDotFormatter(DateTimeFormatter.ofPattern("E dd.MM.", locale));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
{
Expand Down

0 comments on commit 29c3a13

Please sign in to comment.