Skip to content

Commit

Permalink
fix(filter:date) Timezone formatting issues.
Browse files Browse the repository at this point in the history
This commit fixes angular#1261 and angular#1532. This covers
two separate issues:

- Positive timezones were being formatted without
a leading `+` resulting in a formatting string
like: "HH:MM:ssZ" giving "12:13:141000" instead
of "12:13:14+1000". Fixed by checking if timezone
is > 0 and adding a leading "+".

- Timezone output signs were inverted.
mock.TzDate expects the timezone _offset_ as it's
first argument, _not_ the timezone. This means
that a mock.TzDate with a positive offset should
result in a date string with a negative timezone,
and vice-versa.
  • Loading branch information
latentflip committed Jan 30, 2013
1 parent e0295cf commit 09a8f53
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,18 @@ function dateStrGetter(name, shortForm) {

function timeZoneGetter(date) {
var offset = date.getTimezoneOffset();
return padNumber(offset / 60, 2) + padNumber(Math.abs(offset % 60), 2);
var zone = -1 * offset;
var paddedZone;
if (zone === 0) {
return "Z";
} else {
paddedZone = padNumber(zone / 60, 2) + padNumber(Math.abs(zone % 60), 2);

if (zone > 0)
paddedZone = "+"+paddedZone;

return paddedZone;
}
}

function ampmGetter(date, formats) {
Expand Down Expand Up @@ -319,7 +330,7 @@ var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZE']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+
expect(binding("1288323623006 | date:'medium'")).
toMatch(/Oct 2\d, 2010 \d{1,2}:\d{2}:\d{2} (AM|PM)/);
expect(binding("1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'")).
toMatch(/2010\-10\-2\d \d{2}:\d{2}:\d{2} \-?\d{4}/);
toMatch(/2010\-10\-2\d \d{2}:\d{2}:\d{2} (\-|\+)?\d{4}/);
expect(binding("'1288323623006' | date:'MM/dd/yyyy @ h:mma'")).
toMatch(/10\/2\d\/2010 @ \d{1,2}:\d{2}(AM|PM)/);
});
Expand Down
33 changes: 28 additions & 5 deletions test/ng/filter/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ describe('filters', function() {
toEqual('10-09-03 07:05:08');

expect(date(midnight, "yyyy-M-d h=H:m:saZ")).
toEqual('2010-9-3 12=0:5:8AM0500');
toEqual('2010-9-3 12=0:5:8AM-0500');

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

expect(date(noon, "yyyy-MM-dd hh=HH:mm:ssaZ")).
toEqual('2010-09-03 12=12:05:08PM0500');
toEqual('2010-09-03 12=12:05:08PM-0500');

expect(date(noon, "EEE, MMM d, yyyy")).
toEqual('Fri, Sep 3, 2010');
Expand All @@ -209,16 +209,36 @@ describe('filters', function() {

expect(date(earlyDate, "MMMM dd, y")).
toEqual('September 03, 1');

});

it('should format timezones correctly (as per ISO_8601)', function() {
//Note: TzDate's first argument is offset, _not_ timezone.
var utc = new angular.mock.TzDate( 0, '2010-09-03T12:05:08.000Z');
var eastOfUTC = new angular.mock.TzDate(-5, '2010-09-03T12:05:08.000Z');
var westOfUTC = new angular.mock.TzDate(+5, '2010-09-03T12:05:08.000Z');

expect(date(utc, "yyyy-MM-ddTHH:mm:ssZ")).
toEqual('2010-09-03T12:05:08Z')

expect(date(eastOfUTC, "yyyy-MM-ddTHH:mm:ssZ")).
toEqual('2010-09-03T17:05:08+0500')

expect(date(westOfUTC, "yyyy-MM-ddTHH:mm:ssZ")).
toEqual('2010-09-03T07:05:08-0500')

});



it('should treat single quoted strings as string literals', function() {
expect(date(midnight, "yyyy'de' 'a'x'dd' 'adZ' h=H:m:saZ")).
toEqual('2010de axdd adZ 12=0:5:8AM0500');
toEqual('2010de axdd adZ 12=0:5:8AM-0500');
});

it('should treat a sequence of two single quotes as a literal single quote', function() {
expect(date(midnight, "yyyy'de' 'a''dd' 'adZ' h=H:m:saZ")).
toEqual("2010de a'dd adZ 12=0:5:8AM0500");
toEqual("2010de a'dd adZ 12=0:5:8AM-0500");
});

it('should accept default formats', function() {
Expand Down Expand Up @@ -274,6 +294,7 @@ describe('filters', function() {
});



it('should parse iso8061 date strings without timezone as local time', function() {
var format = 'yyyy-MM-dd HH-mm-ss';

Expand All @@ -298,5 +319,7 @@ describe('filters', function() {
expect(date('2003-09-10T13:02:03.12Z', format)).toEqual('2003-09-10');
expect(date('2003-09-10T13:02:03.1Z', format)).toEqual('2003-09-10');
});


});
});

0 comments on commit 09a8f53

Please sign in to comment.