From 5ac14f633a69f49973b5512780c6ec7752405967 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 16 Mar 2012 11:28:32 -0700 Subject: 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 --- src/JSON.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/JSON.js') diff --git a/src/JSON.js b/src/JSON.js index 983a53e8..83d1e927 100644 --- a/src/JSON.js +++ b/src/JSON.js @@ -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) { @@ -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){ -- cgit v1.2.3