Skip to content

Commit f077649

Browse files
committed
TzDate should support various UTC methods
1 parent f3ac2cd commit f077649

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

test/angular-mocks.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,17 @@ angular.service('$browser', function(){
195195
function TzDate(offset, timestamp) {
196196
if (angular.isString(timestamp)) {
197197
var tsStr = timestamp;
198-
timestamp = angular.String.toDate(timestamp).getTime();
198+
199+
this.origDate = angular.String.toDate(timestamp);
200+
201+
timestamp = this.origDate.getTime();
199202
if (isNaN(timestamp))
200203
throw {
201204
name: "Illegal Argument",
202205
message: "Arg '" + tsStr + "' passed into TzDate constructor is not a valid date string"
203206
};
207+
} else {
208+
this.origDate = new Date(timestamp);
204209
}
205210

206211
var localOffset = new Date(timestamp).getTimezoneOffset();
@@ -243,10 +248,34 @@ function TzDate(offset, timestamp) {
243248
return offset * 60;
244249
};
245250

251+
this.getUTCFullYear = function() {
252+
return this.origDate.getUTCFullYear();
253+
};
254+
255+
this.getUTCMonth = function() {
256+
return this.origDate.getUTCMonth();
257+
};
258+
259+
this.getUTCDate = function() {
260+
return this.origDate.getUTCDate();
261+
};
262+
263+
this.getUTCHours = function() {
264+
return this.origDate.getUTCHours();
265+
};
266+
267+
this.getUTCMinutes = function() {
268+
return this.origDate.getUTCMinutes();
269+
};
270+
271+
this.getUTCSeconds = function() {
272+
return this.origDate.getUTCSeconds();
273+
};
274+
275+
246276
//hide all methods not implemented in this mock that the Date prototype exposes
247-
var unimplementedMethods = ['getDay', 'getMilliseconds', 'getTime', 'getUTCDate', 'getUTCDay',
248-
'getUTCFullYear', 'getUTCHours', 'getUTCMilliseconds', 'getUTCMinutes', 'getUTCMonth',
249-
'getUTCSeconds', 'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
277+
var unimplementedMethods = ['getDay', 'getMilliseconds', 'getTime', 'getUTCDay',
278+
'getUTCMilliseconds', 'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
250279
'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear',
251280
'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds',
252281
'setYear', 'toDateString', 'toJSON', 'toGMTString', 'toLocaleFormat', 'toLocaleString',

test/angular-mocksSpec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,26 @@ describe('TzDate', function() {
9696
expect(newYearInBratislava.getHours()).toBe(0);
9797
expect(newYearInBratislava.getMinutes()).toBe(0);
9898
});
99+
100+
101+
it('should delegate all the UTC methods to the original UTC Date object', function() {
102+
//from when created from string
103+
var date1 = new TzDate(-1, '2009-12-31T23:00:00Z');
104+
expect(date1.getUTCFullYear()).toBe(2009);
105+
expect(date1.getUTCMonth()).toBe(11);
106+
expect(date1.getUTCDate()).toBe(31);
107+
expect(date1.getUTCHours()).toBe(23);
108+
expect(date1.getUTCMinutes()).toBe(0);
109+
expect(date1.getUTCSeconds()).toBe(0);
110+
111+
112+
//from when created from millis
113+
var date2 = new TzDate(-1, angular.String.toDate('2009-12-31T23:00:00Z').getTime());
114+
expect(date2.getUTCFullYear()).toBe(2009);
115+
expect(date2.getUTCMonth()).toBe(11);
116+
expect(date2.getUTCDate()).toBe(31);
117+
expect(date2.getUTCHours()).toBe(23);
118+
expect(date2.getUTCMinutes()).toBe(0);
119+
expect(date2.getUTCSeconds()).toBe(0);
120+
});
99121
});

0 commit comments

Comments
 (0)