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

Commit 5ac14f6

Browse files
committed
fix(json): added support for iso8061 timezone
Added support of timezone in dates not just zulu timezone. This fixes issues for date filter which uses json deserialization under the hood. (for now) Closes #/800
1 parent 9918b74 commit 5ac14f6

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

src/Angular.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ var $boolean = 'boolean',
7373
angularModule,
7474
/** @name angular.module.ng */
7575
nodeName_,
76-
uid = ['0', '0', '0'],
77-
DATE_ISOSTRING_LN = 24;
76+
uid = ['0', '0', '0'];
7877

7978
/**
8079
* @ngdoc function

src/JSON.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function fromJson(json, useNative) {
4747
// TODO make forEach optionally recursive and remove this function
4848
// TODO(misko): remove this once the $http service is checked in.
4949
function transformDates(obj) {
50-
if (isString(obj) && obj.length === DATE_ISOSTRING_LN) {
50+
if (isString(obj) && 15 <= obj.length && obj.length <= 24) {
5151
return jsonStringToDate(obj);
5252
} else if (isArray(obj) || isObject(obj)) {
5353
forEach(obj, function(val, name) {
@@ -58,16 +58,25 @@ function fromJson(json, useNative) {
5858
}
5959
}
6060

61-
var R_ISO8061_STR = /^(\d{4})-(\d\d)-(\d\d)(?:T(\d\d)(?:\:(\d\d)(?:\:(\d\d)(?:\.(\d{3}))?)?)?Z)?$/;
61+
var R_ISO8061_STR = /^(\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?:\:?(\d\d)(?:\:?(\d\d)(?:\.(\d{3}))?)?)?(Z|([+-])(\d\d):?(\d\d)))?$/;
6262
function jsonStringToDate(string){
6363
var match;
64-
if (isString(string) && (match = string.match(R_ISO8061_STR))){
65-
var date = new Date(0);
66-
date.setUTCFullYear(match[1], match[2] - 1, match[3]);
67-
date.setUTCHours(match[4]||0, match[5]||0, match[6]||0, match[7]||0);
64+
if (match = string.match(R_ISO8061_STR)) {
65+
var date = new Date(0),
66+
tzHour = 0,
67+
tzMin = 0;
68+
if (match[9]) {
69+
tzHour = int(match[9] + match[10]);
70+
tzMin = int(match[9] + match[11]);
71+
}
72+
date.setUTCFullYear(int(match[1]), int(match[2]) - 1, int(match[3]));
73+
date.setUTCHours(int(match[4]||0) - tzHour, int(match[5]||0) - tzMin, int(match[6]||0), int(match[7]||0));
6874
return date;
6975
}
7076
return string;
77+
function int(str) {
78+
return parseInt(str, 10);
79+
}
7180
}
7281

7382
function jsonDateToString(date){

test/JsonSpec.js

+29-27
Original file line numberDiff line numberDiff line change
@@ -220,42 +220,44 @@ describe('json', function() {
220220
});
221221

222222

223-
it('should read/write to date', function() {
224-
var date = new Date('Sep 10 2003 13:02:03 GMT');
225-
expect(jsonDateToString(date)).toBe('2003-09-10T13:02:03.000Z');
226-
expect(jsonStringToDate(jsonDateToString(date)).getTime()).toBe(date.getTime());
227-
});
223+
describe('iso 8061 date', function() {
224+
it('should read/write to date', function() {
225+
var date = new Date('Sep 10 2003 13:02:03 GMT');
226+
expect(jsonDateToString(date)).toBe('2003-09-10T13:02:03.000Z');
227+
expect(jsonStringToDate(jsonDateToString(date)).getTime()).toBe(date.getTime());
228+
});
228229

229230

230-
it('should convert to date', function() {
231-
//full ISO8061
232-
expect(jsonStringToDate('2003-09-10T13:02:03.000Z')).
233-
toEqual(new Date('Sep 10 2003 13:02:03 GMT'));
231+
it('should convert to date', function() {
232+
//full ISO8061
233+
expect(jsonStringToDate('2003-09-10T13:02:03.000Z')).toEqual(new Date('Sep 10 2003 13:02:03 GMT'));
234234

235-
//no millis
236-
expect(jsonStringToDate('2003-09-10T13:02:03Z')).
237-
toEqual(new Date('Sep 10 2003 13:02:03 GMT'));
235+
expect(jsonStringToDate('2003-09-10T13:02:03.000+00:00')).toEqual(new Date('Sep 10 2003 13:02:03 GMT'));
238236

239-
//no seconds
240-
expect(jsonStringToDate('2003-09-10T13:02Z')).
241-
toEqual(new Date('Sep 10 2003 13:02:00 GMT'));
237+
expect(jsonStringToDate('20030910T033203-0930')).toEqual(new Date('Sep 10 2003 13:02:03 GMT'));
242238

243-
//no minutes
244-
expect(jsonStringToDate('2003-09-10T13Z')).
245-
toEqual(new Date('Sep 10 2003 13:00:00 GMT'));
239+
//no millis
240+
expect(jsonStringToDate('2003-09-10T13:02:03Z')).toEqual(new Date('Sep 10 2003 13:02:03 GMT'));
246241

247-
//no time
248-
expect(jsonStringToDate('2003-09-10')).
249-
toEqual(new Date('Sep 10 2003 00:00:00 GMT'));
250-
});
242+
//no seconds
243+
expect(jsonStringToDate('2003-09-10T13:02Z')).toEqual(new Date('Sep 10 2003 13:02:00 GMT'));
251244

245+
//no minutes
246+
expect(jsonStringToDate('2003-09-10T13Z')).toEqual(new Date('Sep 10 2003 13:00:00 GMT'));
252247

253-
it('should parse date', function() {
254-
var date = jsonStringToDate('2003-09-10T13:02:03.000Z');
255-
expect(jsonDateToString(date)).toBe('2003-09-10T13:02:03.000Z');
256-
expect(jsonStringToDate('str')).toBe('str');
257-
});
248+
//no time
249+
expect(jsonStringToDate('2003-09-10')).toEqual(new Date('Sep 10 2003 00:00:00 GMT'));
258250

251+
expect(jsonStringToDate('2011-12-28T13:02:09-08:00')).toEqual(new Date('Dec 28 2011 21:02:09 GMT'));
252+
});
253+
254+
255+
it('should parse date', function() {
256+
var date = jsonStringToDate('2003-09-10T13:02:03.000Z');
257+
expect(jsonDateToString(date)).toBe('2003-09-10T13:02:03.000Z');
258+
expect(jsonStringToDate('str')).toBe('str');
259+
});
260+
});
259261

260262
describe('string', function() {
261263
it('should quote', function() {

0 commit comments

Comments
 (0)