Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 10 additions & 6 deletions test/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,27 @@ 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);
});

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);
});
Expand Down
34 changes: 34 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})

});

Expand Down Expand Up @@ -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(){

Expand Down