Skip to content

Commit

Permalink
Fix duration converter with multiple units
Browse files Browse the repository at this point in the history
  • Loading branch information
manofthepeace committed Sep 7, 2023
1 parent edb23c9 commit 5173d04
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DurationConverter implements Converter<Duration>, Serializable {
private static final String PERIOD = "P";
private static final String PERIOD_OF_TIME = "PT";
public static final Pattern DIGITS = Pattern.compile("^[-+]?\\d+$");
private static final Pattern DIGITS_AND_UNIT = Pattern.compile("^[-+]?\\d+(?:\\.\\d+)?(?i)[hms]$");
private static final Pattern DIGITS_AND_UNIT = Pattern.compile("^(?:[-+]?\\d+(?:\\.\\d+)?(?i)[hms])+$");
private static final Pattern DAYS = Pattern.compile("^[-+]?\\d+(?i)d$");
private static final Pattern MILLIS = Pattern.compile("^[-+]?\\d+(?i)ms$");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,18 @@ public void testValueIsInSec() {
Duration actualDuration = durationConverter.convert("2s");
assertEquals(expectedDuration, actualDuration);
}

@Test
public void testValuesWithMultipleUnits() {
Duration expectedDuration = Duration.ofSeconds(150);
Duration actualDuration = durationConverter.convert("2m30s");
assertEquals(expectedDuration, actualDuration);
}

@Test
public void testValuesWithMultipleUnitsSigned() {
Duration expectedDuration = Duration.ofSeconds(90);
Duration actualDuration = durationConverter.convert("+2m-30s");
assertEquals(expectedDuration, actualDuration);
}
}

0 comments on commit 5173d04

Please sign in to comment.