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

Commit b001c8e

Browse files
latentflipIgorMinar
authored andcommitted
fix(date): invert timezone sign and always display sign
This commit fixes #1261 and #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. Closes #1261, #1532
1 parent bec4435 commit b001c8e

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/ng/filter/filters.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,12 @@ function dateStrGetter(name, shortForm) {
211211
}
212212

213213
function timeZoneGetter(date) {
214-
var offset = date.getTimezoneOffset();
215-
return padNumber(offset / 60, 2) + padNumber(Math.abs(offset % 60), 2);
214+
var zone = -1 * date.getTimezoneOffset();
215+
var paddedZone = (zone >= 0) ? "+" : "";
216+
217+
paddedZone += padNumber(zone / 60, 2) + padNumber(Math.abs(zone % 60), 2);
218+
219+
return paddedZone;
216220
}
217221

218222
function ampmGetter(date, formats) {
@@ -319,7 +323,7 @@ var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZE']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+
319323
expect(binding("1288323623006 | date:'medium'")).
320324
toMatch(/Oct 2\d, 2010 \d{1,2}:\d{2}:\d{2} (AM|PM)/);
321325
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}/);
326+
toMatch(/2010\-10\-2\d \d{2}:\d{2}:\d{2} (\-|\+)?\d{4}/);
323327
expect(binding("'1288323623006' | date:'MM/dd/yyyy @ h:mma'")).
324328
toMatch(/10\/2\d\/2010 @ \d{1,2}:\d{2}(AM|PM)/);
325329
});

test/ng/filter/filtersSpec.js

+21-5
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');
@@ -211,14 +211,30 @@ describe('filters', function() {
211211
toEqual('September 03, 1');
212212
});
213213

214+
it('should format timezones correctly (as per ISO_8601)', function() {
215+
//Note: TzDate's first argument is offset, _not_ timezone.
216+
var utc = new angular.mock.TzDate( 0, '2010-09-03T12:05:08.000Z');
217+
var eastOfUTC = new angular.mock.TzDate(-5, '2010-09-03T12:05:08.000Z');
218+
var westOfUTC = new angular.mock.TzDate(+5, '2010-09-03T12:05:08.000Z');
219+
220+
expect(date(utc, "yyyy-MM-ddTHH:mm:ssZ")).
221+
toEqual('2010-09-03T12:05:08+0000')
222+
223+
expect(date(eastOfUTC, "yyyy-MM-ddTHH:mm:ssZ")).
224+
toEqual('2010-09-03T17:05:08+0500')
225+
226+
expect(date(westOfUTC, "yyyy-MM-ddTHH:mm:ssZ")).
227+
toEqual('2010-09-03T07:05:08-0500')
228+
});
229+
214230
it('should treat single quoted strings as string literals', function() {
215231
expect(date(midnight, "yyyy'de' 'a'x'dd' 'adZ' h=H:m:saZ")).
216-
toEqual('2010de axdd adZ 12=0:5:8AM0500');
232+
toEqual('2010de axdd adZ 12=0:5:8AM-0500');
217233
});
218234

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

224240
it('should accept default formats', function() {

0 commit comments

Comments
 (0)