]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
parseZone should handle UTC
authorIsaac Cambron <isaac@isaaccambron.com>
Sun, 23 Oct 2016 05:09:15 +0000 (01:09 -0400)
committerIskren Chernev <iskren.chernev@gmail.com>
Sun, 6 Nov 2016 09:32:40 +0000 (01:32 -0800)
src/lib/units/offset.js
src/test/moment/utc_offset.js

index 71d3954bf9e4cd340b0bb8fd2fe402a8dc5be897..f81b096b638b8d33cbdf35d65574364b4a33692a 100644 (file)
@@ -48,12 +48,19 @@ addParseToken(['Z', 'ZZ'], function (input, array, config) {
 var chunkOffset = /([\+\-]|\d\d)/gi;
 
 function offsetFromString(matcher, string) {
-    var matches = ((string || '').match(matcher) || []);
+    var matches = (string || '').match(matcher);
+
+    if (matches === null) {
+        return null;
+    }
+
     var chunk   = matches[matches.length - 1] || [];
     var parts   = (chunk + '').match(chunkOffset) || ['-', 0, 0];
     var minutes = +(parts[1] * 60) + toInt(parts[2]);
 
-    return parts[0] === '+' ? minutes : -minutes;
+    return minutes === 0 ?
+      0 :
+      parts[0] === '+' ? minutes : -minutes;
 }
 
 // Return a moment from input, that is local/utc/zone equivalent to model.
@@ -104,6 +111,9 @@ export function getSetOffset (input, keepLocalTime) {
     if (input != null) {
         if (typeof input === 'string') {
             input = offsetFromString(matchShortOffset, input);
+            if (input === null) {
+                return this;
+            }
         } else if (Math.abs(input) < 16) {
             input = input * 60;
         }
@@ -165,11 +175,11 @@ export function setOffsetToParsedOffset () {
         this.utcOffset(this._tzm);
     } else if (typeof this._i === 'string') {
         var tZone = offsetFromString(matchOffset, this._i);
-
-        if (tZone === 0) {
+        if (tZone != null) {
+            this.utcOffset(tZone);
+        }
+        else {
             this.utcOffset(0, true);
-        } else {
-            this.utcOffset(offsetFromString(matchOffset, this._i));
         }
     }
     return this;
index 8f2a8f298f453bc882d847803915790968fc56f8..a5f5bfc1c15d12f6e5166bec8a238ffcd5464421 100644 (file)
@@ -439,6 +439,12 @@ test('parse zone', function (assert) {
     assert.equal(m.hours(), 0);
 });
 
+test('parse UTC zone', function (assert) {
+    var m = moment('2013-01-01T05:00:00+00:00').parseZone();
+    assert.equal(m.utcOffset(), 0);
+    assert.equal(m.hours(), 5);
+});
+
 test('parse zone static', function (assert) {
     var m = moment.parseZone('2013-01-01T00:00:00-13:00');
     assert.equal(m.utcOffset(), -13 * 60);