Skip to content

Commit

Permalink
Fix MonthDay add/subtract around Feb29 [3528941]
Browse files Browse the repository at this point in the history
  • Loading branch information
jodastephen committed May 23, 2012
1 parent cc3262f commit 2ea8563
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
5 changes: 5 additions & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ None

Bug fixes since 2.1
-------------------
- MonthDay add/subtract [3528941]
Addition and subtraction in MonthDay was fixed.
It previously didn't work when the start value was 29th February.


- DateTimeFormatter.parseInto() [3522138]
The v2.0 changes to handle parsing of month/day on their own (for Feb 29th) cause
parseInto() to lose the year. This fix reverts behaviour to v1.x so that parseInto()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ public int[] add(ReadablePartial partial, int fieldIndex, int[] values, int valu
if (valueToAdd == 0) {
return values;
}
if (partial.size() > 0 && partial.getFieldType(0).equals(DateTimeFieldType.monthOfYear()) && fieldIndex == 0) {
// month is largest field and being added to, such as month-day
int curMonth0 = partial.getValue(0) - 1;
int newMonth = ((curMonth0 + (valueToAdd % 12) + 12) % 12) + 1;
return set(partial, 0, values, newMonth);
}
if (DateTimeUtils.isContiguous(partial)) {
long instant = 0L;
for (int i = 0, isize = partial.size(); i < isize; i++) {
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/org/joda/time/TestMonthDay_Basics.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,34 @@ public void testPlusMonths_int() {
assertEquals(expected, result);
}

public void testPlusMonths_int_fromLeap() {
MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC());
MonthDay result = test.plusMonths(1);
MonthDay expected = new MonthDay(3, 29, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testPlusMonths_int_negativeFromLeap() {
MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC());
MonthDay result = test.plusMonths(-1);
MonthDay expected = new MonthDay(1, 29, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testPlusMonths_int_endOfMonthAdjust() {
MonthDay test = new MonthDay(3, 31, ISOChronology.getInstanceUTC());
MonthDay result = test.plusMonths(1);
MonthDay expected = new MonthDay(4, 30, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testPlusMonths_int_negativeEndOfMonthAdjust() {
MonthDay test = new MonthDay(3, 31, ISOChronology.getInstanceUTC());
MonthDay result = test.plusMonths(-1);
MonthDay expected = new MonthDay(2, 29, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testPlusMonths_int_same() {
MonthDay test = new MonthDay(6, 5, ISO_UTC);
MonthDay result = test.plusMonths(0);
Expand All @@ -468,13 +496,28 @@ public void testPlusMonths_int_adjust() {
assertEquals(expected, result);
}

//-------------------------------------------------------------------------
public void testPlusDays_int() {
MonthDay test = new MonthDay(5, 10, BuddhistChronology.getInstance());
MonthDay result = test.plusDays(1);
MonthDay expected = new MonthDay(5, 11, BuddhistChronology.getInstance());
assertEquals(expected, result);
}

public void testPlusDays_int_fromLeap() {
MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC());
MonthDay result = test.plusDays(1);
MonthDay expected = new MonthDay(3, 1, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testPlusDays_int_negativeFromLeap() {
MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC());
MonthDay result = test.plusDays(-1);
MonthDay expected = new MonthDay(2, 28, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testPlusDays_same() {
MonthDay test = new MonthDay(5, 10, BuddhistChronology.getInstance());
MonthDay result = test.plusDays(0);
Expand All @@ -499,6 +542,34 @@ public void testMinusMonths_int() {
assertEquals(expected, result);
}

public void testMinusMonths_int_fromLeap() {
MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC());
MonthDay result = test.minusMonths(1);
MonthDay expected = new MonthDay(1, 29, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testMinusMonths_int_negativeFromLeap() {
MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC());
MonthDay result = test.minusMonths(-1);
MonthDay expected = new MonthDay(3, 29, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testMinusMonths_int_endOfMonthAdjust() {
MonthDay test = new MonthDay(3, 31, ISOChronology.getInstanceUTC());
MonthDay result = test.minusMonths(1);
MonthDay expected = new MonthDay(2, 29, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testMinusMonths_int_negativeEndOfMonthAdjust() {
MonthDay test = new MonthDay(3, 31, ISOChronology.getInstanceUTC());
MonthDay result = test.minusMonths(-1);
MonthDay expected = new MonthDay(4, 30, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testMinusMonths_int_same() {
MonthDay test = new MonthDay(6, 5, ISO_UTC);
MonthDay result = test.minusMonths(0);
Expand All @@ -519,13 +590,28 @@ public void testMinusMonths_int_adjust() {
assertEquals(expected, result);
}

//-------------------------------------------------------------------------
public void testMinusDays_int() {
MonthDay test = new MonthDay(5, 11, BuddhistChronology.getInstance());
MonthDay result = test.minusDays(1);
MonthDay expected = new MonthDay(5, 10, BuddhistChronology.getInstance());
assertEquals(expected, result);
}

public void testMinusDays_int_fromLeap() {
MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC());
MonthDay result = test.minusDays(1);
MonthDay expected = new MonthDay(2, 28, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testMinusDays_int_negativeFromLeap() {
MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC());
MonthDay result = test.minusDays(-1);
MonthDay expected = new MonthDay(3, 1, ISOChronology.getInstance());
assertEquals(expected, result);
}

public void testMinusDays_same() {
MonthDay test = new MonthDay(5, 11, BuddhistChronology.getInstance());
MonthDay result = test.minusDays(0);
Expand Down

0 comments on commit 2ea8563

Please sign in to comment.