diff --git a/lib/simple.js b/lib/simple.js index 564bb9e..ca6fba1 100644 --- a/lib/simple.js +++ b/lib/simple.js @@ -27,7 +27,7 @@ function Simple() { */ Simple.prototype._parse = function(str) { - var end = str.length; + var end = str.length, offset = 0; // There is a minimum length of 5 characters diff --git a/lib/util.js b/lib/util.js index 1cc776b..43feb5d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -148,15 +148,12 @@ function addDuration(startDate, duration) { end.day += duration.getDays(); } while(end.day && end.day > GlobalUtil.daysInMonth(end.month, end.year)) { + end.day -= GlobalUtil.daysInMonth(end.month, end.year); end.month += 1; if(end.month > 12) { end.month -= 12; end.year += 1; } - end.day -= GlobalUtil.daysInMonth(end.month, end.year); - } - if(end.day != undefined) { - endString = '-'+('00'+end.day).substr(-2,2)+endString; } if(duration.getMonths()) { @@ -166,6 +163,19 @@ function addDuration(startDate, duration) { end.month -= 12; end.year += 1; } + // After readjusting the month, check again for days overflow + if(end.day && end.day > GlobalUtil.daysInMonth(end.month, end.year)){ + end.day = end.day - GlobalUtil.daysInMonth(end.month, end.year); + end.month += 1; + if(end.month > 12) { + end.month -= 12; + end.year += 1; + } + } + + if(end.day != undefined) { + endString = '-'+('00'+end.day).substr(-2,2)+endString; + } if(end.month != undefined) { endString = '-'+('00'+end.month).substr(-2,2)+endString; } @@ -254,12 +264,12 @@ function getDuration(startDate, endDate) { if(end.day != undefined) { while(end.day-start.day < 0) { - end.day += GlobalUtil.daysInMonth(end.month,end.year); end.month -= 1; if(end.month < 1) { end.year -= 1; end.month += 12; } + end.day += GlobalUtil.daysInMonth(end.month,end.year); } if(end.day-start.day > 0) { duration = ('00'+(end.day-start.day)).substr(-2,2)+'D'+duration; diff --git a/test/range.js b/test/range.js index 99e0805..de89403 100644 --- a/test/range.js +++ b/test/range.js @@ -125,7 +125,6 @@ describe('Range', function(){ it('should calculate range correctly', function(){ var range = new Range('+1000-02-03/+2000'); - expect(range.duration.getYears()).to.equal(999); expect(range.duration.getMonths()).to.equal(10); expect(range.duration.getDays()).to.equal(29); @@ -133,15 +132,20 @@ describe('Range', function(){ it('should calculate tricky range correctly', function(){ var range = new Range('+1970-01-31/+1973-02-01'); - - expect(range.duration.getYears()).to.equal(2); - expect(range.duration.getMonths()).to.equal(11); - expect(range.duration.getDays()).to.equal(29); + expect(range.duration.getYears()).to.equal(3); + expect(range.duration.getMonths()).to.equal(undefined); + expect(range.duration.getDays()).to.equal(1); + }); + + it('should calculate days correctly', function(){ + var range = new Range('+1999-02-28/+1999-03-01'); + expect(range.duration.getYears()).to.not.exist; + expect(range.duration.getMonths()).to.not.exist; + expect(range.duration.getDays()).to.equal(1); }); it('should calculate approximate duration', function(){ var range = new Range('A+1000/P1000Y'); - expect(range.end.getYear()).to.equal(2000); expect(range.isApproximate()).to.equal(true); }); diff --git a/test/util.js b/test/util.js index 0b8b0ea..6466255 100644 --- a/test/util.js +++ b/test/util.js @@ -235,6 +235,25 @@ describe('Util', function(){ }).to.throw(Error, 'New date out of range'); }); + + it('should handle simple month overflow', function(){ + var start = new Simple('+1707-02-28'), + duration = new Duration('P1D'), + end = GedcomXDate.addDuration(start, duration); + + expect(end.getYear()).to.equal(1707); + expect(end.getMonth()).to.equal(3); + expect(end.getDay()).to.equal(1); + }); + + it('complex case', function(){ + var start = new Simple('+1771-08-30'), + duration = new Duration('P1M1D'), + end = GedcomXDate.addDuration(start, duration); + expect(end.getYear()).to.equal(1771); + expect(end.getMonth()).to.equal(10); + expect(end.getDay()).to.equal(1); + }) }); @@ -309,6 +328,21 @@ describe('Util', function(){ expect(duration.getSeconds()).to.equal(undefined); }); + + it('should overflow days when months are not the same length', function(){ + + var start = new Simple('+0999-11-30T00:00:00'), + end = new Simple('+0999-12-01T00:00:00'), + duration = GedcomXDate.getDuration(start, end); + + expect(duration.getYears()).to.equal(undefined); + expect(duration.getMonths()).to.equal(undefined); + expect(duration.getDays()).to.equal(1); + expect(duration.getHours()).to.equal(undefined); + expect(duration.getMinutes()).to.equal(undefined); + expect(duration.getSeconds()).to.equal(undefined); + + }); it('should overflow months', function(){