Browse files

fix issue 19: preventOverflow bug

  • Loading branch information...
1 parent 1a7ea75 commit 7653062814454b7a16c2050c287115a125411540 @arshaw committed Mar 31, 2013
Showing with 16 additions and 3 deletions.
  1. +8 −3 src/xdate.js
  2. +8 −0 test/adding.js
View
11 src/xdate.js
@@ -219,14 +219,19 @@ each(methodSubjects, function(subject, fieldIndex) {
function _set(xdate, fieldIndex, value, args, useUTC) {
var getField = curry(_getField, xdate[0], useUTC);
var setField = curry(_setField, xdate[0], useUTC);
- var month = fieldIndex == MONTH ? value % 12 : getField(MONTH);
+ var expectedMonth;
var preventOverflow = false;
if (args.length == 2 && isBoolean(args[1])) {
preventOverflow = args[1];
- args = [value];
+ args = [ value ];
+ }
+ if (fieldIndex == MONTH) {
+ expectedMonth = (value % 12 + 12) % 12;
+ }else{
+ expectedMonth = getField(MONTH);
}
setField(fieldIndex, args);
- if (preventOverflow && getField(MONTH) != month) {
+ if (preventOverflow && getField(MONTH) != expectedMonth) {
setField(MONTH, [ getField(MONTH) - 1 ]);
setField(DATE, [ getDaysInMonth(getField(FULLYEAR), getField(MONTH)) ]);
}
View
8 test/adding.js
@@ -42,6 +42,14 @@ test("addMonths, prevent overflow", function() {
});
+test("addMonths, prevent overflow, backwards january bug", function() {
+ var d1 = new XDate(2013, 0, 28); // Jan 28
+ var d2 = new XDate(2013, 0, 14); // Jan 14
+ return d1.addMonths(-1, true).toString('yyyy-MM-dd') == '2012-12-28' && // Dec 28
+ d2.addMonths(-1, true).toString('yyyy-MM-dd') == '2012-12-14'; // Dec 14
+});
+
+
test("addDays", function() {
var d1 = new XDate(2009, 5, 8);
d1.addDays(30);

0 comments on commit 7653062

Please sign in to comment.