Skip to content

Commit

Permalink
Fix DURATION parsing on negative 'W' durations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerio Gionco committed Jun 6, 2019
1 parent 93f4412 commit 6a0ea2f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Expand Up @@ -115,7 +115,7 @@ private String durationToString(Duration duration) {

public static TemporalAmountAdapter parse(String value) {
TemporalAmount retVal = null;
if (value.matches("P.*(W|D)$")) {
if (value.matches("([+-])?P.*(W|D)")) {
retVal = java.time.Period.parse(value);
} else {
retVal = java.time.Duration.parse(value);
Expand Down
Expand Up @@ -148,4 +148,15 @@ class TemporalAmountAdapterTest extends Specification {
adapter1.hashCode() == adapter2.hashCode()
}

def 'week period parsing and values'() {
given: 'a one week amount adapter'
TemporalAmountAdapter adapter1 = TemporalAmountAdapter.parse('P1W')

and: 'a negative one week identical period'
TemporalAmountAdapter adapter2 = TemporalAmountAdapter.parse('-P1W')

expect: 'same duration, except for the sign'
adapter1.duration == -adapter2.duration
}

}

0 comments on commit 6a0ea2f

Please sign in to comment.