Skip to content

Commit

Permalink
Fix integer overflow error
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jan 15, 2020
1 parent dd08dff commit b70bd54
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Expand Up @@ -131,7 +131,7 @@ public static TemporalAmountAdapter fromDateRange(Date start, Date end) {
TemporalAmount duration;
long durationMillis = end.getTime() - start.getTime();
if (durationMillis % (24 * 60 * 60 * 1000) == 0) {
duration = java.time.Period.ofDays((int) durationMillis / (24 * 60 * 60 * 1000));
duration = java.time.Period.ofDays((int) (durationMillis / (24 * 60 * 60 * 1000)));
} else {
duration = java.time.Duration.ofMillis(durationMillis);
}
Expand Down
Expand Up @@ -51,16 +51,25 @@ class TemporalAmountAdapterTest extends Specification {
new Dur(-9) | java.time.Period.ofWeeks(-9)
}

def 'test creation from date range'() {
expect:
TemporalAmountAdapter.fromDateRange(new DateTime(start), new DateTime(end)).duration == expectedTemporalAmount

where:
start | end | expectedTemporalAmount
'20200107T000000' | '20200331T000000' | java.time.Period.ofWeeks(12)
}

@Unroll
def 'validate string representation: #dur'() {
expect: 'derived string representation equals expected'
dur.toString() == expectedString
TemporalAmountAdapter.from(dur).toString() == expectedString

where:
dur | expectedString
TemporalAmountAdapter.from(new Dur(33)) | 'P33W'
TemporalAmountAdapter.from(new Dur('-P2D')) | '-P2D'
TemporalAmountAdapter.from(new Dur(-2, 0, 0, 0)) | '-P2D'
new Dur(33) | 'P33W'
new Dur('-P2D') | '-P2D'
new Dur(-2, 0, 0, 0) | '-P2D'
}

@Unroll
Expand Down

0 comments on commit b70bd54

Please sign in to comment.