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

Commit 4f2e360

Browse files
kevin-wells-iq4bispetebacondarwin
authored andcommitted
fix(date): correctly format dates with more than 3 sub-second digits
This date {{2003-09-10T13:02:03.123456Z | date: yyyy-mm-dd ss} is now treated as having 123.45ms. Previously it had 123456ms so 123 seconds were added to the formatted date. Use local date in unit tests so they work in any time zone
1 parent 4622af3 commit 4f2e360

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/ng/filter/filters.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,11 @@ function dateFilter($locale) {
356356
tzMin = int(match[9] + match[11]);
357357
}
358358
dateSetter.call(date, int(match[1]), int(match[2]) - 1, int(match[3]));
359-
timeSetter.call(date, int(match[4]||0) - tzHour, int(match[5]||0) - tzMin, int(match[6]||0), int(match[7]||0));
359+
var h = int(match[4]||0) - tzHour;
360+
var m = int(match[5]||0) - tzMin
361+
var s = int(match[6]||0);
362+
var ms = Math.round(parseFloat('0.' + (match[7]||0)) * 1000);
363+
timeSetter.call(date, h, m, s, ms);
360364
return date;
361365
}
362366
return string;

test/ng/filter/filtersSpec.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -300,21 +300,23 @@ describe('filters', function() {
300300
it('should support various iso8061 date strings with timezone as input', function() {
301301
var format = 'yyyy-MM-dd ss';
302302

303+
var localDay = new Date(Date.UTC(2003, 9, 10, 13, 2, 3, 0)).getDate();
304+
303305
//full ISO8061
304-
expect(date('2003-09-10T13:02:03.000Z', format)).toEqual('2003-09-10 03');
306+
expect(date('2003-09-10T13:02:03.000Z', format)).toEqual('2003-09-' + localDay + ' 03');
305307

306-
expect(date('2003-09-10T13:02:03.000+00:00', format)).toEqual('2003-09-10 03');
308+
expect(date('2003-09-10T13:02:03.000+00:00', format)).toEqual('2003-09-' + localDay + ' 03');
307309

308-
expect(date('20030910T033203-0930', format)).toEqual('2003-09-10 03');
310+
expect(date('20030910T033203-0930', format)).toEqual('2003-09-' + localDay + ' 03');
309311

310312
//no millis
311-
expect(date('2003-09-10T13:02:03Z', format)).toEqual('2003-09-10 03');
313+
expect(date('2003-09-10T13:02:03Z', format)).toEqual('2003-09-' + localDay + ' 03');
312314

313315
//no seconds
314-
expect(date('2003-09-10T13:02Z', format)).toEqual('2003-09-10 00');
316+
expect(date('2003-09-10T13:02Z', format)).toEqual('2003-09-' + localDay + ' 00');
315317

316318
//no minutes
317-
expect(date('2003-09-10T13Z', format)).toEqual('2003-09-10 00');
319+
expect(date('2003-09-10T13Z', format)).toEqual('2003-09-' + localDay + ' 00');
318320
});
319321

320322

@@ -331,16 +333,18 @@ describe('filters', function() {
331333
});
332334

333335
it('should support different degrees of subsecond precision', function () {
334-
var format = 'yyyy-MM-dd';
335-
336-
expect(date('2003-09-10T13:02:03.12345678Z', format)).toEqual('2003-09-10');
337-
expect(date('2003-09-10T13:02:03.1234567Z', format)).toEqual('2003-09-10');
338-
expect(date('2003-09-10T13:02:03.123456Z', format)).toEqual('2003-09-10');
339-
expect(date('2003-09-10T13:02:03.12345Z', format)).toEqual('2003-09-10');
340-
expect(date('2003-09-10T13:02:03.1234Z', format)).toEqual('2003-09-10');
341-
expect(date('2003-09-10T13:02:03.123Z', format)).toEqual('2003-09-10');
342-
expect(date('2003-09-10T13:02:03.12Z', format)).toEqual('2003-09-10');
343-
expect(date('2003-09-10T13:02:03.1Z', format)).toEqual('2003-09-10');
336+
var format = 'yyyy-MM-dd ss';
337+
338+
var localDay = new Date(Date.UTC(2003, 9-1, 10, 13, 2, 3, 123)).getDate();
339+
340+
expect(date('2003-09-10T13:02:03.12345678Z', format)).toEqual('2003-09-' + localDay + ' 03');
341+
expect(date('2003-09-10T13:02:03.1234567Z', format)).toEqual('2003-09-' + localDay + ' 03');
342+
expect(date('2003-09-10T13:02:03.123456Z', format)).toEqual('2003-09-' + localDay + ' 03');
343+
expect(date('2003-09-10T13:02:03.12345Z', format)).toEqual('2003-09-' + localDay + ' 03');
344+
expect(date('2003-09-10T13:02:03.1234Z', format)).toEqual('2003-09-' + localDay + ' 03');
345+
expect(date('2003-09-10T13:02:03.123Z', format)).toEqual('2003-09-' + localDay + ' 03');
346+
expect(date('2003-09-10T13:02:03.12Z', format)).toEqual('2003-09-' + localDay + ' 03');
347+
expect(date('2003-09-10T13:02:03.1Z', format)).toEqual('2003-09-' + localDay + ' 03');
344348
});
345349
});
346350
});

0 commit comments

Comments
 (0)