Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit df744f3

Browse files
Maxim GrachIgorMinar
Maxim Grach
authored andcommitted
feat(dateFilter): add [.,]sss formatter for milliseconds
Also Implement getMilliseconds() method of TzDate and add test for this in ngMock.
1 parent 8155c3a commit df744f3

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/ng/filter/filters.js

+4
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ var DATE_FORMATS = {
241241
m: dateGetter('Minutes', 1),
242242
ss: dateGetter('Seconds', 2),
243243
s: dateGetter('Seconds', 1),
244+
// while ISO 8601 requires fractions to be prefixed with `.` or `,`
245+
// we can be just safely rely on using `sss` since we currently don't support single or two digit fractions
246+
sss: dateGetter('Milliseconds', 3),
244247
EEEE: dateStrGetter('Day'),
245248
EEE: dateStrGetter('Day', true),
246249
a: ampmGetter,
@@ -279,6 +282,7 @@ var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZE']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+
279282
* * `'m'`: Minute in hour (0-59)
280283
* * `'ss'`: Second in minute, padded (00-59)
281284
* * `'s'`: Second in minute (0-59)
285+
* * `'.sss' or ',sss'`: Millisecond in second, padded (000-999)
282286
* * `'a'`: am/pm marker
283287
* * `'Z'`: 4 digit (+sign) representation of the timezone offset (-1200-1200)
284288
*

src/ngMock/angular-mocks.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ angular.mock.$LogProvider = function() {
456456
* newYearInBratislava.getDate() => 1;
457457
* newYearInBratislava.getHours() => 0;
458458
* newYearInBratislava.getMinutes() => 0;
459+
* newYearInBratislava.getSeconds() => 0;
459460
* </pre>
460461
*
461462
*/
@@ -512,6 +513,10 @@ angular.mock.$LogProvider = function() {
512513
return self.date.getSeconds();
513514
};
514515

516+
self.getMilliseconds = function() {
517+
return self.date.getMilliseconds();
518+
};
519+
515520
self.getTimezoneOffset = function() {
516521
return offset * 60;
517522
};
@@ -562,7 +567,7 @@ angular.mock.$LogProvider = function() {
562567
}
563568

564569
//hide all methods not implemented in this mock that the Date prototype exposes
565-
var unimplementedMethods = ['getMilliseconds', 'getUTCDay',
570+
var unimplementedMethods = ['getUTCDay',
566571
'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
567572
'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear',
568573
'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds',

test/ng/filter/filtersSpec.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ describe('filters', function() {
162162

163163
describe('date', function() {
164164

165-
var morning = new angular.mock.TzDate(+5, '2010-09-03T12:05:08.000Z'); //7am
166-
var noon = new angular.mock.TzDate(+5, '2010-09-03T17:05:08.000Z'); //12pm
167-
var midnight = new angular.mock.TzDate(+5, '2010-09-03T05:05:08.000Z'); //12am
165+
var morning = new angular.mock.TzDate(+5, '2010-09-03T12:05:08.001Z'); //7am
166+
var noon = new angular.mock.TzDate(+5, '2010-09-03T17:05:08.012Z'); //12pm
167+
var midnight = new angular.mock.TzDate(+5, '2010-09-03T05:05:08.123Z'); //12am
168168
var earlyDate = new angular.mock.TzDate(+5, '0001-09-03T05:05:08.000Z');
169169

170170
var date;
@@ -192,15 +192,24 @@ describe('filters', function() {
192192
expect(date(morning, "yy-MM-dd HH:mm:ss")).
193193
toEqual('10-09-03 07:05:08');
194194

195+
expect(date(morning, "yy-MM-dd HH:mm:ss.sss")).
196+
toEqual('10-09-03 07:05:08.001');
197+
195198
expect(date(midnight, "yyyy-M-d h=H:m:saZ")).
196199
toEqual('2010-9-3 12=0:5:8AM-0500');
197200

198201
expect(date(midnight, "yyyy-MM-dd hh=HH:mm:ssaZ")).
199202
toEqual('2010-09-03 12=00:05:08AM-0500');
200203

204+
expect(date(midnight, "yyyy-MM-dd hh=HH:mm:ss.sssaZ")).
205+
toEqual('2010-09-03 12=00:05:08.123AM-0500');
206+
201207
expect(date(noon, "yyyy-MM-dd hh=HH:mm:ssaZ")).
202208
toEqual('2010-09-03 12=12:05:08PM-0500');
203209

210+
expect(date(noon, "yyyy-MM-dd hh=HH:mm:ss.sssaZ")).
211+
toEqual('2010-09-03 12=12:05:08.012PM-0500');
212+
204213
expect(date(noon, "EEE, MMM d, yyyy")).
205214
toEqual('Fri, Sep 3, 2010');
206215

test/ngMock/angular-mocksSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ describe('ngMock', function() {
109109
});
110110

111111

112+
it('should fake getMilliseconds method', function() {
113+
expect(new angular.mock.TzDate(0, '2010-09-03T23:05:08.003Z').getMilliseconds()).toBe(3);
114+
expect(new angular.mock.TzDate(0, '2010-09-03T23:05:08.023Z').getMilliseconds()).toBe(23);
115+
expect(new angular.mock.TzDate(0, '2010-09-03T23:05:08.123Z').getMilliseconds()).toBe(123);
116+
});
117+
118+
112119
it('should create a date representing new year in Bratislava', function() {
113120
var newYearInBratislava = new angular.mock.TzDate(-1, '2009-12-31T23:00:00.000Z');
114121
expect(newYearInBratislava.getTimezoneOffset()).toBe(-60);
@@ -117,6 +124,7 @@ describe('ngMock', function() {
117124
expect(newYearInBratislava.getDate()).toBe(1);
118125
expect(newYearInBratislava.getHours()).toBe(0);
119126
expect(newYearInBratislava.getMinutes()).toBe(0);
127+
expect(newYearInBratislava.getSeconds()).toBe(0);
120128
});
121129

122130

0 commit comments

Comments
 (0)