Skip to content

Commit 09a8f53

Browse files
committed
fix(filter:date) Timezone formatting issues.
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.
1 parent e0295cf commit 09a8f53

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

src/ng/filter/filters.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,18 @@ function dateStrGetter(name, shortForm) {
212212

213213
function timeZoneGetter(date) {
214214
var offset = date.getTimezoneOffset();
215-
return padNumber(offset / 60, 2) + padNumber(Math.abs(offset % 60), 2);
215+
var zone = -1 * offset;
216+
var paddedZone;
217+
if (zone === 0) {
218+
return "Z";
219+
} else {
220+
paddedZone = padNumber(zone / 60, 2) + padNumber(Math.abs(zone % 60), 2);
221+
222+
if (zone > 0)
223+
paddedZone = "+"+paddedZone;
224+
225+
return paddedZone;
226+
}
216227
}
217228

218229
function ampmGetter(date, formats) {
@@ -319,7 +330,7 @@ var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZE']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+
319330
expect(binding("1288323623006 | date:'medium'")).
320331
toMatch(/Oct 2\d, 2010 \d{1,2}:\d{2}:\d{2} (AM|PM)/);
321332
expect(binding("1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'")).
322-
toMatch(/2010\-10\-2\d \d{2}:\d{2}:\d{2} \-?\d{4}/);
333+
toMatch(/2010\-10\-2\d \d{2}:\d{2}:\d{2} (\-|\+)?\d{4}/);
323334
expect(binding("'1288323623006' | date:'MM/dd/yyyy @ h:mma'")).
324335
toMatch(/10\/2\d\/2010 @ \d{1,2}:\d{2}(AM|PM)/);
325336
});

test/ng/filter/filtersSpec.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ describe('filters', function() {
193193
toEqual('10-09-03 07:05:08');
194194

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

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

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

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

210210
expect(date(earlyDate, "MMMM dd, y")).
211211
toEqual('September 03, 1');
212+
213+
});
214+
215+
it('should format timezones correctly (as per ISO_8601)', function() {
216+
//Note: TzDate's first argument is offset, _not_ timezone.
217+
var utc = new angular.mock.TzDate( 0, '2010-09-03T12:05:08.000Z');
218+
var eastOfUTC = new angular.mock.TzDate(-5, '2010-09-03T12:05:08.000Z');
219+
var westOfUTC = new angular.mock.TzDate(+5, '2010-09-03T12:05:08.000Z');
220+
221+
expect(date(utc, "yyyy-MM-ddTHH:mm:ssZ")).
222+
toEqual('2010-09-03T12:05:08Z')
223+
224+
expect(date(eastOfUTC, "yyyy-MM-ddTHH:mm:ssZ")).
225+
toEqual('2010-09-03T17:05:08+0500')
226+
227+
expect(date(westOfUTC, "yyyy-MM-ddTHH:mm:ssZ")).
228+
toEqual('2010-09-03T07:05:08-0500')
229+
212230
});
213231

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

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

224244
it('should accept default formats', function() {
@@ -274,6 +294,7 @@ describe('filters', function() {
274294
});
275295

276296

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

@@ -298,5 +319,7 @@ describe('filters', function() {
298319
expect(date('2003-09-10T13:02:03.12Z', format)).toEqual('2003-09-10');
299320
expect(date('2003-09-10T13:02:03.1Z', format)).toEqual('2003-09-10');
300321
});
322+
323+
301324
});
302325
});

0 commit comments

Comments
 (0)