From: Tim Wood Date: Sat, 9 Mar 2013 21:11:59 +0000 (-0800) Subject: adding tests for parsing strings for timezone info X-Git-Tag: 2.1.0~32^2~2^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=96e949b75bd2b035c36012dcad260ee055b949f1;p=thirdparty%2Fmoment.git adding tests for parsing strings for timezone info --- diff --git a/moment.js b/moment.js index 5be161c0b..551fba071 100644 --- a/moment.js +++ b/moment.js @@ -631,6 +631,14 @@ } } + function timezoneMinutesFromString(string) { + var tzchunk = (parseTokenTimezone.exec(string) || [])[0], + parts = (tzchunk + '').match(parseTimezoneChunker) || ['-', 0, 0], + minutes = +(parts[1] * 60) + ~~parts[2]; + + return parts[0] === '+' ? -minutes : minutes; + } + // function to convert string input to date function addTimeToArrayFromToken(token, input, config) { var a, b, @@ -705,18 +713,7 @@ case 'Z' : // fall through to ZZ case 'ZZ' : config._useUTC = true; - a = (input + '').match(parseTimezoneChunker); - if (a && a[1]) { - config._tzh = ~~a[1]; - } - if (a && a[2]) { - config._tzm = ~~a[2]; - } - // reverse offsets - if (a && a[0] === '+') { - config._tzh = -config._tzh; - config._tzm = -config._tzm; - } + config._tzm = timezoneMinutesFromString(input); break; } @@ -742,8 +739,8 @@ } // add the offsets to the time to be parsed so that we can have a clean array for checking isValid - input[3] += config._tzh || 0; - input[4] += config._tzm || 0; + input[3] += ~~((config._tzm || 0) / 60); + input[4] += ~~((config._tzm || 0) % 60); date = new Date(0); @@ -1265,6 +1262,9 @@ zone : function (input) { var offset = this._offset || 0; if (input != null) { + if (typeof input === "string") { + input = timezoneMinutesFromString(input); + } this._offset = input; this._isUTC = true; if (offset !== input) { diff --git a/test/moment/zones.js b/test/moment/zones.js index 622d94bbc..7cb2b3a6d 100644 --- a/test/moment/zones.js +++ b/test/moment/zones.js @@ -26,6 +26,21 @@ exports.zones = { test.done(); }, + "set zone with string" : function (test) { + var zone = moment(); + + zone.zone("+00:00"); + test.equal(zone.zone(), 0, "set the zone with a timezone string"); + + zone.zone("2013-03-07T07:00:00-08:00"); + test.equal(zone.zone(), 480, "set the zone with a string that does not begin with the timezone"); + + zone.zone("2013-03-07T07:00:00+0100"); + test.equal(zone.zone(), -60, "set the zone with a string that uses the +0000 syntax"); + + test.done(); + }, + "change hours when changing the zone" : function (test) { var zone = moment.utc([2000, 0, 1, 6]);