TzDate should support various UTC methods

This commit is contained in:
Igor Minar 2010-11-06 23:39:56 -07:00
parent f3ac2cd434
commit f077649f48
2 changed files with 55 additions and 4 deletions

37
test/angular-mocks.js vendored
View file

@ -195,12 +195,17 @@ angular.service('$browser', function(){
function TzDate(offset, timestamp) {
if (angular.isString(timestamp)) {
var tsStr = timestamp;
timestamp = angular.String.toDate(timestamp).getTime();
this.origDate = angular.String.toDate(timestamp);
timestamp = this.origDate.getTime();
if (isNaN(timestamp))
throw {
name: "Illegal Argument",
message: "Arg '" + tsStr + "' passed into TzDate constructor is not a valid date string"
};
} else {
this.origDate = new Date(timestamp);
}
var localOffset = new Date(timestamp).getTimezoneOffset();
@ -243,10 +248,34 @@ function TzDate(offset, timestamp) {
return offset * 60;
};
this.getUTCFullYear = function() {
return this.origDate.getUTCFullYear();
};
this.getUTCMonth = function() {
return this.origDate.getUTCMonth();
};
this.getUTCDate = function() {
return this.origDate.getUTCDate();
};
this.getUTCHours = function() {
return this.origDate.getUTCHours();
};
this.getUTCMinutes = function() {
return this.origDate.getUTCMinutes();
};
this.getUTCSeconds = function() {
return this.origDate.getUTCSeconds();
};
//hide all methods not implemented in this mock that the Date prototype exposes
var unimplementedMethods = ['getDay', 'getMilliseconds', 'getTime', 'getUTCDate', 'getUTCDay',
'getUTCFullYear', 'getUTCHours', 'getUTCMilliseconds', 'getUTCMinutes', 'getUTCMonth',
'getUTCSeconds', 'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
var unimplementedMethods = ['getDay', 'getMilliseconds', 'getTime', 'getUTCDay',
'getUTCMilliseconds', 'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear',
'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds',
'setYear', 'toDateString', 'toJSON', 'toGMTString', 'toLocaleFormat', 'toLocaleString',

View file

@ -96,4 +96,26 @@ describe('TzDate', function() {
expect(newYearInBratislava.getHours()).toBe(0);
expect(newYearInBratislava.getMinutes()).toBe(0);
});
it('should delegate all the UTC methods to the original UTC Date object', function() {
//from when created from string
var date1 = new TzDate(-1, '2009-12-31T23:00:00Z');
expect(date1.getUTCFullYear()).toBe(2009);
expect(date1.getUTCMonth()).toBe(11);
expect(date1.getUTCDate()).toBe(31);
expect(date1.getUTCHours()).toBe(23);
expect(date1.getUTCMinutes()).toBe(0);
expect(date1.getUTCSeconds()).toBe(0);
//from when created from millis
var date2 = new TzDate(-1, angular.String.toDate('2009-12-31T23:00:00Z').getTime());
expect(date2.getUTCFullYear()).toBe(2009);
expect(date2.getUTCMonth()).toBe(11);
expect(date2.getUTCDate()).toBe(31);
expect(date2.getUTCHours()).toBe(23);
expect(date2.getUTCMinutes()).toBe(0);
expect(date2.getUTCSeconds()).toBe(0);
});
});