Skip to content

Commit d2a72ce

Browse files
committed
Fix closeto tolerance when seconds exceed 60 (#62)
1 parent 5ed0815 commit d2a72ce

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/main/java/org/assertj/db/type/TimeValue.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
* This class represents a time value in the database.
2121
*
2222
* @author Régis Pouiller
23-
*
23+
* @author Julien Roy
24+
*
2425
*/
2526
public class TimeValue implements Comparable<TimeValue> {
2627

@@ -376,20 +377,31 @@ public TimeValue move(TimeValue time) {
376377
thisNanoSeconds += 1000000000 + nanoSeconds;
377378
seconds--;
378379
}
379-
if (seconds >= 0 || thisSeconds >= -seconds) {
380+
381+
if (seconds > 0 && (thisSeconds + seconds >= 60)) {
382+
thisSeconds += seconds - 60;
383+
minutes++;
384+
}
385+
else if (seconds >= 0 || thisSeconds >= -seconds) {
380386
thisSeconds += seconds;
381387
}
382388
else {
383389
thisSeconds += 60 + seconds;
384390
minutes--;
385391
}
386-
if (minutes >= 0 || thisMinutes >= -minutes) {
392+
393+
if ( minutes > 0 && (thisMinutes + minutes >= 60 )) {
394+
thisMinutes += minutes - 60;
395+
hours++;
396+
}
397+
else if (minutes >= 0 || thisMinutes >= -minutes) {
387398
thisMinutes += minutes;
388399
}
389400
else {
390401
thisMinutes += 60 + minutes;
391402
hours--;
392403
}
404+
393405
thisHours += hours;
394406

395407
return of(thisHours, thisMinutes, thisSeconds, thisNanoSeconds);

src/test/java/org/assertj/db/api/assertions/impl/AssertionsOnValueCloseness_IsCloseTo_DateTimeValue_TimeValue_Test.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* {@link AssertionsOnValueCloseness#isCloseTo(org.assertj.db.api.AbstractAssert, org.assertj.core.api.WritableAssertionInfo, org.assertj.db.type.Value, org.assertj.db.type.DateTimeValue, org.assertj.db.type.TimeValue)} method.
3434
*
3535
* @author Régis Pouiller
36+
* @author Julien Roy
3637
*
3738
*/
3839
public class AssertionsOnValueCloseness_IsCloseTo_DateTimeValue_TimeValue_Test extends AbstractTest {
@@ -45,15 +46,22 @@ public void test_is_close_to() throws Exception {
4546
WritableAssertionInfo info = new WritableAssertionInfo();
4647
Table table = new Table();
4748
TableAssert tableAssert = assertThat(table);
49+
4850
TableAssert tableAssert2 = AssertionsOnValueCloseness.isCloseTo(tableAssert, info, getValue(
4951
null, Date.valueOf("2007-12-23")), DateTimeValue.of(DateValue.of(2007, 12, 23)), TimeValue.of(0, 0, 0));
5052
Assertions.assertThat(tableAssert2).isSameAs(tableAssert);
53+
5154
tableAssert2 = AssertionsOnValueCloseness.isCloseTo(tableAssert, info, getValue(
5255
null, Timestamp.valueOf("2007-12-23 09:01:00")), DateTimeValue.of(
5356
DateValue.of(2007, 12, 23), TimeValue.of(9, 1)), TimeValue.of(0, 0, 0));
5457
Assertions.assertThat(tableAssert2).isSameAs(tableAssert);
58+
5559
tableAssert2 = AssertionsOnValueCloseness.isCloseTo(tableAssert, info, getValue(null, null), (DateTimeValue) null, TimeValue.of(0, 0, 0));
5660
Assertions.assertThat(tableAssert2).isSameAs(tableAssert);
61+
62+
tableAssert2 = AssertionsOnValueCloseness.isCloseTo(tableAssert, info, getValue(
63+
null, Timestamp.valueOf("2019-12-03 09:25:00")), DateTimeValue.from(Timestamp.valueOf("2019-12-03 09:24:58")), TimeValue.of(0, 0, 10));
64+
Assertions.assertThat(tableAssert2).isSameAs(tableAssert);
5765
}
5866

5967
/**

src/test/java/org/assertj/db/type/TimeValue_Test.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
* Tests on the time value.
2626
*
2727
* @author Régis Pouiller
28-
*
28+
* @author Julien Roy
29+
*
2930
*/
3031
public class TimeValue_Test extends AbstractTest {
3132

@@ -521,6 +522,15 @@ public void test_move_time() {
521522
assertThat(TimeValue.of(9, 1).move(TimeValue.of(0, 0, 2))).as("add 2 seconds").isEqualTo(
522523
TimeValue.of(9, 1, 2));
523524

525+
assertThat(TimeValue.of(9, 1, 58).move(TimeValue.of(0, 0, 10))).as("add 10 seconds to 58").isEqualTo(
526+
TimeValue.of(9, 2, 8));
527+
528+
assertThat(TimeValue.of(9, 59, 58).move(TimeValue.of(0, 0, 50))).as("add 50 seconds to 58").isEqualTo(
529+
TimeValue.of(10, 00, 48));
530+
531+
assertThat(TimeValue.of(9, 0, 0).move(TimeValue.of(0, 0, -10))).as("substract 10 seconds to 0").isEqualTo(
532+
TimeValue.of(8, 59, 50));
533+
524534
assertThat(TimeValue.of(9, 1).move(TimeValue.of(0, 0, -1))).as("substract 1 second").isEqualTo(
525535
TimeValue.of(9, 0, 59, 0));
526536
assertThat(TimeValue.of(9, 1).move(TimeValue.of(0, 0, -2))).as("substract 2 seconds").isEqualTo(

0 commit comments

Comments
 (0)