Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to Issue #1034: Date.parse "parses" dates that should be invalid #1106

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
9 changes: 7 additions & 2 deletions Source/Types/Date.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,12 @@ Date.extend({

if (!(parsed && parsed.isValid())){
parsed = new Date(nativeParse(from));
if (!(parsed && parsed.isValid())) parsed = new Date(from.toInt());

if (!(parsed && parsed.isValid()) && !isNaN(Number(from))) {
parsed = new Date(from.toInt());
}
}

return parsed;
},

Expand Down Expand Up @@ -440,7 +444,7 @@ var replacers = function(key){
case 'x': // iso8601 covers yyyy-mm-dd, so just check if month is first
return ((Date.orderIndex('month') == 1) ? '%m[-./]%d' : '%d[-./]%m') + '([-./]%y)?';
case 'X':
return '%H([.:]%M)?([.:]%S([.:]%s)?)? ?%p? ?%z?';
return '%H([.:]%M([.:]%S([.:]%s)?)?)? ?%p? ?%z?';
}
return null;
};
Expand Down Expand Up @@ -499,6 +503,7 @@ var build = function(format){
re: new RegExp('^' + re + '$', 'i'),
handler: function(bits){
bits = bits.slice(1).associate(parsed);

var date = new Date().clearTime(),
year = bits.y || bits.Y;

Expand Down
27 changes: 27 additions & 0 deletions Tests/Specs/1.3/Types/Date.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ describe('Date', function(){

});




describe('Date.get', function(){

it('should get the hour', function(){
Expand Down Expand Up @@ -671,6 +674,29 @@ describe('Date', function(){
Date.prototype.clearTime = clearTime;
});

it('should not parse invalid dates', function () {
var d = new Date();
expect(d.parse('blah').isValid()).toBeFalsy();
expect(d.parse('12th blah').isValid()).toBeFalsy();
expect(d.parse('47th Oct').isValid()).toBeFalsy();
expect(d.parse('2012-55-11').isValid()).toBeFalsy();
expect(d.parse('2012-02-88').isValid()).toBeFalsy();
expect(d.parse('1981-12-02 37:14:88').isValid()).toBeFalsy();
expect(d.parse('1981-12-02 12:99:69').isValid()).toBeFalsy();
expect(d.parse('1981-12-02 15:14:71').isValid()).toBeFalsy();
});

it('should not parse invalid dates with custom formats', function () {
var d = new Date();
var fmt = '%d-%m-%Y %H:%M:%S';
Date.defineFormat(fmt);
expect(d.parse('02-12-1981 18:00:75').isValid()).toBeFalsy();

// this passes in Webkit and Firefox, but breaks in IE. IEs native
// Date.parse parses dates like '69-7000-1981' as valid dates.
expect(d.parse('69-12-1981 18:00:00').isValid()).toBeFalsy();
});

});

describe('Date.defineFormat', function(){
Expand Down Expand Up @@ -705,6 +731,7 @@ describe('Date', function(){

});


});

})(this);