From: Tim Wood Date: Wed, 25 Jul 2012 17:12:53 +0000 (-0700) Subject: #371 don't add timezone to string + format in moment.utc if it already has a timezone X-Git-Tag: 1.7.0~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cfa347ebc54f51f9f3e094fb35791eff180456fe;p=thirdparty%2Fmoment.git #371 don't add timezone to string + format in moment.utc if it already has a timezone --- diff --git a/moment.js b/moment.js index c959ab31d..78270bf91 100644 --- a/moment.js +++ b/moment.js @@ -664,9 +664,14 @@ if (isArray(input)) { return new Moment(dateFromArray(input, true), true); } - return (format && input) ? - moment(input + ' +0000', format + ' Z').utc() : - moment(input && isoRegex.exec(input) && !parseTokenTimezone.exec(input) ? input + '+0000' : input).utc(); + // if we don't have a timezone, we need to add one to trigger parsing into utc + if (typeof input === 'string' && !parseTokenTimezone.exec(input)) { + input += ' +0000'; + if (format) { + format += ' Z'; + } + } + return moment(input, format).utc(); }; // creating with unix timestamp (in seconds) diff --git a/test/moment/create.js b/test/moment/create.js index a6686ae9b..bb58952c5 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -171,7 +171,7 @@ exports.create = { }, "string with format (timezone offset)" : function(test) { - test.expect(3); + test.expect(4); var a = new Date(Date.UTC(2011, 0, 1, 1)); var b = moment('2011 1 1 0 -01:00', 'YYYY MM DD HH Z'); test.equal(a.getHours(), b.hours(), 'date created with utc == parsed string with timezone offset'); @@ -179,6 +179,9 @@ exports.create = { var c = moment('2011 2 1 10 -05:00', 'YYYY MM DD HH Z'); var d = moment('2011 2 1 8 -07:00', 'YYYY MM DD HH Z'); test.equal(c.hours(), d.hours(), '10 am central time == 8 am pacific time'); + var e = moment.utc('Fri, 20 Jul 2012 17:15:00', 'ddd, DD MMM YYYY HH:mm:ss'); + var f = moment.utc('Fri, 20 Jul 2012 10:15:00 -0700', 'ddd, DD MMM YYYY HH:mm:ss ZZ'); + test.equal(e.hours(), f.hours(), 'parse timezone offset in utc'); test.done(); },