Skip to content

Commit 9421674

Browse files
committed
fix(dateFilter): Correctly format BC years
- Correctly format BC years - Fix a function name collition - Allow TzDate to use BC years
1 parent ece8266 commit 9421674

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/ng/filter/filters.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,15 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
331331
}
332332
}
333333

334-
function padNumber(num, digits, trim) {
334+
function padNumber(num, digits, trim, negWrap) {
335335
var neg = '';
336-
if (num < 0) {
337-
neg = '-';
338-
num = -num;
336+
if (num < 0 || (negWrap && num <= 0)) {
337+
if (negWrap) {
338+
num = -num + 1;
339+
} else {
340+
num = -num;
341+
neg = '-';
342+
}
339343
}
340344
num = '' + num;
341345
while (num.length < digits) num = ZERO_CHAR + num;
@@ -346,15 +350,15 @@ function padNumber(num, digits, trim) {
346350
}
347351

348352

349-
function dateGetter(name, size, offset, trim) {
353+
function dateGetter(name, size, offset, trim, negWrap) {
350354
offset = offset || 0;
351355
return function(date) {
352356
var value = date['get' + name]();
353357
if (offset > 0 || value > -offset) {
354358
value += offset;
355359
}
356360
if (value === 0 && offset == -12) value = 12;
357-
return padNumber(value, size, trim);
361+
return padNumber(value, size, trim, negWrap);
358362
};
359363
}
360364

@@ -417,9 +421,9 @@ function longEraGetter(date, formats) {
417421
}
418422

419423
var DATE_FORMATS = {
420-
yyyy: dateGetter('FullYear', 4),
421-
yy: dateGetter('FullYear', 2, 0, true),
422-
y: dateGetter('FullYear', 1),
424+
yyyy: dateGetter('FullYear', 4, 0, false, true),
425+
yy: dateGetter('FullYear', 2, 0, true, true),
426+
y: dateGetter('FullYear', 1, 0, false, true),
423427
MMMM: dateStrGetter('Month'),
424428
MMM: dateStrGetter('Month', true),
425429
MM: dateGetter('Month', 2, 1),

src/ngMock/angular-mocks.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ angular.mock.$IntervalProvider = function() {
545545
* This directive should go inside the anonymous function but a bug in JSHint means that it would
546546
* not be enacted early enough to prevent the warning.
547547
*/
548-
var R_ISO8061_STR = /^(\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?:\:?(\d\d)(?:\:?(\d\d)(?:\.(\d{3}))?)?)?(Z|([+-])(\d\d):?(\d\d)))?$/;
548+
var R_ISO8061_STR = /^(-?\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?:\:?(\d\d)(?:\:?(\d\d)(?:\.(\d{3}))?)?)?(Z|([+-])(\d\d):?(\d\d)))?$/;
549549

550550
function jsonStringToDate(string) {
551551
var match;
@@ -571,7 +571,7 @@ function toInt(str) {
571571
return parseInt(str, 10);
572572
}
573573

574-
function padNumber(num, digits, trim) {
574+
function padNumberInMock(num, digits, trim) {
575575
var neg = '';
576576
if (num < 0) {
577577
neg = '-';
@@ -720,13 +720,13 @@ angular.mock.TzDate = function(offset, timestamp) {
720720
// provide this method only on browsers that already have it
721721
if (self.toISOString) {
722722
self.toISOString = function() {
723-
return padNumber(self.origDate.getUTCFullYear(), 4) + '-' +
724-
padNumber(self.origDate.getUTCMonth() + 1, 2) + '-' +
725-
padNumber(self.origDate.getUTCDate(), 2) + 'T' +
726-
padNumber(self.origDate.getUTCHours(), 2) + ':' +
727-
padNumber(self.origDate.getUTCMinutes(), 2) + ':' +
728-
padNumber(self.origDate.getUTCSeconds(), 2) + '.' +
729-
padNumber(self.origDate.getUTCMilliseconds(), 3) + 'Z';
723+
return padNumberInMock(self.origDate.getUTCFullYear(), 4) + '-' +
724+
padNumberInMock(self.origDate.getUTCMonth() + 1, 2) + '-' +
725+
padNumberInMock(self.origDate.getUTCDate(), 2) + 'T' +
726+
padNumberInMock(self.origDate.getUTCHours(), 2) + ':' +
727+
padNumberInMock(self.origDate.getUTCMinutes(), 2) + ':' +
728+
padNumberInMock(self.origDate.getUTCSeconds(), 2) + '.' +
729+
padNumberInMock(self.origDate.getUTCMilliseconds(), 3) + 'Z';
730730
};
731731
}
732732

test/ng/filter/filtersSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ describe('filters', function() {
272272
var noon = new angular.mock.TzDate(+5, '2010-09-03T17:05:08.012Z'); //12pm
273273
var midnight = new angular.mock.TzDate(+5, '2010-09-03T05:05:08.123Z'); //12am
274274
var earlyDate = new angular.mock.TzDate(+5, '0001-09-03T05:05:08.000Z');
275+
var year0Date = new angular.mock.TzDate(+5, '0000-12-25T05:05:08.000Z');
276+
var bcDate = new angular.mock.TzDate(+5, '-0026-01-16T05:05:08.000Z');
275277
var secondWeek = new angular.mock.TzDate(+5, '2013-01-11T12:00:00.000Z'); //Friday Jan 11, 2013
276278
var date;
277279

@@ -336,6 +338,15 @@ describe('filters', function() {
336338
expect(date(earlyDate, "MMMM dd, y")).
337339
toEqual('September 03, 1');
338340

341+
expect(date(earlyDate, "MMMM dd, yyyy")).
342+
toEqual('September 03, 0001');
343+
344+
expect(date(year0Date, "dd MMMM y G")).
345+
toEqual('25 December 1 BC');
346+
347+
expect(date(bcDate, "dd MMMM y G")).
348+
toEqual('16 January 27 BC');
349+
339350
expect(date(noon, "MMMM dd, y G")).
340351
toEqual('September 03, 2010 AD');
341352

0 commit comments

Comments
 (0)