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

Commit

Permalink
fix(json): added support for iso8061 timezone
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mhevery committed Mar 19, 2012
1 parent 9918b74 commit 5ac14f6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
3 changes: 1 addition & 2 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ var $boolean = 'boolean',
angularModule,
/** @name angular.module.ng */
nodeName_,
uid = ['0', '0', '0'],
DATE_ISOSTRING_LN = 24;
uid = ['0', '0', '0'];

/**
* @ngdoc function
Expand Down
21 changes: 15 additions & 6 deletions src/JSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function fromJson(json, useNative) {
// TODO make forEach optionally recursive and remove this function
// TODO(misko): remove this once the $http service is checked in.
function transformDates(obj) {
if (isString(obj) && obj.length === DATE_ISOSTRING_LN) {
if (isString(obj) && 15 <= obj.length && obj.length <= 24) {
return jsonStringToDate(obj);
} else if (isArray(obj) || isObject(obj)) {
forEach(obj, function(val, name) {
Expand All @@ -58,16 +58,25 @@ function fromJson(json, useNative) {
}
}

var R_ISO8061_STR = /^(\d{4})-(\d\d)-(\d\d)(?:T(\d\d)(?:\:(\d\d)(?:\:(\d\d)(?:\.(\d{3}))?)?)?Z)?$/;
var R_ISO8061_STR = /^(\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?:\:?(\d\d)(?:\:?(\d\d)(?:\.(\d{3}))?)?)?(Z|([+-])(\d\d):?(\d\d)))?$/;
function jsonStringToDate(string){
var match;
if (isString(string) && (match = string.match(R_ISO8061_STR))){
var date = new Date(0);
date.setUTCFullYear(match[1], match[2] - 1, match[3]);
date.setUTCHours(match[4]||0, match[5]||0, match[6]||0, match[7]||0);
if (match = string.match(R_ISO8061_STR)) {
var date = new Date(0),
tzHour = 0,
tzMin = 0;
if (match[9]) {
tzHour = int(match[9] + match[10]);
tzMin = int(match[9] + match[11]);
}
date.setUTCFullYear(int(match[1]), int(match[2]) - 1, int(match[3]));
date.setUTCHours(int(match[4]||0) - tzHour, int(match[5]||0) - tzMin, int(match[6]||0), int(match[7]||0));
return date;
}
return string;
function int(str) {
return parseInt(str, 10);
}
}

function jsonDateToString(date){
Expand Down
56 changes: 29 additions & 27 deletions test/JsonSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,42 +220,44 @@ describe('json', function() {
});


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


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

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

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

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

//no time
expect(jsonStringToDate('2003-09-10')).
toEqual(new Date('Sep 10 2003 00:00:00 GMT'));
});
//no seconds
expect(jsonStringToDate('2003-09-10T13:02Z')).toEqual(new Date('Sep 10 2003 13:02:00 GMT'));

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

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

expect(jsonStringToDate('2011-12-28T13:02:09-08:00')).toEqual(new Date('Dec 28 2011 21:02:09 GMT'));
});


it('should parse date', function() {
var date = jsonStringToDate('2003-09-10T13:02:03.000Z');
expect(jsonDateToString(date)).toBe('2003-09-10T13:02:03.000Z');
expect(jsonStringToDate('str')).toBe('str');
});
});

describe('string', function() {
it('should quote', function() {
Expand Down

0 comments on commit 5ac14f6

Please sign in to comment.