mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-19 03:51:07 +00:00
feat(TzDate): add support for toISOString method
This commit is contained in:
parent
ac4318a2fa
commit
da9f4dfcf4
2 changed files with 38 additions and 1 deletions
28
src/ngMock/angular-mocks.js
vendored
28
src/ngMock/angular-mocks.js
vendored
|
|
@ -397,6 +397,19 @@ angular.mock.$LogProvider = function() {
|
||||||
return parseInt(str, 10);
|
return parseInt(str, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function padNumber(num, digits, trim) {
|
||||||
|
var neg = '';
|
||||||
|
if (num < 0) {
|
||||||
|
neg = '-';
|
||||||
|
num = -num;
|
||||||
|
}
|
||||||
|
num = '' + num;
|
||||||
|
while(num.length < digits) num = '0' + num;
|
||||||
|
if (trim)
|
||||||
|
num = num.substr(num.length - digits);
|
||||||
|
return neg + num;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ngdoc object
|
* @ngdoc object
|
||||||
|
|
@ -523,12 +536,25 @@ angular.mock.$LogProvider = function() {
|
||||||
return self.date.getDay();
|
return self.date.getDay();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// provide this method only on browsers that already have it
|
||||||
|
if (self.toISOString) {
|
||||||
|
self.toISOString = function() {
|
||||||
|
return padNumber(self.origDate.getUTCFullYear(), 4) + '-' +
|
||||||
|
padNumber(self.origDate.getUTCMonth() + 1, 2) + '-' +
|
||||||
|
padNumber(self.origDate.getUTCDate(), 2) + 'T' +
|
||||||
|
padNumber(self.origDate.getUTCHours(), 2) + ':' +
|
||||||
|
padNumber(self.origDate.getUTCMinutes(), 2) + ':' +
|
||||||
|
padNumber(self.origDate.getUTCSeconds(), 2) + '.' +
|
||||||
|
padNumber(self.origDate.getUTCMilliseconds(), 3) + 'Z'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//hide all methods not implemented in this mock that the Date prototype exposes
|
//hide all methods not implemented in this mock that the Date prototype exposes
|
||||||
var unimplementedMethods = ['getMilliseconds', 'getUTCDay',
|
var unimplementedMethods = ['getMilliseconds', 'getUTCDay',
|
||||||
'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
|
'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
|
||||||
'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear',
|
'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear',
|
||||||
'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds',
|
'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds',
|
||||||
'setYear', 'toDateString', 'toJSON', 'toGMTString', 'toLocaleFormat', 'toLocaleString',
|
'setYear', 'toDateString', 'toGMTString', 'toJSON', 'toLocaleFormat', 'toLocaleString',
|
||||||
'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];
|
'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];
|
||||||
|
|
||||||
angular.forEach(unimplementedMethods, function(methodName) {
|
angular.forEach(unimplementedMethods, function(methodName) {
|
||||||
|
|
|
||||||
11
test/ngMock/angular-mocksSpec.js
vendored
11
test/ngMock/angular-mocksSpec.js
vendored
|
|
@ -63,6 +63,17 @@ describe('ngMock', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should fake toISOString method', function() {
|
||||||
|
var date = new angular.mock.TzDate(-1, '2009-10-09T01:02:03.027Z');
|
||||||
|
|
||||||
|
if (new Date().toISOString) {
|
||||||
|
expect(date.toISOString()).toEqual('2009-10-09T01:02:03.027Z');
|
||||||
|
} else {
|
||||||
|
expect(date.toISOString).toBeUndefined();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should fake getHours method', function() {
|
it('should fake getHours method', function() {
|
||||||
//0 in -3h
|
//0 in -3h
|
||||||
var t0 = new angular.mock.TzDate(-3, 0);
|
var t0 = new angular.mock.TzDate(-3, 0);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue